@@ -98,28 +98,35 @@ export class TreeSitterQuery {
9898 start ?: Position ,
9999 end ?: Position ,
100100 ) : QueryMatch [ ] {
101- const checkCaptures = ide ( ) . runMode !== "production" ;
101+ const shouldCheckCaptures = ide ( ) . runMode !== "production" ;
102+ const matches = this . getTreeMatches ( document , start , end ) ;
102103 const results : QueryMatch [ ] = [ ] ;
103104
104- const matches = this . query . matches (
105- this . treeSitter . getTree ( document ) . rootNode ,
106- {
107- startPosition : start != null ? positionToPoint ( start ) : undefined ,
108- endPosition : end != null ? positionToPoint ( end ) : undefined ,
109- } ,
110- ) ;
111-
112105 for ( const match of matches ) {
113106 const mutableMatch = createMutableQueryMatch ( document , match ) ;
114107
115- if ( this . runPredicates ( mutableMatch ) ) {
116- results . push ( createQueryMatch ( mutableMatch , checkCaptures ) ) ;
108+ if ( ! this . runPredicates ( mutableMatch ) ) {
109+ continue ;
117110 }
111+
112+ results . push ( createQueryMatch ( mutableMatch , shouldCheckCaptures ) ) ;
118113 }
119114
120115 return results ;
121116 }
122117
118+ private getTreeMatches (
119+ document : TextDocument ,
120+ start ?: Position ,
121+ end ?: Position ,
122+ ) {
123+ const { rootNode } = this . treeSitter . getTree ( document ) ;
124+ return this . query . matches ( rootNode , {
125+ startPosition : start != null ? positionToPoint ( start ) : undefined ,
126+ endPosition : end != null ? positionToPoint ( end ) : undefined ,
127+ } ) ;
128+ }
129+
123130 private runPredicates ( match : MutableQueryMatch ) : boolean {
124131 for ( const predicate of this . patternPredicates [ match . patternIdx ] ) {
125132 if ( ! predicate ( match ) ) {
@@ -150,7 +157,7 @@ function createMutableQueryMatch(
150157
151158function createQueryMatch (
152159 match : MutableQueryMatch ,
153- checkCaptures : boolean ,
160+ shouldCheckCaptures : boolean ,
154161) : QueryMatch {
155162 const result : MutableQueryCapture [ ] = [ ] ;
156163 const map = new Map <
@@ -188,17 +195,21 @@ function createQueryMatch(
188195 }
189196 }
190197
191- if ( checkCaptures ) {
192- for ( const captureGroup of map . values ( ) ) {
193- const capturesAreValid = checkCaptureStartEnd (
194- rewriteStartOfEndOf ( captureGroup . captures ) ,
195- ide ( ) . messages ,
196- ) ;
197- if ( ! capturesAreValid && ide ( ) . runMode === "test" ) {
198- throw new Error ( "Invalid captures" ) ;
199- }
200- }
198+ if ( shouldCheckCaptures ) {
199+ checkCaptures ( Array . from ( map . values ( ) ) ) ;
201200 }
202201
203202 return { captures : result } ;
204203}
204+
205+ function checkCaptures ( matches : { captures : MutableQueryCapture [ ] } [ ] ) {
206+ for ( const match of matches ) {
207+ const capturesAreValid = checkCaptureStartEnd (
208+ rewriteStartOfEndOf ( match . captures ) ,
209+ ide ( ) . messages ,
210+ ) ;
211+ if ( ! capturesAreValid && ide ( ) . runMode === "test" ) {
212+ throw new Error ( "Invalid captures" ) ;
213+ }
214+ }
215+ }
0 commit comments