Skip to content

Commit 725321f

Browse files
authored
Prioritize “property names” over punctuation in smart select (#32687)
* Prioritize “property names” over punctuation in smart select * Update doc comment
1 parent e75972c commit 725321f

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

src/services/smartSelection.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace ts.SmartSelectionRange {
1717
break outer;
1818
}
1919

20-
if (positionShouldSnapToNode(pos, node, nextNode)) {
20+
if (positionShouldSnapToNode(sourceFile, pos, node)) {
2121
// 1. Blocks are effectively redundant with SyntaxLists.
2222
// 2. TemplateSpans, along with the SyntaxLists containing them, are a somewhat unintuitive grouping
2323
// of things that should be considered independently.
@@ -97,12 +97,11 @@ namespace ts.SmartSelectionRange {
9797
* count too, unless that position belongs to the next node. In effect, makes
9898
* selections able to snap to preceding tokens when the cursor is on the tail
9999
* end of them with only whitespace ahead.
100+
* @param sourceFile The source file containing the nodes.
100101
* @param pos The position to check.
101102
* @param node The candidate node to snap to.
102-
* @param nextNode The next sibling node in the tree.
103-
* @param sourceFile The source file containing the nodes.
104103
*/
105-
function positionShouldSnapToNode(pos: number, node: Node, nextNode: Node | undefined) {
104+
function positionShouldSnapToNode(sourceFile: SourceFile, pos: number, node: Node) {
106105
// Can’t use 'ts.positionBelongsToNode()' here because it cleverly accounts
107106
// for missing nodes, which can’t really be considered when deciding what
108107
// to select.
@@ -111,9 +110,8 @@ namespace ts.SmartSelectionRange {
111110
return true;
112111
}
113112
const nodeEnd = node.getEnd();
114-
const nextNodeStart = nextNode && nextNode.getStart();
115113
if (nodeEnd === pos) {
116-
return pos !== nextNodeStart;
114+
return getTouchingPropertyName(sourceFile, pos).pos < node.end;
117115
}
118116
return false;
119117
}

tests/baselines/reference/smartSelection_emptyRanges.baseline

+4-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,10 @@ class HomePage {
4444
}
4545

4646

47-
)
47+
username
48+
49+
50+
this.props.username
4851

4952

5053
if (this.props.username) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
console/**/.log();
2+
3+
console
4+
console.log
5+
console.log()
6+
console.log();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////console/**/.log();
4+
5+
verify.baselineSmartSelection();

0 commit comments

Comments
 (0)