Search in sources :

Example 26 with DeleteEdit

use of org.eclipse.text.edits.DeleteEdit in project flux by eclipse.

the class Utils method editsToJsonArray.

public static JSONArray editsToJsonArray(TextEdit edit) {
    final LinkedList<JSONObject> list = new LinkedList<JSONObject>();
    edit.accept(new TextEditVisitor() {

        @Override
        public boolean visit(DeleteEdit delete) {
            try {
                JSONObject json = new JSONObject();
                json.put("offset", delete.getOffset());
                json.put("length", delete.getLength());
                json.put("text", "");
                list.addFirst(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return super.visit(delete);
        }

        @Override
        public boolean visit(InsertEdit insert) {
            try {
                JSONObject json = new JSONObject();
                json.put("offset", insert.getOffset());
                json.put("length", 0);
                json.put("text", insert.getText());
                list.addFirst(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return super.visit(insert);
        }

        @Override
        public boolean visit(ReplaceEdit replace) {
            try {
                JSONObject json = new JSONObject();
                json.put("offset", replace.getOffset());
                json.put("length", replace.getLength());
                json.put("text", replace.getText());
                list.addFirst(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return super.visit(replace);
        }
    });
    return new JSONArray(list);
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) JSONObject(org.json.JSONObject) TextEditVisitor(org.eclipse.text.edits.TextEditVisitor) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) DeleteEdit(org.eclipse.text.edits.DeleteEdit) LinkedList(java.util.LinkedList)

Example 27 with DeleteEdit

use of org.eclipse.text.edits.DeleteEdit in project che by eclipse.

the class CorrectPackageDeclarationProposal method addEdits.

/* (non-Javadoc)
	 * @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jdt.internal.corext.textmanipulation
	 * .TextBuffer)
	 */
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
    super.addEdits(doc, root);
    ICompilationUnit cu = getCompilationUnit();
    IPackageFragment parentPack = (IPackageFragment) cu.getParent();
    IPackageDeclaration[] decls = cu.getPackageDeclarations();
    if (parentPack.isDefaultPackage() && decls.length > 0) {
        for (int i = 0; i < decls.length; i++) {
            ISourceRange range = decls[i].getSourceRange();
            root.addChild(new DeleteEdit(range.getOffset(), range.getLength()));
        }
        return;
    }
    if (!parentPack.isDefaultPackage() && decls.length == 0) {
        String lineDelim = StubUtility.getLineDelimiterUsed(cu);
        //$NON-NLS-1$
        String str = "package " + parentPack.getElementName() + ';' + lineDelim + lineDelim;
        root.addChild(new InsertEdit(0, str));
        return;
    }
    root.addChild(new ReplaceEdit(fLocation.getOffset(), fLocation.getLength(), parentPack.getElementName()));
}
Also used : ICompilationUnit(org.eclipse.jdt.core.ICompilationUnit) IPackageFragment(org.eclipse.jdt.core.IPackageFragment) InsertEdit(org.eclipse.text.edits.InsertEdit) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) IPackageDeclaration(org.eclipse.jdt.core.IPackageDeclaration) DeleteEdit(org.eclipse.text.edits.DeleteEdit) ISourceRange(org.eclipse.jdt.core.ISourceRange)

Example 28 with DeleteEdit

use of org.eclipse.text.edits.DeleteEdit 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 29 with DeleteEdit

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

the class TextEditTests method testCovers1.

@Test
public void testCovers1() throws Exception {
    InsertEdit insert = new InsertEdit(1, "");
    DeleteEdit delete = new DeleteEdit(2, 2);
    Assert.assertEquals(false, insert.covers(delete));
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit) Test(org.junit.Test)

Example 30 with DeleteEdit

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

the class TextEditTests method testDelete2.

@Test
public void testDelete2() throws Exception {
    TextEdit e1 = new DeleteEdit(4, 1);
    TextEdit e2 = new DeleteEdit(3, 1);
    TextEdit e3 = new DeleteEdit(5, 1);
    fRoot.addChild(e1);
    fRoot.addChild(e2);
    fRoot.addChild(e3);
    UndoEdit undo = fRoot.apply(fDocument);
    assertEquals(fRoot, 3, 0);
    assertEquals(e1, 3, 0);
    assertEquals(e2, 3, 0);
    assertEquals(e3, 3, 0);
    Assert.assertEquals("Buffer content", "0126789", fDocument.get());
    doUndoRedo(undo, "0126789");
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit) UndoEdit(org.eclipse.text.edits.UndoEdit) Test(org.junit.Test)

Aggregations

DeleteEdit (org.eclipse.text.edits.DeleteEdit)40 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)26 InsertEdit (org.eclipse.text.edits.InsertEdit)20 TextEdit (org.eclipse.text.edits.TextEdit)17 Test (org.junit.Test)15 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)11 UndoEdit (org.eclipse.text.edits.UndoEdit)10 IRegion (org.eclipse.jface.text.IRegion)6 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)6 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)6 ArrayList (java.util.ArrayList)5 IDocument (org.eclipse.jface.text.IDocument)5 HashSet (java.util.HashSet)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 RangeMarker (org.eclipse.text.edits.RangeMarker)4 Location (org.eclipse.titan.designer.AST.Location)4 Module (org.eclipse.titan.designer.AST.Module)4 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)3 Document (org.eclipse.jface.text.Document)3 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)3