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;
}
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();
}
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();
}
}
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();
}
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;
}
Aggregations