From 725321f08cae97db8c99fafa0fdb283e18a58429 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Fri, 2 Aug 2019 15:57:44 -0700 Subject: [PATCH] =?UTF-8?q?Prioritize=20=E2=80=9Cproperty=20names=E2=80=9D?= =?UTF-8?q?=20over=20punctuation=20in=20smart=20select=20(#32687)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Prioritize “property names” over punctuation in smart select * Update doc comment --- src/services/smartSelection.ts | 10 ++++------ .../reference/smartSelection_emptyRanges.baseline | 5 ++++- .../smartSelection_punctuationPriority.baseline | 6 ++++++ .../fourslash/smartSelection_punctuationPriority.ts | 5 +++++ 4 files changed, 19 insertions(+), 7 deletions(-) create mode 100644 tests/baselines/reference/smartSelection_punctuationPriority.baseline create mode 100644 tests/cases/fourslash/smartSelection_punctuationPriority.ts diff --git a/src/services/smartSelection.ts b/src/services/smartSelection.ts index 3765c39ed7a..0a2adcd9507 100644 --- a/src/services/smartSelection.ts +++ b/src/services/smartSelection.ts @@ -17,7 +17,7 @@ namespace ts.SmartSelectionRange { break outer; } - if (positionShouldSnapToNode(pos, node, nextNode)) { + if (positionShouldSnapToNode(sourceFile, pos, node)) { // 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. @@ -97,12 +97,11 @@ namespace ts.SmartSelectionRange { * count too, unless that position belongs to the next node. In effect, makes * selections able to snap to preceding tokens when the cursor is on the tail * end of them with only whitespace ahead. + * @param sourceFile The source file containing the nodes. * @param pos The position to check. * @param node The candidate node to snap to. - * @param nextNode The next sibling node in the tree. - * @param sourceFile The source file containing the nodes. */ - function positionShouldSnapToNode(pos: number, node: Node, nextNode: Node | undefined) { + function positionShouldSnapToNode(sourceFile: SourceFile, pos: number, node: Node) { // Can’t use 'ts.positionBelongsToNode()' here because it cleverly accounts // for missing nodes, which can’t really be considered when deciding what // to select. @@ -111,9 +110,8 @@ namespace ts.SmartSelectionRange { return true; } const nodeEnd = node.getEnd(); - const nextNodeStart = nextNode && nextNode.getStart(); if (nodeEnd === pos) { - return pos !== nextNodeStart; + return getTouchingPropertyName(sourceFile, pos).pos < node.end; } return false; } diff --git a/tests/baselines/reference/smartSelection_emptyRanges.baseline b/tests/baselines/reference/smartSelection_emptyRanges.baseline index 8ec58d839ab..efcb6e66ee9 100644 --- a/tests/baselines/reference/smartSelection_emptyRanges.baseline +++ b/tests/baselines/reference/smartSelection_emptyRanges.baseline @@ -44,7 +44,10 @@ class HomePage { } - ) + username + + + this.props.username if (this.props.username) { diff --git a/tests/baselines/reference/smartSelection_punctuationPriority.baseline b/tests/baselines/reference/smartSelection_punctuationPriority.baseline new file mode 100644 index 00000000000..176327b8c98 --- /dev/null +++ b/tests/baselines/reference/smartSelection_punctuationPriority.baseline @@ -0,0 +1,6 @@ +console/**/.log(); + +console +console.log +console.log() +console.log(); \ No newline at end of file diff --git a/tests/cases/fourslash/smartSelection_punctuationPriority.ts b/tests/cases/fourslash/smartSelection_punctuationPriority.ts new file mode 100644 index 00000000000..af841444b48 --- /dev/null +++ b/tests/cases/fourslash/smartSelection_punctuationPriority.ts @@ -0,0 +1,5 @@ +/// + +////console/**/.log(); + +verify.baselineSmartSelection();