From f0a3d2bf92cd59d3c068246109c5832f83d3d150 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 19 Apr 2019 09:44:42 -0700 Subject: [PATCH] Filter out zero-width selections --- src/services/smartSelection.ts | 11 ++++--- .../unittests/tsserver/smartSelection.ts | 33 ++++++++++++++++++- 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/services/smartSelection.ts b/src/services/smartSelection.ts index ba2ec60e9c5..ddd3c75c2c7 100644 --- a/src/services/smartSelection.ts +++ b/src/services/smartSelection.ts @@ -70,10 +70,13 @@ namespace ts.SmartSelectionRange { return selectionRange; function pushSelectionRange(start: number, end: number): void { - // Skip ranges that are identical to the parent - const textSpan = createTextSpanFromBounds(start, end); - if (!selectionRange || !textSpansEqual(textSpan, selectionRange.textSpan)) { - selectionRange = { textSpan, ...selectionRange && { parent: selectionRange } }; + // Skip empty ranges + if (start !== end) { + // Skip ranges that are identical to the parent + const textSpan = createTextSpanFromBounds(start, end); + if (!selectionRange || !textSpansEqual(textSpan, selectionRange.textSpan)) { + selectionRange = { textSpan, ...selectionRange && { parent: selectionRange } }; + } } } } diff --git a/src/testRunner/unittests/tsserver/smartSelection.ts b/src/testRunner/unittests/tsserver/smartSelection.ts index 5d4113d3f97..fca96ab848e 100644 --- a/src/testRunner/unittests/tsserver/smartSelection.ts +++ b/src/testRunner/unittests/tsserver/smartSelection.ts @@ -12,7 +12,7 @@ namespace ts.projectSystem { }; } - describe("unittests:: tsserver:: selectionRange", () => { + describe("unittests:: tsserver:: smartSelection", () => { it("works for simple JavaScript", () => { const getSmartSelectionRange = setup("/file.js", ` class Foo { @@ -652,5 +652,36 @@ function square(x) { start: { line: 1, offset: 1 }, end: { line: 1, offset: 13 } } } }]); }); + + it("never returns empty ranges", () => { + const getSmartSelectionRange = setup("/file.ts", ` +class HomePage { + componentDidMount() { + if (this.props.username) { + return ''; + } + } +}`); + const locations = getSmartSelectionRange([ + { line: 3, offset: 23 }, // componentDidMount(/**/) + { line: 4, offset: 32 }, // username/**/) + { line: 5, offset: 21 }, // return '/**/' + ]); + + assert.deepEqual(locations![0].textSpan, { // this.props.username + start: { line: 3, offset: 23 }, + end: { line: 3, offset: 24 }, + }); + + assert.deepEqual(locations![1].textSpan, { // this.props.username + start: { line: 4, offset: 32 }, + end: { line: 4, offset: 33 }, + }); + + assert.deepEqual(locations![2].textSpan, { // '' + start: { line: 5, offset: 20 }, + end: { line: 5, offset: 22 }, + }); + }); }); }