Search in sources :

Example 76 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project AutoRefactor by JnRouvignac.

the class ASTCommentRewriter method addReplacementEdits.

private void addReplacementEdits(List<TextEdit> commentEdits) {
    if (this.replacements.isEmpty()) {
        return;
    }
    for (Pair<Comment, String> pair : this.replacements) {
        final Comment node = pair.getFirst();
        final int start = node.getStartPosition();
        final int length = node.getLength();
        commentEdits.add(new ReplaceEdit(start, length, pair.getSecond()));
    }
}
Also used : LineComment(org.eclipse.jdt.core.dom.LineComment) Comment(org.eclipse.jdt.core.dom.Comment) BlockComment(org.eclipse.jdt.core.dom.BlockComment) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit)

Example 77 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit in project AutoRefactor by JnRouvignac.

the class SourceRewriter method addEdits.

/**
 * Adds the edits contained in the current instance to the provided edits for the provided document.
 *
 * @param document the document to edit
 * @param edits where to add edits
 */
public void addEdits(IDocument document, TextEdit edits) {
    for (SourceLocation loc : this.removals) {
        edits.addChild(new DeleteEdit(loc.getStartPosition(), loc.getLength()));
    }
    for (Entry<SourceLocation, String> entry : this.replacements.entrySet()) {
        SourceLocation loc = entry.getKey();
        String replacement = entry.getValue();
        edits.addChild(new ReplaceEdit(loc.getStartPosition(), loc.getLength(), replacement));
    }
}
Also used : ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit)

Example 78 with ReplaceEdit

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

the class TemplateContextType method resolve.

/**
 * Resolves the variables in <code>buffer</code> within <code>context</code>
 * and edits the template buffer to reflect the resolved variables.
 *
 * @param buffer the template buffer
 * @param context the template context
 * @throws MalformedTreeException if the positions in the buffer overlap
 * @throws BadLocationException if the buffer cannot be successfully modified
 */
public void resolve(TemplateBuffer buffer, TemplateContext context) throws MalformedTreeException, BadLocationException {
    Assert.isNotNull(context);
    TemplateVariable[] variables = buffer.getVariables();
    List<RangeMarker> positions = variablesToPositions(variables);
    List<ReplaceEdit> edits = new ArrayList<>(5);
    // iterate over all variables and try to resolve them
    for (int i = 0; i != variables.length; i++) {
        TemplateVariable variable = variables[i];
        if (!variable.isResolved())
            resolve(variable, context);
        String value = variable.getDefaultValue();
        int[] offsets = variable.getOffsets();
        // update buffer to reflect new value
        for (int k = 0; k != offsets.length; k++) edits.add(new ReplaceEdit(offsets[k], variable.getInitialLength(), value));
    }
    IDocument document = new Document(buffer.getString());
    MultiTextEdit edit = new MultiTextEdit(0, document.getLength());
    edit.addChildren(positions.toArray(new TextEdit[positions.size()]));
    edit.addChildren(edits.toArray(new TextEdit[edits.size()]));
    edit.apply(document, TextEdit.UPDATE_REGIONS);
    positionsToVariables(positions, variables);
    buffer.setContent(document.get(), variables);
}
Also used : ArrayList(java.util.ArrayList) RangeMarker(org.eclipse.text.edits.RangeMarker) Document(org.eclipse.jface.text.Document) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IDocument(org.eclipse.jface.text.IDocument) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Example 79 with ReplaceEdit

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

the class TextEditTests method testInsertReplace1.

@Test
public void testInsertReplace1() throws Exception {
    TextEdit e1 = new ReplaceEdit(2, 1, "y");
    TextEdit e2 = new InsertEdit(2, "xx");
    fRoot.addChild(e1);
    fRoot.addChild(e2);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(fRoot, 2, 3);
    assertEquals(e1, 4, 1);
    assertEquals(e2, 2, 2);
    Assert.assertEquals("Buffer content", "01xxy3456789", fDocument.get());
    doUndoRedo(undo, "01xxy3456789");
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Example 80 with ReplaceEdit

use of org.eclipse.text.edits.ReplaceEdit 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)

Aggregations

ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)131 TextEdit (org.eclipse.text.edits.TextEdit)53 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)43 Test (org.junit.Test)34 InsertEdit (org.eclipse.text.edits.InsertEdit)25 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)22 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)15 BadLocationException (org.eclipse.jface.text.BadLocationException)14 IDocument (org.eclipse.jface.text.IDocument)14 DeleteEdit (org.eclipse.text.edits.DeleteEdit)13 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)13 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)13 UndoEdit (org.eclipse.text.edits.UndoEdit)13 TextFileChange (org.eclipse.ltk.core.refactoring.TextFileChange)11 SearchMatch (org.eclipse.jdt.core.search.SearchMatch)10 ArrayList (java.util.ArrayList)9 SearchResultGroup (org.eclipse.jdt.internal.corext.refactoring.SearchResultGroup)8 TextChange (org.eclipse.ltk.core.refactoring.TextChange)8 List (java.util.List)7 Document (org.eclipse.jface.text.Document)7