Add quotes to hash file autocomplete (#7398)

# Description

Fixes #6741. Autocompleting a dir/file named something like foo#bar will
now complete to \`foo#bar\`
This commit is contained in:
pwygab 2022-12-09 04:37:10 +08:00 committed by GitHub
parent 4240bfb7b1
commit b39d797c1f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 70 additions and 5 deletions

View File

@ -136,8 +136,12 @@ pub fn directory_completion(
file_name.push(SEP); file_name.push(SEP);
} }
// Fix files or folders with quotes // Fix files or folders with quotes or hash
if path.contains('\'') || path.contains('"') || path.contains(' ') { if path.contains('\'')
|| path.contains('"')
|| path.contains(' ')
|| path.contains('#')
{
path = format!("`{}`", path); path = format!("`{}`", path);
} }

View File

@ -141,8 +141,12 @@ pub fn file_path_completion(
file_name.push(SEP); file_name.push(SEP);
} }
// Fix files or folders with quotes // Fix files or folders with quotes or hashes
if path.contains('\'') || path.contains('"') || path.contains(' ') { if path.contains('\'')
|| path.contains('"')
|| path.contains(' ')
|| path.contains('#')
{
path = format!("`{}`", path); path = format!("`{}`", path);
} }

View File

@ -5,7 +5,7 @@ use nu_parser::parse;
use nu_protocol::engine::StateWorkingSet; use nu_protocol::engine::StateWorkingSet;
use reedline::{Completer, Suggestion}; use reedline::{Completer, Suggestion};
use rstest::{fixture, rstest}; 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] #[fixture]
fn completer() -> NuCompleter { fn completer() -> NuCompleter {
@ -411,6 +411,24 @@ fn command_watch_with_filecompletion() {
match_suggestions(expected_paths, suggestions) 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] #[test]
fn flag_completions() { fn flag_completions() {
// Create a new engine // Create a new engine

View File

@ -51,6 +51,45 @@ pub fn new_engine() -> (PathBuf, String, EngineState, Stack) {
(dir, dir_str, engine_state, 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 // match a list of suggestions with the expected values
pub fn match_suggestions(expected: Vec<String>, suggestions: Vec<Suggestion>) { pub fn match_suggestions(expected: Vec<String>, suggestions: Vec<Suggestion>) {
let expected_len = expected.len(); let expected_len = expected.len();

View File

View File

View File