Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ export async function activate(context: ExtensionContext): Promise<ExtensionAPI>
inferSelectionSupport: ["extractMethod", "extractVariable", "extractField"],
moveRefactoringSupport: true,
moveRefactoringConfirmationSupport: true,
inlineRefactoringConfirmationSupport: true,
clientHoverProvider: true,
clientDocumentSymbolProvider: true,
gradleChecksumWrapperPromptSupport: true,
Expand Down
1 change: 1 addition & 0 deletions src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,7 @@ export interface GetRefactorEditParams {
context: CodeActionParams;
options: FormattingOptions;
commandArguments: any[];
confirmationToken?: string;
}

export namespace GetRefactorEditRequest {
Expand Down
37 changes: 12 additions & 25 deletions src/refactorAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { commands, ExtensionContext, Position, QuickPickItem, TextDocument, Uri,
import { FormattingOptions, WorkspaceEdit, RenameFile, DeleteFile, TextDocumentEdit, CodeActionParams, SymbolInformation } from 'vscode-languageclient';
import { LanguageClient } from 'vscode-languageclient/node';
import { Commands as javaCommands } from './commands';
import { GetRefactorEditRequest, MoveRequest, RefactorWorkspaceEdit, RenamePosition, GetMoveDestinationsRequest, SearchSymbols, SelectionInfo, InferSelectionRequest, GetChangeSignatureInfoRequest, ChangeSignatureInfo, MoveParams } from './protocol';
import { GetRefactorEditRequest, MoveRequest, RefactorWorkspaceEdit, RenamePosition, GetMoveDestinationsRequest, SearchSymbols, SelectionInfo, InferSelectionRequest, GetChangeSignatureInfoRequest, ChangeSignatureInfo, MoveParams, GetRefactorEditParams } from './protocol';
import { requestWithConfirmation } from './refactoring/confirmation';
import { ChangeSignaturePanel } from './refactoring/changeSignaturePanel';
import { getExtractInterfaceArguments, revealExtractedInterface } from './refactoring/extractInterface';

Expand Down Expand Up @@ -46,6 +47,9 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
|| command === 'convertVariableToField'
|| command === 'invertVariable'
|| command === 'introduceParameter'
|| command === 'inlineMethod'
|| command === 'inlineConstant'
|| command === 'inlineLocalVariable'
|| command === 'convertAnonymousClassToNestedCommand') {
const currentEditor = window.activeTextEditor;
if (!currentEditor || !currentEditor.options) {
Expand Down Expand Up @@ -121,16 +125,18 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E
return;
}

const result: RefactorWorkspaceEdit = await languageClient.sendRequest(GetRefactorEditRequest.type, {
const refactorParams: GetRefactorEditParams = {
command,
context: params,
options: formattingOptions,
commandArguments,
});
};
const result = await requestWithConfirmation(refactorParams,
requestParams => languageClient.sendRequest(GetRefactorEditRequest.type, requestParams));

await applyRefactorEdit(languageClient, result);

if (command === 'extractInterface') {
if (command === 'extractInterface' && result) {
await revealExtractedInterface(result);
}
} else if (command === 'moveFile') {
Expand Down Expand Up @@ -253,27 +259,8 @@ async function applyRefactorEdit(languageClient: LanguageClient, refactorEdit: R
}

async function requestMoveWithConfirmation(languageClient: LanguageClient, moveParams: MoveParams): Promise<RefactorWorkspaceEdit | undefined> {
let refactorEdit: RefactorWorkspaceEdit = await languageClient.sendRequest(MoveRequest.type, moveParams);
if (!refactorEdit?.confirmationToken) {
await applyRefactorEdit(languageClient, refactorEdit);
return refactorEdit;
}

const continueAction = 'Continue';
const detail = 'Review the details below before continuing:\n\n' + refactorEdit.errorMessage;
const selection = await window.showWarningMessage(
'This refactoring may change program behavior. Continue anyway?',
{ modal: true, detail },
continueAction,
);
if (selection !== continueAction) {
return undefined;
}

refactorEdit = await languageClient.sendRequest(MoveRequest.type, {
...moveParams,
confirmationToken: refactorEdit.confirmationToken,
});
const refactorEdit = await requestWithConfirmation(moveParams,
requestParams => languageClient.sendRequest(MoveRequest.type, requestParams));
await applyRefactorEdit(languageClient, refactorEdit);
return refactorEdit;
}
Expand Down
13 changes: 10 additions & 3 deletions src/refactoring/changeSignaturePanel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import * as path from "path";
import { Disposable, Webview, WebviewPanel, window, Uri, ViewColumn, workspace, WorkspaceEdit } from "vscode";
import { LanguageClient } from "vscode-languageclient/node";
import { GetRefactorEditRequest, RefactorWorkspaceEdit } from "../protocol";
import { GetRefactorEditParams, GetRefactorEditRequest } from "../protocol";
import { requestWithConfirmation } from "./confirmation";
import { getNonce, getUri } from "../webviewUtils";

interface MethodParameter {
Expand Down Expand Up @@ -173,12 +174,18 @@ export class ChangeSignaturePanel {
* @returns true if the refactoring is successful, false otherwise.
*/
private async doRefactor(methodIdentifier: string, isDelegate: boolean, methodName: string, accessType: string, returnType: string, parameters: MethodParameter[], exceptions: MethodException[], preview: boolean): Promise<boolean> {
const clientWorkspaceEdit: RefactorWorkspaceEdit = await this.languageClient.sendRequest(GetRefactorEditRequest.type, {
const refactorParams: GetRefactorEditParams = {
command: this.command,
context: this.params,
options: this.formattingOptions,
commandArguments: [methodIdentifier, isDelegate, methodName, accessType, returnType, parameters, exceptions, preview]
});
};
const clientWorkspaceEdit = await requestWithConfirmation(refactorParams,
params => this.languageClient.sendRequest(GetRefactorEditRequest.type, params));
if (clientWorkspaceEdit?.errorMessage) {
window.showErrorMessage(clientWorkspaceEdit.errorMessage);
return false;
}
if (clientWorkspaceEdit?.edit) {
const codeEdit: WorkspaceEdit = await this.languageClient.protocol2CodeConverter.asWorkspaceEdit(clientWorkspaceEdit.edit);
if (codeEdit) {
Expand Down
28 changes: 28 additions & 0 deletions src/refactoring/confirmation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { window } from 'vscode';
import { RefactorWorkspaceEdit } from '../protocol';

export async function requestWithConfirmation<T extends { confirmationToken?: string }>(
params: T,
request: (params: T) => Promise<RefactorWorkspaceEdit>,
): Promise<RefactorWorkspaceEdit | undefined> {
const refactorEdit = await request(params);
if (!refactorEdit?.confirmationToken) {
return refactorEdit;
}

const continueAction = 'Continue';
const detail = `Review the details below before continuing:\n\n${refactorEdit.errorMessage}`;
const selection = await window.showWarningMessage(
'This refactoring may change program behavior. Continue anyway?',
{ modal: true, detail },
continueAction,
);
if (selection !== continueAction) {
return undefined;
}

return request({
...params,
confirmationToken: refactorEdit.confirmationToken,
});
}
Loading