Search in sources :

Example 6 with CopyTargetEdit

use of org.eclipse.text.edits.CopyTargetEdit in project webtools.sourceediting by eclipse.

the class JSPTranslationUtil method translateTextEdit.

public TextEdit translateTextEdit(TextEdit textEdit) {
    TextEdit translatedTextEdit = null;
    int javaOffset = textEdit.getOffset();
    int jspOffset = getTranslation().getJspOffset(textEdit.getOffset());
    int length = textEdit.getLength();
    if (textEdit instanceof MultiTextEdit) {
        translatedTextEdit = new MultiTextEdit();
        TextEdit[] children = ((MultiTextEdit) textEdit).getChildren();
        for (int i = 0; i < children.length; i++) {
            TextEdit translatedChildTextEdit = translateTextEdit(children[i]);
            if (translatedChildTextEdit != null)
                ((MultiTextEdit) translatedTextEdit).addChild(translatedChildTextEdit);
        }
    } else if (textEdit instanceof ReplaceEdit) {
        if (jspOffset == -1)
            return null;
        if (!getTranslation().javaSpansMultipleJspPartitions(javaOffset, length))
            translatedTextEdit = new ReplaceEdit(jspOffset, length, ((ReplaceEdit) textEdit).getText());
    } else if (textEdit instanceof InsertEdit) {
        translatedTextEdit = new InsertEdit(jspOffset, ((InsertEdit) textEdit).getText());
    } else if (textEdit instanceof DeleteEdit) {
        translatedTextEdit = new DeleteEdit(jspOffset, length);
        TextEdit[] children = ((DeleteEdit) textEdit).getChildren();
        for (int i = 0; i < children.length; i++) {
            TextEdit translatedChildTextEdit = translateTextEdit(children[i]);
            if (translatedChildTextEdit != null)
                ((DeleteEdit) translatedTextEdit).addChild(translatedChildTextEdit);
        }
    } else if (textEdit instanceof CopySourceEdit) {
        translatedTextEdit = new CopySourceEdit(jspOffset, length);
        ((CopySourceEdit) translatedTextEdit).setTargetEdit(((CopySourceEdit) textEdit).getTargetEdit());
        ((CopySourceEdit) translatedTextEdit).setSourceModifier(((CopySourceEdit) textEdit).getSourceModifier());
    } else if (textEdit instanceof CopyTargetEdit) {
        translatedTextEdit = new CopyTargetEdit(jspOffset);
        ((CopyTargetEdit) textEdit).getSourceEdit().setTargetEdit((CopyTargetEdit) translatedTextEdit);
    } else if (textEdit instanceof MoveSourceEdit) {
        translatedTextEdit = new MoveSourceEdit(jspOffset, length);
        ((MoveSourceEdit) translatedTextEdit).setTargetEdit(((MoveSourceEdit) textEdit).getTargetEdit());
    } else if (textEdit instanceof MoveTargetEdit) {
        translatedTextEdit = new MoveTargetEdit(jspOffset);
        ((MoveTargetEdit) textEdit).getSourceEdit().setTargetEdit((MoveTargetEdit) translatedTextEdit);
    } else {
        // $NON-NLS-1$
        System.out.println("Need to translate " + textEdit);
    }
    return translatedTextEdit;
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) CopySourceEdit(org.eclipse.text.edits.CopySourceEdit) MoveTargetEdit(org.eclipse.text.edits.MoveTargetEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CopyTargetEdit(org.eclipse.text.edits.CopyTargetEdit) MoveSourceEdit(org.eclipse.text.edits.MoveSourceEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 7 with CopyTargetEdit

use of org.eclipse.text.edits.CopyTargetEdit in project eclipse.platform.text by eclipse.

the class TextEditTests method testNestedCopySourceWithInsert.

@Test
public void testNestedCopySourceWithInsert() throws Exception {
    CopySourceEdit s1 = new CopySourceEdit(1, 5);
    CopySourceEdit s2 = new CopySourceEdit(2, 3);
    CopySourceEdit s3 = new CopySourceEdit(3, 1);
    InsertEdit i1 = new InsertEdit(4, "x");
    s1.addChild(s2);
    s2.addChild(s3);
    s3.addChild(i1);
    CopyTargetEdit t1 = new CopyTargetEdit(9, s1);
    CopyTargetEdit t2 = new CopyTargetEdit(8, s2);
    CopyTargetEdit t3 = new CopyTargetEdit(7, s3);
    fRoot.addChild(s1);
    fRoot.addChild(t1);
    fRoot.addChild(t2);
    fRoot.addChild(t3);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(s1, 1, 6);
    assertEquals(s2, 2, 4);
    assertEquals(s3, 3, 2);
    assertEquals(i1, 4, 1);
    assertEquals(t1, 16, 6);
    assertEquals(t2, 11, 4);
    assertEquals(t3, 8, 2);
    String result = "0123x4563x723x48123x459";
    Assert.assertEquals("Buffer content", result, fDocument.get());
    doUndoRedo(undo, result);
}
Also used : CopySourceEdit(org.eclipse.text.edits.CopySourceEdit) InsertEdit(org.eclipse.text.edits.InsertEdit) CopyTargetEdit(org.eclipse.text.edits.CopyTargetEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Example 8 with CopyTargetEdit

use of org.eclipse.text.edits.CopyTargetEdit in project eclipse.platform.text by eclipse.

the class TextEditTests method testSwap2InSwap1.

@Test
public void testSwap2InSwap1() throws Exception {
    IDocument document = new Document("foo(1, 2), 3");
    CopySourceEdit innerRoot = new CopySourceEdit(0, 9);
    {
        TextEdit e1 = new ReplaceEdit(4, 1, "");
        CopySourceEdit s1 = new CopySourceEdit(4, 1);
        e1.addChild(s1);
        CopyTargetEdit t1 = new CopyTargetEdit(7, s1);
        TextEdit e2 = new ReplaceEdit(7, 1, "");
        CopySourceEdit s2 = new CopySourceEdit(7, 1);
        e2.addChild(s2);
        CopyTargetEdit t2 = new CopyTargetEdit(4, s2);
        innerRoot.addChild(e1);
        innerRoot.addChild(t2);
        innerRoot.addChild(e2);
        innerRoot.addChild(t1);
    }
    MultiTextEdit root = new MultiTextEdit();
    {
        TextEdit e1 = new ReplaceEdit(0, 9, "");
        e1.addChild(innerRoot);
        CopyTargetEdit t1 = new CopyTargetEdit(11, innerRoot);
        TextEdit e2 = new ReplaceEdit(11, 1, "");
        CopySourceEdit s2 = new CopySourceEdit(11, 1);
        e2.addChild(s2);
        CopyTargetEdit t2 = new CopyTargetEdit(0, s2);
        root.addChild(e1);
        root.addChild(t2);
        root.addChild(e2);
        root.addChild(t1);
    }
    root.apply(document);
    String result = "3, foo(2, 1)";
    Assert.assertEquals("Buffer content", result, document.get());
}
Also used : CopySourceEdit(org.eclipse.text.edits.CopySourceEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) CopyTargetEdit(org.eclipse.text.edits.CopyTargetEdit) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Test(org.junit.Test)

Example 9 with CopyTargetEdit

use of org.eclipse.text.edits.CopyTargetEdit in project eclipse.platform.text by eclipse.

the class TextEditTests method testNestedCopyTarget.

@Test
public void testNestedCopyTarget() throws Exception {
    CopySourceEdit s1 = new CopySourceEdit(1, 2);
    CopySourceEdit s2 = new CopySourceEdit(5, 3);
    CopyTargetEdit t1 = new CopyTargetEdit(6, s1);
    CopyTargetEdit t2 = new CopyTargetEdit(9, s2);
    s2.addChild(t1);
    fRoot.addChild(s1);
    fRoot.addChild(s2);
    fRoot.addChild(t2);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(s1, 1, 2);
    assertEquals(s2, 5, 5);
    assertEquals(t1, 6, 2);
    assertEquals(t2, 11, 5);
    String result = "01234512678512679";
    Assert.assertEquals("Buffer content", result, fDocument.get());
    doUndoRedo(undo, result);
}
Also used : CopySourceEdit(org.eclipse.text.edits.CopySourceEdit) CopyTargetEdit(org.eclipse.text.edits.CopyTargetEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Example 10 with CopyTargetEdit

use of org.eclipse.text.edits.CopyTargetEdit in project eclipse.platform.text by eclipse.

the class TextEditTests method testCopy2.

@Test
public void testCopy2() throws Exception {
    MultiTextEdit root = new MultiTextEdit();
    CopySourceEdit s1 = new CopySourceEdit(5, 2);
    CopyTargetEdit t1 = new CopyTargetEdit(8, s1);
    CopySourceEdit s2 = new CopySourceEdit(5, 2);
    CopyTargetEdit t2 = new CopyTargetEdit(2, s2);
    s1.addChild(s2);
    root.addChild(s1);
    root.addChild(t1);
    root.addChild(t2);
    List<TextEdit> org = flatten(root);
    TextEditCopier copier = new TextEditCopier(root);
    List<TextEdit> copy = flatten(copier.perform());
    compare(org, copy);
}
Also used : TextEditCopier(org.eclipse.text.edits.TextEditCopier) CopySourceEdit(org.eclipse.text.edits.CopySourceEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) CopyTargetEdit(org.eclipse.text.edits.CopyTargetEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) Test(org.junit.Test)

Aggregations

CopySourceEdit (org.eclipse.text.edits.CopySourceEdit)12 CopyTargetEdit (org.eclipse.text.edits.CopyTargetEdit)12 Test (org.junit.Test)10 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)6 TextEdit (org.eclipse.text.edits.TextEdit)6 UndoEdit (org.eclipse.text.edits.UndoEdit)6 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)4 Document (org.eclipse.jface.text.Document)3 IDocument (org.eclipse.jface.text.IDocument)3 InsertEdit (org.eclipse.text.edits.InsertEdit)2 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)2 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)2 DeleteEdit (org.eclipse.text.edits.DeleteEdit)1 TextEditCopier (org.eclipse.text.edits.TextEditCopier)1