Skip to content

Commit

Permalink
Add an Extract Method Command microsoft#18518
Browse files Browse the repository at this point in the history
  • Loading branch information
Harry-Hopkinson committed Feb 14, 2022
1 parent b7d6fc4 commit 7142a3c
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion news/2 Fixes/18514.md
Original file line number Diff line number Diff line change
@@ -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))
12 changes: 12 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -487,6 +488,11 @@
"light": "resources/light/repl.svg"
},
"title": "%python.command.python.viewOutput.title%"
},
{
"category": "Python",
"command": "python.extractMethod",
"title": "Extract Method"
}
],
"configuration": {
Expand Down Expand Up @@ -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": [
Expand Down
1 change: 1 addition & 0 deletions src/client/common/application/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]: [];
Expand Down
1 change: 1 addition & 0 deletions src/client/common/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
65 changes: 63 additions & 2 deletions src/client/extensionActivation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -150,6 +182,35 @@ async function activateLegacy(ext: ExtensionState): Promise<ActivationResult> {
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>(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>(IApplicationDiagnostics).register();

serviceManager.get<ITerminalAutoActivation>(ITerminalAutoActivation).register();
Expand Down

0 comments on commit 7142a3c

Please sign in to comment.