use of org.eclipse.text.edits.ReplaceEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testMoveUpWithInnerMark.
@Test
public void testMoveUpWithInnerMark() throws Exception {
MoveSourceEdit s1 = new MoveSourceEdit(7, 2);
MoveTargetEdit t1 = new MoveTargetEdit(2, s1);
TextEdit m = new ReplaceEdit(4, 1, "yy");
fRoot.addChild(t1);
fRoot.addChild(m);
fRoot.addChild(s1);
UndoEdit undo = fRoot.apply(fDocument);
Assert.assertEquals("Buffer content", "017823yy569", fDocument.get());
assertEquals(s1, 10, 0);
assertEquals(t1, 2, 2);
assertEquals(m, 6, 2);
doUndoRedo(undo, "017823yy569");
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testMoveTree3.
@Test
public void testMoveTree3() {
boolean exception = false;
try {
TextEdit root = new ReplaceEdit(0, 1, "");
root.moveTree(-1);
} catch (Exception e) {
exception = true;
}
assertTrue(exception);
}
use of org.eclipse.text.edits.ReplaceEdit 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.ReplaceEdit in project eclipse.jdt.ls by eclipse.
the class RenameNodeCorrectionProposal method addEdits.
@Override
protected void addEdits(IDocument doc, TextEdit root) throws CoreException {
super.addEdits(doc, root);
// build a full AST
CompilationUnit unit = CoreASTProvider.getInstance().getAST(getCompilationUnit(), CoreASTProvider.WAIT_YES, null);
ASTNode name = NodeFinder.perform(unit, fOffset, fLength);
if (name instanceof SimpleName) {
SimpleName[] names = LinkedNodeFinder.findByProblems(unit, (SimpleName) name);
if (names != null) {
for (int i = 0; i < names.length; i++) {
SimpleName curr = names[i];
root.addChild(new ReplaceEdit(curr.getStartPosition(), curr.getLength(), fNewName));
}
return;
}
}
root.addChild(new ReplaceEdit(fOffset, fLength, fNewName));
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.jdt.ls by eclipse.
the class ReplaceCorrectionProposal method addEdits.
@Override
protected void addEdits(IDocument doc, TextEdit rootEdit) throws CoreException {
super.addEdits(doc, rootEdit);
TextEdit edit = new ReplaceEdit(fOffset, fLength, fReplacementString);
rootEdit.addChild(edit);
}
Aggregations