From bf770d1df91704e317d5a2f4a50c8f8e32f5d0dc Mon Sep 17 00:00:00 2001 From: Valeriy Svydenko Date: Thu, 18 Feb 2016 18:12:49 +0200 Subject: [PATCH] CHE-142:reindex project after the refactoring operation Signed-off-by: Valeriy Svydenko --- .../jdt/refactoring/RefactoringManager.java | 24 +++- .../che/jdt/rest/RefactoringService.java | 119 +++++++++++++----- .../move/wizard/MovePresenter.java | 1 + .../rename/JavaRefactoringRename.java | 19 ++- .../rename/wizard/RenamePresenter.java | 6 + .../service/RefactoringServiceClient.java | 11 +- .../service/RefactoringServiceClientImpl.java | 9 ++ 7 files changed, 149 insertions(+), 40 deletions(-) diff --git a/plugins/plugin-java/che-plugin-java-ext-jdt/che-jdt-ext-machine/src/main/java/org/eclipse/che/jdt/refactoring/RefactoringManager.java b/plugins/plugin-java/che-plugin-java-ext-jdt/che-jdt-ext-machine/src/main/java/org/eclipse/che/jdt/refactoring/RefactoringManager.java index 23f645d41a..1431971840 100644 --- a/plugins/plugin-java/che-plugin-java-ext-jdt/che-jdt-ext-machine/src/main/java/org/eclipse/che/jdt/refactoring/RefactoringManager.java +++ b/plugins/plugin-java/che-plugin-java-ext-jdt/che-jdt-ext-machine/src/main/java/org/eclipse/che/jdt/refactoring/RefactoringManager.java @@ -80,6 +80,7 @@ import static org.eclipse.che.ide.ext.java.shared.dto.refactoring.ReorgDestinati /** * Manager for all refactoring sessions. Handles creating caching and applying refactorings. * @author Evgen Vidolob + * @author Valeriy Svydenko */ @Singleton public class RefactoringManager { @@ -247,7 +248,6 @@ public class RefactoringManager { RefactoringSession session = getRefactoringSession(sessionId); RefactoringResult result = session.apply(); deleteRefactoringSession(sessionId); - return result; } @@ -266,8 +266,10 @@ public class RefactoringManager { * @throws CoreException when impossible to create RenameSupport * @throws RefactoringException when we don't support renaming provided element */ - public RenameRefactoringSession createRenameRefactoring(IJavaElement element, ICompilationUnit cu, int offset, boolean lightweight) - throws CoreException, RefactoringException { + public RenameRefactoringSession createRenameRefactoring(IJavaElement element, + ICompilationUnit cu, + int offset, + boolean lightweight) throws CoreException, RefactoringException { //package fragments are always renamed with wizard RenameRefactoringSession session = DtoFactory.newDto(RenameRefactoringSession.class); @@ -317,7 +319,7 @@ public class RefactoringManager { else return RenameWizard.METHOD; case IJavaElement.FIELD: - if(JdtFlags.isEnum((IMember)element)){ + if (JdtFlags.isEnum((IMember)element)) { return RenameWizard.ENUM_CONSTANT; } return RenameWizard.FIELD; @@ -417,4 +419,18 @@ public class RefactoringManager { } return null; } + + /** + * Make reindex for the project. + * + * @param javaProject + * java project + * @throws JavaModelException + * when something is wrong + */ + public void reindexProject(IJavaProject javaProject) throws JavaModelException { + if (javaProject != null) { + JavaModelManager.getIndexManager().indexAll(javaProject.getProject()); + } + } } diff --git a/plugins/plugin-java/che-plugin-java-ext-jdt/che-jdt-ext-machine/src/main/java/org/eclipse/che/jdt/rest/RefactoringService.java b/plugins/plugin-java/che-plugin-java-ext-jdt/che-jdt-ext-machine/src/main/java/org/eclipse/che/jdt/rest/RefactoringService.java index c90c637f07..201487f4d4 100644 --- a/plugins/plugin-java/che-plugin-java-ext-jdt/che-jdt-ext-machine/src/main/java/org/eclipse/che/jdt/rest/RefactoringService.java +++ b/plugins/plugin-java/che-plugin-java-ext-jdt/che-jdt-ext-machine/src/main/java/org/eclipse/che/jdt/rest/RefactoringService.java @@ -43,13 +43,17 @@ import org.eclipse.jdt.internal.core.JavaModelManager; import org.eclipse.jdt.internal.corext.refactoring.RefactoringAvailabilityTester; import javax.ws.rs.Consumes; +import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.Produces; +import javax.ws.rs.QueryParam; +import javax.ws.rs.core.Response; import java.util.function.Function; /** * Service for all Java refactorings + * * @author Evgen Vidolob */ @Path("/jdt/{ws-id}/refactoring") @@ -64,10 +68,14 @@ public class RefactoringService { /** * Create move refactoring session. - * @param cmr move settings, contains resource paths to move. + * + * @param cmr + * move settings, contains resource paths to move. * @return refactoring session id. - * @throws JavaModelException when JavaModel has a failure - * @throws RefactoringException when impossible to create move refactoring session + * @throws JavaModelException + * when JavaModel has a failure + * @throws RefactoringException + * when impossible to create move refactoring session */ @POST @Path("move/create") @@ -109,10 +117,14 @@ public class RefactoringService { /** * Set destination for reorg refactorings. - * @param destination the destination for reorg refactoring + * + * @param destination + * the destination for reorg refactoring * @return refactoring status - * @throws RefactoringException when there are no corresponding refactoring session - * @throws JavaModelException when JavaModel has a failure + * @throws RefactoringException + * when there are no corresponding refactoring session + * @throws JavaModelException + * when JavaModel has a failure */ @POST @Path("set/destination") @@ -124,8 +136,11 @@ public class RefactoringService { /** * Set move refactoring wizard setting. - * @param settings the move settings - * @throws RefactoringException when there are no corresponding refactoring session + * + * @param settings + * the move settings + * @throws RefactoringException + * when there are no corresponding refactoring session */ @POST @Path("set/move/setting") @@ -137,9 +152,12 @@ public class RefactoringService { /** * Create refactoring change. * Creation of the change starts final checking for refactoring. Without creating change refactoring can't be applied. - * @param refactoringSession the refactoring session. + * + * @param refactoringSession + * the refactoring session. * @return result of creation of the change. - * @throws RefactoringException when there are no corresponding refactoring session + * @throws RefactoringException + * when there are no corresponding refactoring session */ @POST @Path("create/change") @@ -151,9 +169,12 @@ public class RefactoringService { /** * Get refactoring preview. Preview is tree of refactoring changes. - * @param refactoringSession the refactoring session. + * + * @param refactoringSession + * the refactoring session. * @return refactoring preview tree - * @throws RefactoringException when there are no corresponding refactoring session + * @throws RefactoringException + * when there are no corresponding refactoring session */ @POST @Path("get/preview") @@ -165,8 +186,11 @@ public class RefactoringService { /** * Change enabled/disabled state of the corresponding refactoring change. - * @param state the state of refactoring change - * @throws RefactoringException when there are no corresponding refactoring session or refactoring change + * + * @param state + * the state of refactoring change + * @throws RefactoringException + * when there are no corresponding refactoring session or refactoring change */ @POST @Path("change/enabled") @@ -176,7 +200,9 @@ public class RefactoringService { /** * Get refactoring change preview. Preview contains new and old content of the file - * @param change the change to get preview + * + * @param change + * the change to get preview * @return refactoring change preview * @throws RefactoringException */ @@ -190,24 +216,31 @@ public class RefactoringService { /** * Apply refactoring. - * @param session the refactoring session + * + * @param session + * the refactoring session * @return the result fo applied refactoring - * @throws RefactoringException when there are no corresponding refactoring session + * @throws RefactoringException + * when there are no corresponding refactoring session */ @POST @Path("apply") @Produces("application/json") @Consumes("application/json") - public RefactoringResult applyRefactoring(RefactoringSession session) throws RefactoringException { + public RefactoringResult applyRefactoring(RefactoringSession session) throws RefactoringException, JavaModelException { return manager.applyRefactoring(session.getSessionId()); } /** * Create rename refactoring session. - * @param settings rename settings + * + * @param settings + * rename settings * @return the rename refactoring session - * @throws CoreException when RenameSupport can't be created - * @throws RefactoringException when Java element was not found + * @throws CoreException + * when RenameSupport can't be created + * @throws RefactoringException + * when Java element was not found */ @POST @Path("rename/create") @@ -232,7 +265,7 @@ public class RefactoringService { default: elementToRename = null; } - if(elementToRename == null){ + if (elementToRename == null) { throw new RefactoringException("Can't find java element to rename."); } @@ -241,10 +274,14 @@ public class RefactoringService { /** * Apply linked mode rename refactoring. - * @param refactoringApply linked mode setting and refactoring session id + * + * @param refactoringApply + * linked mode setting and refactoring session id * @return the result fo applied refactoring - * @throws RefactoringException when there are no corresponding refactoring session - * @throws CoreException when impossible to apply rename refactoring + * @throws RefactoringException + * when there are no corresponding refactoring session + * @throws CoreException + * when impossible to apply rename refactoring */ @POST @Path("rename/linked/apply") @@ -257,9 +294,12 @@ public class RefactoringService { /** * Validate new name. Used for validation new name in rename refactoring wizard. - * @param newName the new element name + * + * @param newName + * the new element name * @return the status of validation - * @throws RefactoringException when there are no corresponding refactoring session + * @throws RefactoringException + * when there are no corresponding refactoring session */ @POST @Path("rename/validate/name") @@ -271,8 +311,11 @@ public class RefactoringService { /** * Set rename refactoring wizard settings. - * @param settings refactoring wizard settings - * @throws RefactoringException when there are no corresponding refactoring session + * + * @param settings + * refactoring wizard settings + * @throws RefactoringException + * when there are no corresponding refactoring session */ @POST @Path("set/rename/settings") @@ -281,9 +324,25 @@ public class RefactoringService { manager.setRenameSettings(settings); } + /** + * Make reindex for the project. + * + * @param projectPath + * path to the project + * @throws JavaModelException + * when something is wrong + */ + @GET + @Path("reindex") + @Consumes("text/plain") + public Response reindexProject(@QueryParam("projectpath") String projectPath) throws JavaModelException { + manager.reindexProject(model.getJavaProject(projectPath)); + return Response.ok().build(); + } + private IJavaElement getSelectionElement(ICompilationUnit compilationUnit, int offset) throws JavaModelException, RefactoringException { IJavaElement[] javaElements = compilationUnit.codeSelect(offset, 0); - if(javaElements != null && javaElements.length >0){ + if (javaElements != null && javaElements.length > 0) { return javaElements[0]; } throw new RefactoringException("Can't find java element to rename."); diff --git a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/move/wizard/MovePresenter.java b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/move/wizard/MovePresenter.java index dc880778e8..f6ff7ba6a4 100644 --- a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/move/wizard/MovePresenter.java +++ b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/move/wizard/MovePresenter.java @@ -241,6 +241,7 @@ public class MovePresenter implements MoveView.ActionDelegate { if (arg.getSeverity() == OK) { view.hide(); refactoringUpdater.updateAfterRefactoring(refactorInfo, arg.getChanges()); + refactorService.reindexProject(getPathToProject()); } else { view.showErrorMessage(arg); } diff --git a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/rename/JavaRefactoringRename.java b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/rename/JavaRefactoringRename.java index 1075f5aa0a..6957f72a02 100644 --- a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/rename/JavaRefactoringRename.java +++ b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/rename/JavaRefactoringRename.java @@ -78,6 +78,7 @@ public class JavaRefactoringRename { private final MessageLoader loader; private boolean isActiveLinkedEditor; + private TextEditor textEditor; private LinkedMode mode; @Inject @@ -108,7 +109,11 @@ public class JavaRefactoringRename { * editor where user makes refactoring */ public void refactor(final TextEditor textEditorPresenter) { - final CreateRenameRefactoring createRenameRefactoring = createRenameRefactoringDto(textEditorPresenter); + if (!isActiveLinkedEditor) { + textEditor = textEditorPresenter; + } + + final CreateRenameRefactoring createRenameRefactoring = createRenameRefactoringDto(textEditor); textEditorPresenter.setFocus(); @@ -121,12 +126,15 @@ public class JavaRefactoringRename { if (mode != null) { mode.exitLinkedMode(false); } - } else if (session.getLinkedModeModel() != null && textEditorPresenter instanceof HasLinkedMode) { + } else if (session.getLinkedModeModel() != null && textEditor instanceof HasLinkedMode) { isActiveLinkedEditor = true; - activateLinkedModeIntoEditor(session, ((HasLinkedMode)textEditorPresenter), textEditorPresenter.getDocument()); + activateLinkedModeIntoEditor(session, ((HasLinkedMode)textEditor), textEditor.getDocument()); } else { - notificationManager.notify(locale.failedToRename(), locale.renameErrorEditor(), FAIL, true, - textEditorPresenter.getEditorInput().getFile().getProject().getProjectConfig()); + notificationManager.notify(locale.failedToRename(), + locale.renameErrorEditor(), + FAIL, + true, + textEditor.getEditorInput().getFile().getProject().getProjectConfig()); } } }).catchError(new Operation() { @@ -234,6 +242,7 @@ public class JavaRefactoringRename { case OK: RefactorInfo refactorInfo = RefactorInfo.of(RefactoredItemType.JAVA_ELEMENT, null); refactoringUpdater.updateAfterRefactoring(refactorInfo, result.getChanges()); + refactoringServiceClient.reindexProject(textEditor.getDocument().getFile().getProject().getProjectConfig().getPath()); loader.hide(); break; case INFO: diff --git a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/rename/wizard/RenamePresenter.java b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/rename/wizard/RenamePresenter.java index fb77fca1ec..1c39c81339 100644 --- a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/rename/wizard/RenamePresenter.java +++ b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/rename/wizard/RenamePresenter.java @@ -197,6 +197,10 @@ public class RenamePresenter implements ActionDelegate { /** {@inheritDoc} */ @Override public void onCancelButtonClicked() { + setEditorFocus(); + } + + private void setEditorFocus() { EditorPartPresenter activeEditor = editorAgent.getActiveEditor(); if (activeEditor instanceof TextEditor) { ((TextEditor)activeEditor).setFocus(); @@ -332,6 +336,8 @@ public class RenamePresenter implements ActionDelegate { if (arg.getSeverity() == OK) { view.hide(); refactoringUpdater.updateAfterRefactoring(refactorInfo, arg.getChanges()); + refactorService.reindexProject(appContext.getCurrentProject().getProjectConfig().getPath()); + setEditorFocus(); } else { view.showErrorMessage(arg); } diff --git a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/service/RefactoringServiceClient.java b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/service/RefactoringServiceClient.java index 9c4b1e0000..f66c7bc4ee 100644 --- a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/service/RefactoringServiceClient.java +++ b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/service/RefactoringServiceClient.java @@ -48,7 +48,7 @@ public interface RefactoringServiceClient { */ Promise createMoveRefactoring(CreateMoveRefactoring moveRefactoring); - /** + /** * Creates rename refactoring session. * @param settings rename settings * @return an instance of refactoring session id @@ -143,4 +143,13 @@ public interface RefactoringServiceClient { * @return empty promise result */ Promise setRenameSettings(RenameSettings settings); + + /** + * Make reindex for the project. + * + * @param projectPath + * path to the project + * @return empty promise result + */ + Promise reindexProject(String projectPath); } diff --git a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/service/RefactoringServiceClientImpl.java b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/service/RefactoringServiceClientImpl.java index b2d2bc049c..89339f55c6 100644 --- a/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/service/RefactoringServiceClientImpl.java +++ b/plugins/plugin-java/che-plugin-java-ext-lang-client/src/main/java/org/eclipse/che/ide/ext/java/client/refactoring/service/RefactoringServiceClientImpl.java @@ -281,4 +281,13 @@ final class RefactoringServiceClientImpl implements RefactoringServiceClient { } }); } + + @Override + public Promise reindexProject(String projectPath) { + final String url = pathToService + "reindex?projectpath=" + projectPath; + + return asyncRequestFactory.createGetRequest(url) + .loader(loader) + .send(); + } }