@@ -140,23 +140,43 @@ export class BracketPairsTree extends Disposable {
140
140
141
141
const node = this . initialAstWithoutTokens || this . astWithTokens ! ;
142
142
const context = new CollectBracketPairsContext ( result , includeMinIndentation , this . textModel ) ;
143
- collectBracketPairs ( node , lengthZero , node . length , startLength , endLength , context ) ;
143
+ collectBracketPairs ( node , lengthZero , node . length , startLength , endLength , context , 0 , new Map ( ) ) ;
144
144
145
145
return result ;
146
146
}
147
147
}
148
148
149
- function collectBrackets ( node : AstNode , nodeOffsetStart : Length , nodeOffsetEnd : Length , startOffset : Length , endOffset : Length , result : BracketInfo [ ] , level : number = 0 , levelPerBracketType ?: Map < string , number > ) : void {
149
+ function collectBrackets (
150
+ node : AstNode ,
151
+ nodeOffsetStart : Length ,
152
+ nodeOffsetEnd : Length ,
153
+ startOffset : Length ,
154
+ endOffset : Length ,
155
+ result : BracketInfo [ ] ,
156
+ level : number ,
157
+ levelPerBracketType : Map < string , number >
158
+ ) : void {
150
159
if ( node . kind === AstNodeKind . List ) {
151
160
for ( const child of node . children ) {
152
161
nodeOffsetEnd = lengthAdd ( nodeOffsetStart , child . length ) ;
153
- if ( lengthLessThanEqual ( nodeOffsetStart , endOffset ) && lengthGreaterThanEqual ( nodeOffsetEnd , startOffset ) ) {
154
- collectBrackets ( child , nodeOffsetStart , nodeOffsetEnd , startOffset , endOffset , result , level , levelPerBracketType ) ;
162
+ if (
163
+ lengthLessThanEqual ( nodeOffsetStart , endOffset ) &&
164
+ lengthGreaterThanEqual ( nodeOffsetEnd , startOffset )
165
+ ) {
166
+ collectBrackets (
167
+ child ,
168
+ nodeOffsetStart ,
169
+ nodeOffsetEnd ,
170
+ startOffset ,
171
+ endOffset ,
172
+ result ,
173
+ level ,
174
+ levelPerBracketType
175
+ ) ;
155
176
}
156
177
nodeOffsetStart = nodeOffsetEnd ;
157
178
}
158
179
} else if ( node . kind === AstNodeKind . Pair ) {
159
-
160
180
let levelPerBracket = 0 ;
161
181
if ( levelPerBracketType ) {
162
182
let existing = levelPerBracketType . get ( node . openingBracket . text ) ;
@@ -172,25 +192,45 @@ function collectBrackets(node: AstNode, nodeOffsetStart: Length, nodeOffsetEnd:
172
192
{
173
193
const child = node . openingBracket ;
174
194
nodeOffsetEnd = lengthAdd ( nodeOffsetStart , child . length ) ;
175
- if ( lengthLessThanEqual ( nodeOffsetStart , endOffset ) && lengthGreaterThanEqual ( nodeOffsetEnd , startOffset ) ) {
195
+ if (
196
+ lengthLessThanEqual ( nodeOffsetStart , endOffset ) &&
197
+ lengthGreaterThanEqual ( nodeOffsetEnd , startOffset )
198
+ ) {
176
199
const range = lengthsToRange ( nodeOffsetStart , nodeOffsetEnd ) ;
177
- result . push ( new BracketInfo ( range , level , levelPerBracket , ! node . closingBracket ) ) ;
200
+ result . push (
201
+ new BracketInfo ( range , level , levelPerBracket , ! node . closingBracket )
202
+ ) ;
178
203
}
179
204
nodeOffsetStart = nodeOffsetEnd ;
180
205
}
181
206
182
207
if ( node . child ) {
183
208
const child = node . child ;
184
209
nodeOffsetEnd = lengthAdd ( nodeOffsetStart , child . length ) ;
185
- if ( lengthLessThanEqual ( nodeOffsetStart , endOffset ) && lengthGreaterThanEqual ( nodeOffsetEnd , startOffset ) ) {
186
- collectBrackets ( child , nodeOffsetStart , nodeOffsetEnd , startOffset , endOffset , result , level + 1 , levelPerBracketType ) ;
210
+ if (
211
+ lengthLessThanEqual ( nodeOffsetStart , endOffset ) &&
212
+ lengthGreaterThanEqual ( nodeOffsetEnd , startOffset )
213
+ ) {
214
+ collectBrackets (
215
+ child ,
216
+ nodeOffsetStart ,
217
+ nodeOffsetEnd ,
218
+ startOffset ,
219
+ endOffset ,
220
+ result ,
221
+ level + 1 ,
222
+ levelPerBracketType
223
+ ) ;
187
224
}
188
225
nodeOffsetStart = nodeOffsetEnd ;
189
226
}
190
227
if ( node . closingBracket ) {
191
228
const child = node . closingBracket ;
192
229
nodeOffsetEnd = lengthAdd ( nodeOffsetStart , child . length ) ;
193
- if ( lengthLessThanEqual ( nodeOffsetStart , endOffset ) && lengthGreaterThanEqual ( nodeOffsetEnd , startOffset ) ) {
230
+ if (
231
+ lengthLessThanEqual ( nodeOffsetStart , endOffset ) &&
232
+ lengthGreaterThanEqual ( nodeOffsetEnd , startOffset )
233
+ ) {
194
234
const range = lengthsToRange ( nodeOffsetStart , nodeOffsetEnd ) ;
195
235
result . push ( new BracketInfo ( range , level , levelPerBracket , false ) ) ;
196
236
}
@@ -218,33 +258,98 @@ class CollectBracketPairsContext {
218
258
}
219
259
}
220
260
221
- function collectBracketPairs ( node : AstNode , nodeOffset : Length , nodeOffsetEnd : Length , startOffset : Length , endOffset : Length , context : CollectBracketPairsContext , level : number = 0 ) {
261
+ function collectBracketPairs (
262
+ node : AstNode ,
263
+ nodeOffsetStart : Length ,
264
+ nodeOffsetEnd : Length ,
265
+ startOffset : Length ,
266
+ endOffset : Length ,
267
+ context : CollectBracketPairsContext ,
268
+ level : number ,
269
+ levelPerBracketType : Map < string , number >
270
+ ) {
222
271
if ( node . kind === AstNodeKind . Pair ) {
223
- const openingBracketEnd = lengthAdd ( nodeOffset , node . openingBracket . length ) ;
272
+ let levelPerBracket = 0 ;
273
+ if ( levelPerBracketType ) {
274
+ let existing = levelPerBracketType . get ( node . openingBracket . text ) ;
275
+ if ( existing === undefined ) {
276
+ existing = 0 ;
277
+ }
278
+ levelPerBracket = existing ;
279
+ existing ++ ;
280
+ levelPerBracketType . set ( node . openingBracket . text , existing ) ;
281
+ }
282
+
283
+ const openingBracketEnd = lengthAdd ( nodeOffsetStart , node . openingBracket . length ) ;
224
284
let minIndentation = - 1 ;
225
285
if ( context . includeMinIndentation ) {
226
- minIndentation = node . computeMinIndentation ( nodeOffset , context . textModel ) ;
286
+ minIndentation = node . computeMinIndentation (
287
+ nodeOffsetStart ,
288
+ context . textModel
289
+ ) ;
227
290
}
228
291
229
- context . result . push ( new BracketPairWithMinIndentationInfo (
230
- lengthsToRange ( nodeOffset , nodeOffsetEnd ) ,
231
- lengthsToRange ( nodeOffset , openingBracketEnd ) ,
232
- node . closingBracket
233
- ? lengthsToRange ( lengthAdd ( openingBracketEnd , node . child ?. length || lengthZero ) , nodeOffsetEnd )
234
- : undefined ,
235
- level ,
236
- minIndentation
237
- ) ) ;
238
- level ++ ;
239
- }
292
+ context . result . push (
293
+ new BracketPairWithMinIndentationInfo (
294
+ lengthsToRange ( nodeOffsetStart , nodeOffsetEnd ) ,
295
+ lengthsToRange ( nodeOffsetStart , openingBracketEnd ) ,
296
+ node . closingBracket
297
+ ? lengthsToRange (
298
+ lengthAdd ( openingBracketEnd , node . child ?. length || lengthZero ) ,
299
+ nodeOffsetEnd
300
+ )
301
+ : undefined ,
302
+ level ,
303
+ levelPerBracket ,
304
+ minIndentation
305
+ )
306
+ ) ;
240
307
241
- let curOffset = nodeOffset ;
242
- for ( const child of node . children ) {
243
- const childOffset = curOffset ;
244
- curOffset = lengthAdd ( curOffset , child . length ) ;
308
+ nodeOffsetStart = openingBracketEnd ;
309
+ if ( node . child ) {
310
+ const child = node . child ;
311
+ nodeOffsetEnd = lengthAdd ( nodeOffsetStart , child . length ) ;
312
+ if (
313
+ lengthLessThanEqual ( nodeOffsetStart , endOffset ) &&
314
+ lengthGreaterThanEqual ( nodeOffsetEnd , startOffset )
315
+ ) {
316
+ collectBracketPairs (
317
+ child ,
318
+ nodeOffsetStart ,
319
+ nodeOffsetEnd ,
320
+ startOffset ,
321
+ endOffset ,
322
+ context ,
323
+ level + 1 ,
324
+ levelPerBracketType
325
+ ) ;
326
+ }
327
+ }
245
328
246
- if ( lengthLessThanEqual ( childOffset , endOffset ) && lengthLessThanEqual ( startOffset , curOffset ) ) {
247
- collectBracketPairs ( child , childOffset , curOffset , startOffset , endOffset , context , level ) ;
329
+ if ( levelPerBracketType ) {
330
+ levelPerBracketType . set ( node . openingBracket . text , levelPerBracket ) ;
331
+ }
332
+ } else {
333
+ let curOffset = nodeOffsetStart ;
334
+ for ( const child of node . children ) {
335
+ const childOffset = curOffset ;
336
+ curOffset = lengthAdd ( curOffset , child . length ) ;
337
+
338
+ if (
339
+ lengthLessThanEqual ( childOffset , endOffset ) &&
340
+ lengthLessThanEqual ( startOffset , curOffset )
341
+ ) {
342
+ collectBracketPairs (
343
+ child ,
344
+ childOffset ,
345
+ curOffset ,
346
+ startOffset ,
347
+ endOffset ,
348
+ context ,
349
+ level ,
350
+ levelPerBracketType
351
+ ) ;
352
+ }
248
353
}
249
354
}
250
355
}
0 commit comments