Skip to content

Commit 64ee709

Browse files
authored
Merge pull request #110 from 1nVitr0/109-typescript-case-statements-not-sorting
109-typescript-case-statements-not-sorting
2 parents d7a97e5 + c85cd8c commit 64ee709

File tree

6 files changed

+36
-3
lines changed

6 files changed

+36
-3
lines changed

src/providers/BlockSortFormattingProvider.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,9 @@ export default class BlockSortFormattingProvider
121121
): Range {
122122
if (!this.isAttached(document)) this.attachDocument(document, token);
123123
const blockSort = this.blockSortProviders.get(document.uri)!;
124-
const initialRange = "start" in selection ? selection : new Range(selection, selection);
124+
const initialRange = blockSort.trimRange(
125+
"start" in selection ? selection : new Range(selection, selection)
126+
);
125127

126128
if (options.expandSelection === false) return initialRange;
127129

src/providers/BlockSortProvider.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ export default class BlockSortProvider implements Disposable {
338338
let start = selection.start.line;
339339
let end = selection.end.line;
340340

341+
if (start < end && selection.start.character >= this.document.lineAt(start).text.length) start++;
342+
if (end > start && selection.end.character === 0) end--;
343+
341344
while (start < end && this.document.lineAt(start).isEmptyOrWhitespace) start++;
342345
while (end > start && this.document.lineAt(end).isEmptyOrWhitespace) end--;
343346

src/test/fixtures/cancellation.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ export const cancelSortTests: CancellationTest[] = [
55
{
66
file: "cancellation.json.fixture",
77
ranges: [new Range(3, 0, 1500, 7)],
8-
performanceThreshold: 3,
8+
performanceThreshold: 2,
99
},
1010
];

src/test/fixtures/sort.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const sortTests: CompareTest[] = [
55
{
66
file: "block.typescript.fixture",
77
compareFile: "block.typescript.expect",
8-
ranges: [new Range(3, 0, 13, 22), new Range(19, 0, 33, 8), new Range(39, 0, 47, 8)],
8+
ranges: [new Range(3, 0, 13, 22), new Range(19, 0, 33, 8), new Range(39, 0, 47, 8), new Range(87, 0, 95, 7)],
99
},
1010
{
1111
file: "block.cpp.fixture",

test/fixtures/block.typescript.expect

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,18 @@ class Test {
8282
}
8383
};
8484
}
85+
86+
testFencedSwitchCase() {
87+
switch ('foo') {
88+
case 'alpha': {
89+
return 'something';
90+
}
91+
case 'beta': {
92+
return 'something';
93+
}
94+
case 'gamma': {
95+
return 'something';
96+
}
97+
}
98+
}
8599
}

test/fixtures/block.typescript.fixture

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,18 @@ class Test {
8282
}
8383
};
8484
}
85+
86+
testFencedSwitchCase() {
87+
switch ('foo') {
88+
case 'beta': {
89+
return 'something';
90+
}
91+
case 'alpha': {
92+
return 'something';
93+
}
94+
case 'gamma': {
95+
return 'something';
96+
}
97+
}
98+
}
8599
}

0 commit comments

Comments
 (0)