Skip to content

Commit d5d77cb

Browse files
Add rename after 'extract to variable/constant/method' refactoring (#965)
Signed-off-by: Jinbo Wang <jinbwan@microsoft.com>
1 parent 0eab5e4 commit d5d77cb

5 files changed

Lines changed: 104 additions & 1 deletion

File tree

src/commands.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,12 @@ export namespace Commands {
138138
* Generate Delegate Methods.
139139
*/
140140
export const GENERATE_DELEGATE_METHODS_PROMPT = 'java.action.generateDelegateMethodsPrompt';
141+
/**
142+
* Apply Refactoring Command.
143+
*/
144+
export const APPLY_REFACTORING_COMMAND = 'java.action.applyRefactoringCommand';
145+
/**
146+
* Rename Command.
147+
*/
148+
export const RENAME_COMMAND = 'java.action.rename';
141149
}

src/extension.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { ExtensionAPI } from './extension.api';
1717
import * as buildpath from './buildpath';
1818
import * as sourceAction from './sourceAction';
19+
import * as refactorAction from './refactorAction';
1920
import * as net from 'net';
2021
import { getJavaConfiguration } from './utils';
2122
import { onConfigurationChange, excludeProjectSettingsFiles } from './settings';
@@ -72,6 +73,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
7273
advancedGenerateAccessorsSupport: true,
7374
generateConstructorsPromptSupport: true,
7475
generateDelegateMethodsPromptSupport: true,
76+
advancedExtractRefactoringSupport: true,
7577
},
7678
triggerFiles: getTriggerFiles()
7779
},
@@ -286,6 +288,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
286288

287289
buildpath.registerCommands(context);
288290
sourceAction.registerCommands(languageClient, context);
291+
refactorAction.registerCommands(languageClient, context);
289292

290293
context.subscriptions.push(window.onDidChangeActiveTextEditor((editor) => {
291294
toggleItem(editor, item);

src/protocol.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
import { RequestType, NotificationType, TextDocumentIdentifier, ExecuteCommandParams, CodeActionParams, WorkspaceEdit } from 'vscode-languageclient';
3+
import { RequestType, NotificationType, TextDocumentIdentifier, ExecuteCommandParams, CodeActionParams, WorkspaceEdit, FormattingOptions } from 'vscode-languageclient';
44
import { Command, Range } from 'vscode';
55

66
/**
@@ -278,3 +278,24 @@ export interface GenerateDelegateMethodsParams {
278278
export namespace GenerateDelegateMethodsRequest {
279279
export const type = new RequestType<GenerateDelegateMethodsParams, WorkspaceEdit, void, void>('java/generateDelegateMethods');
280280
}
281+
282+
export interface RenamePosition {
283+
uri: string;
284+
offset: number;
285+
length: number;
286+
}
287+
288+
export interface RefactorWorkspaceEdit {
289+
edit: WorkspaceEdit;
290+
command?: Command;
291+
}
292+
293+
export interface GetRefactorEditParams {
294+
command: string;
295+
context: CodeActionParams;
296+
options: FormattingOptions;
297+
}
298+
299+
export namespace GetRefactorEditRequest {
300+
export const type = new RequestType<GetRefactorEditParams, RefactorWorkspaceEdit, void, void>('java/getRefactorEdit');
301+
}

src/refactorAction.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'use strict';
2+
3+
import { commands, window, ExtensionContext, workspace, Position, Uri, TextDocument } from 'vscode';
4+
import { LanguageClient, FormattingOptions } from 'vscode-languageclient';
5+
import { Commands as javaCommands } from './commands';
6+
import { GetRefactorEditRequest, RefactorWorkspaceEdit, RenamePosition } from './protocol';
7+
8+
export function registerCommands(languageClient: LanguageClient, context: ExtensionContext) {
9+
registerApplyRefactorCommand(languageClient, context);
10+
}
11+
12+
function registerApplyRefactorCommand(languageClient: LanguageClient, context: ExtensionContext): void {
13+
context.subscriptions.push(commands.registerCommand(javaCommands.RENAME_COMMAND, async (position: RenamePosition) => {
14+
try {
15+
const uri: Uri = Uri.parse(position.uri);
16+
const document: TextDocument = await workspace.openTextDocument(uri);
17+
if (document == null) {
18+
return;
19+
}
20+
21+
const renamePosition: Position = document.positionAt(position.offset);
22+
await commands.executeCommand('editor.action.rename', [
23+
document.uri,
24+
renamePosition,
25+
]);
26+
} catch (error) {
27+
// do nothing.
28+
}
29+
}));
30+
31+
context.subscriptions.push(commands.registerCommand(javaCommands.APPLY_REFACTORING_COMMAND, async (command: string, params: any) => {
32+
if (command === 'extractVariable'
33+
|| command === 'extractVariableAllOccurrence'
34+
|| command === 'extractConstant'
35+
|| command === 'extractMethod') {
36+
const currentEditor = window.activeTextEditor;
37+
if (!currentEditor || !currentEditor.options) {
38+
return;
39+
}
40+
41+
const formattingOptions: FormattingOptions = {
42+
tabSize: <number> currentEditor.options.tabSize,
43+
insertSpaces: <boolean> currentEditor.options.insertSpaces,
44+
};
45+
const result: RefactorWorkspaceEdit = await languageClient.sendRequest(GetRefactorEditRequest.type, {
46+
command,
47+
context: params,
48+
options: formattingOptions,
49+
});
50+
51+
if (!result || !result.edit) {
52+
return;
53+
}
54+
55+
const edit = languageClient.protocol2CodeConverter.asWorkspaceEdit(result.edit);
56+
if (edit) {
57+
await workspace.applyEdit(edit);
58+
}
59+
60+
if (result.command) {
61+
if (result.command.arguments) {
62+
await commands.executeCommand(result.command.command, ...result.command.arguments);
63+
} else {
64+
await commands.executeCommand(result.command.command);
65+
}
66+
}
67+
}
68+
}));
69+
}

test/extension.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ suite('Java Language Extension', () => {
5050
Commands.GENERATE_ACCESSORS_PROMPT,
5151
Commands.GENERATE_CONSTRUCTORS_PROMPT,
5252
Commands.GENERATE_DELEGATE_METHODS_PROMPT,
53+
Commands.APPLY_REFACTORING_COMMAND,
54+
Commands.RENAME_COMMAND
5355
];
5456
const foundJavaCommands = commands.filter(function(value) {
5557
return JAVA_COMMANDS.indexOf(value)>=0 || value.startsWith('java.');

0 commit comments

Comments
 (0)