Skip to content

Commit

Permalink
Show warning dialog after refactoring operation if something isn't va…
Browse files Browse the repository at this point in the history
…lid (#10080)

Signed-off-by: Valeriy Svydenko <[email protected]>
  • Loading branch information
Valeriy Svydenko authored and tsmaeder committed Oct 1, 2018
1 parent 8106899 commit d2e62a0
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,8 @@ public void onPreviewButtonClicked() {
extensionService
.move(moveSettings)
.then(
workspaceEdit -> {
previewPresenter.show(workspaceEdit, this);
refactoringResult -> {
previewPresenter.show(refactoringResult.getCheWorkspaceEdit(), this);
})
.catchError(
error -> {
Expand All @@ -174,9 +174,9 @@ public void onAcceptButtonClicked() {
extensionService
.move(moveSettings)
.then(
workspaceEdit -> {
refactoringResult -> {
view.close();
applyWorkspaceEditAction.applyWorkspaceEdit(workspaceEdit);
applyWorkspaceEditAction.applyWorkspaceEdit(refactoringResult.getCheWorkspaceEdit());
setEditorFocus();
})
.catchError(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.eclipse.che.ide.api.editor.events.FileEvent.FileOperation.CLOSE;
import static org.eclipse.che.ide.api.notification.StatusNotification.DisplayMode.FLOAT_MODE;
import static org.eclipse.che.ide.api.notification.StatusNotification.Status.FAIL;
import static org.eclipse.che.jdt.ls.extension.api.RefactoringSeverity.FATAL;

import com.google.inject.Inject;
import com.google.inject.Singleton;
Expand Down Expand Up @@ -44,6 +45,8 @@
import org.eclipse.che.ide.ext.java.client.service.JavaLanguageExtensionServiceClient;
import org.eclipse.che.ide.ui.dialogs.DialogFactory;
import org.eclipse.che.jdt.ls.extension.api.RenameKind;
import org.eclipse.che.jdt.ls.extension.api.dto.RefactoringStatus;
import org.eclipse.che.jdt.ls.extension.api.dto.RefactoringStatusEntry;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSettings;
import org.eclipse.che.plugin.languageserver.ide.editor.quickassist.ApplyWorkspaceEditAction;
import org.eclipse.che.plugin.languageserver.ide.service.TextDocumentServiceClient;
Expand Down Expand Up @@ -308,12 +311,21 @@ private void performRename(String newName) {
extensionServiceClient
.rename(settings)
.then(
edits -> {
result -> {
enableAutoSave();
undoChanges();
applyWorkspaceEditAction.applyWorkspaceEdit(edits);
clientServerEventService.sendFileTrackingResumeEvent();
sendOpenEvent();
RefactoringStatus refactoringStatus = result.getRefactoringStatus();
if (!FATAL.equals(refactoringStatus.getRefactoringSeverity())) {
applyWorkspaceEditAction.applyWorkspaceEdit(result.getCheWorkspaceEdit());
clientServerEventService.sendFileTrackingResumeEvent();
sendOpenEvent();
} else {
notificationManager.notify(
locale.failedToRename(),
getErrorMessage(refactoringStatus.getRefactoringStatusEntries()),
FAIL,
FLOAT_MODE);
}
})
.catchError(
error -> {
Expand All @@ -325,6 +337,15 @@ private void performRename(String newName) {
});
}

private String getErrorMessage(List<RefactoringStatusEntry> entries) {
for (RefactoringStatusEntry entry : entries) {
if (FATAL.equals(entry.getRefactoringSeverity())) {
return entry.getMessage();
}
}
return "";
}

private void enableAutoSave() {
if (linkedEditor instanceof EditorWithAutoSave) {
((EditorWithAutoSave) linkedEditor).enableAutoSave();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.inject.Inject;
import com.google.inject.Singleton;
import java.util.List;
import org.eclipse.che.api.promises.client.Promise;
import org.eclipse.che.ide.api.editor.EditorAgent;
import org.eclipse.che.ide.api.editor.EditorPartPresenter;
Expand All @@ -35,8 +36,13 @@
import org.eclipse.che.ide.ext.java.client.refactoring.rename.wizard.RenameView.ActionDelegate;
import org.eclipse.che.ide.ext.java.client.refactoring.rename.wizard.similarnames.SimilarNamesConfigurationPresenter;
import org.eclipse.che.ide.ext.java.client.service.JavaLanguageExtensionServiceClient;
import org.eclipse.che.ide.ui.dialogs.DialogFactory;
import org.eclipse.che.ide.ui.dialogs.confirm.ConfirmCallback;
import org.eclipse.che.jdt.ls.extension.api.RefactoringSeverity;
import org.eclipse.che.jdt.ls.extension.api.RenameKind;
import org.eclipse.che.jdt.ls.extension.api.dto.CheWorkspaceEdit;
import org.eclipse.che.jdt.ls.extension.api.dto.RefactoringResult;
import org.eclipse.che.jdt.ls.extension.api.dto.RefactoringStatusEntry;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSelectionParams;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSettings;
import org.eclipse.che.jdt.ls.extension.api.dto.RenamingElementInfo;
Expand All @@ -58,6 +64,7 @@ public class RenamePresenter implements ActionDelegate, RefactoringActionDelegat
private final ApplyWorkspaceEditAction applyWorkspaceEditAction;
private final JavaLocalizationConstant locale;
private final DtoBuildHelper dtoBuildHelper;
private final DialogFactory dialogFactory;
private final EditorAgent editorAgent;
private final NotificationManager notificationManager;
private final PreviewPresenter previewPresenter;
Expand All @@ -73,6 +80,7 @@ public RenamePresenter(
ApplyWorkspaceEditAction applyWorkspaceEditAction,
JavaLocalizationConstant locale,
DtoBuildHelper dtoBuildHelper,
DialogFactory dialogFactory,
EditorAgent editorAgent,
NotificationManager notificationManager,
PreviewPresenter previewPresenter,
Expand All @@ -83,6 +91,7 @@ public RenamePresenter(
this.applyWorkspaceEditAction = applyWorkspaceEditAction;
this.locale = locale;
this.dtoBuildHelper = dtoBuildHelper;
this.dialogFactory = dialogFactory;
this.editorAgent = editorAgent;
this.notificationManager = notificationManager;
this.view.setDelegate(this);
Expand Down Expand Up @@ -199,7 +208,23 @@ public void onPreviewButtonClicked() {
/** {@inheritDoc} */
@Override
public void onAcceptButtonClicked() {
getChanges().then(this::applyRefactoring);
getChanges()
.then(
refactoringResult -> {
RefactoringSeverity severity =
refactoringResult.getRefactoringStatus().getRefactoringSeverity();
switch (severity) {
case WARNING:
case ERROR:
showWarningDialog(refactoringResult);
break;
case FATAL:
view.showErrorMessage(refactoringResult.getRefactoringStatus());
break;
default:
applyRefactoring(refactoringResult.getCheWorkspaceEdit());
}
});
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -276,13 +301,17 @@ public void validateName() {
private void showPreview() {
getChanges()
.then(
workspaceEdit -> {
previewPresenter.show(workspaceEdit, this);
refactoringResult -> {
CheWorkspaceEdit edit = refactoringResult.getCheWorkspaceEdit();
if (edit == null) {
return;
}
previewPresenter.show(edit, this);
previewPresenter.setTitle(locale.renameItemTitle());
});
}

private Promise<CheWorkspaceEdit> getChanges() {
private Promise<RefactoringResult> getChanges() {
RenameSettings renameSettings = createRenameSettings();
RenameParams renameParams = createRenameParams(renameSettings);

Expand Down Expand Up @@ -335,6 +364,24 @@ private void applyRefactoring(CheWorkspaceEdit workspaceEdit) {
setEditorFocus();
}

private void showWarningDialog(RefactoringResult refactoringResult) {
List<RefactoringStatusEntry> entries =
refactoringResult.getRefactoringStatus().getRefactoringStatusEntries();

ConfirmCallback confirmCallback =
() -> applyRefactoring(refactoringResult.getCheWorkspaceEdit());

dialogFactory
.createConfirmDialog(
locale.warningOperationTitle(),
entries.isEmpty() ? locale.warningOperationContent() : entries.get(0).getMessage(),
locale.renameRename(),
locale.buttonCancel(),
confirmCallback,
() -> {})
.show();
}

private RenameSettings createRenameSettings() {
RenameSettings renameSettings = dtoFactory.createDto(RenameSettings.class);
renameSettings.setDelegateUpdating(view.isUpdateDelegateUpdating());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import org.eclipse.che.api.promises.client.js.RejectFunction;
import org.eclipse.che.ide.api.app.AppContext;
import org.eclipse.che.ide.api.workspace.WorkspaceReadyEvent;
import org.eclipse.che.jdt.ls.extension.api.dto.CheWorkspaceEdit;
import org.eclipse.che.jdt.ls.extension.api.dto.ClasspathEntry;
import org.eclipse.che.jdt.ls.extension.api.dto.CreateMoveParams;
import org.eclipse.che.jdt.ls.extension.api.dto.ExtendedSymbolInformation;
Expand All @@ -66,6 +65,7 @@
import org.eclipse.che.jdt.ls.extension.api.dto.OrganizeImportParams;
import org.eclipse.che.jdt.ls.extension.api.dto.OrganizeImportsResult;
import org.eclipse.che.jdt.ls.extension.api.dto.ReImportMavenProjectsCommandParameters;
import org.eclipse.che.jdt.ls.extension.api.dto.RefactoringResult;
import org.eclipse.che.jdt.ls.extension.api.dto.RefactoringStatus;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSelectionParams;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSettings;
Expand Down Expand Up @@ -295,15 +295,15 @@ public Promise<JarEntry> libraryEntry(String resourceUri) {
}

/** Rename refactoring. */
public Promise<CheWorkspaceEdit> rename(RenameSettings renameSettings) {
public Promise<RefactoringResult> rename(RenameSettings renameSettings) {
return Promises.create(
(resolve, reject) ->
requestTransmitter
.newRequest()
.endpointId(WS_AGENT_JSON_RPC_ENDPOINT_ID)
.methodName(REFACTORING_RENAME)
.paramsAsDto(renameSettings)
.sendAndReceiveResultAsDto(CheWorkspaceEdit.class, REQUEST_TIMEOUT)
.sendAndReceiveResultAsDto(RefactoringResult.class, REQUEST_TIMEOUT)
.onSuccess(resolve::apply)
.onTimeout(() -> onTimeout(reject))
.onFailure(error -> reject.apply(ServiceUtil.getPromiseError(error))));
Expand Down Expand Up @@ -355,15 +355,15 @@ public Promise<List<Range>> getLinkedModeModel(TextDocumentPositionParams linked
}

/** Move refactoring. */
public Promise<CheWorkspaceEdit> move(MoveSettings moveSettings) {
public Promise<RefactoringResult> move(MoveSettings moveSettings) {
return Promises.create(
(resolve, reject) ->
requestTransmitter
.newRequest()
.endpointId(WS_AGENT_JSON_RPC_ENDPOINT_ID)
.methodName(REFACTORING_MOVE)
.paramsAsDto(moveSettings)
.sendAndReceiveResultAsDto(CheWorkspaceEdit.class, REQUEST_TIMEOUT)
.sendAndReceiveResultAsDto(RefactoringResult.class, REQUEST_TIMEOUT)
.onSuccess(resolve::apply)
.onTimeout(() -> onTimeout(reject))
.onFailure(error -> reject.apply(ServiceUtil.getPromiseError(error))));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@
import org.eclipse.che.jdt.ls.extension.api.dto.PackageFragment;
import org.eclipse.che.jdt.ls.extension.api.dto.PackageFragmentRoot;
import org.eclipse.che.jdt.ls.extension.api.dto.ReImportMavenProjectsCommandParameters;
import org.eclipse.che.jdt.ls.extension.api.dto.RefactoringResult;
import org.eclipse.che.jdt.ls.extension.api.dto.RefactoringStatus;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSelectionParams;
import org.eclipse.che.jdt.ls.extension.api.dto.RenameSettings;
Expand Down Expand Up @@ -288,7 +289,7 @@ public void configureMethods() {
.newConfiguration()
.methodName(REFACTORING_RENAME)
.paramsAsDto(RenameSettings.class)
.resultAsDto(CheWorkspaceEdit.class)
.resultAsDto(RefactoringResult.class)
.withFunction(this::rename);

requestHandler
Expand Down Expand Up @@ -323,7 +324,7 @@ public void configureMethods() {
.newConfiguration()
.methodName(REFACTORING_MOVE)
.paramsAsDto(MoveSettings.class)
.resultAsDto(CheWorkspaceEdit.class)
.resultAsDto(RefactoringResult.class)
.withFunction(this::move);

requestHandler
Expand Down Expand Up @@ -681,17 +682,19 @@ private JarEntry getLibraryEntry(String resourceUri) {
return doGetOne(GET_LIBRARY_ENTRY_COMMAND, singletonList(fixJdtUri(resourceUri)), type);
}

private CheWorkspaceEdit rename(RenameSettings renameSettings) {
Type type = new TypeToken<CheWorkspaceEdit>() {}.getType();
private RefactoringResult rename(RenameSettings renameSettings) {
Type type = new TypeToken<RefactoringResult>() {}.getType();
String uri = renameSettings.getRenameParams().getTextDocument().getUri();
renameSettings.getRenameParams().getTextDocument().setUri(prefixURI(uri));

CheWorkspaceEdit cheWorkspaceEdit =
RefactoringResult refactoringResult =
doGetOne(RENAME_COMMAND, singletonList(renameSettings), type);
CheWorkspaceEdit cheWorkspaceEdit = refactoringResult.getCheWorkspaceEdit();
List<CheResourceChange> resourceChanges = getResourceChanges(cheWorkspaceEdit);
cheWorkspaceEdit.setCheResourceChanges(resourceChanges);
cheWorkspaceEdit.setChanges(getTextChanges(cheWorkspaceEdit));
return cheWorkspaceEdit;
refactoringResult.setCheWorkspaceEdit(cheWorkspaceEdit);
return refactoringResult;
}

private List<CheResourceChange> getResourceChanges(CheWorkspaceEdit cheWorkspaceEdit) {
Expand Down Expand Up @@ -770,8 +773,8 @@ private void convertUrisToPath(List<JavaProjectStructure> projectStructures) {
}
}

private CheWorkspaceEdit move(MoveSettings moveSettings) {
Type type = new TypeToken<CheWorkspaceEdit>() {}.getType();
private RefactoringResult move(MoveSettings moveSettings) {
Type type = new TypeToken<RefactoringResult>() {}.getType();
String destinationUri = moveSettings.getDestination();
moveSettings.setDestination(prefixURI(destinationUri));

Expand All @@ -784,12 +787,14 @@ private CheWorkspaceEdit move(MoveSettings moveSettings) {

moveSettings.setElements(resourceToMove);

CheWorkspaceEdit cheWorkspaceEdit =
RefactoringResult refactoringResult =
doGetOne(Commands.MOVE_COMMAND, singletonList(moveSettings), type);
CheWorkspaceEdit cheWorkspaceEdit = refactoringResult.getCheWorkspaceEdit();
List<CheResourceChange> resourceChanges = getResourceChanges(cheWorkspaceEdit);
cheWorkspaceEdit.setCheResourceChanges(resourceChanges);
cheWorkspaceEdit.setChanges(getTextChanges(cheWorkspaceEdit));
return cheWorkspaceEdit;
refactoringResult.setCheWorkspaceEdit(cheWorkspaceEdit);
return refactoringResult;
}

private Boolean validateMove(CreateMoveParams moveParams) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public class RenameStaticMethodsTest {
private static final String pathToPackageInChePrefix =
NAME_OFP_ROJECT + "/src" + "/main" + "/java" + "/renameStaticMethods";
private static final String testsFail5ErrorMess =
"Related method 'm' (declared in 'renameStaticMethods.testFail5.A') is native. Renaming will cause an UnsatisfiedLinkError on runtime.";
"Renaming native methods will cause an unsatisfied link error on runtime.";

private String pathToCurrentPackage;
private String contentFromInA;
Expand Down

0 comments on commit d2e62a0

Please sign in to comment.