Skip to content

Commit

Permalink
fix(37030): Expand Selection in function and arrow function skips bod…
Browse files Browse the repository at this point in the history
…y block (#50711)

* fix(37030): include curly braces from function body in the selection

* add missed sourceFile argument
  • Loading branch information
a-tarasyuk committed Sep 21, 2022
1 parent e2dd508 commit 4d91204
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 12 deletions.
5 changes: 5 additions & 0 deletions src/services/smartSelection.ts
Expand Up @@ -24,6 +24,11 @@ namespace ts.SmartSelectionRange {
}

if (positionShouldSnapToNode(sourceFile, pos, node)) {
if (isFunctionBody(node)
&& isFunctionLikeDeclaration(parentNode) && !positionsAreOnSameLine(node.getStart(sourceFile), node.getEnd(), sourceFile)) {
pushSelectionRange(node.getStart(sourceFile), node.getEnd());
}

// 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.
Expand Down
29 changes: 17 additions & 12 deletions src/testRunner/unittests/tsserver/smartSelection.ts
Expand Up @@ -44,23 +44,28 @@ class Foo {
parent: {
textSpan: { // SyntaxList + whitespace (body of method)
start: { line: 3, offset: 16 },
end: { line: 8, offset: 5 } },
end: { line: 8, offset: 5 }
},
parent: {
textSpan: { // MethodDeclaration
start: { line: 3, offset: 5 },
textSpan: { // {}
start: { line: 3, offset: 15 },
end: { line: 8, offset: 6 } },
parent: {
textSpan: { // SyntaxList + whitespace (body of class)
start: { line: 2, offset: 12 },
end: { line: 9, offset: 1 } },
textSpan: { // MethodDeclaration
start: { line: 3, offset: 5 },
end: { line: 8, offset: 6 } },
parent: {
textSpan: { // ClassDeclaration
start: { line: 2, offset: 1 },
end: { line: 9, offset: 2 } },
textSpan: { // SyntaxList + whitespace (body of class)
start: { line: 2, offset: 12 },
end: { line: 9, offset: 1 } },
parent: {
textSpan: { // SourceFile (all text)
start: { line: 1, offset: 1 },
end: { line: 9, offset: 2 }, } } } } } } } } }]);
textSpan: { // ClassDeclaration
start: { line: 2, offset: 1 },
end: { line: 9, offset: 2 } },
parent: {
textSpan: { // SourceFile (all text)
start: { line: 1, offset: 1 },
end: { line: 9, offset: 2 } } } } } } } } } } }]);
});
});
}
14 changes: 14 additions & 0 deletions tests/baselines/reference/smartSelection_emptyRanges.baseline
Expand Up @@ -62,6 +62,13 @@ class HomePage {
••


{
if (this.props.username) {
return '';
}
}


componentDidMount() {
if (this.props.username) {
return '';
Expand Down Expand Up @@ -119,6 +126,13 @@ class HomePage {
••


{
if (this.props.username) {
return '';
}
}


componentDidMount() {
if (this.props.username) {
return '';
Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/smartSelection_function1.baseline
@@ -0,0 +1,16 @@
const f1 = () => {
/**/
};


{

}

() => {

}

const f1 = () => {

};
12 changes: 12 additions & 0 deletions tests/baselines/reference/smartSelection_function2.baseline
@@ -0,0 +1,12 @@
function f2() {
/**/
}


{

}

function f2() {

}
16 changes: 16 additions & 0 deletions tests/baselines/reference/smartSelection_function3.baseline
@@ -0,0 +1,16 @@
const f3 = function () {
/**/
}


{

}

function () {

}

const f3 = function () {

}
16 changes: 16 additions & 0 deletions tests/baselines/reference/smartSelection_simple1.baseline
Expand Up @@ -27,6 +27,14 @@ class Foo {
••


{
if (a === b) {
return true;
}
return false;
}


bar(a, b) {
if (a === b) {
return true;
Expand Down Expand Up @@ -89,6 +97,14 @@ class Foo {
••


{
if (a === b) {
return true;
}
return false;
}


bar(a, b) {
if (a === b) {
return true;
Expand Down
7 changes: 7 additions & 0 deletions tests/cases/fourslash/smartSelection_function1.ts
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

////const f1 = () => {
//// /**/
////};

verify.baselineSmartSelection();
7 changes: 7 additions & 0 deletions tests/cases/fourslash/smartSelection_function2.ts
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

////function f2() {
//// /**/
////}

verify.baselineSmartSelection();
7 changes: 7 additions & 0 deletions tests/cases/fourslash/smartSelection_function3.ts
@@ -0,0 +1,7 @@
/// <reference path="fourslash.ts" />

////const f3 = function () {
//// /**/
////}

verify.baselineSmartSelection();

0 comments on commit 4d91204

Please sign in to comment.