Search in sources :

Example 1 with TextEditVisitor

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

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

the class ASTCommentRewriter method overlaps.

private boolean overlaps(TextEdit edits, TextEdit edit2) {
    final SourceLocation range = toSourceLoc(edit2);
    final AtomicBoolean overlaps = new AtomicBoolean();
    edits.accept(new TextEditVisitor() {

        @Override
        public boolean visit(MultiTextEdit edit) {
            // move on there is nothing to check here
            return VISIT_SUBTREE;
        }

        @Override
        public boolean visitNode(TextEdit edit) {
            if (!overlaps.get()) {
                overlaps.set(range.overlapsWith(toSourceLoc(edit)));
            }
            return !overlaps.get();
        }
    });
    return overlaps.get();
}
Also used : SourceLocation(org.autorefactor.refactoring.SourceLocation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) 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 3 with TextEditVisitor

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

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

the class Utils method getOffsetAdjustment.

public static int getOffsetAdjustment(TextEdit edit, final int offset) {
    final int[] holder = new int[] { 0 };
    edit.accept(new TextEditVisitor() {

        @Override
        public boolean visit(DeleteEdit edit) {
            if (offset >= edit.getOffset()) {
                holder[0] -= edit.getLength();
            }
            return super.visit(edit);
        }

        @Override
        public boolean visit(InsertEdit edit) {
            if (offset >= edit.getOffset()) {
                holder[0] += edit.getText().length();
            }
            return super.visit(edit);
        }

        @Override
        public boolean visit(ReplaceEdit edit) {
            if (offset >= edit.getOffset()) {
                holder[0] += edit.getText().length() - edit.getLength();
            }
            return super.visit(edit);
        }
    });
    return holder[0];
}
Also used : InsertEdit(org.eclipse.text.edits.InsertEdit) TextEditVisitor(org.eclipse.text.edits.TextEditVisitor) ReplaceEdit(org.eclipse.text.edits.ReplaceEdit) DeleteEdit(org.eclipse.text.edits.DeleteEdit)

Example 5 with TextEditVisitor

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

the class ASTCommentRewriter method overlaps.

private boolean overlaps(final TextEdit edits, final TextEdit edit2) {
    final SourceLocation range = toSourceLoc(edit2);
    final AtomicBoolean overlaps = new AtomicBoolean();
    edits.accept(new TextEditVisitor() {

        @Override
        public boolean visit(final MultiTextEdit edit) {
            // Move on there is nothing to check here
            return true;
        }

        @Override
        public boolean visitNode(final TextEdit edit) {
            if (!overlaps.get()) {
                overlaps.lazySet(range.overlapsWith(toSourceLoc(edit)));
            }
            return !overlaps.get();
        }
    });
    return overlaps.get();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) TextEditVisitor(org.eclipse.text.edits.TextEditVisitor) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) MultiTextEdit(org.eclipse.text.edits.MultiTextEdit)

Aggregations

TextEditVisitor (org.eclipse.text.edits.TextEditVisitor)5 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)3 TextEdit (org.eclipse.text.edits.TextEdit)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 DeleteEdit (org.eclipse.text.edits.DeleteEdit)2 InsertEdit (org.eclipse.text.edits.InsertEdit)2 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)2 LinkedList (java.util.LinkedList)1 SourceLocation (org.autorefactor.refactoring.SourceLocation)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1