@@ -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
@@ -39,7 +44,11 @@ export function getGlobalDeclarations({
39
44
const diagnostics : LSP . Diagnostic [ ] = [ ]
40
45
const globalDeclarations : GlobalDeclarations = { }
41
46
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.
43
52
if ( node . type === 'ERROR' ) {
44
53
diagnostics . push (
45
54
LSP . Diagnostic . create (
@@ -50,7 +59,6 @@ export function getGlobalDeclarations({
50
59
'bash-language-server' ,
51
60
) ,
52
61
)
53
- return
54
62
}
55
63
56
64
if ( TreeSitterUtil . isDefinition ( node ) ) {
@@ -61,6 +69,8 @@ export function getGlobalDeclarations({
61
69
globalDeclarations [ word ] = symbol
62
70
}
63
71
}
72
+
73
+ return followChildren
64
74
} )
65
75
66
76
return { diagnostics, globalDeclarations }
0 commit comments