protected void replaceText(SPath path, int offset, String replacedText,
String text, User source) {
IFile file = path.getFile();
FileEditorInput input = new FileEditorInput(file);
IDocumentProvider provider = getDocumentProvider(input);
try {
provider.connect(input);
} catch (CoreException e) {
log.error(
"Could not connect document provider for file: "
+ file.toString(), e);
// TODO Trigger a consistency recovery
return;
}
try {
IDocument doc = provider.getDocument(input);
if (doc == null) {
log.error("Could not connect document provider for file: "
+ file.toString(), new StackTrace());
// TODO Trigger a consistency recovery
return;
}
// Check if the replaced text is really there.
if (log.isDebugEnabled()) {
String is;
try {
is = doc.get(offset, replacedText.length());
if (!is.equals(replacedText)) {
log.error("replaceText should be '"
+ StringEscapeUtils.escapeJava(replacedText)
+ "' is '" + StringEscapeUtils.escapeJava(is) + "'");
}
} catch (BadLocationException e) {
// Ignore, because this is going to fail again just below
}
}
// Try to replace
try {
doc.replace(offset, replacedText.length(), text);
} catch (BadLocationException e) {
log.error(String.format(
"Could not apply TextEdit at %d-%d of document "
+ "with length %d.\nWas supposed to replace"
+ " '%s' with '%s'.", offset,
offset + replacedText.length(), doc.getLength(),
replacedText, text));
return;
}
lastRemoteEditTimes.put(path, System.currentTimeMillis());
for (IEditorPart editorPart : editorPool.getEditors(path)) {
if (editorPart instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) editorPart;
IAnnotationModel model = textEditor.getDocumentProvider()
.getAnnotationModel(textEditor.getEditorInput());
contributionAnnotationManager.insertAnnotation(model,
offset, text.length(), source);
}
}
IAnnotationModel model = provider.getAnnotationModel(input);
contributionAnnotationManager.insertAnnotation(model, offset,
text.length(), source);
} finally {
provider.disconnect(input);
}
}