Skip to content

Commit 420417a

Browse files
committed
Consider variables in read commands as declarations
1 parent 4c9a218 commit 420417a

File tree

3 files changed

+25
-7
lines changed

3 files changed

+25
-7
lines changed

server/src/analyser.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -785,13 +785,7 @@ export default class Analyzer {
785785
}
786786
}
787787

788-
if (
789-
node.type === 'word' &&
790-
node.parent?.type === 'command' &&
791-
node.parent.firstChild?.text === 'read' &&
792-
!node.text.startsWith('-') &&
793-
!/^-.*[dinNptu]$/.test(node.previousSibling?.text ?? '')
794-
) {
788+
if (TreeSitterUtil.isVariableInReadCommand(node)) {
795789
return {
796790
word: node.text,
797791
range: TreeSitterUtil.range(node),

server/src/util/declarations.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,16 @@ export function findDeclarationUsingGlobalSemantics({
361361
return true
362362
}
363363

364+
if (
365+
kind === LSP.SymbolKind.Variable &&
366+
TreeSitterUtil.isVariableInReadCommand(n) &&
367+
n.text === word
368+
) {
369+
declaration = n
370+
continueSearching = false
371+
return false
372+
}
373+
364374
if (
365375
kind === LSP.SymbolKind.Function &&
366376
n.type === 'function_definition' &&

server/src/util/tree-sitter.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ export function isReference(n: SyntaxNode): boolean {
4343
}
4444
}
4545

46+
export function isVariableInReadCommand(n: SyntaxNode): boolean {
47+
if (
48+
n.type === 'word' &&
49+
n.parent?.type === 'command' &&
50+
n.parent.firstChild?.text === 'read' &&
51+
!n.text.startsWith('-') &&
52+
!/^-.*[dinNptu]$/.test(n.previousSibling?.text ?? '')
53+
) {
54+
return true
55+
}
56+
57+
return false
58+
}
59+
4660
export function findParent(
4761
start: SyntaxNode,
4862
predicate: (n: SyntaxNode) => boolean,

0 commit comments

Comments
 (0)