diff --git a/crates/nu-cli/src/completions/directory_completions.rs b/crates/nu-cli/src/completions/directory_completions.rs index 3ac4b7767b..7c936b5bb6 100644 --- a/crates/nu-cli/src/completions/directory_completions.rs +++ b/crates/nu-cli/src/completions/directory_completions.rs @@ -1,16 +1,12 @@ -use crate::completions::{completion_common::complete_item, Completer, CompletionOptions}; +use crate::completions::{completion_common::complete_item, Completer, CompletionOptions, SortBy}; use nu_protocol::{ engine::{EngineState, StateWorkingSet}, levenshtein_distance, Span, }; use reedline::Suggestion; -use std::path::Path; +use std::path::{Path, MAIN_SEPARATOR as SEP}; use std::sync::Arc; -use super::SortBy; - -const SEP: char = std::path::MAIN_SEPARATOR; - #[derive(Clone)] pub struct DirectoryCompletion { engine_state: Arc, diff --git a/crates/nu-cli/src/completions/dotnu_completions.rs b/crates/nu-cli/src/completions/dotnu_completions.rs index c1a5ae9e91..fd5c346d95 100644 --- a/crates/nu-cli/src/completions/dotnu_completions.rs +++ b/crates/nu-cli/src/completions/dotnu_completions.rs @@ -1,13 +1,13 @@ -use crate::completions::{ - file_path_completion, partial_from, Completer, CompletionOptions, SortBy, -}; +use crate::completions::{file_path_completion, Completer, CompletionOptions, SortBy}; use nu_protocol::{ engine::{EngineState, StateWorkingSet}, Span, }; use reedline::Suggestion; -use std::sync::Arc; -const SEP: char = std::path::MAIN_SEPARATOR; +use std::{ + path::{is_separator, MAIN_SEPARATOR as SEP, MAIN_SEPARATOR_STR}, + sync::Arc, +}; #[derive(Clone)] pub struct DotNuCompletion { @@ -30,9 +30,16 @@ impl Completer for DotNuCompletion { _: usize, options: &CompletionOptions, ) -> Vec { - let prefix_str = String::from_utf8_lossy(&prefix).to_string(); + let prefix_str = String::from_utf8_lossy(&prefix).replace('`', ""); let mut search_dirs: Vec = vec![]; - let (base_dir, mut partial) = partial_from(&prefix_str); + + // If prefix_str is only a word we want to search in the current dir + let (base, partial) = prefix_str + .rsplit_once(is_separator) + .unwrap_or((".", &prefix_str)); + let base_dir = base.replace(is_separator, MAIN_SEPARATOR_STR); + let mut partial = partial.to_string(); + // On windows, this standardizes paths to use \ let mut is_current_folder = false; // Fetch the lib dirs @@ -58,7 +65,8 @@ impl Completer for DotNuCompletion { }; // Check if the base_dir is a folder - if base_dir != format!(".{SEP}") { + // rsplit_once removes the separator + if base_dir != "." { // Add the base dir into the directories to be searched search_dirs.push(base_dir.clone()); diff --git a/crates/nu-cli/src/completions/file_completions.rs b/crates/nu-cli/src/completions/file_completions.rs index 54f547ae42..3f4176b427 100644 --- a/crates/nu-cli/src/completions/file_completions.rs +++ b/crates/nu-cli/src/completions/file_completions.rs @@ -1,16 +1,12 @@ -use crate::completions::{completion_common::complete_item, Completer, CompletionOptions}; +use crate::completions::{completion_common::complete_item, Completer, CompletionOptions, SortBy}; use nu_protocol::{ engine::{EngineState, StateWorkingSet}, levenshtein_distance, Span, }; use reedline::Suggestion; -use std::path::{is_separator, Path}; +use std::path::{Path, MAIN_SEPARATOR as SEP}; use std::sync::Arc; -use super::SortBy; - -const SEP: char = std::path::MAIN_SEPARATOR; - #[derive(Clone)] pub struct FileCompletion { engine_state: Arc, @@ -106,20 +102,6 @@ impl Completer for FileCompletion { } } -pub fn partial_from(input: &str) -> (String, String) { - let partial = input.replace('`', ""); - - // If partial is only a word we want to search in the current dir - let (base, rest) = partial.rsplit_once(is_separator).unwrap_or((".", &partial)); - // On windows, this standardizes paths to use \ - let mut base = base.replace(is_separator, &SEP.to_string()); - - // rsplit_once removes the separator - base.push(SEP); - - (base.to_string(), rest.to_string()) -} - pub fn file_path_completion( span: nu_protocol::Span, partial: &str, diff --git a/crates/nu-cli/src/completions/mod.rs b/crates/nu-cli/src/completions/mod.rs index d8fb0b463a..010995b135 100644 --- a/crates/nu-cli/src/completions/mod.rs +++ b/crates/nu-cli/src/completions/mod.rs @@ -17,6 +17,6 @@ pub use completion_options::{CompletionOptions, MatchAlgorithm, SortBy}; pub use custom_completions::CustomCompletion; pub use directory_completions::DirectoryCompletion; pub use dotnu_completions::DotNuCompletion; -pub use file_completions::{file_path_completion, matches, partial_from, FileCompletion}; +pub use file_completions::{file_path_completion, matches, FileCompletion}; pub use flag_completions::FlagCompletion; pub use variable_completions::VariableCompletion;