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