@@ -7,35 +7,38 @@ export const ReferenceProvider: vscode.ReferenceProvider = {
77 // vscode.window.showInformationMessage(JSON.stringify("references"));
88 const tree = getTrees ( document ) . jsonTree ;
99 const point = toPoint ( position ) ;
10- let queryString : string ;
1110 // vscode.window.showInformationMessage(JSON.stringify(tree.rootNode.namedDescendantForPosition(point).text));
1211
13- queryString = `;scm
12+ const cursorQuery = `;scm
1413 ;(json (scopeName (value) @scopeName))
1514 (include (value) @include)
1615 (repo (key) @repo)
1716 ` ;
18- const referenceQueryCapture = queryNode ( tree . rootNode , queryString , point ) ;
19- if ( referenceQueryCapture == null ) {
17+ const cursorQueryCapture = queryNode ( tree . rootNode , cursorQuery , point ) ;
18+ if ( cursorQueryCapture == null ) {
2019 return ;
2120 }
22- const node = referenceQueryCapture . node ;
23- const text = node . text ;
21+ const cursorNode = cursorQueryCapture . node ;
22+ const cursorText = cursorNode . text ;
2423 // vscode.window.showInformationMessage(JSON.stringify(node.toString()));
2524
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 ?? '' ;
2827
29- switch ( referenceQueryCapture . name ) {
28+ let queryString : string ;
29+ switch ( cursorQueryCapture . name ) {
3030 case 'repo' :
31- if ( text == '$self' || text == '$base' ) {
31+ if ( cursorText == '$self' || cursorText == '$base' ) {
3232 return ;
3333 }
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+ ` ;
3942 break ;
4043 case 'include' : // move to own function. Can be used for code-lens and symbol-highlight etc
4144 /*
@@ -63,18 +66,18 @@ export const ReferenceProvider: vscode.ReferenceProvider = {
6366 source.other#$self other
6467 source.other#include other#include
6568 */
66- if ( text == '' ) { // *fail
69+ if ( cursorText == '' ) { // *fail
6770 // vscode.window.showInformationMessage("*fail");
6871 return ;
6972 }
7073
71- if ( node . childForFieldName ( 'base' ) ) { // $base
74+ if ( cursorNode . childForFieldName ( 'base' ) ) { // $base
7275 // vscode.window.showInformationMessage("$base");
7376 queryString = `(include (value) @include (#match? @include "^([^#]*#)?\\\\$base$"))` ;
7477 }
7578 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 ;
7881 if ( scopeName && scopeName != rootScopeName ) {
7982 if ( ruleName ) { // other#include
8083 // vscode.window.showInformationMessage("other#include");
@@ -88,7 +91,7 @@ export const ReferenceProvider: vscode.ReferenceProvider = {
8891 `(include (value) @include (#eq? @include "${ scopeName } #$self"))` ;
8992 }
9093 }
91- else if ( node . childForFieldName ( 'self' ) || ( scopeName && ! ruleName ) ) { // $self
94+ else if ( cursorNode . childForFieldName ( 'self' ) || ( scopeName && ! ruleName ) ) { // $self
9295 // vscode.window.showInformationMessage("$self");
9396 queryString =
9497 `(include (value) @include (#match? @include "^#?\\\\$self$"))` ;
@@ -137,7 +140,7 @@ export const ReferenceProvider: vscode.ReferenceProvider = {
137140
138141
139142 if ( ( /* referenceQueryCapture.name == 'repo' && */ locations . length == 0 ) ||
140- ( referenceQueryCapture . name == 'include' && locations . length <= 1 ) ) {
143+ ( cursorQueryCapture . name == 'include' && locations . length <= 1 ) ) {
141144 for ( const queryCapture of queryCaptures ) {
142145 if ( queryCapture . name == 'repo' ) {
143146 const range = toRange ( queryCapture . node ) ; // .parent?
0 commit comments