Search in sources :

Example 26 with ReplaceEdit

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

the class TextEditTests method testMoveUpWithInnerMark.

@Test
public void testMoveUpWithInnerMark() throws Exception {
    MoveSourceEdit s1 = new MoveSourceEdit(7, 2);
    MoveTargetEdit t1 = new MoveTargetEdit(2, s1);
    TextEdit m = new ReplaceEdit(4, 1, "yy");
    fRoot.addChild(t1);
    fRoot.addChild(m);
    fRoot.addChild(s1);
    UndoEdit undo = fRoot.apply(fDocument);
    Assert.assertEquals("Buffer content", "017823yy569", fDocument.get());
    assertEquals(s1, 10, 0);
    assertEquals(t1, 2, 2);
    assertEquals(m, 6, 2);
    doUndoRedo(undo, "017823yy569");
}
Also used : MoveTargetEdit(org.eclipse.text.edits.MoveTargetEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MoveSourceEdit(org.eclipse.text.edits.MoveSourceEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Example 27 with ReplaceEdit

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

the class TextEditTests method testMoveTree3.

@Test
public void testMoveTree3() {
    boolean exception = false;
    try {
        TextEdit root = new ReplaceEdit(0, 1, "");
        root.moveTree(-1);
    } catch (Exception e) {
        exception = true;
    }
    assertTrue(exception);
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) Test(org.junit.Test)

Example 28 with ReplaceEdit

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

the class LinkedPositionGroup method handleEvent.

/**
 * Creates an edition of a document change that will forward any
 * modification in one position to all linked siblings. The return value is
 * a map from <code>IDocument</code> to <code>TextEdit</code>.
 *
 * @param event the document event to check
 * @return a map of edits, grouped by edited document, or <code>null</code>
 *         if there are no edits
 */
Map<IDocument, TextEdit> handleEvent(DocumentEvent event) {
    if (fLastPosition != null) {
        Map<IDocument, List<ReplaceEdit>> map = new HashMap<>();
        int relativeOffset = event.getOffset() - fLastRegion.getOffset();
        if (relativeOffset < 0) {
            relativeOffset = 0;
        }
        int eventEnd = event.getOffset() + event.getLength();
        int lastEnd = fLastRegion.getOffset() + fLastRegion.getLength();
        int length;
        if (eventEnd > lastEnd)
            length = lastEnd - relativeOffset - fLastRegion.getOffset();
        else
            length = eventEnd - relativeOffset - fLastRegion.getOffset();
        String text = event.getText();
        if (text == null)
            // $NON-NLS-1$
            text = "";
        for (LinkedPosition p : fPositions) {
            if (p == fLastPosition || p.isDeleted())
                // don't re-update the origin of the change
                continue;
            List<ReplaceEdit> edits = map.get(p.getDocument());
            if (edits == null) {
                edits = new ArrayList<>();
                map.put(p.getDocument(), edits);
            }
            if (fMustEnforceEqualContents) {
                try {
                    edits.add(new ReplaceEdit(p.getOffset(), p.getLength(), fLastPosition.getContent()));
                } catch (BadLocationException e) {
                    // should not happen
                    throw new RuntimeException(e);
                }
            } else {
                edits.add(new ReplaceEdit(p.getOffset() + relativeOffset, length, text));
            }
        }
        fMustEnforceEqualContents = false;
        try {
            Map<IDocument, TextEdit> result = new HashMap<>();
            for (IDocument d : map.keySet()) {
                TextEdit edit = new MultiTextEdit(0, d.getLength());
                edit.addChildren(map.get(d).toArray(new TextEdit[0]));
                result.put(d, edit);
            }
            return result;
        } catch (MalformedTreeException x) {
            // manager
            return null;
        }
    }
    return null;
}
Also used : HashMap(java.util.HashMap) MalformedTreeException(org.eclipse.text.edits.MalformedTreeException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) ArrayList(java.util.ArrayList) List(java.util.List) LinkedList(java.util.LinkedList) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 29 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project eclipse.jdt.ls by eclipse.

the class RenameNodeCorrectionProposal method addEdits.

@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
    super.addEdits(doc, root);
    // build a full AST
    CompilationUnit unit = CoreASTProvider.getInstance().getAST(getCompilationUnit(), CoreASTProvider.WAIT_YES, null);
    ASTNode name = NodeFinder.perform(unit, fOffset, fLength);
    if (name instanceof SimpleName) {
        SimpleName[] names = LinkedNodeFinder.findByProblems(unit, (SimpleName) name);
        if (names != null) {
            for (int i = 0; i < names.length; i++) {
                SimpleName curr = names[i];
                root.addChild(new ReplaceEdit(curr.getStartPosition(), curr.getLength(), fNewName));
            }
            return;
        }
    }
    root.addChild(new ReplaceEdit(fOffset, fLength, fNewName));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) CompilationUnit(org.eclipse.jdt.core.dom.CompilationUnit) SimpleName(org.eclipse.jdt.core.dom.SimpleName) ASTNode(org.eclipse.jdt.core.dom.ASTNode) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 30 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project eclipse.jdt.ls by eclipse.

the class ReplaceCorrectionProposal method addEdits.

@Override
protected void addEdits(IDocument doc, TextEdit rootEdit) throws CoreException {
    super.addEdits(doc, rootEdit);
    TextEdit edit = new ReplaceEdit(fOffset, fLength, fReplacementString);
    rootEdit.addChild(edit);
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Aggregations

ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)117 TextEdit (org.eclipse.text.edits.TextEdit)48 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)42 Test (org.junit.Test)34 InsertEdit (org.eclipse.text.edits.InsertEdit)24 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)14 IDocument (org.eclipse.jface.text.IDocument)13 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)13 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)13 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 BadLocationException (org.eclipse.jface.text.BadLocationException)12 DeleteEdit (org.eclipse.text.edits.DeleteEdit)12 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)10 ArrayList (java.util.ArrayList)9 List (java.util.List)7 Document (org.eclipse.jface.text.Document)7 CoreException (org.eclipse.core.runtime.CoreException)6 Position (org.eclipse.jface.text.Position)6 TextChange (org.eclipse.ltk.core.refactoring.TextChange)6