Skip to content

Commit 4a4a41d

Browse files
committed
test
1 parent ecb9799 commit 4a4a41d

File tree

9 files changed

+187
-58
lines changed

9 files changed

+187
-58
lines changed

crates/nu-lsp/src/ast.rs

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ fn try_find_id_in_mod(
114114
Argument::Positional(expr) => {
115115
let name = expr.as_string()?;
116116
let module_id = working_set.find_module(name.as_bytes())?;
117-
let found_id = Id::Module(module_id);
117+
let found_id = Id::Module(module_id, name.as_bytes().to_vec());
118118
let found_span = strip_quotes(arg.span(), working_set);
119119
id_ref
120120
.is_none_or(|id_r| found_id == *id_r)
@@ -160,14 +160,16 @@ fn try_find_id_in_use(
160160
(*decl_id == *decl_id_ref).then_some(Id::Declaration(*decl_id))
161161
}),
162162
// this is only for argument `members`
163-
Some(Id::Module(module_id_ref)) => module.submodules.get(name).and_then(|module_id| {
164-
(*module_id == *module_id_ref).then_some(Id::Module(*module_id))
165-
}),
163+
Some(Id::Module(module_id_ref, name_ref)) => {
164+
module.submodules.get(name).and_then(|module_id| {
165+
(*module_id == *module_id_ref && name_ref == name)
166+
.then_some(Id::Module(*module_id, name.to_vec()))
167+
})
168+
}
166169
None => module
167170
.submodules
168171
.get(name)
169-
.cloned()
170-
.map(Id::Module)
172+
.map(|id| Id::Module(*id, name.to_vec()))
171173
.or(module.decls.get(name).cloned().map(Id::Declaration))
172174
.or(module.constants.get(name).cloned().map(Id::Variable)),
173175
_ => None,
@@ -178,16 +180,18 @@ fn try_find_id_in_use(
178180
// Get module id if required
179181
let module_name = call.arguments.first()?;
180182
let span = module_name.span();
181-
if let Some(Id::Module(id_ref)) = id {
183+
let clean_span = strip_quotes(span, working_set);
184+
let span_content = working_set.get_span_contents(clean_span);
185+
if let Some(Id::Module(id_ref, name_ref)) = id {
182186
// still need to check the rest, if id not matched
183-
if module_id == *id_ref {
184-
return Some((Id::Module(module_id), strip_quotes(span, working_set)));
187+
if module_id == *id_ref && name_ref == span_content {
188+
return Some((Id::Module(module_id, span_content.to_vec()), clean_span));
185189
}
186190
}
187191
if let Some(pos) = location {
188192
// first argument of `use`/`hide` should always be module name
189193
if span.contains(*pos) {
190-
return Some((Id::Module(module_id), strip_quotes(span, working_set)));
194+
return Some((Id::Module(module_id, span_content.to_vec()), clean_span));
191195
}
192196
}
193197

@@ -206,8 +210,7 @@ fn try_find_id_in_use(
206210
})
207211
};
208212

209-
let arguments = call.arguments.get(1..)?;
210-
for arg in arguments {
213+
for arg in call.arguments.get(1..)?.iter().rev() {
211214
let Argument::Positional(expr) = arg else {
212215
continue;
213216
};
@@ -248,38 +251,42 @@ fn try_find_id_in_overlay(
248251
id: Option<&Id>,
249252
) -> Option<(Id, Span)> {
250253
let check_location = |span: &Span| location.is_none_or(|pos| span.contains(*pos));
251-
let module_from_parser_info = |span: Span| {
254+
let module_from_parser_info = |span: Span, name: &str| {
252255
let Expression {
253256
expr: Expr::Overlay(Some(module_id)),
254257
..
255258
} = call.get_parser_info("overlay_expr")?
256259
else {
257260
return None;
258261
};
259-
let found_id = Id::Module(*module_id);
262+
let found_id = Id::Module(*module_id, name.as_bytes().to_vec());
260263
id.is_none_or(|id_r| found_id == *id_r)
261264
.then_some((found_id, strip_quotes(span, working_set)))
262265
};
263-
// NOTE: `overlay_expr` doesn't work for `overlay hide`
266+
// NOTE: `overlay_expr` doesn't exist for `overlay hide`
264267
let module_from_overlay_name = |name: &str, span: Span| {
265-
let found_id = Id::Module(working_set.find_overlay(name.as_bytes())?.origin);
268+
let found_id = Id::Module(
269+
working_set.find_overlay(name.as_bytes())?.origin,
270+
name.as_bytes().to_vec(),
271+
);
266272
id.is_none_or(|id_r| found_id == *id_r)
267273
.then_some((found_id, strip_quotes(span, working_set)))
268274
};
269275

270-
for arg in call.arguments.iter() {
276+
// check `as alias` first
277+
for arg in call.arguments.iter().rev() {
271278
let Argument::Positional(expr) = arg else {
272279
continue;
273280
};
274281
if !check_location(&expr.span) {
275282
continue;
276283
};
277284
let matched = match &expr.expr {
278-
Expr::String(name) => module_from_parser_info(expr.span)
285+
Expr::String(name) => module_from_parser_info(expr.span, name)
279286
.or_else(|| module_from_overlay_name(name, expr.span)),
280287
// keyword 'as'
281288
Expr::Keyword(kwd) => match &kwd.expr.expr {
282-
Expr::String(name) => module_from_parser_info(kwd.expr.span)
289+
Expr::String(name) => module_from_parser_info(kwd.expr.span, name)
283290
.or_else(|| module_from_overlay_name(name, kwd.expr.span)),
284291
_ => None,
285292
},
@@ -349,7 +356,9 @@ fn find_id_in_expr(
349356
FindMapResult::Found((Id::CellPath(var_id, tail), span))
350357
}
351358
}
352-
Expr::Overlay(Some(module_id)) => FindMapResult::Found((Id::Module(*module_id), span)),
359+
Expr::Overlay(Some(module_id)) => {
360+
FindMapResult::Found((Id::Module(*module_id, vec![]), span))
361+
}
353362
// terminal value expressions
354363
Expr::Bool(_)
355364
| Expr::Binary(_)

crates/nu-lsp/src/goto.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ impl LanguageServer {
5555
let var = working_set.get_variable(*var_id);
5656
Some(var.declaration_span)
5757
}
58-
Id::Module(module_id) => {
58+
Id::Module(module_id, _) => {
5959
let module = working_set.get_module(*module_id);
6060
module.span
6161
}

crates/nu-lsp/src/hover.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ impl LanguageServer {
178178
working_set.get_decl(decl_id),
179179
false,
180180
)),
181-
Id::Module(module_id) => {
181+
Id::Module(module_id, _) => {
182182
let description = working_set
183183
.get_module_comments(module_id)?
184184
.iter()

crates/nu-lsp/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) enum Id {
4343
Variable(VarId),
4444
Declaration(DeclId),
4545
Value(Type),
46-
Module(ModuleId),
46+
Module(ModuleId, Vec<u8>),
4747
CellPath(VarId, Vec<PathMember>),
4848
External(String),
4949
}

crates/nu-lsp/src/symbols.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ impl SymbolCache {
124124
range,
125125
})
126126
}
127-
Id::Module(module_id) => {
127+
Id::Module(module_id, _) => {
128128
let module = working_set.get_module(module_id);
129129
let span = module.span?;
130130
if !doc_span.contains(span.start) {
@@ -165,7 +165,7 @@ impl SymbolCache {
165165
.chain((0..working_set.num_modules()).filter_map(|id| {
166166
Self::get_symbol_by_id(
167167
working_set,
168-
Id::Module(ModuleId::new(id)),
168+
Id::Module(ModuleId::new(id), vec![]),
169169
doc,
170170
&cached_file.covered_span,
171171
)

0 commit comments

Comments
 (0)