diff --git a/crates/nu-cli/src/completions/command_completions.rs b/crates/nu-cli/src/completions/command_completions.rs index 98a3d2ace3..77961a6278 100644 --- a/crates/nu-cli/src/completions/command_completions.rs +++ b/crates/nu-cli/src/completions/command_completions.rs @@ -1,7 +1,5 @@ -use crate::completions::{ - file_completions::file_path_completion, Completer, CompletionOptions, MatchAlgorithm, SortBy, -}; -use nu_parser::{unescape_unquote_string, FlatShape}; +use crate::completions::{Completer, CompletionOptions, MatchAlgorithm, SortBy}; +use nu_parser::FlatShape; use nu_protocol::{ engine::{EngineState, StateWorkingSet}, Span, @@ -12,7 +10,6 @@ use std::sync::Arc; pub struct CommandCompletion { engine_state: Arc, flattened: Vec<(Span, FlatShape)>, - flat_idx: usize, flat_shape: FlatShape, } @@ -21,13 +18,11 @@ impl CommandCompletion { engine_state: Arc, _: &StateWorkingSet, flattened: Vec<(Span, FlatShape)>, - flat_idx: usize, flat_shape: FlatShape, ) -> Self { Self { engine_state, flattened, - flat_idx, flat_shape, } } @@ -161,7 +156,7 @@ impl Completer for CommandCompletion { fn fetch( &mut self, working_set: &StateWorkingSet, - prefix: Vec, + _prefix: Vec, span: Span, offset: usize, pos: usize, @@ -214,66 +209,8 @@ impl Completer for CommandCompletion { vec![] }; - let cwd = if let Some(d) = self.engine_state.get_env_var("PWD") { - match d.as_string() { - Ok(s) => s, - Err(_) => "".to_string(), - } - } else { - "".to_string() - }; - - let preceding_byte = if span.start > offset { - working_set - .get_span_contents(Span { - start: span.start - 1, - end: span.start, - }) - .to_vec() - } else { - vec![] - }; - // let prefix = working_set.get_span_contents(flat.0); - let prefix = String::from_utf8_lossy(&prefix).to_string(); - - file_path_completion(span, &prefix, &cwd, options) + subcommands .into_iter() - .map(move |x| { - if self.flat_idx == 0 { - // We're in the command position - if (x.1.starts_with('"') || x.1.starts_with('\'') || x.1.starts_with('`')) - && !matches!(preceding_byte.get(0), Some(b'^')) - { - let (trimmed, _) = unescape_unquote_string(x.1.as_bytes(), span); - let expanded = nu_path::canonicalize_with(trimmed, &cwd); - - if let Ok(expanded) = expanded { - if is_executable::is_executable(expanded) { - (x.0, format!("^{}", x.1)) - } else { - (x.0, x.1) - } - } else { - (x.0, x.1) - } - } else { - (x.0, x.1) - } - } else { - (x.0, x.1) - } - }) - .map(move |x| Suggestion { - value: x.1, - description: None, - extra: None, - span: reedline::Span { - start: x.0.start - offset, - end: x.0.end - offset, - }, - append_whitespace: false, - }) - .chain(subcommands.into_iter()) .chain(commands.into_iter()) .collect::>() } diff --git a/crates/nu-cli/src/completions/completer.rs b/crates/nu-cli/src/completions/completer.rs index ed1adc4c1d..a5a7700572 100644 --- a/crates/nu-cli/src/completions/completer.rs +++ b/crates/nu-cli/src/completions/completer.rs @@ -176,37 +176,39 @@ impl NuCompleter { pos, ); } - FlatShape::Filepath - | FlatShape::GlobPattern - | FlatShape::ExternalArg => { - let mut completer = FileCompletion::new(self.engine_state.clone()); - - return self.process_completion( - &mut completer, - &working_set, - prefix, - new_span, - offset, - pos, - ); - } flat_shape => { let mut completer = CommandCompletion::new( self.engine_state.clone(), &working_set, flattened.clone(), - flat_idx, + // flat_idx, flat_shape.clone(), ); - return self.process_completion( + let out: Vec<_> = self.process_completion( &mut completer, &working_set, - prefix, + prefix.clone(), new_span, offset, pos, ); + + if out.is_empty() { + let mut completer = + FileCompletion::new(self.engine_state.clone()); + + return self.process_completion( + &mut completer, + &working_set, + prefix, + new_span, + offset, + pos, + ); + } + + return out; } }; }