Search in sources :

Example 6 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange 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)

Example 7 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange 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 8 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange 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 9 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange in project xtext-eclipse by eclipse.

the class TextChangeCombinerTest method createEditorDocumentChange.

protected Change createEditorDocumentChange(ITextEditor editor, int offset, int length, String replacement) {
    ReplaceEdit edit = new ReplaceEdit(offset, length, replacement);
    EditorDocumentChange documentChange = new EditorDocumentChange("document change", editor, false);
    documentChange.setEdit(edit);
    documentChange.setTextType(TEXT_TYPE);
    return documentChange;
}
Also used : ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)

Example 10 with EditorDocumentChange

use of org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange in project xtext-eclipse by eclipse.

the class RefactoringDocumentProviderTest method testCleanEditorDocument.

@Test
public void testCleanEditorDocument() throws Exception {
    XtextEditor editor = openEditor(testFile);
    assertFalse(editor.isDirty());
    IRefactoringDocument cleanDocument = createAndCheckDocument(testFile);
    assertTrue(cleanDocument instanceof EditorDocument);
    IXtextDocument editorDocument = editor.getDocument();
    assertEquals(editorDocument, ((EditorDocument) cleanDocument).getDocument());
    assertEquals(TEST_FILE_CONTENT, cleanDocument.getOriginalContents());
    Change change = cleanDocument.createChange(CHANGE_NAME, textEdit);
    assertTrue(change instanceof EditorDocumentChange);
    assertEquals(TEST_FILE_NAME + " - " + TEST_PROJECT, change.getName());
    assertEquals(editor, ((EditorDocumentChange) change).getEditor());
    assertTrue(((EditorDocumentChange) change).isDoSave());
    Change undoChange = checkEdit(cleanDocument, textEdit);
    assertNotNull(undoChange);
    IRefactoringDocument dirtyDocument = createAndCheckDocument(testFile);
    assertTrue(cleanDocument instanceof EditorDocument);
    assertEquals(editorDocument, ((EditorDocument) dirtyDocument).getDocument());
}
Also used : EditorDocument(org.eclipse.xtext.ui.refactoring.impl.DefaultRefactoringDocumentProvider.EditorDocument) XtextEditor(org.eclipse.xtext.ui.editor.XtextEditor) IRefactoringDocument(org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument) Change(org.eclipse.ltk.core.refactoring.Change) TextFileChange(org.eclipse.ltk.core.refactoring.TextFileChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) EditorDocumentChange(org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument) AbstractEditorTest(org.eclipse.xtext.ui.testing.AbstractEditorTest) Test(org.junit.Test)

Aggregations

EditorDocumentChange (org.eclipse.xtext.ui.refactoring.impl.EditorDocumentChange)12 Change (org.eclipse.ltk.core.refactoring.Change)6 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)6 CompositeChange (org.eclipse.ltk.core.refactoring.CompositeChange)4 TextChange (org.eclipse.ltk.core.refactoring.TextChange)4 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)4 TextEdit (org.eclipse.text.edits.TextEdit)4 IFile (org.eclipse.core.resources.IFile)3 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)3 IRefactoringDocument (org.eclipse.xtext.ui.refactoring.impl.IRefactoringDocument)3 Test (org.junit.Test)3 CompilationUnitChange (org.eclipse.jdt.core.refactoring.CompilationUnitChange)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2 IEditorPart (org.eclipse.ui.IEditorPart)2 XtextEditor (org.eclipse.xtext.ui.editor.XtextEditor)2 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)2 EditorDocument (org.eclipse.xtext.ui.refactoring.impl.DefaultRefactoringDocumentProvider.EditorDocument)2 AbstractEditorTest (org.eclipse.xtext.ui.testing.AbstractEditorTest)2 Predicate (com.google.common.base.Predicate)1 Inject (com.google.inject.Inject)1