From f2e6de94262bc0cadeebafb612499abf87783c21 Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 16 May 2024 00:10:42 -0700 Subject: [PATCH 1/3] move to code action changes and tests --- .../launch.template.json => src/launch.json | 128 +++++++++--------- src/services/refactors/moveToFile.ts | 7 +- src/services/refactors/moveToNewFile.ts | 14 ++ .../moveToNewFile_prologueDirectives4.ts | 13 +- .../moveToNewFile_refactorAvailable1.ts | 18 +++ .../moveToNewFile_refactorAvailable2.ts | 20 +++ ...oveToNewFile_refactorAvailableImplicit1.ts | 19 +++ ...oveToNewFile_refactorAvailableImplicit2.ts | 16 +++ ...oveToNewFile_refactorAvailableImplicit3.ts | 23 ++++ ...moveToNewFile_refactorAvailableInvoked1.ts | 16 +++ ...moveToNewFile_refactorAvailableInvoked2.ts | 23 ++++ ...moveToNewFile_refactorAvailableInvoked3.ts | 20 +++ 12 files changed, 238 insertions(+), 79 deletions(-) rename .vscode/launch.template.json => src/launch.json (97%) create mode 100644 tests/cases/fourslash/moveToNewFile_refactorAvailable1.ts create mode 100644 tests/cases/fourslash/moveToNewFile_refactorAvailable2.ts create mode 100644 tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit1.ts create mode 100644 tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit2.ts create mode 100644 tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit3.ts create mode 100644 tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked1.ts create mode 100644 tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked2.ts create mode 100644 tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked3.ts diff --git a/.vscode/launch.template.json b/src/launch.json similarity index 97% rename from .vscode/launch.template.json rename to src/launch.json index de410368e9a37..3dd2c363997ca 100644 --- a/.vscode/launch.template.json +++ b/src/launch.json @@ -1,64 +1,64 @@ -/* - - Copy this file into '.vscode/launch.json' or merge its - contents into your existing configurations. - - If you want to remove the errors in comments for all JSON - files, add this to your settings in ~/.vscode/User/settings.json - - "files.associations": { - "*.json": "jsonc" - }, - -*/ - -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "node", - "request": "launch", - "name": "Mocha Tests (currently opened test)", - "runtimeArgs": ["--nolazy"], - "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", - "args": [ - "-u", - "bdd", - "--no-timeouts", - "--colors", - "built/local/run.js", - "-f", - // You can change this to be the name of a specific test file (without the file extension) - // to consistently launch the same test - "${fileBasenameNoExtension}", - "--skip-percent", - "0" - ], - "env": { - "NODE_ENV": "development" - }, - "outFiles": [ - "${workspaceFolder}/built/**/*.js", - "${workspaceFolder}/built/**/*.mjs", - "${workspaceFolder}/built/**/*.cjs", - "!**/node_modules/**" - ], - "sourceMaps": true, - "smartStep": true, - "preLaunchTask": "npm: build:tests", - "console": "integratedTerminal", - "customDescriptionGenerator": "'__tsDebuggerDisplay' in this ? this.__tsDebuggerDisplay(defaultValue) : defaultValue" - }, - { - // See: https://github.com/microsoft/TypeScript/wiki/Debugging-Language-Service-in-VS-Code - "type": "node", - "request": "attach", - "name": "Attach to VS Code TS Server via Port", - "processId": "${command:PickProcess}", - "customDescriptionGenerator": "'__tsDebuggerDisplay' in this ? this.__tsDebuggerDisplay(defaultValue) : defaultValue" - } - ] -} +/* + + Copy this file into '.vscode/launch.json' or merge its + contents into your existing configurations. + + If you want to remove the errors in comments for all JSON + files, add this to your settings in ~/.vscode/User/settings.json + + "files.associations": { + "*.json": "jsonc" + }, + +*/ + +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "node", + "request": "launch", + "name": "Mocha Tests (currently opened test)", + "runtimeArgs": ["--nolazy"], + "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", + "args": [ + "-u", + "bdd", + "--no-timeouts", + "--colors", + "built/local/run.js", + "-f", + // You can change this to be the name of a specific test file (without the file extension) + // to consistently launch the same test + "${fileBasenameNoExtension}", + "--skip-percent", + "0" + ], + "env": { + "NODE_ENV": "development" + }, + "outFiles": [ + "${workspaceFolder}/built/**/*.js", + "${workspaceFolder}/built/**/*.mjs", + "${workspaceFolder}/built/**/*.cjs", + "!**/node_modules/**" + ], + "sourceMaps": true, + "smartStep": true, + "preLaunchTask": "npm: build:tests", + "console": "integratedTerminal", + "customDescriptionGenerator": "'__tsDebuggerDisplay' in this ? this.__tsDebuggerDisplay(defaultValue) : defaultValue" + }, + { + // See: https://github.com/microsoft/TypeScript/wiki/Debugging-Language-Service-in-VS-Code + "type": "node", + "request": "attach", + "name": "Attach to VS Code TS Server via Port", + "processId": "${command:PickProcess}", + "customDescriptionGenerator": "'__tsDebuggerDisplay' in this ? this.__tsDebuggerDisplay(defaultValue) : defaultValue" + } + ] +} diff --git a/src/services/refactors/moveToFile.ts b/src/services/refactors/moveToFile.ts index dea0ebc4c43e1..9b5c4d196c9fa 100644 --- a/src/services/refactors/moveToFile.ts +++ b/src/services/refactors/moveToFile.ts @@ -56,6 +56,7 @@ import { getModifiers, getNameForExportedSymbol, getNormalizedAbsolutePath, + getOriginalNode, getPropertySymbolFromBindingElement, getQuotePreference, getRangesWhere, @@ -175,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)) { @@ -440,7 +441,7 @@ function createRequireCall(moduleSpecifier: StringLiteralLike): CallExpression { export function moduleSpecifierFromImport(i: SupportedImport): StringLiteralLike { return (i.kind === SyntaxKind.ImportDeclaration ? i.moduleSpecifier : i.kind === SyntaxKind.ImportEqualsDeclaration ? i.moduleReference.expression - : i.initializer.arguments[0]); + : i.initializer.arguments[0]); } /** @internal */ @@ -952,7 +953,7 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[], function makeUniqueFilename(proposedFilename: string, extension: string, inDirectory: string, host: LanguageServiceHost): string { let newFilename = proposedFilename; - for (let i = 1;; i++) { + for (let i = 1; ; i++) { const name = combinePaths(inDirectory, newFilename + extension); if (!host.fileExists(name)) return newFilename; newFilename = `${proposedFilename}.${i}`; diff --git a/src/services/refactors/moveToNewFile.ts b/src/services/refactors/moveToNewFile.ts index 2665239e10483..37cdd35d07019 100644 --- a/src/services/refactors/moveToNewFile.ts +++ b/src/services/refactors/moveToNewFile.ts @@ -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 = { diff --git a/tests/cases/fourslash/moveToNewFile_prologueDirectives4.ts b/tests/cases/fourslash/moveToNewFile_prologueDirectives4.ts index d30090bbe3df3..bfadbfd3e2e7a 100644 --- a/tests/cases/fourslash/moveToNewFile_prologueDirectives4.ts +++ b/tests/cases/fourslash/moveToNewFile_prologueDirectives4.ts @@ -5,15 +5,4 @@ //// [|"use strict";|] ////} -verify.moveToNewFile({ - newFileContents: { - "/a.ts": -``, - - "/foo.ts": -`function foo() { - "use strict"; -} -`, - }, -}); +verify.noMoveToNewFile(); diff --git a/tests/cases/fourslash/moveToNewFile_refactorAvailable1.ts b/tests/cases/fourslash/moveToNewFile_refactorAvailable1.ts new file mode 100644 index 0000000000000..ab0689f481304 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_refactorAvailable1.ts @@ -0,0 +1,18 @@ +/// + +////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); diff --git a/tests/cases/fourslash/moveToNewFile_refactorAvailable2.ts b/tests/cases/fourslash/moveToNewFile_refactorAvailable2.ts new file mode 100644 index 0000000000000..3b191a7c6df85 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_refactorAvailable2.ts @@ -0,0 +1,20 @@ +/// + +/////*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); + diff --git a/tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit1.ts b/tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit1.ts new file mode 100644 index 0000000000000..cb552982f9c49 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit1.ts @@ -0,0 +1,19 @@ +/// + + +//// 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); diff --git a/tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit2.ts b/tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit2.ts new file mode 100644 index 0000000000000..7391bbe48ef70 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit2.ts @@ -0,0 +1,16 @@ +/// + +//// 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); diff --git a/tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit3.ts b/tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit3.ts new file mode 100644 index 0000000000000..d1b5606214842 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_refactorAvailableImplicit3.ts @@ -0,0 +1,23 @@ +/// + + +////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); diff --git a/tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked1.ts b/tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked1.ts new file mode 100644 index 0000000000000..2e360588fb510 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked1.ts @@ -0,0 +1,16 @@ +/// + +//// 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); diff --git a/tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked2.ts b/tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked2.ts new file mode 100644 index 0000000000000..904e6cf96f351 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked2.ts @@ -0,0 +1,23 @@ +/// + + +////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); diff --git a/tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked3.ts b/tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked3.ts new file mode 100644 index 0000000000000..7476fe09db6e9 --- /dev/null +++ b/tests/cases/fourslash/moveToNewFile_refactorAvailableInvoked3.ts @@ -0,0 +1,20 @@ +/// + + + +//// 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); From d942d0a6a6ef5be685522faecfa163c30b69b60d Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 16 May 2024 00:14:43 -0700 Subject: [PATCH 2/3] removed launch config --- .../launch.template.json | 129 +++++++++--------- 1 file changed, 65 insertions(+), 64 deletions(-) rename src/launch.json => .vscode/launch.template.json (97%) diff --git a/src/launch.json b/.vscode/launch.template.json similarity index 97% rename from src/launch.json rename to .vscode/launch.template.json index 3dd2c363997ca..6d83c3b74ac3e 100644 --- a/src/launch.json +++ b/.vscode/launch.template.json @@ -1,64 +1,65 @@ -/* - - Copy this file into '.vscode/launch.json' or merge its - contents into your existing configurations. - - If you want to remove the errors in comments for all JSON - files, add this to your settings in ~/.vscode/User/settings.json - - "files.associations": { - "*.json": "jsonc" - }, - -*/ - -{ - // Use IntelliSense to learn about possible attributes. - // Hover to view descriptions of existing attributes. - // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 - "version": "0.2.0", - "configurations": [ - { - "type": "node", - "request": "launch", - "name": "Mocha Tests (currently opened test)", - "runtimeArgs": ["--nolazy"], - "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", - "args": [ - "-u", - "bdd", - "--no-timeouts", - "--colors", - "built/local/run.js", - "-f", - // You can change this to be the name of a specific test file (without the file extension) - // to consistently launch the same test - "${fileBasenameNoExtension}", - "--skip-percent", - "0" - ], - "env": { - "NODE_ENV": "development" - }, - "outFiles": [ - "${workspaceFolder}/built/**/*.js", - "${workspaceFolder}/built/**/*.mjs", - "${workspaceFolder}/built/**/*.cjs", - "!**/node_modules/**" - ], - "sourceMaps": true, - "smartStep": true, - "preLaunchTask": "npm: build:tests", - "console": "integratedTerminal", - "customDescriptionGenerator": "'__tsDebuggerDisplay' in this ? this.__tsDebuggerDisplay(defaultValue) : defaultValue" - }, - { - // See: https://github.com/microsoft/TypeScript/wiki/Debugging-Language-Service-in-VS-Code - "type": "node", - "request": "attach", - "name": "Attach to VS Code TS Server via Port", - "processId": "${command:PickProcess}", - "customDescriptionGenerator": "'__tsDebuggerDisplay' in this ? this.__tsDebuggerDisplay(defaultValue) : defaultValue" - } - ] -} +/* + + Copy this file into '.vscode/launch.json' or merge its + contents into your existing configurations. + + If you want to remove the errors in comments for all JSON + files, add this to your settings in ~/.vscode/User/settings.json + + "files.associations": { + "*.json": "jsonc" + }, + +*/ + +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + + { + "type": "node", + "request": "launch", + "name": "Mocha Tests (currently opened test)", + "runtimeArgs": ["--nolazy"], + "program": "${workspaceRoot}/node_modules/mocha/bin/_mocha", + "args": [ + "-u", + "bdd", + "--no-timeouts", + "--colors", + "built/local/run.js", + "-f", + // You can change this to be the name of a specific test file (without the file extension) + // to consistently launch the same test + "${fileBasenameNoExtension}", + "--skip-percent", + "0" + ], + "env": { + "NODE_ENV": "development" + }, + "outFiles": [ + "${workspaceFolder}/built/**/*.js", + "${workspaceFolder}/built/**/*.mjs", + "${workspaceFolder}/built/**/*.cjs", + "!**/node_modules/**" + ], + "sourceMaps": true, + "smartStep": true, + "preLaunchTask": "npm: build:tests", + "console": "integratedTerminal", + "customDescriptionGenerator": "'__tsDebuggerDisplay' in this ? this.__tsDebuggerDisplay(defaultValue) : defaultValue" + }, + { + // See: https://github.com/microsoft/TypeScript/wiki/Debugging-Language-Service-in-VS-Code + "type": "node", + "request": "attach", + "name": "Attach to VS Code TS Server via Port", + "processId": "${command:PickProcess}", + "customDescriptionGenerator": "'__tsDebuggerDisplay' in this ? this.__tsDebuggerDisplay(defaultValue) : defaultValue" + } + ] +} From d067c1f784fa2d409eabbd38d483aead8380ef1f Mon Sep 17 00:00:00 2001 From: Your Name Date: Thu, 16 May 2024 00:17:20 -0700 Subject: [PATCH 3/3] formating changes --- .vscode/launch.template.json | 1 - src/services/refactors/moveToFile.ts | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/.vscode/launch.template.json b/.vscode/launch.template.json index 6d83c3b74ac3e..de410368e9a37 100644 --- a/.vscode/launch.template.json +++ b/.vscode/launch.template.json @@ -18,7 +18,6 @@ // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 "version": "0.2.0", "configurations": [ - { "type": "node", "request": "launch", diff --git a/src/services/refactors/moveToFile.ts b/src/services/refactors/moveToFile.ts index 9b5c4d196c9fa..f060940454132 100644 --- a/src/services/refactors/moveToFile.ts +++ b/src/services/refactors/moveToFile.ts @@ -56,7 +56,6 @@ import { getModifiers, getNameForExportedSymbol, getNormalizedAbsolutePath, - getOriginalNode, getPropertySymbolFromBindingElement, getQuotePreference, getRangesWhere, @@ -441,7 +440,7 @@ function createRequireCall(moduleSpecifier: StringLiteralLike): CallExpression { export function moduleSpecifierFromImport(i: SupportedImport): StringLiteralLike { return (i.kind === SyntaxKind.ImportDeclaration ? i.moduleSpecifier : i.kind === SyntaxKind.ImportEqualsDeclaration ? i.moduleReference.expression - : i.initializer.arguments[0]); + : i.initializer.arguments[0]); } /** @internal */ @@ -953,7 +952,7 @@ export function getUsageInfo(oldFile: SourceFile, toMove: readonly Statement[], function makeUniqueFilename(proposedFilename: string, extension: string, inDirectory: string, host: LanguageServiceHost): string { let newFilename = proposedFilename; - for (let i = 1; ; i++) { + for (let i = 1;; i++) { const name = combinePaths(inDirectory, newFilename + extension); if (!host.fileExists(name)) return newFilename; newFilename = `${proposedFilename}.${i}`;