Skip to content

Commit 9149579

Browse files
committed
refactor: command identified by name instead of span content
1 parent af6c4bd commit 9149579

File tree

5 files changed

+24
-23
lines changed

5 files changed

+24
-23
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ struct Context<'a> {
134134
/// For argument completion
135135
struct PositionalArguments<'a> {
136136
/// command name
137-
command_head: &'a [u8],
137+
command_head: &'a str,
138138
/// indices of positional arguments
139139
positional_arg_indices: Vec<usize>,
140140
/// argument list
@@ -395,7 +395,7 @@ impl NuCompleter {
395395
Argument::Positional(_) if prefix == b"-" => flag_completion_helper(),
396396
// complete according to expression type and command head
397397
Argument::Positional(expr) => {
398-
let command_head = working_set.get_span_contents(call.head);
398+
let command_head = working_set.get_decl(call.decl_id).name();
399399
positional_arg_indices.push(arg_idx);
400400
self.argument_completion_helper(
401401
PositionalArguments {
@@ -537,19 +537,19 @@ impl NuCompleter {
537537
// special commands
538538
match command_head {
539539
// complete module file/directory
540-
b"use" | b"export use" | b"overlay use" | b"source-env"
540+
"use" | "export use" | "overlay use" | "source-env"
541541
if positional_arg_indices.len() == 1 =>
542542
{
543543
return self.process_completion(
544544
&mut DotNuCompletion {
545-
std_virtual_path: command_head != b"source-env",
545+
std_virtual_path: command_head != "source-env",
546546
},
547547
ctx,
548548
);
549549
}
550550
// NOTE: if module file already specified,
551551
// should parse it to get modules/commands/consts to complete
552-
b"use" | b"export use" => {
552+
"use" | "export use" => {
553553
let Some(Argument::Positional(Expression {
554554
expr: Expr::String(module_name),
555555
span,
@@ -613,7 +613,7 @@ impl NuCompleter {
613613
_ => return vec![],
614614
}
615615
}
616-
b"which" => {
616+
"which" => {
617617
let mut completer = CommandCompletion {
618618
internals: true,
619619
externals: true,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -586,7 +586,8 @@ fn dotnu_stdlib_completions() {
586586
assert!(load_standard_library(&mut engine).is_ok());
587587
let mut completer = NuCompleter::new(Arc::new(engine), Arc::new(stack));
588588

589-
let completion_str = "export use std/ass";
589+
// `export use` should be recognized as command `export use`
590+
let completion_str = "export use std/ass";
590591
let suggestions = completer.complete(completion_str, completion_str.len());
591592
match_suggestions(&vec!["assert"], &suggestions);
592593

crates/nu-lsp/src/ast.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ fn try_find_id_in_misc(
2525
location: Option<&usize>,
2626
id_ref: Option<&Id>,
2727
) -> Option<(Id, Span)> {
28-
let call_name = working_set.get_span_contents(call.head);
28+
let call_name = working_set.get_decl(call.decl_id).name();
2929
match call_name {
30-
b"def" | b"export def" => try_find_id_in_def(call, working_set, location, id_ref),
31-
b"module" | b"export module" => try_find_id_in_mod(call, working_set, location, id_ref),
32-
b"use" | b"export use" | b"hide" => {
30+
"def" | "export def" => try_find_id_in_def(call, working_set, location, id_ref),
31+
"module" | "export module" => try_find_id_in_mod(call, working_set, location, id_ref),
32+
"use" | "export use" | "hide" => {
3333
try_find_id_in_use(call, working_set, location, id_ref, call_name)
3434
}
35-
b"overlay use" | b"overlay hide" => {
35+
"overlay use" | "overlay hide" => {
3636
try_find_id_in_overlay(call, working_set, location, id_ref)
3737
}
3838
_ => None,
@@ -141,7 +141,7 @@ fn try_find_id_in_use(
141141
working_set: &StateWorkingSet,
142142
location: Option<&usize>,
143143
id: Option<&Id>,
144-
call_name: &[u8],
144+
call_name: &str,
145145
) -> Option<(Id, Span)> {
146146
// TODO: for keyword `hide`, the decl/var is already hidden in working_set,
147147
// this function will always return None.
@@ -176,7 +176,7 @@ fn try_find_id_in_use(
176176
if let Some(pos) = location {
177177
// first argument of `use` should always be module name
178178
// while it is optional in `hide`
179-
if span.contains(*pos) && call_name != b"hide" {
179+
if span.contains(*pos) && call_name != "hide" {
180180
return get_matched_module_id(working_set, span, id);
181181
}
182182
}
@@ -196,7 +196,7 @@ fn try_find_id_in_use(
196196
})
197197
};
198198

199-
let arguments = if call_name != b"hide" {
199+
let arguments = if call_name != "hide" {
200200
call.arguments.get(1..)?
201201
} else {
202202
call.arguments.as_slice()

crates/nu-lsp/src/workspace.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,7 @@ mod tests {
658658

659659
let message_num = 5;
660660
let messages =
661-
send_reference_request(&client_connection, script.clone(), 6, 11, message_num);
661+
send_reference_request(&client_connection, script.clone(), 6, 12, message_num);
662662
assert_eq!(messages.len(), message_num);
663663
for message in messages {
664664
match message {
@@ -676,7 +676,7 @@ mod tests {
676676
assert!(array.contains(&serde_json::json!(
677677
{
678678
"uri": script.to_string(),
679-
"range": { "start": { "line": 6, "character": 12 }, "end": { "line": 6, "character": 19 } }
679+
"range": { "start": { "line": 6, "character": 13 }, "end": { "line": 6, "character": 20 } }
680680
}
681681
)
682682
));
@@ -712,7 +712,7 @@ mod tests {
712712
&client_connection,
713713
script.clone(),
714714
6,
715-
11,
715+
12,
716716
message_num,
717717
false,
718718
);
@@ -723,8 +723,8 @@ mod tests {
723723
Message::Response(r) => assert_json_eq!(
724724
r.result,
725725
serde_json::json!({
726-
"start": { "line": 6, "character": 12 },
727-
"end": { "line": 6, "character": 19 }
726+
"start": { "line": 6, "character": 13 },
727+
"end": { "line": 6, "character": 20 }
728728
}),
729729
),
730730
_ => panic!("unexpected message type"),
@@ -738,7 +738,7 @@ mod tests {
738738
changes[script.to_string()],
739739
serde_json::json!([
740740
{
741-
"range": { "start": { "line": 6, "character": 12 }, "end": { "line": 6, "character": 19 } },
741+
"range": { "start": { "line": 6, "character": 13 }, "end": { "line": 6, "character": 20 } },
742742
"newText": "new"
743743
}
744744
])
@@ -860,7 +860,7 @@ mod tests {
860860
&client_connection,
861861
script.clone(),
862862
6,
863-
11,
863+
12,
864864
message_num,
865865
true,
866866
);

tests/fixtures/lsp/workspace/foo.nu

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@ export def foooo [
44
$param
55
}
66

7-
export def "foo str" [] { "foo" }
7+
export def "foo str" [] { "foo" }

0 commit comments

Comments
 (0)