Search in sources :

Example 61 with TextEdit

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

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

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

the class SelectionProcessor method doReplace.

/**
 * Convenience method that applies the edit returned from {@link #replace(ISelection, String)}
 * to the underlying document and adapts the selection accordingly.
 *
 * @param selection the selection to replace
 * @param replacement the replacement text
 * @throws BadLocationException if accessing the document failed
 */
public void doReplace(ISelection selection, String replacement) throws BadLocationException {
    TextEdit edit = replace(selection, replacement);
    boolean complex = edit.hasChildren();
    if (complex && fRewriteTarget != null)
        fRewriteTarget.beginCompoundChange();
    try {
        edit.apply(fDocument, TextEdit.UPDATE_REGIONS);
        if (fSelectionProvider != null) {
            ISelection empty = makeReplaceSelection(selection, replacement);
            fSelectionProvider.setSelection(empty);
        }
    } finally {
        if (complex && fRewriteTarget != null)
            fRewriteTarget.endCompoundChange();
    }
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ISelection(org.eclipse.jface.viewers.ISelection)

Example 64 with TextEdit

use of org.eclipse.text.edits.TextEdit in project eclipse-pmd by acanda.

the class ASTQuickFixTestCase method rewriteAST.

private String rewriteAST(final org.eclipse.jface.text.Document document, final CompilationUnit ast) throws BadLocationException {
    final TextEdit edit = ast.rewrite(document, getRewriteOptions());
    edit.apply(document);
    return document.get();
}
Also used : TextEdit(org.eclipse.text.edits.TextEdit)

Example 65 with TextEdit

use of org.eclipse.text.edits.TextEdit in project erlide_eclipse by erlang.

the class ChangesetMaker method createEdits.

// @SuppressWarnings("unchecked")
// static public ArrayList<TextEdit> createEdits(File in, File out)
// throws IOException {
// inFile = in;
// outFile = out;
// 
// ArrayList<TextEdit> edits = new ArrayList<TextEdit>();
// inFileCharArray = null;
// outFileCharArray = null;
// 
// inFileCharArray = splitFile(inFile);
// outFileCharArray = splitFile(outFile);
// 
// algorithm = new Diff(inFileCharArray, outFileCharArray);
// 
// differencesList = algorithm.diff();
// for (Difference d : differencesList) {
// edits.add(createEditFromDiff(d));
// }
// 
// return edits;
// }
/**
 * Reads the input file, compares with the given new string, then creates
 * Eclipse's <code>TextEdit</code>-s.
 *
 * @param in
 *            original file
 * @param out
 *            modified file content
 * @return list of edit objects
 * @throws IOException
 *             if the file could not be read
 */
public static List<TextEdit> createEdits(final File in, final String out) throws IOException {
    ChangesetMaker.inFile = in;
    final List<TextEdit> edits = new ArrayList<>();
    ChangesetMaker.inFileCharArray = null;
    ChangesetMaker.outFileCharArray = null;
    ChangesetMaker.inFileCharArray = ChangesetMaker.readFile(ChangesetMaker.inFile);
    ChangesetMaker.outFileCharArray = new ArrayList<>();
    ChangesetMaker.outFileCharArray = ChangesetMaker.convertArrayToArrayList(out.toCharArray());
    ChangesetMaker.algorithm = new Diff<>(ChangesetMaker.inFileCharArray, ChangesetMaker.outFileCharArray);
    ChangesetMaker.differencesList = ChangesetMaker.algorithm.diff();
    for (final Difference d : ChangesetMaker.differencesList) {
        edits.add(ChangesetMaker.createEditFromDiff(d));
    }
    return edits;
}
Also used : MultiTextEdit(org.eclipse.text.edits.MultiTextEdit) TextEdit(org.eclipse.text.edits.TextEdit) ArrayList(java.util.ArrayList) Difference(org.incava.util.diff.Difference)

Aggregations

TextEdit (org.eclipse.text.edits.TextEdit)190 MultiTextEdit (org.eclipse.text.edits.MultiTextEdit)113 Test (org.junit.Test)49 IDocument (org.eclipse.jface.text.IDocument)48 ReplaceEdit (org.eclipse.text.edits.ReplaceEdit)48 BadLocationException (org.eclipse.jface.text.BadLocationException)34 Document (org.eclipse.jface.text.Document)33 ICompilationUnit (org.eclipse.jdt.core.ICompilationUnit)32 UndoEdit (org.eclipse.text.edits.UndoEdit)26 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)23 InsertEdit (org.eclipse.text.edits.InsertEdit)22 CoreException (org.eclipse.core.runtime.CoreException)20 CompilationUnit (org.eclipse.jdt.core.dom.CompilationUnit)19 MalformedTreeException (org.eclipse.text.edits.MalformedTreeException)19 DeleteEdit (org.eclipse.text.edits.DeleteEdit)17 MoveSourceEdit (org.eclipse.text.edits.MoveSourceEdit)16 ArrayList (java.util.ArrayList)15 ASTRewrite (org.eclipse.jdt.core.dom.rewrite.ASTRewrite)15 MoveTargetEdit (org.eclipse.text.edits.MoveTargetEdit)15 TextEditGroup (org.eclipse.text.edits.TextEditGroup)15