Skip to content

Commit 4d91204

Browse files
authoredSep 21, 2022
fix(37030): Expand Selection in function and arrow function skips body block (#50711)
* fix(37030): include curly braces from function body in the selection * add missed sourceFile argument
1 parent e2dd508 commit 4d91204

10 files changed

+117
-12
lines changed
 

‎src/services/smartSelection.ts

+5
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ namespace ts.SmartSelectionRange {
2424
}
2525

2626
if (positionShouldSnapToNode(sourceFile, pos, node)) {
27+
if (isFunctionBody(node)
28+
&& isFunctionLikeDeclaration(parentNode) && !positionsAreOnSameLine(node.getStart(sourceFile), node.getEnd(), sourceFile)) {
29+
pushSelectionRange(node.getStart(sourceFile), node.getEnd());
30+
}
31+
2732
// 1. Blocks are effectively redundant with SyntaxLists.
2833
// 2. TemplateSpans, along with the SyntaxLists containing them, are a somewhat unintuitive grouping
2934
// of things that should be considered independently.

‎src/testRunner/unittests/tsserver/smartSelection.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -44,23 +44,28 @@ class Foo {
4444
parent: {
4545
textSpan: { // SyntaxList + whitespace (body of method)
4646
start: { line: 3, offset: 16 },
47-
end: { line: 8, offset: 5 } },
47+
end: { line: 8, offset: 5 }
48+
},
4849
parent: {
49-
textSpan: { // MethodDeclaration
50-
start: { line: 3, offset: 5 },
50+
textSpan: { // {}
51+
start: { line: 3, offset: 15 },
5152
end: { line: 8, offset: 6 } },
5253
parent: {
53-
textSpan: { // SyntaxList + whitespace (body of class)
54-
start: { line: 2, offset: 12 },
55-
end: { line: 9, offset: 1 } },
54+
textSpan: { // MethodDeclaration
55+
start: { line: 3, offset: 5 },
56+
end: { line: 8, offset: 6 } },
5657
parent: {
57-
textSpan: { // ClassDeclaration
58-
start: { line: 2, offset: 1 },
59-
end: { line: 9, offset: 2 } },
58+
textSpan: { // SyntaxList + whitespace (body of class)
59+
start: { line: 2, offset: 12 },
60+
end: { line: 9, offset: 1 } },
6061
parent: {
61-
textSpan: { // SourceFile (all text)
62-
start: { line: 1, offset: 1 },
63-
end: { line: 9, offset: 2 }, } } } } } } } } }]);
62+
textSpan: { // ClassDeclaration
63+
start: { line: 2, offset: 1 },
64+
end: { line: 9, offset: 2 } },
65+
parent: {
66+
textSpan: { // SourceFile (all text)
67+
start: { line: 1, offset: 1 },
68+
end: { line: 9, offset: 2 } } } } } } } } } } }]);
6469
});
6570
});
6671
}

‎tests/baselines/reference/smartSelection_emptyRanges.baseline

+14
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,13 @@ class HomePage {
6262
••
6363

6464

65+
{
66+
if (this.props.username) {
67+
return '';
68+
}
69+
}
70+
71+
6572
componentDidMount() {
6673
if (this.props.username) {
6774
return '';
@@ -119,6 +126,13 @@ class HomePage {
119126
••
120127

121128

129+
{
130+
if (this.props.username) {
131+
return '';
132+
}
133+
}
134+
135+
122136
componentDidMount() {
123137
if (this.props.username) {
124138
return '';
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const f1 = () => {
2+
/**/
3+
};
4+
5+
6+
{
7+
8+
}
9+
10+
() => {
11+
12+
}
13+
14+
const f1 = () => {
15+
16+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
function f2() {
2+
/**/
3+
}
4+
5+
6+
{
7+
8+
}
9+
10+
function f2() {
11+
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const f3 = function () {
2+
/**/
3+
}
4+
5+
6+
{
7+
8+
}
9+
10+
function () {
11+
12+
}
13+
14+
const f3 = function () {
15+
16+
}

‎tests/baselines/reference/smartSelection_simple1.baseline

+16
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,14 @@ class Foo {
2727
••
2828

2929

30+
{
31+
if (a === b) {
32+
return true;
33+
}
34+
return false;
35+
}
36+
37+
3038
bar(a, b) {
3139
if (a === b) {
3240
return true;
@@ -89,6 +97,14 @@ class Foo {
8997
••
9098

9199

100+
{
101+
if (a === b) {
102+
return true;
103+
}
104+
return false;
105+
}
106+
107+
92108
bar(a, b) {
93109
if (a === b) {
94110
return true;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const f1 = () => {
4+
//// /**/
5+
////};
6+
7+
verify.baselineSmartSelection();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////function f2() {
4+
//// /**/
5+
////}
6+
7+
verify.baselineSmartSelection();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/// <reference path="fourslash.ts" />
2+
3+
////const f3 = function () {
4+
//// /**/
5+
////}
6+
7+
verify.baselineSmartSelection();

0 commit comments

Comments
 (0)
Please sign in to comment.