Skip to content

Commit f0a3d2b

Browse files
committed
Filter out zero-width selections
1 parent 6177596 commit f0a3d2b

File tree

2 files changed

+39
-5
lines changed

2 files changed

+39
-5
lines changed

src/services/smartSelection.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,13 @@ namespace ts.SmartSelectionRange {
7070
return selectionRange;
7171

7272
function pushSelectionRange(start: number, end: number): void {
73-
// Skip ranges that are identical to the parent
74-
const textSpan = createTextSpanFromBounds(start, end);
75-
if (!selectionRange || !textSpansEqual(textSpan, selectionRange.textSpan)) {
76-
selectionRange = { textSpan, ...selectionRange && { parent: selectionRange } };
73+
// Skip empty ranges
74+
if (start !== end) {
75+
// Skip ranges that are identical to the parent
76+
const textSpan = createTextSpanFromBounds(start, end);
77+
if (!selectionRange || !textSpansEqual(textSpan, selectionRange.textSpan)) {
78+
selectionRange = { textSpan, ...selectionRange && { parent: selectionRange } };
79+
}
7780
}
7881
}
7982
}

src/testRunner/unittests/tsserver/smartSelection.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace ts.projectSystem {
1212
};
1313
}
1414

15-
describe("unittests:: tsserver:: selectionRange", () => {
15+
describe("unittests:: tsserver:: smartSelection", () => {
1616
it("works for simple JavaScript", () => {
1717
const getSmartSelectionRange = setup("/file.js", `
1818
class Foo {
@@ -652,5 +652,36 @@ function square(x) {
652652
start: { line: 1, offset: 1 },
653653
end: { line: 1, offset: 13 } } } }]);
654654
});
655+
656+
it("never returns empty ranges", () => {
657+
const getSmartSelectionRange = setup("/file.ts", `
658+
class HomePage {
659+
componentDidMount() {
660+
if (this.props.username) {
661+
return '';
662+
}
663+
}
664+
}`);
665+
const locations = getSmartSelectionRange([
666+
{ line: 3, offset: 23 }, // componentDidMount(/**/)
667+
{ line: 4, offset: 32 }, // username/**/)
668+
{ line: 5, offset: 21 }, // return '/**/'
669+
]);
670+
671+
assert.deepEqual(locations![0].textSpan, { // this.props.username
672+
start: { line: 3, offset: 23 },
673+
end: { line: 3, offset: 24 },
674+
});
675+
676+
assert.deepEqual(locations![1].textSpan, { // this.props.username
677+
start: { line: 4, offset: 32 },
678+
end: { line: 4, offset: 33 },
679+
});
680+
681+
assert.deepEqual(locations![2].textSpan, { // ''
682+
start: { line: 5, offset: 20 },
683+
end: { line: 5, offset: 22 },
684+
});
685+
});
655686
});
656687
}

0 commit comments

Comments
 (0)