From f653992b4a4f52f5d3e9c16ab1855137d7bb824a Mon Sep 17 00:00:00 2001 From: Jonathan Turner Date: Sun, 8 Dec 2019 19:42:43 +1300 Subject: [PATCH] A little cleanup --- src/shell/completer.rs | 129 +++++++++++++++++++++++------------------ 1 file changed, 72 insertions(+), 57 deletions(-) diff --git a/src/shell/completer.rs b/src/shell/completer.rs index fc089a3ab..c40b67d86 100644 --- a/src/shell/completer.rs +++ b/src/shell/completer.rs @@ -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)> { - use nu_source::{HasSpan, Text}; - let commands: Vec = 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::(); - - 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::(); - 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 { + let mut matching_arguments = vec![]; + + let mut line_copy = line.to_string(); + let substring = line_chars[replace_pos..pos].iter().collect::(); + let replace_string = (replace_pos..pos).map(|_| " ").collect::(); + 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 + } }