Skip to content

Commit 2958795

Browse files
committed
Traverse additional nodes for global definitions
1 parent 23f05c5 commit 2958795

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

server/src/util/declarations.ts

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,16 @@ const TREE_SITTER_TYPE_TO_LSP_KIND: { [type: string]: LSP.SymbolKind | undefined
1313
export type GlobalDeclarations = { [word: string]: LSP.SymbolInformation }
1414
export type Declarations = { [word: string]: LSP.SymbolInformation[] }
1515

16+
const GLOBAL_DECLARATION_LEAF_NODE_TYPES = new Set([
17+
'if_statement',
18+
'function_definition',
19+
])
20+
1621
/**
1722
* Returns declarations (functions or variables) from a given root node
1823
* that would be available after sourcing the file. This currently does
19-
* not include global variables defined inside function as we do not do
20-
* any flow tracing.
24+
* not include global variables defined inside if statements or functions
25+
* as we do not do any flow tracing.
2126
*
2227
* Will only return one declaration per symbol name – the latest definition.
2328
* This behavior is consistent with how Bash behaves, but differs between
@@ -39,7 +44,11 @@ export function getGlobalDeclarations({
3944
const diagnostics: LSP.Diagnostic[] = []
4045
const globalDeclarations: GlobalDeclarations = {}
4146

42-
tree.rootNode.children.forEach((node) => {
47+
TreeSitterUtil.forEach(tree.rootNode, (node) => {
48+
const followChildren = !GLOBAL_DECLARATION_LEAF_NODE_TYPES.has(node.type)
49+
50+
// TODO: is there really a need for this error
51+
// Rather weird that this is here.
4352
if (node.type === 'ERROR') {
4453
diagnostics.push(
4554
LSP.Diagnostic.create(
@@ -50,7 +59,6 @@ export function getGlobalDeclarations({
5059
'bash-language-server',
5160
),
5261
)
53-
return
5462
}
5563

5664
if (TreeSitterUtil.isDefinition(node)) {
@@ -61,6 +69,8 @@ export function getGlobalDeclarations({
6169
globalDeclarations[word] = symbol
6270
}
6371
}
72+
73+
return followChildren
6474
})
6575

6676
return { diagnostics, globalDeclarations }

server/src/util/tree-sitter.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
import { Range } from 'vscode-languageserver/node'
22
import { SyntaxNode } from 'web-tree-sitter'
33

4-
export function forEach(node: SyntaxNode, cb: (n: SyntaxNode) => void) {
5-
cb(node)
6-
if (node.children.length) {
7-
node.children.forEach((n) => forEach(n, cb))
4+
/**
5+
* Recursively iterate over all nodes in a tree.
6+
*
7+
* @param node The node to start iterating from
8+
* @param callback The callback to call for each node. Return false to stop following children.
9+
*/
10+
export function forEach(node: SyntaxNode, callback: (n: SyntaxNode) => void | boolean) {
11+
const followChildren = callback(node) !== false
12+
if (followChildren && node.children.length) {
13+
node.children.forEach((n) => forEach(n, callback))
814
}
915
}
1016

0 commit comments

Comments
 (0)