Skip to content

Commit 744b8a2

Browse files
committed
fix: resolve multi-line code lenses
1 parent 39c5deb commit 744b8a2

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/providers/BlockSortFormattingProvider.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,9 @@ export default class BlockSortFormattingProvider
9393
public getBlockSortMarkerAtRange(document: TextDocument, range: Range): BlockSortMarker | undefined {
9494
const markers = this.getBlockSortMarkers(document);
9595

96-
return markers.find(({ line }) => range.intersection(line.range) !== undefined);
96+
return markers.find(
97+
({ range: markerRange }) => markerRange && markerRange.intersection(range) !== undefined
98+
);
9799
}
98100

99101
public preComputeLineMeta(document: TextDocument, ranges?: Range[], token?: CancellationToken) {
@@ -233,13 +235,14 @@ export default class BlockSortFormattingProvider
233235
): BlockSortMarker[] {
234236
const markers: TextLine[] = [];
235237
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}`));
237240

238241
for (let i = range.start.line; i <= range.end.line; i++) {
239242
if (token?.isCancellationRequested) return [];
240243

241244
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);
243246
}
244247

245248
const expandRange = this.expandSelection.bind(this, document);
@@ -254,4 +257,8 @@ export default class BlockSortFormattingProvider
254257

255258
return result;
256259
}
260+
261+
private escapeRegExp(input: string) {
262+
return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"); // $& means the whole matched string
263+
}
257264
}

0 commit comments

Comments
 (0)