Skip to main content

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