mirror of
https://github.com/nushell/nushell
synced 2025-01-24 10:55:17 +00:00
93e8f6c05e
We split off the evaluation engine part of nu-cli into its own crate. This helps improve build times for nu-cli by 17% in my tests. It also helps us see a bit better what's the core engine portion vs the part specific to the interactive CLI piece. There's more than can be done here, but I think it's a good start in the right direction.
41 lines
1.2 KiB
Rust
41 lines
1.2 KiB
Rust
use super::matchers::Matcher;
|
|
use crate::completion::{Completer, CompletionContext, Suggestion};
|
|
use nu_engine::EvaluationContext;
|
|
|
|
pub struct FlagCompleter {
|
|
pub(crate) cmd: String,
|
|
}
|
|
|
|
impl Completer for FlagCompleter {
|
|
fn complete(
|
|
&self,
|
|
ctx: &CompletionContext<'_>,
|
|
partial: &str,
|
|
matcher: &dyn Matcher,
|
|
) -> Vec<Suggestion> {
|
|
let context: &EvaluationContext = ctx.as_ref();
|
|
|
|
if let Some(cmd) = context.scope.get_command(&self.cmd) {
|
|
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()
|
|
.filter(|v| matcher.matches(partial, v))
|
|
.map(|v| Suggestion {
|
|
replacement: format!("{} ", v),
|
|
display: v,
|
|
})
|
|
.collect()
|
|
} else {
|
|
Vec::new()
|
|
}
|
|
}
|
|
}
|