Search in sources :

Example 31 with TextEdit

use of org.eclipse.text.edits.TextEdit in project xtext-eclipse by eclipse.

the class TextChangeCombiner method addIfNotDuplicate.

protected void addIfNotDuplicate(MultiTextEdit multiTextEdit, final TextEdit editToBeAdded) {
    final boolean[] overlaps = new boolean[] { false };
    TextEditVisitor textEditVisitor = new TextEditVisitor() {

        @Override
        public boolean visitNode(TextEdit edit) {
            overlaps[0] |= !(edit instanceof MultiTextEdit) && edit.covers(editToBeAdded);
            return super.visitNode(edit);
        }
    };
    multiTextEdit.accept(textEditVisitor);
    if (!overlaps[0])
        multiTextEdit.addChild(editToBeAdded.copy());
}
Also used : TextEditVisitor(org.eclipse.text.edits.TextEditVisitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 32 with TextEdit

use of org.eclipse.text.edits.TextEdit in project xtext-eclipse by eclipse.

the class AbstractTextEditComposerTest method testObjectReplacement.

@Test
public void testObjectReplacement() throws Exception {
    Resource res = getResource(newTestGrammar());
    composer.beginRecording(res);
    Grammar grammar = (Grammar) res.getContents().get(0);
    ParserRule rule = (ParserRule) grammar.getRules().get(0);
    Keyword keyword = XtextFactory.eINSTANCE.createKeyword();
    keyword.setValue("baz");
    rule.setAlternatives(keyword);
    TextEdit edit = composer.endRecording();
    assertMatches("Foo: \"baz\";", edit);
}
Also used : ParserRule(org.eclipse.xtext.ParserRule) Keyword(org.eclipse.xtext.Keyword) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) Resource(org.eclipse.emf.ecore.resource.Resource) Grammar(org.eclipse.xtext.Grammar) Test(org.junit.Test)

Example 33 with TextEdit

use of org.eclipse.text.edits.TextEdit in project xtext-eclipse by eclipse.

the class AbstractTextEditComposerTest method testObjectRemoval.

@Test
public void testObjectRemoval() throws Exception {
    Resource res = getResource(newTestGrammar());
    composer.beginRecording(res);
    Grammar grammar = (Grammar) res.getContents().get(0);
    AbstractRule rule = grammar.getRules().get(0);
    Alternatives alternatives = (Alternatives) rule.getAlternatives();
    alternatives.getElements().remove(2);
    TextEdit edit = composer.endRecording();
    assertMatches("'foo' | 'bar'", edit);
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) Resource(org.eclipse.emf.ecore.resource.Resource) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) Alternatives(org.eclipse.xtext.Alternatives) Test(org.junit.Test)

Example 34 with TextEdit

use of org.eclipse.text.edits.TextEdit in project xtext-eclipse by eclipse.

the class AbstractTextEditComposerTest method testRemoveRootObject.

@Test
public void testRemoveRootObject() throws Exception {
    Resource res = getResource(newTestGrammar());
    composer.beginRecording(res);
    res.getContents().clear();
    TextEdit edit = composer.endRecording();
    assertEquals("", ((ReplaceEdit) edit).getText());
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) Resource(org.eclipse.emf.ecore.resource.Resource) Test(org.junit.Test)

Example 35 with TextEdit

use of org.eclipse.text.edits.TextEdit in project xtext-eclipse by eclipse.

the class AbstractTextEditComposerTest method testObjectModificationAndRemoval.

/* see https://bugs.eclipse.org/bugs/show_bug.cgi?id=292349 */
@Test
public void testObjectModificationAndRemoval() throws Exception {
    Resource res = getResource(newTestGrammar());
    composer.beginRecording(res);
    Grammar grammar = (Grammar) res.getContents().get(0);
    AbstractRule rule = grammar.getRules().get(0);
    Alternatives alternatives = (Alternatives) rule.getAlternatives();
    Keyword bazKeyword = (Keyword) alternatives.getElements().get(2);
    bazKeyword.setValue("BAZ");
    alternatives.getElements().remove(bazKeyword);
    TextEdit edit = composer.endRecording();
    assertMatches("'foo' | 'bar'", edit);
}
Also used : Keyword(org.eclipse.xtext.Keyword) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) Resource(org.eclipse.emf.ecore.resource.Resource) Grammar(org.eclipse.xtext.Grammar) AbstractRule(org.eclipse.xtext.AbstractRule) Alternatives(org.eclipse.xtext.Alternatives) Test(org.junit.Test)

Aggregations

TextEdit (org.eclipse.text.edits.TextEdit)190 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)113 Test (org.junit.Test)49 IDocument (org.eclipse.jface.text.IDocument)48 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)48 BadLocationException (org.eclipse.jface.text.BadLocationException)34 Document (org.eclipse.jface.text.Document)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 UndoEdit (org.eclipse.text.edits.UndoEdit)26 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)23 InsertEdit (org.eclipse.text.edits.InsertEdit)22 CoreException (org.eclipse.core.runtime.CoreException)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)19 DeleteEdit (org.eclipse.text.edits.DeleteEdit)17 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)16 ArrayList (java.util.ArrayList)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)15 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)15 TextEditGroup (org.eclipse.text.edits.TextEditGroup)15