Skip to content

Commit 5cf827e

Browse files
committed
fix: overlay use with prefixes
1 parent 5271c43 commit 5cf827e

File tree

1 file changed

+24
-12
lines changed

1 file changed

+24
-12
lines changed

crates/nu-lsp/src/ast.rs

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,33 @@ fn try_find_id_in_def(
134134
}
135135
}
136136
}
137-
let (name, span) = strip_quotes(span?, working_set);
138-
let decl_id = Id::Declaration(working_set.find_decl(&name).or_else(|| {
139-
// for defs inside def
137+
138+
let block_span_of_this_def = call.positional_iter().last()?.span;
139+
let decl_on_spot = |decl_id: &DeclId| -> bool {
140+
working_set
141+
.get_decl(*decl_id)
142+
.block_id()
143+
.and_then(|block_id| working_set.get_block(block_id).span)
144+
.is_some_and(|block_span| block_span == block_span_of_this_def)
145+
};
146+
147+
let (_, span) = strip_quotes(span?, working_set);
148+
let id_found = if let Some(id_r) = id_ref {
149+
let Id::Declaration(decl_id_ref) = id_r else {
150+
return None;
151+
};
152+
decl_on_spot(decl_id_ref).then_some(id_r.clone())?
153+
} else {
154+
// Find declaration by name, e.g. `workspace.find_decl`, is not reliable
155+
// considering shadowing and overlay prefixes
140156
// TODO: get scope by position
141157
// https://github.com/nushell/nushell/issues/15291
142-
(0..working_set.num_decls()).rev().find_map(|id| {
158+
Id::Declaration((0..working_set.num_decls()).rev().find_map(|id| {
143159
let decl_id = DeclId::new(id);
144-
let decl = working_set.get_decl(decl_id);
145-
let span = working_set.get_block(decl.block_id()?).span?;
146-
call.span().contains_span(span).then_some(decl_id)
147-
})
148-
})?);
149-
id_ref
150-
.is_none_or(|id_r| decl_id == *id_r)
151-
.then_some((decl_id, span))
160+
decl_on_spot(&decl_id).then_some(decl_id)
161+
})?)
162+
};
163+
Some((id_found, span))
152164
}
153165

154166
/// For situations like

0 commit comments

Comments
 (0)