Skip to content

Commit

Permalink
fix: trim empty lines from block selection
Browse files Browse the repository at this point in the history
  • Loading branch information
1nVitr0 committed Aug 11, 2022
1 parent 934bf76 commit e46a924
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/providers/BlockSortFormattingProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ export default class BlockSortFormattingProvider
): TextEdit {
const blockSort = new BlockSortProvider(document);
const initialRange = "start" in position ? position : new Range(position, position);
const range = blockSort.expandRange(initialRange, 0, token);
const range = blockSort.trimRange(blockSort.expandRange(initialRange, token));
const blocks = blockSort.getBlocks(range, token);
const sorted = blockSort.sortBlocks(blocks, options.sortFunction, options.sortChildren, options.edits, token);

Expand Down
16 changes: 12 additions & 4 deletions src/providers/BlockSortProvider.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { off } from "process";
import {
CancellationToken,
Disposable,
Expand Down Expand Up @@ -180,8 +179,7 @@ export default class BlockSortProvider implements Disposable {
return [];
}

// TODO: Make this more efficient
public expandRange(selection: Range, indent = 0, token?: CancellationToken): Range {
public expandRange(selection: Range, token?: CancellationToken): Range {
const { stringProcessor } = this;
let range: Range = this.document.validateRange(new Range(selection.start.line, 0, selection.end.line, Infinity));
let folding: Folding;
Expand All @@ -190,7 +188,7 @@ export default class BlockSortProvider implements Disposable {
!token?.isCancellationRequested &&
range.end.line < this.document.lineCount &&
this.stringProcessor.totalOpenFolding(
(folding = stringProcessor.getFolding(this.document.getText(range), this.document, undefined, true))
(folding = stringProcessor.getFolding(this.document.getText(range), this.document, undefined))
) > 0
)
range = new Range(range.start, range.end.with(range.end.line + 1));
Expand Down Expand Up @@ -240,6 +238,16 @@ export default class BlockSortProvider implements Disposable {
return this.document.validateRange(range);
}

public trimRange(selection: Range): Range {
let start = selection.start.line;
let end = selection.end.line;

while (start < end && this.document.lineAt(start).isEmptyOrWhitespace) start++;
while (end > start && this.document.lineAt(end).isEmptyOrWhitespace) end--;

return this.document.validateRange(new Range(start, 0, end, Infinity));
}

public watch(): void {
this.disposables.push(workspace.onDidChangeTextDocument(this.computeLineMeta, this, this.disposables));
}
Expand Down
4 changes: 2 additions & 2 deletions src/test/fixtures/expand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import { RangeTest } from "../suite/types";
export const expandTests: RangeTest[] = [
{
file: "expand.typescript.fixture",
ranges: [new Range(3, 9, 3, 9)],
targetRanges: [new Range(3, 0, 16, 20)],
ranges: [new Range(3, 9, 3, 9), new Range(20, 0, 24, 0)],
targetRanges: [new Range(3, 0, 16, 20), new Range(21, 0, 23, 1)],
},
{
file: "expand.cpp.fixture",
Expand Down
2 changes: 1 addition & 1 deletion src/test/suite/BlockSortProvider.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ suite("Unit Suite for BlockSortProvider", async () => {
await languages.setTextDocumentLanguage(document, lang);
const blockSortProvider = new BlockSortProvider(document);
const selection = new Selection(position.start, position.end);
const expanded = blockSortProvider.expandRange(selection);
const expanded = blockSortProvider.trimRange(blockSortProvider.expandRange(selection));

assert.deepStrictEqual(expanded, target, "range did not expand correctly");
});
Expand Down
6 changes: 5 additions & 1 deletion test/fixtures/expand.typescript.fixture
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,8 @@ class Test {
return null;
}
}
}
}

class Test2 {
public test: string;
}

0 comments on commit e46a924

Please sign in to comment.