use of org.eclipse.text.edits.ReplaceEdit in project che by eclipse.
the class ReplaceCorrectionProposal method addEdits.
/* (non-Javadoc)
* @see org.eclipse.jdt.internal.ui.text.correction.CUCorrectionProposal#addEdits(org.eclipse.jface.text.IDocument)
*/
@Override
protected void addEdits(IDocument doc, TextEdit rootEdit) throws CoreException {
super.addEdits(doc, rootEdit);
TextEdit edit = new ReplaceEdit(fOffset, fLength, fReplacementString);
rootEdit.addChild(edit);
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.platform.text by eclipse.
the class ReplaceRefactoring method createFileChange.
private TextChange createFileChange(IFile file, Pattern pattern, Set<FileMatch> matches, RefactoringStatus resultingStatus, Collection<MatchGroup> matchGroups) throws PatternSyntaxException, CoreException {
PositionTracker tracker = InternalSearchUI.getInstance().getPositionTracker();
TextFileChange change = new TextFileChange(Messages.format(SearchMessages.ReplaceRefactoring_group_label_change_for_file, file.getName()), file);
change.setEdit(new MultiTextEdit());
ITextFileBufferManager manager = FileBuffers.getTextFileBufferManager();
manager.connect(file.getFullPath(), LocationKind.IFILE, null);
try {
ITextFileBuffer textFileBuffer = manager.getTextFileBuffer(file.getFullPath(), LocationKind.IFILE);
if (textFileBuffer == null) {
resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_accessing_file_buffer, file.getName()));
return null;
}
IDocument document = textFileBuffer.getDocument();
String lineDelimiter = TextUtilities.getDefaultLineDelimiter(document);
for (FileMatch match : matches) {
int offset = match.getOffset();
int length = match.getLength();
Position currentPosition = tracker.getCurrentPosition(match);
if (currentPosition != null) {
offset = currentPosition.offset;
if (length != currentPosition.length) {
resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
continue;
}
}
String originalText = getOriginalText(document, offset, length);
if (originalText == null) {
resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
continue;
}
String replacementString = PatternConstructor.interpretReplaceEscapes(fReplaceString, originalText, lineDelimiter);
replacementString = computeReplacementString(pattern, document, offset, replacementString);
if (replacementString == null) {
resultingStatus.addError(Messages.format(SearchMessages.ReplaceRefactoring_error_match_content_changed, file.getName()));
continue;
}
ReplaceEdit replaceEdit = new ReplaceEdit(offset, length, replacementString);
change.addEdit(replaceEdit);
TextEditChangeGroup textEditChangeGroup = new TextEditChangeGroup(change, new TextEditGroup(SearchMessages.ReplaceRefactoring_group_label_match_replace, replaceEdit));
change.addTextEditChangeGroup(textEditChangeGroup);
matchGroups.add(new MatchGroup(textEditChangeGroup, match));
}
} finally {
manager.disconnect(file.getFullPath(), LocationKind.IFILE, null);
}
return change;
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testCopy1.
@Test
public void testCopy1() throws Exception {
MultiTextEdit root = new MultiTextEdit();
TextEdit e1 = new InsertEdit(2, "yy");
TextEdit e2 = new ReplaceEdit(2, 3, "3456");
root.addChild(e1);
root.addChild(e2);
List<TextEdit> org = flatten(root);
TextEditCopier copier = new TextEditCopier(root);
List<TextEdit> copy = flatten(copier.perform());
compare(org, copy);
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testSourceTranformationSplit2.
@Test
public void testSourceTranformationSplit2() throws Exception {
MoveSourceEdit ms = new MoveSourceEdit(2, 4);
MoveTargetEdit mt = new MoveTargetEdit(9, ms);
fRoot.addChild(ms);
fRoot.addChild(mt);
RangeMarker r1 = new RangeMarker(3, 2);
ms.addChild(r1);
ms.setSourceModifier(new ISourceModifier() {
@Override
public ISourceModifier copy() {
return this;
}
@Override
public ReplaceEdit[] getModifications(String source) {
return new ReplaceEdit[] { new ReplaceEdit(2, 2, "aa") };
}
});
fRoot.apply(fDocument);
String result = "0167823aa9";
Assert.assertEquals("Buffer content", result, fDocument.get());
assertEquals(r1, 6, 3);
}
use of org.eclipse.text.edits.ReplaceEdit in project eclipse.platform.text by eclipse.
the class TextEditTests method testInsert1.
@Test
public void testInsert1() throws Exception {
// [][ ]
TextEdit e1 = new InsertEdit(2, "yy");
TextEdit e2 = new ReplaceEdit(2, 3, "3456");
fRoot.addChild(e1);
fRoot.addChild(e2);
UndoEdit undo = fRoot.apply(fDocument);
assertEquals(fRoot, 2, 6);
assertEquals(e1, 2, 2);
assertEquals(e2, 4, 4);
Assert.assertEquals("Buffer content", "01yy345656789", fDocument.get());
doUndoRedo(undo, "01yy345656789");
}
Aggregations