From 53c94f8a06347b2d75ed0beee8c667d70b25ef92 Mon Sep 17 00:00:00 2001 From: blindfs Date: Fri, 11 Apr 2025 11:51:39 +0800 Subject: [PATCH] fix: edge cases --- .../src/completions/cell_path_completions.rs | 18 ++++++++---------- crates/nu-cli/tests/completions/mod.rs | 4 +++- crates/nu-lsp/src/completion.rs | 4 ++-- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/crates/nu-cli/src/completions/cell_path_completions.rs b/crates/nu-cli/src/completions/cell_path_completions.rs index 23315b22f0..6ff407e139 100644 --- a/crates/nu-cli/src/completions/cell_path_completions.rs +++ b/crates/nu-cli/src/completions/cell_path_completions.rs @@ -17,16 +17,13 @@ pub struct CellPathCompletion<'a> { fn prefix_from_path_member(member: &PathMember, pos: usize) -> (String, Span) { let (prefix_str, start) = match member { - PathMember::String { val, span, .. } => (val.clone(), span.start), - PathMember::Int { val, span, .. } => (val.to_string(), span.start), + PathMember::String { val, span, .. } => (val, span.start), + PathMember::Int { val, span, .. } => (&val.to_string(), span.start), }; - let prefix_str = prefix_str - .get(..pos + 1 - start) - .map(str::to_string) - .unwrap_or(prefix_str); + let prefix_str = prefix_str.get(..pos + 1 - start).unwrap_or(prefix_str); // strip wrapping quotes let quotations = ['"', '\'', '`']; - let prefix_str = prefix_str.strip_prefix(quotations).unwrap_or(&prefix_str); + let prefix_str = prefix_str.strip_prefix(quotations).unwrap_or(prefix_str); (prefix_str.to_string(), Span::new(start, pos + 1)) } @@ -113,9 +110,10 @@ fn get_suggestions_by_value( ) -> Vec { let to_suggestion = |s: String, v: Option<&Value>| { // Check if the string needs quoting (has spaces or punctuation) - let value = if s.contains(|c: char| { - c.is_whitespace() || (c.is_ascii_punctuation() && !(['_', '-'].contains(&c))) - }) { + let value = if s.is_empty() + || s.chars() + .any(|c: char| !(c.is_ascii_alphabetic() || ['_', '-'].contains(&c))) + { format!("{:?}", s) } else { s diff --git a/crates/nu-cli/tests/completions/mod.rs b/crates/nu-cli/tests/completions/mod.rs index 5c6a10059a..8acc6e8409 100644 --- a/crates/nu-cli/tests/completions/mod.rs +++ b/crates/nu-cli/tests/completions/mod.rs @@ -2010,12 +2010,14 @@ fn table_cell_path_completions() { #[test] fn quoted_cell_path_completions() { let (_, _, mut engine, mut stack) = new_engine(); - let command = r#"let foo = {'foo bar':1 'foo\\"bar"': 1 '.': 1 '|': 1}"#; + let command = r#"let foo = {'foo bar':1 'foo\\"bar"': 1 '.': 1 '|': 1 1: 1 "": 1}"#; assert!(support::merge_input(command.as_bytes(), &mut engine, &mut stack).is_ok()); let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack)); let expected: Vec<_> = vec![ + "\"\"", "\".\"", + "\"1\"", "\"foo bar\"", "\"foo\\\\\\\\\\\"bar\\\"\"", "\"|\"", diff --git a/crates/nu-lsp/src/completion.rs b/crates/nu-lsp/src/completion.rs index 2d03acbb27..874d138eab 100644 --- a/crates/nu-lsp/src/completion.rs +++ b/crates/nu-lsp/src/completion.rs @@ -486,10 +486,10 @@ mod tests { actual: result_from_message(resp), expected: serde_json::json!([ { - "label": "1", + "label": "\"1\"", "detail": "string", "textEdit": { - "newText": "1", + "newText": "\"1\"", "range": { "start": { "line": 1, "character": 5 }, "end": { "line": 1, "character": 5 } } }, "kind": 10