From 6600b3edfbd0d7ac58e604b14b4336f69dcbea3b Mon Sep 17 00:00:00 2001 From: Anton Sagel <76914086+Ancient77@users.noreply.github.com> Date: Mon, 9 Sep 2024 20:39:18 +0200 Subject: [PATCH] Expand multiple dots in path in completions (#13725) # Description This is my first PR, and I'm looking for feedback to help me improve! This PR fixes #13380 by expanding the path prior to parsing it. Also I've removed some unused code in [completion_common.rs](https://github.com/Ancient77/nushell/blob/84e92bb02c78d4bb71a045fbbe73b9351ab8d16a/crates/nu-cli/src/completions/completion_common.rs ) # User-Facing Changes Auto-completion for "cd .../" now works by expanding to "cd ../../". # Tests + Formatting Formatted and added 2 tests for triple dots in the middle of a path and at the end. Also added a test for the expand_ndots() function. --- .../src/completions/completion_common.rs | 66 ++++- crates/nu-cli/tests/completions/mod.rs | 240 +++++++++++++++++- crates/nu-path/src/dots.rs | 11 + .../folder_inside_folder/myfile | 0 4 files changed, 303 insertions(+), 14 deletions(-) create mode 100644 tests/fixtures/completions/directory_completion/folder_inside_folder/myfile diff --git a/crates/nu-cli/src/completions/completion_common.rs b/crates/nu-cli/src/completions/completion_common.rs index 8aae72b4f3..77ae165c73 100644 --- a/crates/nu-cli/src/completions/completion_common.rs +++ b/crates/nu-cli/src/completions/completion_common.rs @@ -1,3 +1,4 @@ +use super::MatchAlgorithm; use crate::{ completions::{matches, CompletionOptions}, SemanticSuggestion, @@ -5,6 +6,7 @@ use crate::{ use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher}; use nu_ansi_term::Style; use nu_engine::env_to_string; +use nu_path::dots::expand_ndots; use nu_path::{expand_to_real_path, home_dir}; use nu_protocol::{ engine::{EngineState, Stack, StateWorkingSet}, @@ -13,8 +15,6 @@ use nu_protocol::{ use nu_utils::get_ls_colors; use std::path::{is_separator, Component, Path, PathBuf, MAIN_SEPARATOR as SEP}; -use super::MatchAlgorithm; - #[derive(Clone, Default)] pub struct PathBuiltFromString { parts: Vec, @@ -41,7 +41,7 @@ pub fn complete_rec( let mut completions = vec![]; if let Some((&base, rest)) = partial.split_first() { - if (base == "." || base == "..") && (isdir || !rest.is_empty()) { + if base.chars().all(|c| c == '.') && (isdir || !rest.is_empty()) { let mut built = built.clone(); built.parts.push(base.to_string()); built.isdir = true; @@ -156,16 +156,25 @@ pub fn complete_item( engine_state: &EngineState, stack: &Stack, ) -> Vec<(nu_protocol::Span, String, Option