Skip to content

Commit 5189e94

Browse files
committed
fix(completion): quoted cell path completion
1 parent dfca117 commit 5189e94

File tree

2 files changed

+48
-9
lines changed

2 files changed

+48
-9
lines changed

crates/nu-cli/src/completions/cell_path_completions.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,10 @@ fn prefix_from_path_member(member: &PathMember, pos: usize) -> (String, Span) {
2424
.get(..pos + 1 - start)
2525
.map(str::to_string)
2626
.unwrap_or(prefix_str);
27-
(prefix_str, Span::new(start, pos + 1))
27+
// strip wrapping quotes
28+
let quotations = ['"', '\'', '`'];
29+
let prefix_str = prefix_str.strip_prefix(quotations).unwrap_or(&prefix_str);
30+
(prefix_str.to_string(), Span::new(start, pos + 1))
2831
}
2932

3033
impl Completer for CellPathCompletion<'_> {
@@ -108,14 +111,23 @@ fn get_suggestions_by_value(
108111
value: &Value,
109112
current_span: reedline::Span,
110113
) -> Vec<SemanticSuggestion> {
111-
let to_suggestion = |s: String, v: Option<&Value>| SemanticSuggestion {
112-
suggestion: Suggestion {
113-
value: s,
114-
span: current_span,
115-
description: v.map(|v| v.get_type().to_string()),
116-
..Suggestion::default()
117-
},
118-
kind: Some(SuggestionKind::CellPath),
114+
let to_suggestion = |s: String, v: Option<&Value>| {
115+
// Check if the string needs quoting (has spaces or punctuation)
116+
let value = if s.contains(|c: char| c.is_whitespace() || c.is_ascii_punctuation()) {
117+
format!("{:?}", s)
118+
} else {
119+
s
120+
};
121+
122+
SemanticSuggestion {
123+
suggestion: Suggestion {
124+
value,
125+
span: current_span,
126+
description: v.map(|v| v.get_type().to_string()),
127+
..Suggestion::default()
128+
},
129+
kind: Some(SuggestionKind::CellPath),
130+
}
119131
};
120132
match value {
121133
Value::Record { val, .. } => val

crates/nu-cli/tests/completions/mod.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2007,6 +2007,33 @@ fn table_cell_path_completions() {
20072007
match_suggestions(&expected, &suggestions);
20082008
}
20092009

2010+
#[test]
2011+
fn quoted_cell_path_completions() {
2012+
let (_, _, mut engine, mut stack) = new_engine();
2013+
let command = r#"let foo = {'foo bar':1 'foo\\"bar"': 1 '.': 1 '|': 1}"#;
2014+
assert!(support::merge_input(command.as_bytes(), &mut engine, &mut stack).is_ok());
2015+
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
2016+
2017+
let expected: Vec<_> = vec![
2018+
"\".\"",
2019+
"\"foo bar\"",
2020+
"\"foo\\\\\\\\\\\"bar\\\"\"",
2021+
"\"|\"",
2022+
];
2023+
let completion_str = "$foo.";
2024+
let suggestions = completer.complete(completion_str, completion_str.len());
2025+
match_suggestions(&expected, &suggestions);
2026+
2027+
let expected: Vec<_> = vec!["\"foo bar\"", "\"foo\\\\\\\\\\\"bar\\\"\""];
2028+
let completion_str = "$foo.`foo";
2029+
let suggestions = completer.complete(completion_str, completion_str.len());
2030+
match_suggestions(&expected, &suggestions);
2031+
2032+
let completion_str = "$foo.foo";
2033+
let suggestions = completer.complete(completion_str, completion_str.len());
2034+
match_suggestions(&expected, &suggestions);
2035+
}
2036+
20102037
#[test]
20112038
fn alias_of_command_and_flags() {
20122039
let (_, _, mut engine, mut stack) = new_engine();

0 commit comments

Comments
 (0)