Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/services/refactors/moveToFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ registerRefactor(refactorNameForMoveToFile, {
}
/** If the start/end nodes of the selection are inside a block like node do not show the `Move to file` code action
* This condition is used in order to show less often the `Move to file` code action */
if (context.endPosition !== undefined) {
if (context.triggerReason === "implicit" && context.endPosition !== undefined) {
const startNodeAncestor = findAncestor(getTokenAtPosition(file, context.startPosition), isBlockLike);
const endNodeAncestor = findAncestor(getTokenAtPosition(file, context.endPosition), isBlockLike);
if (startNodeAncestor && !isSourceFile(startNodeAncestor) && endNodeAncestor && !isSourceFile(endNodeAncestor)) {
Expand Down
14 changes: 14 additions & 0 deletions src/services/refactors/moveToNewFile.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ import {
Debug,
Diagnostics,
emptyArray,
findAncestor,
getLineAndCharacterOfPosition,
getLocaleSpecificMessage,
getTokenAtPosition,
hostGetCanonicalFileName,
isBlockLike,
isSourceFile,
LanguageServiceHost,
last,
ModuleKind,
Expand Down Expand Up @@ -40,6 +44,16 @@ registerRefactor(refactorName, {
kinds: [moveToNewFileAction.kind],
getAvailableActions: function getRefactorActionsToMoveToNewFile(context): readonly ApplicableRefactorInfo[] {
const statements = getStatementsToMove(context);

const file = context.file;
if (context.triggerReason === "implicit" && context.endPosition !== undefined) {
const startNodeAncestor = findAncestor(getTokenAtPosition(file, context.startPosition), isBlockLike);
const endNodeAncestor = findAncestor(getTokenAtPosition(file, context.endPosition), isBlockLike);
if (startNodeAncestor && !isSourceFile(startNodeAncestor) && endNodeAncestor && !isSourceFile(endNodeAncestor)) {
return emptyArray;
}
}

if (context.preferences.allowTextChangesInNewFiles && statements) {
const file = context.file;
const affectedTextRange = {
Expand Down
13 changes: 1 addition & 12 deletions tests/cases/fourslash/moveToNewFile_prologueDirectives4.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,4 @@
//// [|"use strict";|]
////}

verify.moveToNewFile({
newFileContents: {
"/a.ts":
``,

"/foo.ts":
`function foo() {
"use strict";
}
`,
},
});
verify.noMoveToNewFile();
18 changes: 18 additions & 0 deletions tests/cases/fourslash/moveToNewFile_refactorAvailable1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// <reference path='fourslash.ts' />

////namespace ns {
//// /*a*/export function fn() {
//// }
//// fn();
//// /*b*/
////}

goTo.select("a", "b");
verify.not.refactorAvailable("Move to a new file",
/*actionName*/ undefined,
/*actionDescription*/ undefined,
/*kind*/ undefined,
{
allowTextChangesInNewFiles: true
},
/*includeInteractiveActions*/ true);
20 changes: 20 additions & 0 deletions tests/cases/fourslash/moveToNewFile_refactorAvailable2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />

/////*a*/
////namespace ns {
//// export function fn() {
//// }
//// fn();
////}
/////*b*/

goTo.select("a", "b");
verify.refactorAvailable("Move to a new file",
/*actionName*/ undefined,
/*actionDescription*/ undefined,
/*kind*/ undefined,
{
allowTextChangesInNewFiles: true
},
/*includeInteractiveActions*/ true);

19 changes: 19 additions & 0 deletions tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/// <reference path='fourslash.ts' />


//// export /*a*//*b*/function fn() {
//// console.log('Hello')
//// }
//// fn();



goTo.select("a", "b");
verify.refactorAvailableForTriggerReason("implicit", "Move to a new file",
/*actionName*/ undefined,
/*actionDescription*/ undefined,
/*kind*/ undefined,
{
allowTextChangesInNewFiles: true
},
/*includeInteractiveActions*/ true);
16 changes: 16 additions & 0 deletions tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />

//// export function fn(){
//// console.log('Hello')/*a*//*b*/
//// }
//// fn();

goTo.select("a", "b");
verify.not.refactorAvailableForTriggerReason("implicit", "Move to a new file",
/*actionName*/ undefined,
/*actionDescription*/ undefined,
/*kind*/ undefined,
{
allowTextChangesInNewFiles: true
},
/*includeInteractiveActions*/ true);
23 changes: 23 additions & 0 deletions tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />


////class ns /*a*//*b*/extends ap{
//// constructor() {}
////
//// export function fn() {
//// console.log("Hello");
//// }
//// fn();
////
////}


goTo.select("a", "b");
verify.refactorAvailableForTriggerReason("implicit", "Move to a new file",
/*actionName*/ undefined,
/*actionDescription*/ undefined,
/*kind*/ undefined,
{
allowTextChangesInNewFiles: true
},
/*includeInteractiveActions*/ true);
16 changes: 16 additions & 0 deletions tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked1.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/// <reference path='fourslash.ts' />

//// export function fn(){
//// console.log('Hello')/*a*//*b*/
//// }
//// fn();

goTo.select("a", "b");
verify.refactorAvailableForTriggerReason("invoked", "Move to a new file",
/*actionName*/ undefined,
/*actionDescription*/ undefined,
/*kind*/ undefined,
{
allowTextChangesInNewFiles: true
},
/*includeInteractiveActions*/ true);
23 changes: 23 additions & 0 deletions tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/// <reference path='fourslash.ts' />


////class ns extends ap{
//// constructor() {}
////
//// export function fn() {
//// console.log("Hello"); /*a*//*b*/
//// }
//// fn();
////
////}


goTo.select("a", "b");
verify.refactorAvailableForTriggerReason("invoked", "Move to a new file",
/*actionName*/ undefined,
/*actionDescription*/ undefined,
/*kind*/ undefined,
{
allowTextChangesInNewFiles: true
},
/*includeInteractiveActions*/ true);
20 changes: 20 additions & 0 deletions tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked3.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/// <reference path='fourslash.ts' />



//// export function fn() {
//// /*a*/console.log("Hello"); /*b*/
//// }
//// fn();



goTo.select("a", "b");
verify.refactorAvailableForTriggerReason("invoked", "Move to a new file",
/*actionName*/ undefined,
/*actionDescription*/ undefined,
/*kind*/ undefined,
{
allowTextChangesInNewFiles: true
},
/*includeInteractiveActions*/ true);