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);
}
}
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;
}
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());
}
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;
}
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());
}
Aggregations