forked from extern/nushell
A little cleanup
This commit is contained in:
parent
b5f8c1cc50
commit
f653992b4a
@ -1,6 +1,7 @@
|
||||
use crate::context::CommandRegistry;
|
||||
|
||||
use derive_new::new;
|
||||
use nu_source::{HasSpan, Text};
|
||||
use rustyline::completion::{Completer, FilenameCompleter};
|
||||
|
||||
#[derive(new)]
|
||||
@ -16,8 +17,6 @@ impl NuCompleter {
|
||||
pos: usize,
|
||||
context: &rustyline::Context,
|
||||
) -> rustyline::Result<(usize, Vec<rustyline::completion::Pair>)> {
|
||||
use nu_source::{HasSpan, Text};
|
||||
|
||||
let commands: Vec<String> = self.commands.names();
|
||||
|
||||
let line_chars: Vec<_> = line[..pos].chars().collect();
|
||||
@ -30,64 +29,11 @@ impl NuCompleter {
|
||||
replace_pos -= 1;
|
||||
}
|
||||
|
||||
let substring = line_chars[replace_pos..pos].iter().collect::<String>();
|
||||
|
||||
let mut completions = vec![];
|
||||
let mut completions;
|
||||
|
||||
// See if we're a flag
|
||||
if pos > 0 && line_chars[replace_pos] == '-' {
|
||||
let mut line_copy = line.to_string();
|
||||
let replace_string = (replace_pos..pos).map(|_| " ").collect::<String>();
|
||||
line_copy.replace_range(replace_pos..pos, &replace_string);
|
||||
match nu_parser::parse(&line_copy) {
|
||||
Ok(val) => {
|
||||
let source = Text::from(line);
|
||||
let pipeline_list = vec![val.clone()];
|
||||
let mut iterator =
|
||||
nu_parser::TokensIterator::all(&pipeline_list, source.clone(), val.span());
|
||||
|
||||
let expand_context = nu_parser::ExpandContext {
|
||||
homedir: None,
|
||||
registry: Box::new(self.commands.clone()),
|
||||
source: &source,
|
||||
};
|
||||
|
||||
let result = nu_parser::expand_syntax(
|
||||
&nu_parser::PipelineShape,
|
||||
&mut iterator,
|
||||
&expand_context,
|
||||
);
|
||||
|
||||
if let Ok(result) = result {
|
||||
for command in result.commands.list {
|
||||
match command {
|
||||
nu_parser::ClassifiedCommand::Internal(
|
||||
nu_parser::InternalCommand { args, .. },
|
||||
) => {
|
||||
if replace_pos >= args.span.start()
|
||||
&& replace_pos <= args.span.end()
|
||||
{
|
||||
if let Some(named) = args.named {
|
||||
for (name, _) in named.iter() {
|
||||
let full_flag = format!("--{}", name);
|
||||
|
||||
if full_flag.starts_with(&substring) {
|
||||
completions.push(rustyline::completion::Pair {
|
||||
display: full_flag.clone(),
|
||||
replacement: full_flag,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
completions = self.get_matching_arguments(&line_chars, line, replace_pos, pos);
|
||||
} else {
|
||||
completions = self.file_completer.complete(line, pos, context)?.1;
|
||||
|
||||
@ -136,4 +82,73 @@ impl NuCompleter {
|
||||
|
||||
Ok((replace_pos, completions))
|
||||
}
|
||||
|
||||
fn get_matching_arguments(
|
||||
&self,
|
||||
line_chars: &[char],
|
||||
line: &str,
|
||||
replace_pos: usize,
|
||||
pos: usize,
|
||||
) -> Vec<rustyline::completion::Pair> {
|
||||
let mut matching_arguments = vec![];
|
||||
|
||||
let mut line_copy = line.to_string();
|
||||
let substring = line_chars[replace_pos..pos].iter().collect::<String>();
|
||||
let replace_string = (replace_pos..pos).map(|_| " ").collect::<String>();
|
||||
line_copy.replace_range(replace_pos..pos, &replace_string);
|
||||
|
||||
match nu_parser::parse(&line_copy) {
|
||||
Ok(val) => {
|
||||
let source = Text::from(line);
|
||||
let pipeline_list = vec![val.clone()];
|
||||
let mut iterator =
|
||||
nu_parser::TokensIterator::all(&pipeline_list, source.clone(), val.span());
|
||||
|
||||
let expand_context = nu_parser::ExpandContext {
|
||||
homedir: None,
|
||||
registry: Box::new(self.commands.clone()),
|
||||
source: &source,
|
||||
};
|
||||
|
||||
let result = nu_parser::expand_syntax(
|
||||
&nu_parser::PipelineShape,
|
||||
&mut iterator,
|
||||
&expand_context,
|
||||
);
|
||||
|
||||
if let Ok(result) = result {
|
||||
for command in result.commands.list {
|
||||
match command {
|
||||
nu_parser::ClassifiedCommand::Internal(
|
||||
nu_parser::InternalCommand { args, .. },
|
||||
) => {
|
||||
if replace_pos >= args.span.start()
|
||||
&& replace_pos <= args.span.end()
|
||||
{
|
||||
if let Some(named) = args.named {
|
||||
for (name, _) in named.iter() {
|
||||
let full_flag = format!("--{}", name);
|
||||
|
||||
if full_flag.starts_with(&substring) {
|
||||
matching_arguments.push(
|
||||
rustyline::completion::Pair {
|
||||
display: full_flag.clone(),
|
||||
replacement: full_flag,
|
||||
},
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
matching_arguments
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user