@@ -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