Skip to content

Commit

Permalink
refactor: inline fn partial_from in completer (#10705)
Browse files Browse the repository at this point in the history
# 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.
  • Loading branch information
lavafroth committed Oct 13, 2023
1 parent ec3e4ce commit 6cff54e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 35 deletions.
8 changes: 2 additions & 6 deletions crates/nu-cli/src/completions/directory_completions.rs
Original file line number Diff line number Diff line change
@@ -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<EngineState>,
Expand Down
24 changes: 16 additions & 8 deletions crates/nu-cli/src/completions/dotnu_completions.rs
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -30,9 +30,16 @@ impl Completer for DotNuCompletion {
_: usize,
options: &CompletionOptions,
) -> Vec<Suggestion> {
let prefix_str = String::from_utf8_lossy(&prefix).to_string();
let prefix_str = String::from_utf8_lossy(&prefix).replace('`', "");
let mut search_dirs: Vec<String> = 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
Expand All @@ -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());

Expand Down
22 changes: 2 additions & 20 deletions crates/nu-cli/src/completions/file_completions.rs
Original file line number Diff line number Diff line change
@@ -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<EngineState>,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion crates/nu-cli/src/completions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

0 comments on commit 6cff54e

Please sign in to comment.