Search in sources :

Example 31 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project xtext-eclipse by eclipse.

the class RefactoringUpdateAcceptor method createCompositeChange.

@Override
public Change createCompositeChange(String name, IProgressMonitor monitor) {
    if (document2change.isEmpty() && document2textEdits.isEmpty())
        return null;
    CompositeChange compositeChange = new CompositeChange(name);
    for (IRefactoringDocument document : document2textEdits.keySet()) {
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        Iterable<TextEdit> textEdits = document2textEdits.get(document);
        MultiTextEdit multiTextEdit = new MultiTextEdit();
        for (TextEdit textEdit : textEdits) {
            multiTextEdit.addChild(textEdit);
        }
        Change change = document.createChange(name, multiTextEdit);
        compositeChange.add(change);
    }
    for (IRefactoringDocument document : document2change.keySet()) {
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        Iterable<Change> documentChanges = document2change.get(document);
        CompositeChange documentCompositeChange = new CompositeChange(name);
        documentCompositeChange.addAll(Iterables.toArray(documentChanges, Change.class));
        compositeChange.add(documentCompositeChange);
    }
    return compositeChange;
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) Change(org.eclipse.ltk.core.refactoring.Change) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 32 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project xtext-eclipse by eclipse.

the class CombinedJvmJdtRenameProcessor method createChange.

@Override
public Change createChange(IProgressMonitor pm) throws CoreException, OperationCanceledException {
    SubMonitor monitor = SubMonitor.convert(pm, jvmElements2jdtProcessors.size() + 1);
    CompositeChange compositeChange = new CompositeChange(getProcessorName());
    compositeChange.add(super.createChange(monitor.newChild(1)));
    for (JavaRenameProcessor processor : jvmElements2jdtProcessors.values()) {
        if (monitor.isCanceled()) {
            throw new OperationCanceledException();
        }
        compositeChange.add(processor.createChange(monitor.newChild(1)));
    }
    return compositeChange;
}
Also used : OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) SubMonitor(org.eclipse.core.runtime.SubMonitor) JavaRenameProcessor(org.eclipse.jdt.internal.corext.refactoring.rename.JavaRenameProcessor) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange)

Example 33 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project xtext-eclipse by eclipse.

the class TextChangeCombinerTest method testMultipleDocumentChanges.

@Test
public void testMultipleDocumentChanges() throws Exception {
    ITextEditor editor = openInEditor(file0);
    CompositeChange compositeChange = new CompositeChange("test");
    compositeChange.add(createEditorDocumentChange(editor, 1, 1, "foo"));
    compositeChange.add(createEditorDocumentChange(editor, 2, 1, "bar"));
    CompositeChange compositeChange1 = new CompositeChange("test");
    compositeChange.add(compositeChange1);
    compositeChange1.add(createEditorDocumentChange(editor, 3, 1, "baz"));
    compositeChange1.add(createEditorDocumentChange(editor, 2, 1, "bar"));
    compositeChange1.add(createMultiEditorDocumentChange(editor, 1, 1, "foo", 4, 1, "foo"));
    Change combined = combiner.combineChanges(compositeChange);
    assertTrue(combined instanceof CompositeChange);
    assertEquals(1, ((CompositeChange) combined).getChildren().length);
    assertTrue(((CompositeChange) combined).getChildren()[0] instanceof EditorDocumentChange);
    Change undo = combined.perform(new NullProgressMonitor());
    IDocument document = getDocument(editor);
    assertEquals(MODEL.replace("1234", "foobarbazfoo"), document.get());
    undo.perform(new NullProgressMonitor());
    assertEquals(MODEL, document.get());
}
Also used : NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) ITextEditor(org.eclipse.ui.texteditor.ITextEditor) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) TextEditBasedChange(org.eclipse.ltk.core.refactoring.TextEditBasedChange) Change(org.eclipse.ltk.core.refactoring.Change) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) IDocument(org.eclipse.jface.text.IDocument) Test(org.junit.Test)

Example 34 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project xtext-eclipse by eclipse.

the class TextChangeCombiner method combineChanges.

public Change combineChanges(Change masterChange) {
    if (!(masterChange instanceof CompositeChange))
        return masterChange;
    Map<Object, TextChange> resource2textChange = newLinkedHashMap();
    List<Change> otherChanges = newArrayList();
    Set<IEditorPart> editorsToSave = newHashSet();
    visitCompositeChange((CompositeChange) masterChange, resource2textChange, otherChanges, editorsToSave);
    CompositeChange compositeChange = new FilteringCompositeChange(masterChange.getName());
    for (TextChange combinedTextChange : resource2textChange.values()) {
        if (((MultiTextEdit) combinedTextChange.getEdit()).getChildrenSize() > 0) {
            if (combinedTextChange instanceof EditorDocumentChange) {
                ((EditorDocumentChange) combinedTextChange).setDoSave(editorsToSave.contains(((EditorDocumentChange) combinedTextChange).getEditor()));
                compositeChange.add(combinedTextChange);
            } else
                compositeChange.add(DisplayChangeWrapper.wrap(combinedTextChange));
        }
    }
    for (Change otherChange : otherChanges) compositeChange.add(DisplayChangeWrapper.wrap(otherChange));
    if (compositeChange.getChildren().length == 0)
        return null;
    return compositeChange;
}
Also used : TextChange(org.eclipse.ltk.core.refactoring.TextChange) CompilationUnitChange(org.eclipse.jdt.core.refactoring.CompilationUnitChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) TextChange(org.eclipse.ltk.core.refactoring.TextChange) Change(org.eclipse.ltk.core.refactoring.Change) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) IEditorPart(org.eclipse.ui.IEditorPart) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)

Example 35 with CompositeChange

use of org.eclipse.ltk.core.refactoring.CompositeChange in project xtext-eclipse by eclipse.

the class TextChangeCombiner method visitChange.

protected void visitChange(Change sourceChange, Map<Object, TextChange> resource2textChange, List<Change> otherChanges, Set<IEditorPart> editorsToSave) {
    if (sourceChange instanceof DisplayChangeWrapper.Wrapper)
        visitChange(((DisplayChangeWrapper.Wrapper) sourceChange).getDelegate(), resource2textChange, otherChanges, editorsToSave);
    else if (sourceChange instanceof CompositeChange) {
        visitCompositeChange((CompositeChange) sourceChange, resource2textChange, otherChanges, editorsToSave);
    } else if (sourceChange instanceof TextChange) {
        if (sourceChange instanceof EditorDocumentChange)
            editorsToSave.add(((EditorDocumentChange) sourceChange).getEditor());
        Object key = getKey((TextChange) sourceChange);
        if (key != null) {
            TextChange textChange = resource2textChange.get(key);
            if (textChange == null) {
                textChange = createTextChange(key, ((TextChange) sourceChange).getTextType());
                resource2textChange.put(key, textChange);
            }
            MultiTextEdit combinedEdits = (MultiTextEdit) textChange.getEdit();
            TextEdit newEdit = ((TextChange) sourceChange).getEdit().copy();
            if (newEdit instanceof MultiTextEdit) {
                for (TextEdit newTextEdit : ((MultiTextEdit) newEdit).getChildren()) {
                    addIfNotDuplicate(combinedEdits, newTextEdit);
                }
            } else {
                addIfNotDuplicate(combinedEdits, newEdit);
            }
        }
    } else {
        CompositeChange parent = (CompositeChange) sourceChange.getParent();
        if (parent != null)
            parent.remove(sourceChange);
        otherChanges.add(sourceChange);
    }
}
Also used : DisplayChangeWrapper(org.eclipse.xtext.ui.refactoring.impl.DisplayChangeWrapper) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) TextChange(org.eclipse.ltk.core.refactoring.TextChange) CompositeChange(org.eclipse.ltk.core.refactoring.CompositeChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Aggregations

CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)49 Change (org.eclipse.ltk.core.refactoring.Change)22 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)14 IFile (org.eclipse.core.resources.IFile)10 IResource (org.eclipse.core.resources.IResource)9 TextChange (org.eclipse.ltk.core.refactoring.TextChange)9 IResourceVisitor (org.eclipse.core.resources.IResourceVisitor)7 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)6 EditorDocumentChange (org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)6 CoreException (org.eclipse.core.runtime.CoreException)5 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)5 Test (org.junit.Test)5 ArrayList (java.util.ArrayList)4 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)4 RefactoringStatus (org.eclipse.ltk.core.refactoring.RefactoringStatus)4 TextEditBasedChange (org.eclipse.ltk.core.refactoring.TextEditBasedChange)4 IPackageFragment (org.eclipse.jdt.core.IPackageFragment)3 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)3 TextEdit (org.eclipse.text.edits.TextEdit)3 IOException (java.io.IOException)2