1- import type { Position , TextDocument , TreeSitter } from "@cursorless/common" ;
1+ import type {
2+ Mutable ,
3+ Position ,
4+ TextDocument ,
5+ TreeSitter ,
6+ } from "@cursorless/common" ;
27import type * as treeSitter from "web-tree-sitter" ;
38import { ide } from "../../singletons/ide.singleton" ;
49import {
@@ -13,10 +18,13 @@ import { parsePredicatesWithErrorHandling } from "./parsePredicatesWithErrorHand
1318import { positionToPoint } from "./positionToPoint" ;
1419import type {
1520 MutableQueryCapture ,
16- MutableQueryMatch ,
21+ PatternPredicate ,
22+ QueryCapture ,
1723 QueryMatch ,
1824} from "./QueryCapture" ;
1925import {
26+ createTestQueryCapture ,
27+ getStartOfEndOfNodeRange ,
2028 getStartOfEndOfRange ,
2129 rewriteStartOfEndOf ,
2230} from "./rewriteStartOfEndOf" ;
@@ -42,7 +50,7 @@ export class TreeSitterQuery {
4250 * array corresponds to a pattern, and each element of the inner array
4351 * corresponds to a predicate for that pattern.
4452 */
45- private patternPredicates : ( ( match : MutableQueryMatch ) => boolean ) [ ] [ ] ,
53+ private patternPredicates : PatternPredicate [ ] [ ] ,
4654 ) {
4755 this . shouldCheckCaptures = ide ( ) . runMode !== "production" ;
4856 }
@@ -126,30 +134,22 @@ export class TreeSitterQuery {
126134 continue ;
127135 }
128136
129- const hasPatternPredicates =
130- this . patternPredicates [ match . patternIndex ] . length > 0 ;
131-
132- const mutableMatch = this . createMutableQueryMatch (
133- document ,
134- match ,
135- // If there are pattern predicates, we need to include all captures when
136- // creating the mutable match, since the predicates may depend on any of
137- // the captures.
138- ! hasPatternPredicates ? captureNameFilter : undefined ,
139- ) ;
140-
141- if ( ! this . runPredicates ( mutableMatch ) ) {
142- continue ;
143- }
144-
145- const queryMatch = this . createQueryMatch (
146- mutableMatch ,
147- // We only need to filter here if we didn't filter in createMutableQueryMatch()
148- hasPatternPredicates ? captureNameFilter : undefined ,
149- ) ;
150-
151- if ( queryMatch != null ) {
152- results . push ( queryMatch ) ;
137+ const patternPredicates = this . patternPredicates [ match . patternIndex ] ;
138+
139+ // The split path here is to avoid creating query captures if there are no
140+ // predicates, Solely for performance.
141+ const captures =
142+ patternPredicates . length > 0
143+ ? this . createQueryCapturesWithPredicates (
144+ document ,
145+ match ,
146+ patternPredicates ,
147+ captureNameFilter ,
148+ )
149+ : this . createQueryCapturesWithoutPredicates ( match , captureNameFilter ) ;
150+
151+ if ( captures . length > 0 ) {
152+ results . push ( { captures } ) ;
153153 }
154154 }
155155
@@ -158,8 +158,8 @@ export class TreeSitterQuery {
158158
159159 private getTreeMatches (
160160 document : TextDocument ,
161- start ? : Position ,
162- end ? : Position ,
161+ start : Position | undefined ,
162+ end : Position | undefined ,
163163 ) {
164164 const { rootNode } = this . treeSitter . getTree ( document ) ;
165165 return this . query . matches ( rootNode , {
@@ -168,79 +168,62 @@ export class TreeSitterQuery {
168168 } ) ;
169169 }
170170
171- private createMutableQueryMatch (
171+ private createQueryCapturesWithPredicates (
172172 document : TextDocument ,
173173 match : treeSitter . QueryMatch ,
174+ predicates : PatternPredicate [ ] ,
174175 captureNameFilter : Set < number > | undefined ,
175- ) : MutableQueryMatch {
176+ ) : QueryCapture [ ] {
176177 const captures : MutableQueryCapture [ ] = [ ] ;
177178
178- for ( const { name, node } of match . captures ) {
179- if (
180- captureNameFilter != null &&
181- ! captureNameFilter . has ( getNormalizedCaptureIndex ( name ) )
182- ) {
183- continue ;
184- }
185-
179+ for ( const capture of match . captures ) {
186180 captures . push ( {
187- name,
188- node,
181+ name : capture . name ,
182+ node : capture . node ,
189183 document,
190- range : getNodeRange ( node ) ,
191- insertionDelimiter : undefined ,
184+ range : getNodeRange ( capture . node ) ,
192185 allowMultiple : false ,
193- hasError : ( ) => isContainedInErrorNode ( node ) ,
186+ insertionDelimiter : undefined ,
187+ hasError : ( ) => isContainedInErrorNode ( capture . node ) ,
194188 } ) ;
195189 }
196190
197- return {
198- patternIdx : match . patternIndex ,
199- captures,
200- } ;
201- }
202-
203- private runPredicates ( match : MutableQueryMatch ) : boolean {
204- for ( const predicate of this . patternPredicates [ match . patternIdx ] ) {
205- if ( ! predicate ( match ) ) {
206- return false ;
191+ for ( const predicate of predicates ) {
192+ if ( ! predicate ( { captures } ) ) {
193+ return [ ] ;
207194 }
208195 }
209- return true ;
210- }
211196
212- private createQueryMatch (
213- match : MutableQueryMatch ,
214- captureNameFilter : Set < number > | undefined ,
215- ) : QueryMatch | undefined {
216- const result : MutableQueryCapture [ ] = [ ] ;
197+ const result : QueryCapture [ ] = [ ] ;
217198 const map = new Map <
218199 string ,
219- { acc : MutableQueryCapture ; captures : MutableQueryCapture [ ] }
200+ { acc : Mutable < QueryCapture > ; captures : QueryCapture [ ] }
220201 > ( ) ;
221202
222203 // Merge the ranges of all captures with the same name into a single
223204 // range and return one capture with that name. We consider captures
224205 // with names `@foo`, `@foo.start`, and `@foo.end` to have the same
225206 // name, for which we'd return a capture with name `foo`.
226207
227- for ( const capture of match . captures ) {
208+ for ( const capture of captures ) {
228209 if (
229210 captureNameFilter != null &&
230211 ! captureNameFilter . has ( getNormalizedCaptureIndex ( capture . name ) )
231212 ) {
232213 continue ;
233214 }
215+
234216 const name = getNormalizedCaptureName ( capture . name ) ;
235217 const range = getStartOfEndOfRange ( capture ) ;
236218 const existing = map . get ( name ) ;
237219
238220 if ( existing == null ) {
239221 const captures = [ capture ] ;
240- const acc = {
241- ...capture ,
222+ const acc : QueryCapture = {
242223 name,
243224 range,
225+ allowMultiple : capture . allowMultiple ,
226+ insertionDelimiter : capture . insertionDelimiter ,
244227 hasError : ( ) => captures . some ( ( c ) => c . hasError ( ) ) ,
245228 } ;
246229 result . push ( acc ) ;
@@ -255,26 +238,77 @@ export class TreeSitterQuery {
255238 }
256239 }
257240
258- if ( result . length === 0 ) {
259- return undefined ;
241+ if ( this . shouldCheckCaptures ) {
242+ checkCaptures ( Array . from ( map . values ( ) ) ) ;
243+ }
244+
245+ return result ;
246+ }
247+
248+ private createQueryCapturesWithoutPredicates (
249+ match : treeSitter . QueryMatch ,
250+ captureNameFilter : Set < number > | undefined ,
251+ ) : QueryCapture [ ] {
252+ const result : QueryCapture [ ] = [ ] ;
253+ const map = new Map <
254+ string ,
255+ {
256+ acc : Mutable < QueryCapture > ;
257+ captures : treeSitter . QueryCapture [ ] ;
258+ }
259+ > ( ) ;
260+
261+ for ( const capture of match . captures ) {
262+ if (
263+ captureNameFilter != null &&
264+ ! captureNameFilter . has ( getNormalizedCaptureIndex ( capture . name ) )
265+ ) {
266+ continue ;
267+ }
268+
269+ const name = getNormalizedCaptureName ( capture . name ) ;
270+ const range = getStartOfEndOfNodeRange ( capture . name , capture . node ) ;
271+ const existing = map . get ( name ) ;
272+
273+ if ( existing == null ) {
274+ const captures = [ capture ] ;
275+ const acc : QueryCapture = {
276+ name,
277+ range,
278+ allowMultiple : false ,
279+ insertionDelimiter : undefined ,
280+ hasError : ( ) => captures . some ( ( c ) => isContainedInErrorNode ( c . node ) ) ,
281+ } ;
282+ result . push ( acc ) ;
283+ map . set ( name , { acc, captures } ) ;
284+ } else {
285+ existing . acc . range = existing . acc . range . union ( range ) ;
286+ existing . captures . push ( capture ) ;
287+ }
260288 }
261289
262290 if ( this . shouldCheckCaptures ) {
263- this . checkCaptures ( Array . from ( map . values ( ) ) ) ;
291+ checkCaptures (
292+ Array . from ( map . values ( ) , ( v ) => ( {
293+ captures : v . captures . map ( ( c ) =>
294+ createTestQueryCapture ( c . name , getNodeRange ( c . node ) ) ,
295+ ) ,
296+ } ) ) ,
297+ ) ;
264298 }
265299
266- return { captures : result } ;
300+ return result ;
267301 }
302+ }
268303
269- private checkCaptures ( matches : { captures : MutableQueryCapture [ ] } [ ] ) {
270- for ( const match of matches ) {
271- const capturesAreValid = checkCaptureStartEnd (
272- rewriteStartOfEndOf ( match . captures ) ,
273- ide ( ) . messages ,
274- ) ;
275- if ( ! capturesAreValid && ide ( ) . runMode === "test" ) {
276- throw new Error ( "Invalid captures" ) ;
277- }
304+ function checkCaptures ( matches : { captures : QueryCapture [ ] } [ ] ) {
305+ for ( const match of matches ) {
306+ const capturesAreValid = checkCaptureStartEnd (
307+ rewriteStartOfEndOf ( match . captures ) ,
308+ ide ( ) . messages ,
309+ ) ;
310+ if ( ! capturesAreValid && ide ( ) . runMode === "test" ) {
311+ throw new Error ( "Invalid captures" ) ;
278312 }
279313 }
280314}
0 commit comments