Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(37030): Expand Selection in function and arrow function skips body block #50711

Merged
merged 2 commits into from Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();