Skip to content

Commit 187971a

Browse files
committed
Improve findOriginalDeclaration heuristics
Handle multiple variable assignments and declarations per declaration command when searching inside functions
1 parent d94155f commit 187971a

File tree

2 files changed

+19
-13
lines changed

2 files changed

+19
-13
lines changed

server/src/analyser.ts

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -382,19 +382,21 @@ export default class Analyzer {
382382
return false
383383
}
384384

385-
for (const a of n.descendantsOfType('variable_assignment')) {
386-
const definedVariable = a.descendantsOfType('variable_name').at(0)
387-
// In cases of var="$var", if $var is being renamed, the
388-
// definition containing $var should be skipped and a higher scope
389-
// should be checked.
385+
for (const v of n.descendantsOfType('variable_name')) {
386+
if (TreeSitterUtil.findParentOfType(v, ['simple_expansion', 'expansion'])) {
387+
continue
388+
}
389+
390+
// In cases of var="$var", if $var is being renamed, var should be
391+
// skipped and a higher scope should be checked for the original
392+
// declaration.
390393
const definedVariableInExpression =
391-
a.endPosition.row >= position.line &&
392-
!!definedVariable &&
393-
(definedVariable.endPosition.column < position.character ||
394-
definedVariable.endPosition.row < position.line)
394+
n.endPosition.row >= position.line &&
395+
(v.endPosition.column < position.character ||
396+
v.endPosition.row < position.line)
395397

396-
if (definedVariable?.text === word && !definedVariableInExpression) {
397-
declaration = definedVariable
398+
if (v.text === word && !definedVariableInExpression) {
399+
declaration = v
398400
continueSearching = false
399401
break
400402
}

server/src/util/tree-sitter.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ export function findParent(
5757
return null
5858
}
5959

60-
export function findParentOfType(start: SyntaxNode, type: string) {
61-
return findParent(start, (n) => n.type === type)
60+
export function findParentOfType(start: SyntaxNode, type: string | string[]) {
61+
if (typeof type === 'string') {
62+
return findParent(start, (n) => n.type === type)
63+
}
64+
65+
return findParent(start, (n) => type.includes(n.type))
6266
}

0 commit comments

Comments
 (0)