You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Plugin: support custom completions in command flags (nushell#16859)
Closes: nushell#15766
It's similar to nushell#16383, but
allows to generate completion dynamically, which can be useful for
plugin commands.
To implement this, I added a new `get_dynamic_completion` method to
`Command` trait, so nushell gets the ability to ask plugins for a
completion. Here is how it goes when I type `plugin-cmd --flag1 <tab>`.
1. the completion logic detect it's behind a flag, and it should have a
value, then it goes to `ArgValueDynamicCompletion`
2. inside `ArgValueDynamicCompletion` it calls
`get_dynamic_completion(ArgType::Flag(flag1))` on the command, which can
be plugin command, and it will issue a
`get_dynamic_completion(ArgType::Flag(flag1))` call to the plugin
3. the plugin returns `Some(Vec<DynamicSemanticSuggestion>)` if there
are available items.
It also supports dynamic completion for positional arguments, here is
how it goes when I type `plugin-cmd x<tab>`.
1. the completion logic detect it's behind a flag, and it should have a
value, then it goes to `ArgValueDynamicCompletion`
2. inside `ArgValueDynamicCompletion` it calls
`get_dynamic_completion(ArgType::Positional(0))` on the command, which
can be plugin command, and it will issue a
`get_dynamic_completion(ArgType::Positional(0))` call to the plugin
3. the plugin returns `Some(Vec<DynamicSemanticSuggestion>)` if there
are available items.
## Release notes summary - What our users need to know
### Plugin developer: auto completion is available for plugin command
arguments.
User can define auto completions for plugin commands, here is the way to
define this:
```rust
impl PluginCommand for FlagCompletion {
type Plugin = ExamplePlugin;
...
fn get_dynamic_completion(
&self,
_plugin: &Self::Plugin,
_engine: &EngineInterface,
arg_type: ArgType,
) -> Option<Vec<DynamicSemanticSuggestion>> {
match arg_type {
// enable completion for flag.
ArgType::Flag(flag_name) => match flag_name {
"flag1" => Some(vec!["flag_val1".to_string().into(), "flag_val2".to_string().into()]),
"flag2" => Some(vec!["flag_val3".to_string().into(), "flag_val4".to_string().into()]),
// for other flags don't need to provide a completion
_ => None,
},
// enable completion for positional arguments.
ArgType::Positional(index) => None,
}
}
}
```
For the detailed example, you can refer to `get_dynamic_completion` in
`crates/nu_plugin_example/src/commands/arg_completion.rs`.
## Tasks after submitting
- [ ] Update the
[documentation](https://github.com/nushell/nushell.github.io)
0 commit comments