Skip to content

Commit 558a01b

Browse files
committed
Improve findOriginalDeclaration heuristics
1 parent c61370c commit 558a01b

File tree

1 file changed

+27
-25
lines changed

1 file changed

+27
-25
lines changed

server/src/analyser.ts

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,9 @@ export default class Analyzer {
315315
}
316316

317317
let definedSymbol: Parser.SyntaxNode | null | undefined
318-
// Check for var="$var" cases
318+
// In cases of var="$var", if $var is being renamed, the definition
319+
// containing $var should be skipped and a higher scope should be
320+
// checked.
319321
let definedVariableInExpression = false
320322

321323
if (
@@ -341,10 +343,6 @@ export default class Analyzer {
341343
continueSearching = n.type === 'command'
342344
}
343345

344-
if (n.type === 'function_definition') {
345-
return false
346-
}
347-
348346
return true
349347
})
350348
}
@@ -366,23 +364,25 @@ export default class Analyzer {
366364
}
367365

368366
if (n.type === 'declaration_command') {
369-
const isLocal = ['local', 'declare', 'typeset'].includes(
370-
n.firstChild?.text as any,
371-
)
372-
const definedVariable = n.descendantsOfType('variable_name').at(0)
373-
// Check for var="$var" cases
374-
const definedVariableInExpression =
375-
n.endPosition.row >= position.line &&
376-
definedVariable &&
377-
(definedVariable.endPosition.column < position.character ||
378-
definedVariable.endPosition.row < position.line)
379-
if (
380-
isLocal &&
381-
definedVariable?.text === word &&
382-
!definedVariableInExpression
383-
) {
384-
declaration = definedVariable
385-
continueSearching = false
367+
if (!['local', 'declare', 'typeset'].includes(n.firstChild?.text as any)) {
368+
return false
369+
}
370+
371+
for (const a of n.descendantsOfType('variable_assignment')) {
372+
const definedVariable = a.descendantsOfType('variable_name').at(0)
373+
// In cases of var="$var", if $var is being renamed, the
374+
// definition containing $var should be skipped and a higher scope
375+
// should be checked.
376+
const definedVariableInExpression =
377+
a.endPosition.row >= position.line &&
378+
!!definedVariable &&
379+
(definedVariable.endPosition.column < position.character ||
380+
definedVariable.endPosition.row < position.line)
381+
382+
if (definedVariable?.text === word && !definedVariableInExpression) {
383+
declaration = definedVariable
384+
continueSearching = false
385+
}
386386
}
387387

388388
return false
@@ -403,12 +403,14 @@ export default class Analyzer {
403403
}
404404

405405
if (!parent && (!declaration || continueSearching)) {
406-
const uris = this.getReachableUris({ fromUri: uri })
407-
408-
for (const u of uris) {
406+
for (const u of this.getReachableUris({ fromUri: uri })) {
409407
const root = this.uriToAnalyzedDocument[u]?.tree.rootNode
410408

411409
if (root) {
410+
if (u !== uri) {
411+
boundary = root.endPosition.row
412+
}
413+
412414
findUsingGlobalSemantics(root)
413415
}
414416

0 commit comments

Comments
 (0)