diff --git a/crates/nu-lsp/src/ast.rs b/crates/nu-lsp/src/ast.rs index 7baa5bb4bd..977570926d 100644 --- a/crates/nu-lsp/src/ast.rs +++ b/crates/nu-lsp/src/ast.rs @@ -159,7 +159,16 @@ fn try_find_id_in_use( Some(Id::Variable(var_id_ref)) => module .constants .get(name) - .and_then(|var_id| (*var_id == *var_id_ref).then_some(Id::Variable(*var_id))), + .cloned() + .or_else(|| { + // NOTE: for module record variable: https://www.nushell.sh/book/modules/using_modules.html#importing-constants + // the definition span is located at the head of the use command + let var_id = working_set.find_variable(name)?; + call.span() + .contains_span(working_set.get_variable(var_id).declaration_span) + .then_some(var_id) + }) + .and_then(|var_id| (var_id == *var_id_ref).then_some(Id::Variable(var_id))), Some(Id::Declaration(decl_id_ref)) => module.decls.get(name).and_then(|decl_id| { (*decl_id == *decl_id_ref).then_some(Id::Declaration(*decl_id)) }), diff --git a/crates/nu-lsp/src/workspace.rs b/crates/nu-lsp/src/workspace.rs index 071c372cc7..b581d0a9ef 100644 --- a/crates/nu-lsp/src/workspace.rs +++ b/crates/nu-lsp/src/workspace.rs @@ -1130,7 +1130,6 @@ mod tests { ); } - /// TODO: associate the module record with the submodule name in `use`? #[test] fn document_highlight_module_record() { let mut script = fixtures(); @@ -1149,6 +1148,7 @@ mod tests { assert_json_eq!( r.result, serde_json::json!([ + { "range": { "start": { "line": 6, "character": 26 }, "end": { "line": 6, "character": 33 } }, "kind": 1 }, { "range": { "start": { "line": 8, "character": 1 }, "end": { "line": 8, "character": 8 } }, "kind": 1 }, ]), );