use of org.eclipse.text.edits.ReplaceEdit in project AutoRefactor by JnRouvignac.
the class ASTCommentRewriter method addReplacementEdits.
private void addReplacementEdits(List<TextEdit> commentEdits) {
if (this.replacements.isEmpty()) {
return;
}
for (Pair<Comment, String> pair : this.replacements) {
final Comment node = pair.getFirst();
final int start = node.getStartPosition();
final int length = node.getLength();
commentEdits.add(new ReplaceEdit(start, length, pair.getSecond()));
}
}
use of org.eclipse.text.edits.ReplaceEdit 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));
}
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.platform.text by eclipse.
the class TemplateContextType method resolve.
/**
* Resolves the variables in <code>buffer</code> within <code>context</code>
* and edits the template buffer to reflect the resolved variables.
*
* @param buffer the template buffer
* @param context the template context
* @throws MalformedTreeException if the positions in the buffer overlap
* @throws BadLocationException if the buffer cannot be successfully modified
*/
public void resolve(TemplateBuffer buffer, TemplateContext context) throws MalformedTreeException, BadLocationException {
Assert.isNotNull(context);
TemplateVariable[] variables = buffer.getVariables();
List<RangeMarker> positions = variablesToPositions(variables);
List<ReplaceEdit> edits = new ArrayList<>(5);
// iterate over all variables and try to resolve them
for (int i = 0; i != variables.length; i++) {
TemplateVariable variable = variables[i];
if (!variable.isResolved())
resolve(variable, context);
String value = variable.getDefaultValue();
int[] offsets = variable.getOffsets();
// update buffer to reflect new value
for (int k = 0; k != offsets.length; k++) edits.add(new ReplaceEdit(offsets[k], variable.getInitialLength(), value));
}
IDocument document = new Document(buffer.getString());
MultiTextEdit edit = new MultiTextEdit(0, document.getLength());
edit.addChildren(positions.toArray(new TextEdit[positions.size()]));
edit.addChildren(edits.toArray(new TextEdit[edits.size()]));
edit.apply(document, TextEdit.UPDATE_REGIONS);
positionsToVariables(positions, variables);
buffer.setContent(document.get(), variables);
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testInsertReplace1.
@Test
public void testInsertReplace1() throws Exception {
TextEdit e1 = new ReplaceEdit(2, 1, "y");
TextEdit e2 = new InsertEdit(2, "xx");
fRoot.addChild(e1);
fRoot.addChild(e2);
UndoEdit undo = fRoot.apply(fDocument);
assertEquals(fRoot, 2, 3);
assertEquals(e1, 4, 1);
assertEquals(e2, 2, 2);
Assert.assertEquals("Buffer content", "01xxy3456789", fDocument.get());
doUndoRedo(undo, "01xxy3456789");
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testSwap2InSwap1.
@Test
public void testSwap2InSwap1() throws Exception {
IDocument document = new Document("foo(1, 2), 3");
CopySourceEdit innerRoot = new CopySourceEdit(0, 9);
{
TextEdit e1 = new ReplaceEdit(4, 1, "");
CopySourceEdit s1 = new CopySourceEdit(4, 1);
e1.addChild(s1);
CopyTargetEdit t1 = new CopyTargetEdit(7, s1);
TextEdit e2 = new ReplaceEdit(7, 1, "");
CopySourceEdit s2 = new CopySourceEdit(7, 1);
e2.addChild(s2);
CopyTargetEdit t2 = new CopyTargetEdit(4, s2);
innerRoot.addChild(e1);
innerRoot.addChild(t2);
innerRoot.addChild(e2);
innerRoot.addChild(t1);
}
MultiTextEdit root = new MultiTextEdit();
{
TextEdit e1 = new ReplaceEdit(0, 9, "");
e1.addChild(innerRoot);
CopyTargetEdit t1 = new CopyTargetEdit(11, innerRoot);
TextEdit e2 = new ReplaceEdit(11, 1, "");
CopySourceEdit s2 = new CopySourceEdit(11, 1);
e2.addChild(s2);
CopyTargetEdit t2 = new CopyTargetEdit(0, s2);
root.addChild(e1);
root.addChild(t2);
root.addChild(e2);
root.addChild(t1);
}
root.apply(document);
String result = "3, foo(2, 1)";
Assert.assertEquals("Buffer content", result, document.get());
}
Aggregations