diff --git a/src/services/selectionRange.ts b/src/services/selectionRange.ts index 1c2a525167a..63a65d42e3b 100644 --- a/src/services/selectionRange.ts +++ b/src/services/selectionRange.ts @@ -18,16 +18,19 @@ namespace ts.SelectionRange { } if (positionShouldSnapToNode(pos, node, nextNode)) { - // Blocks are effectively redundant with SyntaxLists. - // TemplateSpans, along with the SyntaxLists containing them, - // are a somewhat unintuitive grouping of things that should be - // considered independently. A VariableStatement’s children are just - // a VaraiableDeclarationList and a semicolon. + // 1. Blocks are effectively redundant with SyntaxLists. + // 2. TemplateSpans, along with the SyntaxLists containing them, are a somewhat unintuitive grouping + // of things that should be considered independently. + // 3. A VariableStatement’s children are just a VaraiableDeclarationList and a semicolon. + // 4. A lone VariableDeclaration in a VaraibleDeclaration feels redundant with the VariableStatement. + // // Dive in without pushing a selection range. if (isBlock(node) || isTemplateSpan(node) || isTemplateHead(node) || prevNode && isTemplateHead(prevNode) - || isVariableDeclarationList(node) && isVariableStatement(parentNode)) { + || isVariableDeclarationList(node) && isVariableStatement(parentNode) + || isSyntaxList(node) && isVariableDeclarationList(parentNode) + || isVariableDeclaration(node) && isSyntaxList(parentNode) && children.length === 1) { parentNode = node; break; } diff --git a/src/testRunner/unittests/tsserver/selectionRange.ts b/src/testRunner/unittests/tsserver/selectionRange.ts index 3c0a12a58f7..7940edcdebb 100644 --- a/src/testRunner/unittests/tsserver/selectionRange.ts +++ b/src/testRunner/unittests/tsserver/selectionRange.ts @@ -568,18 +568,13 @@ const { x, y: a, ...zs = {} } = {};`); start: { line: 2, offset: 7 }, end: { line: 2, offset: 30 } }, parent: { - textSpan: { // { x, y: a, ...zs = {} } = {} - start: { line: 2, offset: 7 }, - end: { line: 2, offset: 35 } }, + textSpan: { // whole line + start: { line: 2, offset: 1 }, + end: { line: 2, offset: 36 } }, parent: { - textSpan: { // whole line - start: { line: 2, offset: 1 }, - end: { line: 2, offset: 36 } }, - parent: { - textSpan: { - start: { line: 1, offset: 1 }, - end: { line: 2, offset: 36 } } } } } } } } }, - }); + textSpan: { + start: { line: 1, offset: 1 }, + end: { line: 2, offset: 36 } } } } } } } } }); }); it("consumes all whitespace in a multi-line function parameter list", () => { @@ -644,5 +639,18 @@ function square(x) { start: { line: 1, offset: 1 }, end: { line: 7, offset: 2 } } } } } }]); }); + + it("skips lone VariableDeclarations in a declaration list", () => { + const getSelectionRange = setup("/file.ts", `const x = 3;`); + const locations = getSelectionRange([{ line: 1, offset: 7 }]); // x + assert.deepEqual(locations, [{ + textSpan: { + start: { line: 1, offset: 7 }, + end: { line: 1, offset: 8 } }, + parent: { + textSpan: { + start: { line: 1, offset: 1 }, + end: { line: 1, offset: 13 } } } }]); + }); }); }