Skip to content

Commit

Permalink
Show error message if move file operation failed
Browse files Browse the repository at this point in the history
Signed-off-by: Jinbo Wang <[email protected]>
  • Loading branch information
testforstephen committed Jul 30, 2019
1 parent 7e33a58 commit 88aafeb
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 24 deletions.
3 changes: 2 additions & 1 deletion src/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,7 @@ export interface RenamePosition {
export interface RefactorWorkspaceEdit {
edit: WorkspaceEdit;
command?: Command;
errorMessage?: string;
}

export interface GetRefactorEditParams {
Expand Down Expand Up @@ -325,5 +326,5 @@ export interface MoveFileParams {
}

export namespace MoveFileRequest {
export const type = new RequestType<MoveFileParams, WorkspaceEdit, void, void>('java/moveFile');
export const type = new RequestType<MoveFileParams, RefactorWorkspaceEdit, void, void>('java/moveFile');
}
54 changes: 31 additions & 23 deletions src/refactorAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.");
Expand Down Expand Up @@ -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);
}
}

Expand Down

0 comments on commit 88aafeb

Please sign in to comment.