move to code actions update and tests (#58548)

This commit is contained in:
Justin Chen 2024-05-23 12:34:42 -07:00 committed by GitHub
parent a1dfb1abe5
commit abc37af88d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 171 additions and 13 deletions

View File

@ -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)) {

View File

@ -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 = {

View File

@ -5,15 +5,4 @@
//// [|"use strict";|]
////}
verify.moveToNewFile({
newFileContents: {
"/a.ts":
``,
"/foo.ts":
`function foo() {
"use strict";
}
`,
},
});
verify.noMoveToNewFile();

View 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);

View 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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);