mirror of
https://github.com/nushell/nushell.git
synced 2025-04-17 17:58:18 +02:00
Merge 180cb856d5
into 61dbcf3de6
This commit is contained in:
commit
cdcd9a3121
@ -17,14 +17,14 @@ pub struct CellPathCompletion<'a> {
|
|||||||
|
|
||||||
fn prefix_from_path_member(member: &PathMember, pos: usize) -> (String, Span) {
|
fn prefix_from_path_member(member: &PathMember, pos: usize) -> (String, Span) {
|
||||||
let (prefix_str, start) = match member {
|
let (prefix_str, start) = match member {
|
||||||
PathMember::String { val, span, .. } => (val.clone(), span.start),
|
PathMember::String { val, span, .. } => (val, span.start),
|
||||||
PathMember::Int { val, span, .. } => (val.to_string(), span.start),
|
PathMember::Int { val, span, .. } => (&val.to_string(), span.start),
|
||||||
};
|
};
|
||||||
let prefix_str = prefix_str
|
let prefix_str = prefix_str.get(..pos + 1 - start).unwrap_or(prefix_str);
|
||||||
.get(..pos + 1 - start)
|
// strip wrapping quotes
|
||||||
.map(str::to_string)
|
let quotations = ['"', '\'', '`'];
|
||||||
.unwrap_or(prefix_str);
|
let prefix_str = prefix_str.strip_prefix(quotations).unwrap_or(prefix_str);
|
||||||
(prefix_str, Span::new(start, pos + 1))
|
(prefix_str.to_string(), Span::new(start, pos + 1))
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Completer for CellPathCompletion<'_> {
|
impl Completer for CellPathCompletion<'_> {
|
||||||
@ -108,14 +108,26 @@ fn get_suggestions_by_value(
|
|||||||
value: &Value,
|
value: &Value,
|
||||||
current_span: reedline::Span,
|
current_span: reedline::Span,
|
||||||
) -> Vec<SemanticSuggestion> {
|
) -> Vec<SemanticSuggestion> {
|
||||||
let to_suggestion = |s: String, v: Option<&Value>| SemanticSuggestion {
|
let to_suggestion = |s: String, v: Option<&Value>| {
|
||||||
suggestion: Suggestion {
|
// Check if the string needs quoting
|
||||||
value: s,
|
let value = if s.is_empty()
|
||||||
span: current_span,
|
|| s.chars()
|
||||||
description: v.map(|v| v.get_type().to_string()),
|
.any(|c: char| !(c.is_ascii_alphabetic() || ['_', '-'].contains(&c)))
|
||||||
..Suggestion::default()
|
{
|
||||||
},
|
format!("{:?}", s)
|
||||||
kind: Some(SuggestionKind::CellPath),
|
} else {
|
||||||
|
s
|
||||||
|
};
|
||||||
|
|
||||||
|
SemanticSuggestion {
|
||||||
|
suggestion: Suggestion {
|
||||||
|
value,
|
||||||
|
span: current_span,
|
||||||
|
description: v.map(|v| v.get_type().to_string()),
|
||||||
|
..Suggestion::default()
|
||||||
|
},
|
||||||
|
kind: Some(SuggestionKind::CellPath),
|
||||||
|
}
|
||||||
};
|
};
|
||||||
match value {
|
match value {
|
||||||
Value::Record { val, .. } => val
|
Value::Record { val, .. } => val
|
||||||
|
@ -2005,6 +2005,35 @@ fn table_cell_path_completions() {
|
|||||||
match_suggestions(&expected, &suggestions);
|
match_suggestions(&expected, &suggestions);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[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 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\\\"\"",
|
||||||
|
"\"|\"",
|
||||||
|
];
|
||||||
|
let completion_str = "$foo.";
|
||||||
|
let suggestions = completer.complete(completion_str, completion_str.len());
|
||||||
|
match_suggestions(&expected, &suggestions);
|
||||||
|
|
||||||
|
let expected: Vec<_> = vec!["\"foo bar\"", "\"foo\\\\\\\\\\\"bar\\\"\""];
|
||||||
|
let completion_str = "$foo.`foo";
|
||||||
|
let suggestions = completer.complete(completion_str, completion_str.len());
|
||||||
|
match_suggestions(&expected, &suggestions);
|
||||||
|
|
||||||
|
let completion_str = "$foo.foo";
|
||||||
|
let suggestions = completer.complete(completion_str, completion_str.len());
|
||||||
|
match_suggestions(&expected, &suggestions);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn alias_of_command_and_flags() {
|
fn alias_of_command_and_flags() {
|
||||||
let (_, _, mut engine, mut stack) = new_engine();
|
let (_, _, mut engine, mut stack) = new_engine();
|
||||||
|
@ -486,10 +486,10 @@ mod tests {
|
|||||||
actual: result_from_message(resp),
|
actual: result_from_message(resp),
|
||||||
expected: serde_json::json!([
|
expected: serde_json::json!([
|
||||||
{
|
{
|
||||||
"label": "1",
|
"label": "\"1\"",
|
||||||
"detail": "string",
|
"detail": "string",
|
||||||
"textEdit": {
|
"textEdit": {
|
||||||
"newText": "1",
|
"newText": "\"1\"",
|
||||||
"range": { "start": { "line": 1, "character": 5 }, "end": { "line": 1, "character": 5 } }
|
"range": { "start": { "line": 1, "character": 5 }, "end": { "line": 1, "character": 5 } }
|
||||||
},
|
},
|
||||||
"kind": 10
|
"kind": 10
|
||||||
|
Loading…
Reference in New Issue
Block a user