From 6cff54ed0df2b54b2ecb35b8f03b5551f518a1c8 Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee Date: Fri, 13 Oct 2023 15:57:19 +0000 Subject: [PATCH] refactor: inline `fn partial_from` in completer (#10705) # Description After the addition of the prefix tab completion support, the older `partial_from` function is left with a single invocation. This PR moves the code inside the function to the point of invocation. # User-Facing Changes No user facing changes. # Tests + Formatting Tests are passing. --- .../src/completions/directory_completions.rs | 8 ++----- .../src/completions/dotnu_completions.rs | 24 ++++++++++++------- .../src/completions/file_completions.rs | 22 ++--------------- crates/nu-cli/src/completions/mod.rs | 2 +- 4 files changed, 21 insertions(+), 35 deletions(-) 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;