2020-09-17 14:52:58 +00:00
|
|
|
use super::matchers::Matcher;
|
2020-09-19 21:29:51 +00:00
|
|
|
use crate::completion::{Completer, CompletionContext, Suggestion};
|
2021-01-10 02:50:49 +00:00
|
|
|
use nu_engine::EvaluationContext;
|
2020-08-21 19:37:51 +00:00
|
|
|
|
2020-09-16 04:37:43 +00:00
|
|
|
pub struct FlagCompleter {
|
|
|
|
pub(crate) cmd: String,
|
|
|
|
}
|
2020-08-21 19:37:51 +00:00
|
|
|
|
2020-09-16 04:37:43 +00:00
|
|
|
impl Completer for FlagCompleter {
|
2020-09-19 21:29:51 +00:00
|
|
|
fn complete(
|
|
|
|
&self,
|
|
|
|
ctx: &CompletionContext<'_>,
|
|
|
|
partial: &str,
|
|
|
|
matcher: &dyn Matcher,
|
|
|
|
) -> Vec<Suggestion> {
|
|
|
|
let context: &EvaluationContext = ctx.as_ref();
|
2020-08-21 19:37:51 +00:00
|
|
|
|
2020-12-18 07:53:49 +00:00
|
|
|
if let Some(cmd) = context.scope.get_command(&self.cmd) {
|
2020-08-21 19:37:51 +00:00
|
|
|
let sig = cmd.signature();
|
|
|
|
let mut suggestions = Vec::new();
|
|
|
|
for (name, (named_type, _desc)) in sig.named.iter() {
|
|
|
|
suggestions.push(format!("--{}", name));
|
|
|
|
|
|
|
|
if let Some(c) = named_type.get_short() {
|
|
|
|
suggestions.push(format!("-{}", c));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
suggestions
|
|
|
|
.into_iter()
|
2020-09-17 14:52:58 +00:00
|
|
|
.filter(|v| matcher.matches(partial, v))
|
2020-08-21 19:37:51 +00:00
|
|
|
.map(|v| Suggestion {
|
|
|
|
replacement: format!("{} ", v),
|
|
|
|
display: v,
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|