@@ -17,7 +17,7 @@ import type {
1717import type { ScopeHandlerFactory } from "../ScopeHandlerFactory" ;
1818import { CollectionItemIterationScopeHandler } from "./CollectionItemIterationScopeHandler" ;
1919import { createTargetScope } from "./createTargetScope" ;
20- import { getDelimiterOccurrences } from "./getDelimiterOccurrences " ;
20+ import { getSeparatorOccurrences } from "./getSeparatorOccurrences " ;
2121
2222export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
2323 public scopeType : ScopeType = { type : "collectionItem" } ;
@@ -50,7 +50,7 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
5050 delimiter,
5151 } ,
5252 this . languageId ,
53- ) ! ;
53+ ) ;
5454 return Array . from (
5555 scopeHandler . generateScopes ( editor , new Position ( 0 , 0 ) , "forward" , {
5656 containment : undefined ,
@@ -69,17 +69,17 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
6969 ) : Iterable < TargetScope > {
7070 const { document } = editor ;
7171 const isEveryScope = hints . containment == null && hints . skipAncestorScopes ;
72- const delimiterRanges = getDelimiterOccurrences ( document ) ;
72+ const separatorRanges = getSeparatorOccurrences ( document ) ;
7373
7474 const interiorRanges = this . getInteriorRanges ( editor , "collectionBoundary" ) ;
7575 const stringRanges = this . getInteriorRanges ( editor , "string" ) ;
7676
7777 const scopes : TargetScope [ ] = [ ] ;
7878 const usedInteriors = new Set < Range > ( ) ;
79- const previousIterationRanges : IterationState [ ] = [ ] ;
79+ const iterationStatesStack : IterationState [ ] = [ ] ;
8080
8181 function addScopes ( state : IterationState ) {
82- const { delimiters, range : iterationRange } = state ;
82+ const { delimiters, iterationRange : iterationRange } = state ;
8383
8484 if ( delimiters . length === 0 ) {
8585 return ;
@@ -122,59 +122,63 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
122122 }
123123 }
124124
125- for ( const delimiter of delimiterRanges ) {
126- // Delimiters in a string are not considered
127- if ( stringRanges . some ( ( range ) => range . contains ( delimiter ) ) ) {
125+ // TODO: fixed performance on large files
126+ for ( const separator of separatorRanges ) {
127+ // Separators in a string are not considered
128+ if ( stringRanges . some ( ( range ) => range . contains ( separator ) ) ) {
128129 continue ;
129130 }
130131
131- const currentState =
132- previousIterationRanges [ previousIterationRanges . length - 1 ] ;
132+ const currentIterationState =
133+ iterationStatesStack [ iterationStatesStack . length - 1 ] ;
133134
134135 // Get range for smallest containing interior
135136 const containingInteriorRange : Range | undefined = interiorRanges
136- . filter ( ( range ) => range . contains ( delimiter ) )
137+ . filter ( ( range ) => range . contains ( separator ) )
137138 . sort ( ( a , b ) => ( a . contains ( b ) ? 1 : b . contains ( a ) ? - 1 : 0 ) ) [ 0 ] ;
138139
139- // The contain range is either the interior or the line containing the delimiter
140- const containingRange =
141- containingInteriorRange ?? document . lineAt ( delimiter . start . line ) . range ;
140+ // The contain range is either the interior or the line containing the separator
141+ const containingIterationRange =
142+ containingInteriorRange ?? document . lineAt ( separator . start . line ) . range ;
142143
143- if ( currentState != null ) {
144- // The current containing range is the same as the previous one. Just append delimiter.
145- if ( currentState . range . isRangeEqual ( containingRange ) ) {
146- currentState . delimiters . push ( delimiter ) ;
144+ if ( currentIterationState != null ) {
145+ // The current containing iteration range is the same as the previous one. Just append delimiter.
146+ if (
147+ currentIterationState . iterationRange . isRangeEqual (
148+ containingIterationRange ,
149+ )
150+ ) {
151+ currentIterationState . delimiters . push ( separator ) ;
147152 continue ;
148153 }
149154
150155 // The current containing range does not intersect previous one. Add scopes and remove state.
151- if ( ! currentState . range . contains ( delimiter ) ) {
152- addScopes ( currentState ) ;
156+ if ( ! currentIterationState . iterationRange . contains ( separator ) ) {
157+ addScopes ( currentIterationState ) ;
153158 // Remove already added state
154- previousIterationRanges . pop ( ) ;
159+ iterationStatesStack . pop ( ) ;
155160 }
156161 }
157162
158163 // The current containing range is the same as the previous one. Just append delimiter.
159- if ( previousIterationRanges . length > 0 ) {
160- const lastState =
161- previousIterationRanges [ previousIterationRanges . length - 1 ] ;
162- if ( lastState . range . isRangeEqual ( containingRange ) ) {
163- lastState . delimiters . push ( delimiter ) ;
164+ if ( iterationStatesStack . length > 0 ) {
165+ const lastState = iterationStatesStack [ iterationStatesStack . length - 1 ] ;
166+ if ( lastState . iterationRange . isRangeEqual ( containingIterationRange ) ) {
167+ lastState . delimiters . push ( separator ) ;
164168 continue ;
165169 }
166170 }
167171
168172 // New containing range. Add it to the list.
169173 usedInteriors . add ( containingInteriorRange ) ;
170174
171- previousIterationRanges . push ( {
172- range : containingRange ,
173- delimiters : [ delimiter ] ,
175+ iterationStatesStack . push ( {
176+ iterationRange : containingIterationRange ,
177+ delimiters : [ separator ] ,
174178 } ) ;
175179 }
176180
177- for ( const state of previousIterationRanges ) {
181+ for ( const state of iterationStatesStack ) {
178182 addScopes ( state ) ;
179183 }
180184
@@ -195,6 +199,6 @@ export class CollectionItemTextualScopeHandler extends BaseScopeHandler {
195199}
196200
197201interface IterationState {
198- range : Range ;
202+ iterationRange : Range ;
199203 delimiters : Range [ ] ;
200204}
0 commit comments