Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: move the partial_from function to the single place it is invoked #10705

Merged
merged 2 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 3 additions & 5 deletions crates/nu-cli/src/completions/directory_completions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::completions::{completion_common::complete_item, Completer, CompletionOptions};
use crate::completions::{
completion_common::complete_item, Completer, CompletionOptions, SortBy, SEP,
};
use nu_protocol::{
engine::{EngineState, StateWorkingSet},
levenshtein_distance, Span,
Expand All @@ -7,10 +9,6 @@ use reedline::Suggestion;
use std::path::Path;
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, SEP};
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_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
24 changes: 4 additions & 20 deletions crates/nu-cli/src/completions/file_completions.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
use crate::completions::{completion_common::complete_item, Completer, CompletionOptions};
use crate::completions::{
completion_common::complete_item, Completer, CompletionOptions, SortBy, SEP,
};
use nu_protocol::{
engine::{EngineState, StateWorkingSet},
levenshtein_distance, Span,
};
use reedline::Suggestion;
use std::path::{is_separator, Path};
use std::path::Path;
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 +104,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
3 changes: 2 additions & 1 deletion crates/nu-cli/src/completions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ 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 std::path::MAIN_SEPARATOR as SEP;
lavafroth marked this conversation as resolved.
Show resolved Hide resolved
pub use variable_completions::VariableCompletion;