diff --git a/src/protocol.ts b/src/protocol.ts index 35d8d4a72..e994ad2fb 100644 --- a/src/protocol.ts +++ b/src/protocol.ts @@ -288,6 +288,7 @@ export interface RenamePosition { export interface RefactorWorkspaceEdit { edit: WorkspaceEdit; command?: Command; + errorMessage?: string; } export interface GetRefactorEditParams { @@ -325,5 +326,5 @@ export interface MoveFileParams { } export namespace MoveFileRequest { - export const type = new RequestType('java/moveFile'); + export const type = new RequestType('java/moveFile'); } diff --git a/src/refactorAction.ts b/src/refactorAction.ts index fb8613b2f..5fd1364db 100644 --- a/src/refactorAction.ts +++ b/src/refactorAction.ts @@ -75,22 +75,7 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E commandArguments, }); - if (!result || !result.edit) { - return; - } - - const edit = languageClient.protocol2CodeConverter.asWorkspaceEdit(result.edit); - if (edit) { - await workspace.applyEdit(edit); - } - - if (result.command) { - if (result.command.arguments) { - await commands.executeCommand(result.command.command, ...result.command.arguments); - } else { - await commands.executeCommand(result.command.command); - } - } + await applyRefactorEdit(languageClient, result); } else if (command === 'moveFile') { if (!commandInfo || !commandInfo.uri) { return; @@ -101,6 +86,32 @@ function registerApplyRefactorCommand(languageClient: LanguageClient, context: E })); } +async function applyRefactorEdit(languageClient: LanguageClient, refactorEdit: RefactorWorkspaceEdit) { + if (!refactorEdit) { + return; + } + + if (refactorEdit.errorMessage) { + window.showErrorMessage(refactorEdit.errorMessage); + return; + } + + if (refactorEdit.edit) { + const edit = languageClient.protocol2CodeConverter.asWorkspaceEdit(refactorEdit.edit); + if (edit) { + await workspace.applyEdit(edit); + } + } + + if (refactorEdit.command) { + if (refactorEdit.command.arguments) { + await commands.executeCommand(refactorEdit.command.command, ...refactorEdit.command.arguments); + } else { + await commands.executeCommand(refactorEdit.command.command); + } + } +} + async function moveFile(languageClient: LanguageClient, fileUris: Uri[]) { if (!hasCommonParent(fileUris)) { window.showErrorMessage("Moving files of different directories are not supported. Please make sure they are from the same directory."); @@ -156,18 +167,15 @@ async function moveFile(languageClient: LanguageClient, fileUris: Uri[]) { fileUris = moveUris; } - const workspaceEdit = await languageClient.sendRequest(MoveFileRequest.type, { + const refactorEdit: RefactorWorkspaceEdit = await languageClient.sendRequest(MoveFileRequest.type, { documentUris: fileUris.map(uri => uri.toString()), targetUri: selectPackageNodeItem.packageNode.uri, updateReferences: true, }); - if (workspaceEdit) { - const edit = languageClient.protocol2CodeConverter.asWorkspaceEdit(workspaceEdit); - if (edit) { - await workspace.applyEdit(edit); - } - await saveEdit(workspaceEdit); + await applyRefactorEdit(languageClient, refactorEdit); + if (refactorEdit && refactorEdit.edit) { + await saveEdit(refactorEdit.edit); } }