mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-14 19:16:17 -06:00
move to code actions update and tests (#58548)
This commit is contained in:
parent
a1dfb1abe5
commit
abc37af88d
@ -176,7 +176,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)) {
|
||||
|
||||
@ -5,9 +5,13 @@ import {
|
||||
Debug,
|
||||
Diagnostics,
|
||||
emptyArray,
|
||||
findAncestor,
|
||||
getLineAndCharacterOfPosition,
|
||||
getLocaleSpecificMessage,
|
||||
getTokenAtPosition,
|
||||
hostGetCanonicalFileName,
|
||||
isBlockLike,
|
||||
isSourceFile,
|
||||
LanguageServiceHost,
|
||||
last,
|
||||
ModuleKind,
|
||||
@ -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 = {
|
||||
|
||||
@ -5,15 +5,4 @@
|
||||
//// [|"use strict";|]
|
||||
////}
|
||||
|
||||
verify.moveToNewFile({
|
||||
newFileContents: {
|
||||
"/a.ts":
|
||||
``,
|
||||
|
||||
"/foo.ts":
|
||||
`function foo() {
|
||||
"use strict";
|
||||
}
|
||||
`,
|
||||
},
|
||||
});
|
||||
verify.noMoveToNewFile();
|
||||
|
||||
18
tests/cases/fourslash/moveToNewFile_refactorAvailable1.ts
Normal file
18
tests/cases/fourslash/moveToNewFile_refactorAvailable1.ts
Normal file
@ -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
tests/cases/fourslash/moveToNewFile_refactorAvailable2.ts
Normal file
20
tests/cases/fourslash/moveToNewFile_refactorAvailable2.ts
Normal file
@ -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);
|
||||
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
@ -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);
|
||||
Loading…
x
Reference in New Issue
Block a user