From 7142a3ca720957d3160f0bf7311badd31494c607 Mon Sep 17 00:00:00 2001 From: Harry Hopkinson Date: Mon, 14 Feb 2022 10:33:48 +0000 Subject: [PATCH] Add an Extract Method Command #18518 --- news/2 Fixes/18514.md | 2 +- package.json | 12 +++++ src/client/common/application/commands.ts | 1 + src/client/common/constants.ts | 1 + src/client/extensionActivation.ts | 65 ++++++++++++++++++++++- 5 files changed, 78 insertions(+), 3 deletions(-) diff --git a/news/2 Fixes/18514.md b/news/2 Fixes/18514.md index 29c679c95fa2..d91697bc6b11 100644 --- a/news/2 Fixes/18514.md +++ b/news/2 Fixes/18514.md @@ -1 +1 @@ -Not use --live-stream with conda run (Thanks [Harry-Hopkinson](https://github.com/Harry-Hopkinson)) +Not use --live-stream with conda run and supported the extract method command. (Thanks [Harry-Hopkinson](https://github.com/Harry-Hopkinson)) diff --git a/package.json b/package.json index fea002930776..cc90889ed361 100644 --- a/package.json +++ b/package.json @@ -72,6 +72,7 @@ "onCommand:python.setShebangInterpreter", "onCommand:python.viewLanguageServerOutput", "onCommand:python.viewOutput", + "onCommand:python.extractMethod", "onCommand:python.execSelectionInTerminal", "onCommand:python.execSelectionInDjangoShell", "onCommand:python.startREPL", @@ -487,6 +488,11 @@ "light": "resources/light/repl.svg" }, "title": "%python.command.python.viewOutput.title%" + }, + { + "category": "Python", + "command": "python.extractMethod", + "title": "Extract Method" } ], "configuration": { @@ -1810,6 +1816,12 @@ "command": "python.viewOutput", "title": "%python.command.python.viewOutput.title%", "when": "!virtualWorkspace && shellExecutionSupported" + }, + { + "category": "Python", + "command": "python.extractMethod", + "title": "Extract Method", + "when": "!virtualWorkspace && shellExecutionSupported" } ], "editor/context": [ diff --git a/src/client/common/application/commands.ts b/src/client/common/application/commands.ts index eb4e827d42bb..4060acfe3f12 100644 --- a/src/client/common/application/commands.ts +++ b/src/client/common/application/commands.ts @@ -33,6 +33,7 @@ interface ICommandNameWithoutArgumentTypeMapping { ['editor.action.formatDocument']: []; ['editor.action.rename']: []; [Commands.ViewOutput]: []; + [Commands.ExtractMethod]: []; [Commands.Set_Linter]: []; [Commands.Start_REPL]: []; [Commands.Enable_SourceMap_Support]: []; diff --git a/src/client/common/constants.ts b/src/client/common/constants.ts index 6cd283daab99..9ca8df8de058 100644 --- a/src/client/common/constants.ts +++ b/src/client/common/constants.ts @@ -47,6 +47,7 @@ export namespace Commands { export const Test_Stop_Refreshing = 'python.stopRefreshingTests'; export const Sort_Imports = 'python.sortImports'; export const ViewOutput = 'python.viewOutput'; + export const ExtractMethod = 'python.extractMethod'; export const Start_REPL = 'python.startREPL'; export const Create_Terminal = 'python.createTerminal'; export const Set_Linter = 'python.setLinter'; diff --git a/src/client/extensionActivation.ts b/src/client/extensionActivation.ts index 74b6cc066c7b..15f5e6519744 100644 --- a/src/client/extensionActivation.ts +++ b/src/client/extensionActivation.ts @@ -3,13 +3,27 @@ 'use strict'; -import { CodeActionKind, debug, DebugConfigurationProvider, languages, OutputChannel, window } from 'vscode'; +import { + CodeActionKind, + debug, + DebugConfigurationProvider, + languages, + OutputChannel, + window, + workspace, + WorkspaceEdit, +} from 'vscode'; import { registerTypes as activationRegisterTypes } from './activation/serviceRegistry'; import { IExtensionActivationManager } from './activation/types'; import { registerTypes as appRegisterTypes } from './application/serviceRegistry'; import { IApplicationDiagnostics } from './application/types'; -import { IApplicationEnvironment, ICommandManager, IWorkspaceService } from './common/application/types'; +import { + IApplicationEnvironment, + IApplicationShell, + ICommandManager, + IWorkspaceService, +} from './common/application/types'; import { Commands, PYTHON, PYTHON_LANGUAGE, STANDARD_OUTPUT_CHANNEL, UseProposedApi } from './common/constants'; import { registerTypes as installerRegisterTypes } from './common/installer/serviceRegistry'; import { IFileSystem } from './common/platform/types'; @@ -82,6 +96,24 @@ export async function activateComponents( return Promise.all([legacyActivationResult, ...promises]); } +// function UriToFileChanges(uri: any): vscode.TextEdit[] { +// const result: vscode.TextEdit[] = []; +// for (const edit of uri) { +// result.push(new vscode.TextEdit(new vscode.Range(edit.range.start, edit.range.end), edit.newText)); +// } +// return result; +// } + +// function WorkspaceEditToVSCodeEdit(workspaceEdit: any): vscode.WorkspaceEdit { +// const result: any = { +// changes: {}, +// }; +// for (const key of Object.keys(workspaceEdit)) { +// result.changes[key] = UriToFileChanges(workspaceEdit.changes[key]); +// } +// return result; +// } + /// ////////////////////////// // old activation code @@ -150,6 +182,35 @@ async function activateLegacy(ext: ExtensionState): Promise { disposables.push(cmdManager.registerCommand(Commands.ViewOutput, () => outputChannel.show())); cmdManager.executeCommand('setContext', 'python.vscode.channel', applicationEnv.channel).then(noop, noop); + disposables.push( + cmdManager.registerCommand(Commands.ExtractMethod, async () => { + const editor = window.activeTextEditor; + const shell: IApplicationShell = serviceManager.get(IApplicationShell); + if (editor) { + if (editor.selection.isEmpty) { + return shell.showErrorMessage('Please Select Text before Extracting a Method'); + } else { + const selection = editor.selection; + const document = editor.document; + const text = document.getText(selection); + const position = selection.start; + const edit = new WorkspaceEdit(); + const methodName = await window.showInputBox({ prompt: 'Enter Method Name' }); + if (methodName) { + const newText = `def ${methodName}():\n\t${text}`; + edit.delete(document.uri, selection); + edit.insert(document.uri, position, newText); + await workspace.applyEdit(edit); + } else { + return shell.showErrorMessage('Method Name is Required'); + } + } + } else { + return shell.showErrorMessage('Open an Active Editor before Extracting a Method'); + } + }), + ); + serviceContainer.get(IApplicationDiagnostics).register(); serviceManager.get(ITerminalAutoActivation).register();