Skip to content

Commit

Permalink
fix: correctly update blocksort code lenses
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed May 11, 2022
1 parent 0d75473 commit b61a8ba
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
25 changes: 18 additions & 7 deletions src/providers/BlockSortActionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,25 +198,36 @@ export default class BlockSortActionProvider
return markers;
}

for (const change of changes) {
let lineOffset = 0;
for (const change of changes.sort((a, b) => a.range.start.compareTo(b.range.start))) {
if (token.isCancellationRequested) return markers;
const { range, text } = change;
const lineCountBefore = range.end.line - range.start.line + 1;
const { start, end } = range;
const lineCountBefore = end.line - start.line + 1;
const lines = text.split("\n");

// If line count changes, update all markers after the change
if (lineCountBefore !== lines.length) {
const lineCountChange = lines.length - lineCountBefore;
const lineCountChange = lines.length - lineCountBefore;
if (lineCountChange) {
for (let i = 0; i < markers.length; i++)
if (markers[i].isAfter(range.end)) markers[i] = markers[i].translate(lineCountChange, 0);
if (markers[i].isAfter(end)) markers[i] = markers[i].translate(lineCountChange, 0);
}

// Delete and recreate all markers inside the changed range
for (let i = markers.length - 1; i >= 0; i--) if (range.contains(markers[i])) markers.splice(i, 1);
for (let i = markers.length - 1; i >= 0; i--) {
if (range.contains(markers[i]) || start.line == markers[i].line || end.line == markers[i].line)
markers.splice(i, 1);
}

markers.push(
...BlockSortFormattingProvider.getBlockSortMarkers(document, range, token).map(({ range: { start } }) => start)
...BlockSortFormattingProvider.getBlockSortMarkers(
document,
range.with(start.translate(lineOffset), end.translate(lineCountChange)),
token
).map(({ range: { start } }) => start)
);

lineOffset += lineCountChange;
}

markers.sort((a, b) => a.compareTo(b));
Expand Down
2 changes: 1 addition & 1 deletion src/providers/BlockSortFormattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export default class BlockSortFormattingProvider
const comments = commentMarkers[document.languageId] ?? commentMarkers.default;
const markerPrefixes = comments.map((comment) => `${comment.start} ${BlockSortFormattingProvider.blockSortMarker}`);

for (let i = range?.start.line ?? 0; i < (range?.end.line ?? document.lineCount); i++) {
for (let i = range?.start.line ?? 0; i <= (range?.end.line ?? document.lineCount - 1); i++) {
if (token?.isCancellationRequested) return markers;

const line = document.lineAt(i);
Expand Down

0 comments on commit b61a8ba

Please sign in to comment.