From 64425969497a443bab5a76a5959e733ee9b724aa Mon Sep 17 00:00:00 2001 From: ysthakur <45539777+ysthakur@users.noreply.github.com> Date: Sun, 20 Oct 2024 23:04:50 -0400 Subject: [PATCH] Fix conflict resolution --- .../src/completions/command_completions.rs | 2 +- .../src/completions/completion_common.rs | 43 ++++++++++++++++--- .../src/completions/custom_completions.rs | 2 +- .../src/completions/flag_completions.rs | 2 +- .../src/completions/operator_completions.rs | 24 +++++------ 5 files changed, 51 insertions(+), 22 deletions(-) diff --git a/crates/nu-cli/src/completions/command_completions.rs b/crates/nu-cli/src/completions/command_completions.rs index 2dc4f6be11..e880c7fb43 100644 --- a/crates/nu-cli/src/completions/command_completions.rs +++ b/crates/nu-cli/src/completions/command_completions.rs @@ -147,7 +147,7 @@ impl Completer for CommandCompletion { &mut self, working_set: &StateWorkingSet, _stack: &Stack, - prefix: &[u8], + _prefix: &[u8], span: Span, offset: usize, pos: usize, diff --git a/crates/nu-cli/src/completions/completion_common.rs b/crates/nu-cli/src/completions/completion_common.rs index 8bb6213b81..b44abc8fcf 100644 --- a/crates/nu-cli/src/completions/completion_common.rs +++ b/crates/nu-cli/src/completions/completion_common.rs @@ -1,8 +1,5 @@ use super::{completion_options::NuMatcher, MatchAlgorithm}; -use crate::{ - completions::{matches, CompletionOptions}, - SemanticSuggestion, -}; +use crate::completions::CompletionOptions; use nu_ansi_term::Style; use nu_engine::env_to_string; use nu_path::dots::expand_ndots; @@ -165,7 +162,7 @@ pub struct FileSuggestion { /// # Parameters /// * `cwds` - A list of directories in which to search. The only reason this isn't a single string -/// is because dotnu_completions searches in multiple directories at once +/// is because dotnu_completions searches in multiple directories at once pub fn complete_item( want_directory: bool, span: nu_protocol::Span, @@ -264,6 +261,7 @@ pub fn complete_item( if should_collapse_dots { p = collapse_ndots(p); } + let cwd = p.cwd.clone(); let path = original_cwd.apply(p, path_separator); let style = ls_colors.as_ref().map(|lsc| { lsc.style_for_path_with_metadata( @@ -344,3 +342,38 @@ pub fn adjust_if_intermediate( readjusted, } } + +/// Collapse multiple ".." components into n-dots. +/// +/// It performs the reverse operation of `expand_ndots`, collapsing sequences of ".." into n-dots, +/// such as "..." and "....". +/// +/// The resulting path will use platform-specific path separators, regardless of what path separators were used in the input. +fn collapse_ndots(path: PathBuiltFromString) -> PathBuiltFromString { + let mut result = PathBuiltFromString { + parts: Vec::with_capacity(path.parts.len()), + isdir: path.isdir, + cwd: path.cwd, + }; + + let mut dot_count = 0; + + for part in path.parts { + if part == ".." { + dot_count += 1; + } else { + if dot_count > 0 { + result.parts.push(".".repeat(dot_count + 1)); + dot_count = 0; + } + result.parts.push(part); + } + } + + // Add any remaining dots + if dot_count > 0 { + result.parts.push(".".repeat(dot_count + 1)); + } + + result +} diff --git a/crates/nu-cli/src/completions/custom_completions.rs b/crates/nu-cli/src/completions/custom_completions.rs index d8d02cfabe..63e65053b4 100644 --- a/crates/nu-cli/src/completions/custom_completions.rs +++ b/crates/nu-cli/src/completions/custom_completions.rs @@ -123,7 +123,7 @@ impl Completer for CustomCompletion { .unwrap_or_default(); let options = custom_completion_options.unwrap_or(completion_options.clone()); - let mut matcher = NuMatcher::new(String::from_utf8_lossy(&prefix), options); + let mut matcher = NuMatcher::new(String::from_utf8_lossy(prefix), options); for sugg in suggestions { matcher.add_semantic_suggestion(sugg); } diff --git a/crates/nu-cli/src/completions/flag_completions.rs b/crates/nu-cli/src/completions/flag_completions.rs index 100a247896..1df95f04ba 100644 --- a/crates/nu-cli/src/completions/flag_completions.rs +++ b/crates/nu-cli/src/completions/flag_completions.rs @@ -35,7 +35,7 @@ impl Completer for FlagCompletion { let decl = working_set.get_decl(call.decl_id); let sig = decl.signature(); - let mut matcher = NuMatcher::new(String::from_utf8_lossy(&prefix), options.clone()); + let mut matcher = NuMatcher::new(String::from_utf8_lossy(prefix), options.clone()); for named in &sig.named { let flag_desc = &named.desc; diff --git a/crates/nu-cli/src/completions/operator_completions.rs b/crates/nu-cli/src/completions/operator_completions.rs index 460e7f590b..0ce4e02e17 100644 --- a/crates/nu-cli/src/completions/operator_completions.rs +++ b/crates/nu-cli/src/completions/operator_completions.rs @@ -1,5 +1,5 @@ use crate::completions::{ - Completer, CompletionOptions, MatchAlgorithm, SemanticSuggestion, SuggestionKind, + completion_options::NuMatcher, Completer, CompletionOptions, SemanticSuggestion, SuggestionKind, }; use nu_protocol::{ ast::{Expr, Expression}, @@ -28,7 +28,7 @@ impl Completer for OperatorCompletion { span: Span, offset: usize, _pos: usize, - _options: &CompletionOptions, + options: &CompletionOptions, ) -> Vec { //Check if int, float, or string let partial = std::str::from_utf8(working_set.get_span_contents(span)).unwrap_or(""); @@ -129,17 +129,12 @@ impl Completer for OperatorCompletion { _ => vec![], }; - let match_algorithm = MatchAlgorithm::Prefix; - let input_fuzzy_search = - |(operator, _): &(&str, &str)| match_algorithm.matches_str(operator, partial); - - possible_operations - .into_iter() - .filter(input_fuzzy_search) - .map(move |x| SemanticSuggestion { + let mut matcher = NuMatcher::new(partial, options.clone()); + for (symbol, desc) in possible_operations.into_iter() { + matcher.add_semantic_suggestion(SemanticSuggestion { suggestion: Suggestion { - value: x.0.to_string(), - description: Some(x.1.to_string()), + value: symbol.to_string(), + description: Some(desc.to_string()), span: reedline::Span::new(span.start - offset, span.end - offset), append_whitespace: true, ..Suggestion::default() @@ -147,8 +142,9 @@ impl Completer for OperatorCompletion { kind: Some(SuggestionKind::Command( nu_protocol::engine::CommandType::Builtin, )), - }) - .collect() + }); + } + matcher.results() } }