mirror of
https://github.com/nushell/nushell.git
synced 2025-06-20 18:08:36 +02:00
fix: edge cases
This commit is contained in:
parent
a715d04055
commit
53c94f8a06
@ -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<SemanticSuggestion> {
|
||||
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
|
||||
|
@ -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\\\"\"",
|
||||
"\"|\"",
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user