diff --git a/src/providers/BlockSortFormattingProvider.ts b/src/providers/BlockSortFormattingProvider.ts index 9431b1b..77992e2 100644 --- a/src/providers/BlockSortFormattingProvider.ts +++ b/src/providers/BlockSortFormattingProvider.ts @@ -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); diff --git a/src/providers/BlockSortProvider.ts b/src/providers/BlockSortProvider.ts index 5d579e9..3edd504 100644 --- a/src/providers/BlockSortProvider.ts +++ b/src/providers/BlockSortProvider.ts @@ -1,4 +1,3 @@ -import { off } from "process"; import { CancellationToken, Disposable, @@ -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; @@ -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)); @@ -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)); } diff --git a/src/test/fixtures/expand.ts b/src/test/fixtures/expand.ts index 7374abd..6daa61e 100644 --- a/src/test/fixtures/expand.ts +++ b/src/test/fixtures/expand.ts @@ -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", diff --git a/src/test/suite/BlockSortProvider.unit.test.ts b/src/test/suite/BlockSortProvider.unit.test.ts index 9b13cb8..948487d 100644 --- a/src/test/suite/BlockSortProvider.unit.test.ts +++ b/src/test/suite/BlockSortProvider.unit.test.ts @@ -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"); }); diff --git a/test/fixtures/expand.typescript.fixture b/test/fixtures/expand.typescript.fixture index 258985f..cafe548 100644 --- a/test/fixtures/expand.typescript.fixture +++ b/test/fixtures/expand.typescript.fixture @@ -17,4 +17,8 @@ class Test { return null; } } -} \ No newline at end of file +} + +class Test2 { + public test: string; +}