nu_cmd_lang/core_commands/attr/
complete.rs

1use nu_engine::command_prelude::*;
2
3#[derive(Clone)]
4pub struct AttrComplete;
5
6impl Command for AttrComplete {
7    fn name(&self) -> &str {
8        "attr complete"
9    }
10
11    fn signature(&self) -> Signature {
12        Signature::build("attr complete")
13            .input_output_type(Type::Nothing, Type::String)
14            .allow_variants_without_examples(true)
15            .required(
16                "completer",
17                SyntaxShape::String,
18                "Name of the completion command.",
19            )
20            .category(Category::Core)
21    }
22
23    fn description(&self) -> &str {
24        "Attribute for using another command as a completion source for all arguments."
25    }
26
27    fn run(
28        &self,
29        engine_state: &EngineState,
30        stack: &mut Stack,
31        call: &Call,
32        _input: PipelineData,
33    ) -> Result<PipelineData, ShellError> {
34        let arg: Spanned<String> = call.req(engine_state, stack, 0)?;
35        run_impl(arg)
36    }
37
38    fn run_const(
39        &self,
40        working_set: &StateWorkingSet,
41        call: &Call,
42        _input: PipelineData,
43    ) -> Result<PipelineData, ShellError> {
44        let arg: Spanned<String> = call.req_const(working_set, 0)?;
45        run_impl(arg)
46    }
47
48    fn is_const(&self) -> bool {
49        true
50    }
51
52    fn examples(&self) -> Vec<Example<'_>> {
53        vec![Example {
54            description: "Use another command as completion source",
55            example: "\
56                def complete-foo [spans: list<string>] {\n    \
57                    [bar baz qux spam eggs] | where $it not-in $spans\n\
58                }\n\n\
59                @complete 'complete-foo'\n\
60                def foo [...args] { $args }\
61            ",
62            result: None,
63        }]
64    }
65}
66
67fn run_impl(Spanned { item, span }: Spanned<String>) -> Result<PipelineData, ShellError> {
68    Ok(Value::string(item, span).into_pipeline_data())
69}
70
71#[derive(Clone)]
72pub struct AttrCompleteExternal;
73
74impl Command for AttrCompleteExternal {
75    fn name(&self) -> &str {
76        "attr complete external"
77    }
78
79    fn signature(&self) -> Signature {
80        Signature::build("attr complete external")
81            .input_output_type(Type::Nothing, Type::Nothing)
82            .allow_variants_without_examples(true)
83            .category(Category::Core)
84    }
85
86    fn description(&self) -> &str {
87        "Attribute for enabling use of the external completer for internal commands."
88    }
89
90    fn run(
91        &self,
92        _: &EngineState,
93        _: &mut Stack,
94        _: &Call,
95        _: PipelineData,
96    ) -> Result<PipelineData, ShellError> {
97        Ok(PipelineData::empty())
98    }
99
100    fn run_const(
101        &self,
102        _: &StateWorkingSet,
103        _: &Call,
104        _: PipelineData,
105    ) -> Result<PipelineData, ShellError> {
106        Ok(PipelineData::empty())
107    }
108
109    fn is_const(&self) -> bool {
110        true
111    }
112
113    fn examples(&self) -> Vec<Example<'_>> {
114        vec![Example {
115            description: "Use the external completer for a wrapper command",
116            example: "\
117                @complete external\n\
118                def --wrapped jc [...args] {\n    \
119                    ^jc ...$args | from json\n\
120                }\
121            ",
122            result: None,
123        }]
124    }
125}