@@ -7,35 +7,38 @@ export const ReferenceProvider: vscode.ReferenceProvider = {
7
7
// vscode.window.showInformationMessage(JSON.stringify("references"));
8
8
const tree = getTrees ( document ) . jsonTree ;
9
9
const point = toPoint ( position ) ;
10
- let queryString : string ;
11
10
// vscode.window.showInformationMessage(JSON.stringify(tree.rootNode.namedDescendantForPosition(point).text));
12
11
13
- queryString = `;scm
12
+ const cursorQuery = `;scm
14
13
;(json (scopeName (value) @scopeName))
15
14
(include (value) @include)
16
15
(repo (key) @repo)
17
16
` ;
18
- const referenceQueryCapture = queryNode ( tree . rootNode , queryString , point ) ;
19
- if ( referenceQueryCapture == null ) {
17
+ const cursorQueryCapture = queryNode ( tree . rootNode , cursorQuery , point ) ;
18
+ if ( cursorQueryCapture == null ) {
20
19
return ;
21
20
}
22
- const node = referenceQueryCapture . node ;
23
- const text = node . text ;
21
+ const cursorNode = cursorQueryCapture . node ;
22
+ const cursorText = cursorNode . text ;
24
23
// vscode.window.showInformationMessage(JSON.stringify(node.toString()));
25
24
26
- queryString = `(json (scopeName (value) @scopeName))` ;
27
- const rootScopeName = queryNode ( tree . rootNode , queryString ) . pop ( ) . text ;
25
+ const rootScopeNameQuery = `(json (scopeName (value) @scopeName))` ;
26
+ const rootScopeName = queryNode ( tree . rootNode , rootScopeNameQuery ) . pop ( ) ?. node ?. text ?? '' ;
28
27
29
- switch ( referenceQueryCapture . name ) {
28
+ let queryString : string ;
29
+ switch ( cursorQueryCapture . name ) {
30
30
case 'repo' :
31
- if ( text == '$self' || text == '$base' ) {
31
+ if ( cursorText == '$self' || cursorText == '$base' ) {
32
32
return ;
33
33
}
34
- queryString =
35
- `(include (value) @include (#eq? @include "#${ text } "))` +
36
- `(repo (key) @repo (#eq? @repo "${ text } "))` ;
37
- if ( rootScopeName && text )
38
- queryString += `(include (value) @include (#eq? @include "${ rootScopeName } #${ text } "))` ;
34
+ queryString = `;scm
35
+ (include
36
+ (value
37
+ (scopeName)? @_scopeName (#eq? @_scopeName "${ rootScopeName } ")
38
+ (ruleName) @_ruleName (#eq? @_ruleName "${ cursorText } ")
39
+ ) @include
40
+ )
41
+ ` ;
39
42
break ;
40
43
case 'include' : // move to own function. Can be used for code-lens and symbol-highlight etc
41
44
/*
@@ -63,18 +66,18 @@ export const ReferenceProvider: vscode.ReferenceProvider = {
63
66
source.other#$self other
64
67
source.other#include other#include
65
68
*/
66
- if ( text == '' ) { // *fail
69
+ if ( cursorText == '' ) { // *fail
67
70
// vscode.window.showInformationMessage("*fail");
68
71
return ;
69
72
}
70
73
71
- if ( node . childForFieldName ( 'base' ) ) { // $base
74
+ if ( cursorNode . childForFieldName ( 'base' ) ) { // $base
72
75
// vscode.window.showInformationMessage("$base");
73
76
queryString = `(include (value) @include (#match? @include "^([^#]*#)?\\\\$base$"))` ;
74
77
}
75
78
else {
76
- const scopeName = node . childForFieldName ( 'scopeName' ) ?. text ;
77
- const ruleName = node . childForFieldName ( 'ruleName' ) ?. text ;
79
+ const scopeName = cursorNode . childForFieldName ( 'scopeName' ) ?. text ;
80
+ const ruleName = cursorNode . childForFieldName ( 'ruleName' ) ?. text ;
78
81
if ( scopeName && scopeName != rootScopeName ) {
79
82
if ( ruleName ) { // other#include
80
83
// vscode.window.showInformationMessage("other#include");
@@ -88,7 +91,7 @@ export const ReferenceProvider: vscode.ReferenceProvider = {
88
91
`(include (value) @include (#eq? @include "${ scopeName } #$self"))` ;
89
92
}
90
93
}
91
- else if ( node . childForFieldName ( 'self' ) || ( scopeName && ! ruleName ) ) { // $self
94
+ else if ( cursorNode . childForFieldName ( 'self' ) || ( scopeName && ! ruleName ) ) { // $self
92
95
// vscode.window.showInformationMessage("$self");
93
96
queryString =
94
97
`(include (value) @include (#match? @include "^#?\\\\$self$"))` ;
@@ -137,7 +140,7 @@ export const ReferenceProvider: vscode.ReferenceProvider = {
137
140
138
141
139
142
if ( ( /* referenceQueryCapture.name == 'repo' && */ locations . length == 0 ) ||
140
- ( referenceQueryCapture . name == 'include' && locations . length <= 1 ) ) {
143
+ ( cursorQueryCapture . name == 'include' && locations . length <= 1 ) ) {
141
144
for ( const queryCapture of queryCaptures ) {
142
145
if ( queryCapture . name == 'repo' ) {
143
146
const range = toRange ( queryCapture . node ) ; // .parent?
0 commit comments