Skip to content

Commit d8d86ee

Browse files
testforstephenfbricon
authored andcommitted
Enable refactoring: Extract to field
Signed-off-by: Jinbo Wang <jinbwan@microsoft.com>
1 parent d5d77cb commit d8d86ee

2 files changed

Lines changed: 27 additions & 2 deletions

File tree

src/protocol.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ export interface GetRefactorEditParams {
294294
command: string;
295295
context: CodeActionParams;
296296
options: FormattingOptions;
297+
commandArguments: any[];
297298
}
298299

299300
export namespace GetRefactorEditRequest {

src/refactorAction.ts

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,13 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
2828
}
2929
}));
3030

31-
context.subscriptions.push(commands.registerCommand(javaCommands.APPLY_REFACTORING_COMMAND, async (command: string, params: any) => {
31+
context.subscriptions.push(commands.registerCommand(javaCommands.APPLY_REFACTORING_COMMAND, async (command: string, params: any, commandInfo: any) => {
3232
if (command === 'extractVariable'
3333
|| command === 'extractVariableAllOccurrence'
3434
|| command === 'extractConstant'
35-
|| command === 'extractMethod') {
35+
|| command === 'extractMethod'
36+
|| command === 'extractField'
37+
|| command === 'convertVariableToField') {
3638
const currentEditor = window.activeTextEditor;
3739
if (!currentEditor || !currentEditor.options) {
3840
return;
@@ -42,10 +44,32 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
4244
tabSize: <number> currentEditor.options.tabSize,
4345
insertSpaces: <boolean> currentEditor.options.insertSpaces,
4446
};
47+
const commandArguments: any[] = [];
48+
if (command === 'extractField' || command === 'convertVariableToField') {
49+
if (commandInfo.initializedScopes && Array.isArray(commandInfo.initializedScopes)) {
50+
const scopes: any[] = commandInfo.initializedScopes;
51+
let initializeIn: string;
52+
if (scopes.length === 1) {
53+
initializeIn = scopes[0];
54+
} else if (scopes.length > 1) {
55+
initializeIn = await window.showQuickPick(scopes, {
56+
placeHolder: "Initialize the field in",
57+
});
58+
59+
if (!initializeIn) {
60+
return;
61+
}
62+
}
63+
64+
commandArguments.push(initializeIn);
65+
}
66+
}
67+
4568
const result: RefactorWorkspaceEdit = await languageClient.sendRequest(GetRefactorEditRequest.type, {
4669
command,
4770
context: params,
4871
options: formattingOptions,
72+
commandArguments,
4973
});
5074

5175
if (!result || !result.edit) {

0 commit comments

Comments
 (0)