@@ -93,7 +93,9 @@ export default class BlockSortFormattingProvider
93
93
public getBlockSortMarkerAtRange ( document : TextDocument , range : Range ) : BlockSortMarker | undefined {
94
94
const markers = this . getBlockSortMarkers ( document ) ;
95
95
96
- return markers . find ( ( { line } ) => range . intersection ( line . range ) !== undefined ) ;
96
+ return markers . find (
97
+ ( { range : markerRange } ) => markerRange && markerRange . intersection ( range ) !== undefined
98
+ ) ;
97
99
}
98
100
99
101
public preComputeLineMeta ( document : TextDocument , ranges ?: Range [ ] , token ?: CancellationToken ) {
@@ -233,13 +235,14 @@ export default class BlockSortFormattingProvider
233
235
) : BlockSortMarker [ ] {
234
236
const markers : TextLine [ ] = [ ] ;
235
237
const comments = commentMarkers [ document . languageId ] ?? commentMarkers . default ;
236
- const markerPrefixes = comments . map ( ( comment ) => `${ comment . start } ${ BlockSortFormattingProvider . blockSortMarker } ` ) ;
238
+ const escapedBlockSortMarker = this . escapeRegExp ( BlockSortFormattingProvider . blockSortMarker ) ;
239
+ const markerPrefixes = comments . map ( ( comment ) => new RegExp ( `^${ comment . start } ${ escapedBlockSortMarker } ` ) ) ;
237
240
238
241
for ( let i = range . start . line ; i <= range . end . line ; i ++ ) {
239
242
if ( token ?. isCancellationRequested ) return [ ] ;
240
243
241
244
const line = document . lineAt ( i ) ;
242
- if ( markerPrefixes . some ( ( prefix ) => line . text . trim ( ) . startsWith ( prefix ) ) ) markers . push ( line ) ;
245
+ if ( markerPrefixes . some ( ( prefix ) => prefix . test ( line . text . trim ( ) ) ) ) markers . push ( line ) ;
243
246
}
244
247
245
248
const expandRange = this . expandSelection . bind ( this , document ) ;
@@ -254,4 +257,8 @@ export default class BlockSortFormattingProvider
254
257
255
258
return result ;
256
259
}
260
+
261
+ private escapeRegExp ( input : string ) {
262
+ return input . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ; // $& means the whole matched string
263
+ }
257
264
}
0 commit comments