File tree Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Expand file tree Collapse file tree 2 files changed +22
-7
lines changed Original file line number Diff line number Diff line change @@ -13,11 +13,16 @@ const TREE_SITTER_TYPE_TO_LSP_KIND: { [type: string]: LSP.SymbolKind | undefined
13
13
export type GlobalDeclarations = { [ word : string ] : LSP . SymbolInformation }
14
14
export type Declarations = { [ word : string ] : LSP . SymbolInformation [ ] }
15
15
16
+ const GLOBAL_DECLARATION_LEAF_NODE_TYPES = new Set ( [
17
+ 'if_statement' ,
18
+ 'function_definition' ,
19
+ ] )
20
+
16
21
/**
17
22
* Returns declarations (functions or variables) from a given root node
18
23
* 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.
21
26
*
22
27
* Will only return one declaration per symbol name – the latest definition.
23
28
* This behavior is consistent with how Bash behaves, but differs between
@@ -35,14 +40,18 @@ export function getGlobalDeclarations({
35
40
} ) : GlobalDeclarations {
36
41
const globalDeclarations : GlobalDeclarations = { }
37
42
38
- tree . rootNode . children . forEach ( ( node ) => {
43
+ TreeSitterUtil . forEach ( tree . rootNode , ( node ) => {
44
+ const followChildren = ! GLOBAL_DECLARATION_LEAF_NODE_TYPES . has ( node . type )
45
+
39
46
if ( TreeSitterUtil . isDefinition ( node ) ) {
40
47
const symbol = nodeToSymbolInformation ( { node, uri } )
41
48
if ( symbol ) {
42
49
const word = symbol . name
43
50
globalDeclarations [ word ] = symbol
44
51
}
45
52
}
53
+
54
+ return followChildren
46
55
} )
47
56
48
57
return globalDeclarations
Original file line number Diff line number Diff line change 1
1
import { Range } from 'vscode-languageserver/node'
2
2
import { SyntaxNode } from 'web-tree-sitter'
3
3
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 ) )
8
14
}
9
15
}
10
16
You can’t perform that action at this time.
0 commit comments