Skip to content

Commit abc37af

Browse files
authored
move to code actions update and tests (#58548)
1 parent a1dfb1a commit abc37af

11 files changed

+171
-13
lines changed

src/services/refactors/moveToFile.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ registerRefactor(refactorNameForMoveToFile, {
176176
}
177177
/** If the start/end nodes of the selection are inside a block like node do not show the `Move to file` code action
178178
* This condition is used in order to show less often the `Move to file` code action */
179-
if (context.endPosition !== undefined) {
179+
if (context.triggerReason === "implicit" && context.endPosition !== undefined) {
180180
const startNodeAncestor = findAncestor(getTokenAtPosition(file, context.startPosition), isBlockLike);
181181
const endNodeAncestor = findAncestor(getTokenAtPosition(file, context.endPosition), isBlockLike);
182182
if (startNodeAncestor && !isSourceFile(startNodeAncestor) && endNodeAncestor && !isSourceFile(endNodeAncestor)) {

src/services/refactors/moveToNewFile.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@ import {
55
Debug,
66
Diagnostics,
77
emptyArray,
8+
findAncestor,
89
getLineAndCharacterOfPosition,
910
getLocaleSpecificMessage,
11+
getTokenAtPosition,
1012
hostGetCanonicalFileName,
13+
isBlockLike,
14+
isSourceFile,
1115
LanguageServiceHost,
1216
last,
1317
ModuleKind,
@@ -40,6 +44,16 @@ registerRefactor(refactorName, {
4044
kinds: [moveToNewFileAction.kind],
4145
getAvailableActions: function getRefactorActionsToMoveToNewFile(context): readonly ApplicableRefactorInfo[] {
4246
const statements = getStatementsToMove(context);
47+
48+
const file = context.file;
49+
if (context.triggerReason === "implicit" && context.endPosition !== undefined) {
50+
const startNodeAncestor = findAncestor(getTokenAtPosition(file, context.startPosition), isBlockLike);
51+
const endNodeAncestor = findAncestor(getTokenAtPosition(file, context.endPosition), isBlockLike);
52+
if (startNodeAncestor && !isSourceFile(startNodeAncestor) && endNodeAncestor && !isSourceFile(endNodeAncestor)) {
53+
return emptyArray;
54+
}
55+
}
56+
4357
if (context.preferences.allowTextChangesInNewFiles && statements) {
4458
const file = context.file;
4559
const affectedTextRange = {

tests/cases/fourslash/moveToNewFile_prologueDirectives4.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,4 @@
55
//// [|"use strict";|]
66
////}
77

8-
verify.moveToNewFile({
9-
newFileContents: {
10-
"/a.ts":
11-
``,
12-
13-
"/foo.ts":
14-
`function foo() {
15-
"use strict";
16-
}
17-
`,
18-
},
19-
});
8+
verify.noMoveToNewFile();
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
////namespace ns {
4+
//// /*a*/export function fn() {
5+
//// }
6+
//// fn();
7+
//// /*b*/
8+
////}
9+
10+
goTo.select("a", "b");
11+
verify.not.refactorAvailable("Move to a new file",
12+
/*actionName*/ undefined,
13+
/*actionDescription*/ undefined,
14+
/*kind*/ undefined,
15+
{
16+
allowTextChangesInNewFiles: true
17+
},
18+
/*includeInteractiveActions*/ true);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
/////*a*/
4+
////namespace ns {
5+
//// export function fn() {
6+
//// }
7+
//// fn();
8+
////}
9+
/////*b*/
10+
11+
goTo.select("a", "b");
12+
verify.refactorAvailable("Move to a new file",
13+
/*actionName*/ undefined,
14+
/*actionDescription*/ undefined,
15+
/*kind*/ undefined,
16+
{
17+
allowTextChangesInNewFiles: true
18+
},
19+
/*includeInteractiveActions*/ true);
20+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
4+
//// export /*a*//*b*/function fn() {
5+
//// console.log('Hello')
6+
//// }
7+
//// fn();
8+
9+
10+
11+
goTo.select("a", "b");
12+
verify.refactorAvailableForTriggerReason("implicit", "Move to a new file",
13+
/*actionName*/ undefined,
14+
/*actionDescription*/ undefined,
15+
/*kind*/ undefined,
16+
{
17+
allowTextChangesInNewFiles: true
18+
},
19+
/*includeInteractiveActions*/ true);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// export function fn(){
4+
//// console.log('Hello')/*a*//*b*/
5+
//// }
6+
//// fn();
7+
8+
goTo.select("a", "b");
9+
verify.not.refactorAvailableForTriggerReason("implicit", "Move to a new file",
10+
/*actionName*/ undefined,
11+
/*actionDescription*/ undefined,
12+
/*kind*/ undefined,
13+
{
14+
allowTextChangesInNewFiles: true
15+
},
16+
/*includeInteractiveActions*/ true);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
4+
////class ns /*a*//*b*/extends ap{
5+
//// constructor() {}
6+
////
7+
//// export function fn() {
8+
//// console.log("Hello");
9+
//// }
10+
//// fn();
11+
////
12+
////}
13+
14+
15+
goTo.select("a", "b");
16+
verify.refactorAvailableForTriggerReason("implicit", "Move to a new file",
17+
/*actionName*/ undefined,
18+
/*actionDescription*/ undefined,
19+
/*kind*/ undefined,
20+
{
21+
allowTextChangesInNewFiles: true
22+
},
23+
/*includeInteractiveActions*/ true);
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
//// export function fn(){
4+
//// console.log('Hello')/*a*//*b*/
5+
//// }
6+
//// fn();
7+
8+
goTo.select("a", "b");
9+
verify.refactorAvailableForTriggerReason("invoked", "Move to a new file",
10+
/*actionName*/ undefined,
11+
/*actionDescription*/ undefined,
12+
/*kind*/ undefined,
13+
{
14+
allowTextChangesInNewFiles: true
15+
},
16+
/*includeInteractiveActions*/ true);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
4+
////class ns extends ap{
5+
//// constructor() {}
6+
////
7+
//// export function fn() {
8+
//// console.log("Hello"); /*a*//*b*/
9+
//// }
10+
//// fn();
11+
////
12+
////}
13+
14+
15+
goTo.select("a", "b");
16+
verify.refactorAvailableForTriggerReason("invoked", "Move to a new file",
17+
/*actionName*/ undefined,
18+
/*actionDescription*/ undefined,
19+
/*kind*/ undefined,
20+
{
21+
allowTextChangesInNewFiles: true
22+
},
23+
/*includeInteractiveActions*/ true);
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/// <reference path='fourslash.ts' />
2+
3+
4+
5+
//// export function fn() {
6+
//// /*a*/console.log("Hello"); /*b*/
7+
//// }
8+
//// fn();
9+
10+
11+
12+
goTo.select("a", "b");
13+
verify.refactorAvailableForTriggerReason("invoked", "Move to a new file",
14+
/*actionName*/ undefined,
15+
/*actionDescription*/ undefined,
16+
/*kind*/ undefined,
17+
{
18+
allowTextChangesInNewFiles: true
19+
},
20+
/*includeInteractiveActions*/ true);

0 commit comments

Comments
 (0)