Skip to content

Commit f91ad5e

Browse files
authored
Infer expressions if there is no selection range when extracting method (#1680)
* Infer expressions if there is no selection range when extracting method Signed-off-by: Shi Chen <[email protected]> * Add the capability of inferExpresstion Signed-off-by: Shi Chen <[email protected]> * tune wording according to PR in eclipse.jdt.ls Signed-off-by: Shi Chen <[email protected]> * Tune wording to keep the name consistent with the server Signed-off-by: Shi Chen <[email protected]>
1 parent 9e4e53a commit f91ad5e

File tree

3 files changed

+60
-2
lines changed

3 files changed

+60
-2
lines changed

src/extension.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ export function activate(context: ExtensionContext): Promise<ExtensionAPI> {
174174
generateConstructorsPromptSupport: true,
175175
generateDelegateMethodsPromptSupport: true,
176176
advancedExtractRefactoringSupport: true,
177+
inferSelectionSupport: ["extractMethod"],
177178
moveRefactoringSupport: true,
178179
clientHoverProvider: true,
179180
clientDocumentSymbolProvider: true,

src/protocol.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,21 @@ export namespace GetRefactorEditRequest {
329329
export const type = new RequestType<GetRefactorEditParams, RefactorWorkspaceEdit, void, void>('java/getRefactorEdit');
330330
}
331331

332+
export interface SelectionInfo {
333+
name: string;
334+
length: number;
335+
offset: number;
336+
}
337+
338+
export interface InferSelectionParams {
339+
command: string;
340+
context: CodeActionParams;
341+
}
342+
343+
export namespace InferSelectionRequest {
344+
export const type = new RequestType<InferSelectionParams, SelectionInfo[], void, void>('java/inferSelection');
345+
}
346+
332347
export interface PackageNode {
333348
displayName: string;
334349
uri: string;

src/refactorAction.ts

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import { existsSync } from 'fs';
44
import * as path from 'path';
5-
import { commands, ExtensionContext, Position, TextDocument, Uri, window, workspace } from 'vscode';
5+
import { commands, ExtensionContext, Position, QuickPickItem, TextDocument, Uri, window, workspace } from 'vscode';
66
import { FormattingOptions, LanguageClient, WorkspaceEdit, CreateFile, RenameFile, DeleteFile, TextDocumentEdit, CodeActionParams, SymbolInformation } from 'vscode-languageclient';
77
import { Commands as javaCommands } from './commands';
8-
import { GetRefactorEditRequest, MoveRequest, RefactorWorkspaceEdit, RenamePosition, GetMoveDestinationsRequest, SearchSymbols } from './protocol';
8+
import { GetRefactorEditRequest, MoveRequest, RefactorWorkspaceEdit, RenamePosition, GetMoveDestinationsRequest, SearchSymbols, SelectionInfo, InferSelectionRequest } from './protocol';
99

1010
export function registerCommands(languageClient: LanguageClient, context: ExtensionContext) {
1111
registerApplyRefactorCommand(languageClient, context);
@@ -70,6 +70,42 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
7070

7171
commandArguments.push(initializeIn);
7272
}
73+
} else if (command === 'extractMethod') {
74+
if (!params || !params.range) {
75+
return;
76+
}
77+
if (params.range.start.character === params.range.end.character && params.range.start.line === params.range.end.line) {
78+
const expressions: SelectionInfo[] = await languageClient.sendRequest(InferSelectionRequest.type, {
79+
command: command,
80+
context: params,
81+
});
82+
const options: IExpressionItem[] = [];
83+
for (const expression of expressions) {
84+
const extractMethodItem: IExpressionItem = {
85+
label: expression.name,
86+
length: expression.length,
87+
offset: expression.offset,
88+
};
89+
options.push(extractMethodItem);
90+
}
91+
let resultItem: IExpressionItem;
92+
if (options.length === 1) {
93+
resultItem = options[0];
94+
} else if (options.length > 1) {
95+
resultItem = await window.showQuickPick<IExpressionItem>(options, {
96+
placeHolder: "Choose the expression to extract",
97+
});
98+
}
99+
if (!resultItem) {
100+
return;
101+
}
102+
const resultExpression: SelectionInfo = {
103+
name: resultItem.label,
104+
length: resultItem.length,
105+
offset: resultItem.offset,
106+
};
107+
commandArguments.push(resultExpression);
108+
}
73109
}
74110

75111
const result: RefactorWorkspaceEdit = await languageClient.sendRequest(GetRefactorEditRequest.type, {
@@ -96,6 +132,12 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
96132
}));
97133
}
98134

135+
interface IExpressionItem extends QuickPickItem {
136+
label: string;
137+
length: number;
138+
offset: number;
139+
}
140+
99141
async function applyRefactorEdit(languageClient: LanguageClient, refactorEdit: RefactorWorkspaceEdit) {
100142
if (!refactorEdit) {
101143
return;

0 commit comments

Comments
 (0)