refactor: command identified by name instead of span content (#15471)

This should be a more robust method.

# Description

Previously, `export use` with double-space in between will fail to be
recognized as command `export use`.

# User-Facing Changes

minor bug fix

# Tests + Formatting

test cases made harder

# After Submitting
This commit is contained in:
zc he
2025-04-02 19:12:38 +08:00
committed by GitHub
parent af6c4bdc9c
commit df74a0c961
5 changed files with 24 additions and 23 deletions

View File

@ -25,14 +25,14 @@ fn try_find_id_in_misc(
location: Option<&usize>,
id_ref: Option<&Id>,
) -> Option<(Id, Span)> {
let call_name = working_set.get_span_contents(call.head);
let call_name = working_set.get_decl(call.decl_id).name();
match call_name {
b"def" | b"export def" => try_find_id_in_def(call, working_set, location, id_ref),
b"module" | b"export module" => try_find_id_in_mod(call, working_set, location, id_ref),
b"use" | b"export use" | b"hide" => {
"def" | "export def" => try_find_id_in_def(call, working_set, location, id_ref),
"module" | "export module" => try_find_id_in_mod(call, working_set, location, id_ref),
"use" | "export use" | "hide" => {
try_find_id_in_use(call, working_set, location, id_ref, call_name)
}
b"overlay use" | b"overlay hide" => {
"overlay use" | "overlay hide" => {
try_find_id_in_overlay(call, working_set, location, id_ref)
}
_ => None,
@ -141,7 +141,7 @@ fn try_find_id_in_use(
working_set: &StateWorkingSet,
location: Option<&usize>,
id: Option<&Id>,
call_name: &[u8],
call_name: &str,
) -> Option<(Id, Span)> {
// TODO: for keyword `hide`, the decl/var is already hidden in working_set,
// this function will always return None.
@ -176,7 +176,7 @@ fn try_find_id_in_use(
if let Some(pos) = location {
// first argument of `use` should always be module name
// while it is optional in `hide`
if span.contains(*pos) && call_name != b"hide" {
if span.contains(*pos) && call_name != "hide" {
return get_matched_module_id(working_set, span, id);
}
}
@ -196,7 +196,7 @@ fn try_find_id_in_use(
})
};
let arguments = if call_name != b"hide" {
let arguments = if call_name != "hide" {
call.arguments.get(1..)?
} else {
call.arguments.as_slice()

View File

@ -658,7 +658,7 @@ mod tests {
let message_num = 5;
let messages =
send_reference_request(&client_connection, script.clone(), 6, 11, message_num);
send_reference_request(&client_connection, script.clone(), 6, 12, message_num);
assert_eq!(messages.len(), message_num);
for message in messages {
match message {
@ -676,7 +676,7 @@ mod tests {
assert!(array.contains(&serde_json::json!(
{
"uri": script.to_string(),
"range": { "start": { "line": 6, "character": 12 }, "end": { "line": 6, "character": 19 } }
"range": { "start": { "line": 6, "character": 13 }, "end": { "line": 6, "character": 20 } }
}
)
));
@ -712,7 +712,7 @@ mod tests {
&client_connection,
script.clone(),
6,
11,
12,
message_num,
false,
);
@ -723,8 +723,8 @@ mod tests {
Message::Response(r) => assert_json_eq!(
r.result,
serde_json::json!({
"start": { "line": 6, "character": 12 },
"end": { "line": 6, "character": 19 }
"start": { "line": 6, "character": 13 },
"end": { "line": 6, "character": 20 }
}),
),
_ => panic!("unexpected message type"),
@ -738,7 +738,7 @@ mod tests {
changes[script.to_string()],
serde_json::json!([
{
"range": { "start": { "line": 6, "character": 12 }, "end": { "line": 6, "character": 19 } },
"range": { "start": { "line": 6, "character": 13 }, "end": { "line": 6, "character": 20 } },
"newText": "new"
}
])
@ -860,7 +860,7 @@ mod tests {
&client_connection,
script.clone(),
6,
11,
12,
message_num,
true,
);