Skip to content

Commit

Permalink
Add quotes to hash file autocomplete (#7398)
Browse files Browse the repository at this point in the history
# Description

Fixes #6741. Autocompleting a dir/file named something like foo#bar will
now complete to \`foo#bar\`
  • Loading branch information
merelymyself committed Dec 8, 2022
1 parent 4240bfb commit b39d797
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 5 deletions.
8 changes: 6 additions & 2 deletions crates/nu-cli/src/completions/directory_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,12 @@ pub fn directory_completion(
file_name.push(SEP);
}

// Fix files or folders with quotes
if path.contains('\'') || path.contains('"') || path.contains(' ') {
// Fix files or folders with quotes or hash
if path.contains('\'')
|| path.contains('"')
|| path.contains(' ')
|| path.contains('#')
{
path = format!("`{}`", path);
}

Expand Down
8 changes: 6 additions & 2 deletions crates/nu-cli/src/completions/file_completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,12 @@ pub fn file_path_completion(
file_name.push(SEP);
}

// Fix files or folders with quotes
if path.contains('\'') || path.contains('"') || path.contains(' ') {
// Fix files or folders with quotes or hashes
if path.contains('\'')
|| path.contains('"')
|| path.contains(' ')
|| path.contains('#')
{
path = format!("`{}`", path);
}

Expand Down
20 changes: 19 additions & 1 deletion crates/nu-cli/tests/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nu_parser::parse;
use nu_protocol::engine::StateWorkingSet;
use reedline::{Completer, Suggestion};
use rstest::{fixture, rstest};
use support::{file, folder, match_suggestions, new_engine};
use support::{completions_helpers::new_quote_engine, file, folder, match_suggestions, new_engine};

#[fixture]
fn completer() -> NuCompleter {
Expand Down Expand Up @@ -411,6 +411,24 @@ fn command_watch_with_filecompletion() {
match_suggestions(expected_paths, suggestions)
}

#[test]
fn file_completion_quoted() {
let (_, _, engine, stack) = new_quote_engine();

let mut completer = NuCompleter::new(std::sync::Arc::new(engine), stack);

let target_dir = "open ";
let suggestions = completer.complete(target_dir, target_dir.len());

let expected_paths: Vec<String> = vec![
"`te st.txt`".to_string(),
"`te#st.txt`".to_string(),
"`te'st.txt`".to_string(),
];

match_suggestions(expected_paths, suggestions)
}

#[test]
fn flag_completions() {
// Create a new engine
Expand Down
39 changes: 39 additions & 0 deletions crates/nu-cli/tests/support/completions_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,45 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
(dir, dir_str, engine_state, stack)
}

pub fn new_quote_engine() -> (PathBuf, String, EngineState, Stack) {
// Target folder inside assets
let dir = fs::fixtures().join("quoted_completions");
let mut dir_str = dir
.clone()
.into_os_string()
.into_string()
.unwrap_or_default();
dir_str.push(SEP);

// Create a new engine with default context
let mut engine_state = create_default_context();

// New stack
let mut stack = Stack::new();

// Add pwd as env var
stack.add_env_var(
"PWD".to_string(),
Value::String {
val: dir_str.clone(),
span: nu_protocol::Span::new(0, dir_str.len()),
},
);
stack.add_env_var(
"TEST".to_string(),
Value::String {
val: "NUSHELL".to_string(),
span: nu_protocol::Span::new(0, dir_str.len()),
},
);

// Merge environment into the permanent state
let merge_result = engine_state.merge_env(&mut stack, &dir);
assert!(merge_result.is_ok());

(dir, dir_str, engine_state, stack)
}

// match a list of suggestions with the expected values
pub fn match_suggestions(expected: Vec<String>, suggestions: Vec<Suggestion>) {
let expected_len = expected.len();
Expand Down
Empty file.
Empty file.
Empty file.

0 comments on commit b39d797

Please sign in to comment.