Search in sources :

Example 1 with ChangeInfo

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo in project che by eclipse.

the class RefactoringSession method prepareRenamePackageChange.

private void prepareRenamePackageChange(List<ChangeInfo> changesInfo, ChangeInfo changeInfo, Change ch) {
    changeInfo.setName(ChangeInfo.ChangeName.RENAME_PACKAGE);
    RenamePackageChange renameChange = (RenamePackageChange) ch;
    IPath oldPackageName = new Path(renameChange.getOldName().replace('.', IPath.SEPARATOR));
    IPath newPackageName = new Path(renameChange.getNewName().replace('.', IPath.SEPARATOR));
    changeInfo.setOldPath(renameChange.getResourcePath().removeLastSegments(oldPackageName.segmentCount()).append(newPackageName).toString());
    changeInfo.setPath(renameChange.getResourcePath().toString());
    Set<IResource> compilationUnits = renameChange.getFCompilationUnitStamps().keySet();
    for (IResource iResource : compilationUnits) {
        ChangeInfo change = DtoFactory.newDto(ChangeInfo.class);
        change.setName(ChangeInfo.ChangeName.UPDATE);
        IPath fullPathOldPath = iResource.getFullPath();
        IPath newPath = renameChange.getResourcePath().append(fullPathOldPath.toFile().getName());
        change.setOldPath(fullPathOldPath.toString());
        change.setPath(newPath.toString());
        changesInfo.add(change);
    }
}
Also used : IPath(org.eclipse.core.runtime.IPath) Path(org.eclipse.core.runtime.Path) IPath(org.eclipse.core.runtime.IPath) ChangeInfo(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo) RenamePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenamePackageChange) IResource(org.eclipse.core.resources.IResource)

Example 2 with ChangeInfo

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo in project che by eclipse.

the class RenamePresenterTest method changesShouldBeAppliedWithOkStatus.

@Test
public void changesShouldBeAppliedWithOkStatus() throws Exception {
    RefactorInfo refactorInfo = RefactorInfo.of(MoveType.REFACTOR_MENU, RefactoredItemType.COMPILATION_UNIT, new Resource[] { file });
    List<ChangeInfo> changes = new ArrayList<>();
    when(refactoringStatus.getChanges()).thenReturn(changes);
    when(changeCreationResult.isCanShowPreviewPage()).thenReturn(true);
    when(refactoringStatus.getSeverity()).thenReturn(0);
    renamePresenter.show(refactorInfo);
    verify(refactorService).createRenameRefactoring(createRenameRefactoringDto);
    verify(renameRefactoringSessionPromise).then(renameRefactoringSessionCaptor.capture());
    renameRefactoringSessionCaptor.getValue().apply(session);
    renamePresenter.onAcceptButtonClicked();
    verify(refactoringSession).setSessionId(SESSION_ID);
    verifyPreparingRenameSettingsDto();
    verifyPreparingRenameChanges();
    verify(changeCreationResultPromise).then(changeCreationResultCaptor.capture());
    changeCreationResultCaptor.getValue().apply(changeCreationResult);
    verify(refactorService).applyRefactoring(refactoringSession);
    verify(refactoringStatusPromise).then(refactoringStatusCaptor.capture());
    refactoringStatusCaptor.getValue().apply(refactoringStatus);
    verify(refactoringStatus, times(2)).getSeverity();
    verify(view).hide();
    verify(refactoringUpdater).updateAfterRefactoring(changes);
}
Also used : ChangeInfo(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo) ArrayList(java.util.ArrayList) RefactorInfo(org.eclipse.che.ide.ext.java.client.refactoring.RefactorInfo) Test(org.junit.Test)

Example 3 with ChangeInfo

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo in project che by eclipse.

the class RefactoringSession method prepareChangesInfo.

/**
     * Prepare the information about changes which were applied.
     *
     * @param changes array of the applied changes
     * @param changesInfo prepared list of {@link ChangeInfo}
     */
public void prepareChangesInfo(Change[] changes, List<ChangeInfo> changesInfo) {
    for (Change ch : changes) {
        if (ch instanceof DynamicValidationStateChange) {
            prepareChangesInfo(((DynamicValidationStateChange) ch).getChildren(), changesInfo);
        } else {
            ChangeInfo changeInfo = DtoFactory.newDto(ChangeInfo.class);
            String refactoringName = ch.getName();
            if (ch instanceof UndoTextFileChange) {
                changeInfo.setName(ChangeInfo.ChangeName.UPDATE);
                changeInfo.setPath(((CompilationUnit) ch.getModifiedElement()).getPath().toString());
            }
            if (refactoringName.startsWith("Rename")) {
                if (ch instanceof RenameCompilationUnitChange) {
                    prepareRenameCompilationUnitChange(changeInfo, ch);
                } else if (ch instanceof RenamePackageChange) {
                    prepareRenamePackageChange(changesInfo, changeInfo, ch);
                }
            }
            if (refactoringName.startsWith("Move")) {
                prepareMoveChange(changeInfo, ch);
            }
            changesInfo.add(changeInfo);
        }
    }
}
Also used : CompilationUnit(org.eclipse.jdt.internal.core.CompilationUnit) ChangeInfo(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo) RenamePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenamePackageChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) MoveCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange) UndoTextFileChange(org.eclipse.ltk.core.refactoring.UndoTextFileChange) Change(org.eclipse.ltk.core.refactoring.Change) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange) RenamePackageChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenamePackageChange) UndoTextFileChange(org.eclipse.ltk.core.refactoring.UndoTextFileChange) DynamicValidationStateChange(org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange) RenameCompilationUnitChange(org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange)

Example 4 with ChangeInfo

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo in project che by eclipse.

the class RenameLinkedModeRefactoringSession method doRename.

/**
     * Make rename operation.
     * @param newName the name which will be applied
     * @return result of the rename operation
     * @throws CoreException if an error occurs while creating the refactoring instance
     * @throws InvocationTargetException if an error occurred while executing the
     * operation.
     * @throws InterruptedException if the operation has been canceled by the
     * user.
     */
public RefactoringResult doRename(String newName) throws CoreException, InvocationTargetException, InterruptedException {
    if (fOriginalName.equals(newName)) {
        return DtoConverter.toRefactoringResultDto(new RefactoringStatus());
    }
    RenameSupport renameSupport = undoAndCreateRenameSupport(newName);
    if (renameSupport == null)
        return DtoConverter.toRefactoringResultDto(RefactoringStatus.createFatalErrorStatus("Can't create rename refactoring"));
    RefactoringResult refactoringResult = DtoConverter.toRefactoringResultDto(renameSupport.perform());
    PerformChangeOperation operation = renameSupport.getfPerformChangeOperation();
    if (operation == null) {
        return refactoringResult;
    }
    CompositeChange operationChange = (CompositeChange) operation.getUndoChange();
    Change[] changes = operationChange.getChildren();
    List<ChangeInfo> changesInfo = new ArrayList<>();
    prepareChangesInfo(changes, changesInfo);
    refactoringResult.setChanges(changesInfo);
    return refactoringResult;
}
Also used : PerformChangeOperation(org.eclipse.ltk.core.refactoring.PerformChangeOperation) ChangeInfo(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo) RefactoringResult(org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringResult) ArrayList(java.util.ArrayList) RefactoringStatus(org.eclipse.ltk.core.refactoring.RefactoringStatus) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) Change(org.eclipse.ltk.core.refactoring.Change) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) RenameSupport(org.eclipse.jdt.ui.refactoring.RenameSupport)

Example 5 with ChangeInfo

use of org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo in project che by eclipse.

the class RefactoringUpdater method updateAfterRefactoring.

/**
     * Iterates over each refactoring change and according to change type performs specific update operation.
     * i.e. for {@code ChangeName#UPDATE} updates only opened editors, for {@code ChangeName#MOVE or ChangeName#RENAME_COMPILATION_UNIT}
     * updates only new paths and opened editors, for {@code ChangeName#RENAME_PACKAGE} reloads package structure and restore expansion.
     *
     * @param changes
     *         applied changes
     */
public void updateAfterRefactoring(List<ChangeInfo> changes) {
    if (changes == null || changes.isEmpty()) {
        return;
    }
    ExternalResourceDelta[] deltas = new ExternalResourceDelta[0];
    final List<String> pathChanged = new ArrayList<>();
    for (ChangeInfo change : changes) {
        final ExternalResourceDelta delta;
        final Path newPath = Path.valueOf(change.getPath());
        final Path oldPath = !isNullOrEmpty(change.getOldPath()) ? Path.valueOf(change.getOldPath()) : Path.EMPTY;
        switch(change.getName()) {
            case MOVE:
            case RENAME_COMPILATION_UNIT:
                delta = new ExternalResourceDelta(newPath, oldPath, ADDED | MOVED_FROM | MOVED_TO);
                registerRemovedFile(change);
                break;
            case RENAME_PACKAGE:
                delta = new ExternalResourceDelta(newPath, oldPath, ADDED | MOVED_FROM | MOVED_TO);
                break;
            case UPDATE:
                if (!isFileRemoved(change.getPath(), changes)) {
                    pathChanged.add(change.getPath());
                    registerRemovedFile(change);
                }
            default:
                continue;
        }
        final int index = deltas.length;
        deltas = Arrays.copyOf(deltas, index + 1);
        deltas[index] = delta;
    }
    //here we need to remove file for file that moved or renamed JDT lib sent it to
    for (int i = 0; i < deltas.length; i++) {
        if (pathChanged.contains(deltas[i].getToPath().toString())) {
            pathChanged.remove(deltas[i].getToPath().toString());
        }
        if (pathChanged.contains(deltas[i].getFromPath().toString())) {
            pathChanged.remove(deltas[i].getFromPath().toString());
        }
    }
    if (deltas.length > 0) {
        appContext.getWorkspaceRoot().synchronize(deltas).then(new Operation<ResourceDelta[]>() {

            @Override
            public void apply(final ResourceDelta[] appliedDeltas) throws OperationException {
                for (ResourceDelta delta : appliedDeltas) {
                    eventBus.fireEvent(new RevealResourceEvent(delta.getToPath()));
                }
                for (EditorPartPresenter editorPartPresenter : editorAgent.getOpenedEditors()) {
                    final String path = editorPartPresenter.getEditorInput().getFile().getLocation().toString();
                    if (pathChanged.contains(path)) {
                        eventBus.fireEvent(new FileContentUpdateEvent(editorPartPresenter.getEditorInput().getFile().getLocation().toString()));
                    }
                }
                setActiveEditor();
            }
        });
    } else {
        Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {

            @Override
            public void execute() {
                for (EditorPartPresenter editorPartPresenter : editorAgent.getOpenedEditors()) {
                    eventBus.fireEvent(new FileContentUpdateEvent(editorPartPresenter.getEditorInput().getFile().getLocation().toString()));
                }
                setActiveEditor();
            }
        });
    }
}
Also used : ExternalResourceDelta(org.eclipse.che.ide.api.resources.ExternalResourceDelta) Path(org.eclipse.che.ide.resource.Path) FileContentUpdateEvent(org.eclipse.che.ide.api.event.FileContentUpdateEvent) ChangeInfo(org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo) Scheduler(com.google.gwt.core.client.Scheduler) ArrayList(java.util.ArrayList) ExternalResourceDelta(org.eclipse.che.ide.api.resources.ExternalResourceDelta) ResourceDelta(org.eclipse.che.ide.api.resources.ResourceDelta) EditorPartPresenter(org.eclipse.che.ide.api.editor.EditorPartPresenter) RevealResourceEvent(org.eclipse.che.ide.resources.reveal.RevealResourceEvent) OperationException(org.eclipse.che.api.promises.client.OperationException)

Aggregations

ChangeInfo (org.eclipse.che.ide.ext.java.shared.dto.refactoring.ChangeInfo)9 ArrayList (java.util.ArrayList)5 RefactoringResult (org.eclipse.che.ide.ext.java.shared.dto.refactoring.RefactoringResult)3 RenamePackageChange (org.eclipse.jdt.internal.corext.refactoring.changes.RenamePackageChange)3 Change (org.eclipse.ltk.core.refactoring.Change)3 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)3 Test (org.junit.Test)3 OperationException (org.eclipse.che.api.promises.client.OperationException)2 RefactorInfo (org.eclipse.che.ide.ext.java.client.refactoring.RefactorInfo)2 DynamicValidationStateChange (org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange)2 MoveCompilationUnitChange (org.eclipse.jdt.internal.corext.refactoring.changes.MoveCompilationUnitChange)2 RenameCompilationUnitChange (org.eclipse.jdt.internal.corext.refactoring.changes.RenameCompilationUnitChange)2 PerformChangeOperation (org.eclipse.ltk.core.refactoring.PerformChangeOperation)2 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)2 UndoTextFileChange (org.eclipse.ltk.core.refactoring.UndoTextFileChange)2 Scheduler (com.google.gwt.core.client.Scheduler)1 Operation (org.eclipse.che.api.promises.client.Operation)1 PromiseError (org.eclipse.che.api.promises.client.PromiseError)1 EditorInput (org.eclipse.che.ide.api.editor.EditorInput)1 EditorPartPresenter (org.eclipse.che.ide.api.editor.EditorPartPresenter)1