use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.
the class QuickDiffRestoreAction method getModel.
/**
* Returns the annotation model of the document displayed in this action's editor, if it
* implements the {@link IAnnotationModelExtension IAnnotationModelExtension} interface.
*
* @return the displayed document's annotation model if it is an <code>IAnnotationModelExtension</code>, or <code>null</code>
*/
private IAnnotationModelExtension getModel() {
if (getTextEditor() == null)
return null;
IDocumentProvider provider = getTextEditor().getDocumentProvider();
IEditorInput editorInput = getTextEditor().getEditorInput();
IAnnotationModel m = provider.getAnnotationModel(editorInput);
if (m instanceof IAnnotationModelExtension)
return (IAnnotationModelExtension) m;
return null;
}
use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.
the class ReferenceSelectionAction method getDiffer.
/**
* Fetches the differ installed with the current editor's document's annotation model. If none
* is installed yet, and <code>createIfNeeded</code> is true, one is created and attached to the
* model.
*
* @param createIfNeeded when set to <code>true</code>, a new differ will be created if needed.
* @return the differ installed with the annotation model, or <code>null</code>.
*/
private DocumentLineDiffer getDiffer(boolean createIfNeeded) {
// get annotation model
if (fEditor == null)
return null;
IDocumentProvider provider = fEditor.getDocumentProvider();
IEditorInput editorInput = fEditor.getEditorInput();
if (provider == null || editorInput == null)
return null;
IAnnotationModel m = provider.getAnnotationModel(editorInput);
IAnnotationModelExtension model = null;
if (m instanceof IAnnotationModelExtension) {
model = (IAnnotationModelExtension) m;
} else {
return null;
}
// get differ
DocumentLineDiffer differ = (DocumentLineDiffer) model.getAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID);
// create if needed
if (differ == null && createIfNeeded) {
differ = new DocumentLineDiffer();
model.addAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID, differ);
}
return differ;
}
use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.
the class HighlightStrategy method applyHighlights.
private void applyHighlights(int offset) {
if (sourceViewer == null || !enabled) {
return;
}
String text = document.get();
offset = ((ITextViewerExtension5) sourceViewer).widgetOffset2ModelOffset(offset);
int wordStartOffset = Math.max(text.lastIndexOf('/', offset), text.lastIndexOf('<', offset)) + 1;
int wordEndOffset = findEndingOffset(text, offset);
if (wordEndOffset <= wordStartOffset || wordEndOffset == -1 || wordStartOffset == -1)
return;
String word = text.substring(wordStartOffset, wordEndOffset);
if (word.indexOf('>') != -1 || word.indexOf('<') != -1) {
removeOccurrenceAnnotations();
return;
}
Matcher m = TAG_NAME_PATTERN.matcher(text);
Map<Annotation, Position> annotationMap = new HashMap<>();
while (m.find()) {
if (m.group(1).equals(word)) {
annotationMap.put(new Annotation(ANNOTATION_TYPE, false, null), new Position(m.start(1), m.end(1) - m.start(1)));
}
}
if (annotationMap.size() < 2) {
removeOccurrenceAnnotations();
return;
}
IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
synchronized (getLockObject(annotationModel)) {
if (annotationModel instanceof IAnnotationModelExtension) {
((IAnnotationModelExtension) annotationModel).replaceAnnotations(fOccurrenceAnnotations, annotationMap);
} else {
removeOccurrenceAnnotations();
Iterator<Entry<Annotation, Position>> iter = annotationMap.entrySet().iterator();
while (iter.hasNext()) {
Entry<Annotation, Position> mapEntry = iter.next();
annotationModel.addAnnotation(mapEntry.getKey(), mapEntry.getValue());
}
}
fOccurrenceAnnotations = annotationMap.keySet().toArray(new Annotation[annotationMap.keySet().size()]);
}
}
use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.
the class SpellCheckDocumentListener method documentChanged.
@Override
public void documentChanged(final DocumentEvent event) {
if (this.lastJob != null) {
this.lastJob.cancel();
}
this.lastJob = new Job("Spellcheck") {
@Override
protected IStatus run(IProgressMonitor monitor) {
ITextFileBuffer iTextFileBuffer = ITextFileBufferManager.DEFAULT.getTextFileBuffer(event.getDocument());
if (iTextFileBuffer == null) {
return Status.CANCEL_STATUS;
}
IAnnotationModel model = iTextFileBuffer.getAnnotationModel();
String text = event.getDocument().get();
int commentStart = text.indexOf("<comment>");
if (commentStart < 0) {
return Status.OK_STATUS;
}
commentStart += "<comment>".length();
int commentEnd = text.indexOf("</comment>", commentStart);
if (commentEnd <= commentStart) {
return Status.OK_STATUS;
}
Region region = new Region(commentStart, commentEnd - commentStart);
service.check(event.getDocument(), new Region[] { region }, new SpellingContext(), new ISpellingProblemCollector() {
private Map<SpellingAnnotation, Position> annotations = new HashMap<>();
@Override
public void endCollecting() {
Set<SpellingAnnotation> previous = new HashSet<>();
model.getAnnotationIterator().forEachRemaining(annotation -> {
if (annotation instanceof SpellingAnnotation) {
previous.add((SpellingAnnotation) annotation);
}
});
if (model instanceof IAnnotationModelExtension) {
((IAnnotationModelExtension) model).replaceAnnotations(previous.toArray(new SpellingAnnotation[previous.size()]), annotations);
} else {
previous.forEach(model::removeAnnotation);
annotations.forEach(model::addAnnotation);
}
}
@Override
public void beginCollecting() {
}
@Override
public void accept(SpellingProblem problem) {
this.annotations.put(new SpellingAnnotation(problem), new Position(problem.getOffset(), problem.getLength()));
}
}, monitor);
return Status.OK_STATUS;
}
};
this.lastJob.setUser(false);
this.lastJob.setPriority(Job.DECORATE);
// set a delay before reacting to user action to handle continuous typing
this.lastJob.schedule(500);
}
use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.
the class LineNumberColumn method ensureQuickDiffProvider.
/**
* Ensures that quick diff information is displayed and the quick diff provider is the one with
* the specified id. If a different quick diff provider is in use, the user may be asked whether
* he wants to switch.
*
* @param diffProviderId the quick diff provider id to use
* @return <code>true</code> if quick diff could be enabled for the given id,
* <code>false</code> otherwise
*/
private boolean ensureQuickDiffProvider(String diffProviderId) {
if (!isShowingChangeInformation())
// FIXME pass provider id
installChangeRulerModel(fDelegate);
IAnnotationModel annotationModel = fViewer.getAnnotationModel();
IAnnotationModel oldDiffer = getDiffer();
if (oldDiffer == null && annotationModel != null)
// quick diff is enabled, but no differ? not working for whatever reason
return false;
if (annotationModel == null)
annotationModel = new AnnotationModel();
if (!(annotationModel instanceof IAnnotationModelExtension))
return false;
QuickDiff util = new QuickDiff();
Object oldDifferId = util.getConfiguredQuickDiffProvider(oldDiffer);
if (oldDifferId.equals(diffProviderId)) {
if (oldDiffer instanceof ILineDifferExtension)
((ILineDifferExtension) oldDiffer).resume();
return true;
}
// Check whether the desired provider is available at all
IAnnotationModel newDiffer = util.createQuickDiffAnnotationModel(getEditor(), diffProviderId);
if (util.getConfiguredQuickDiffProvider(newDiffer).equals(oldDifferId)) {
if (oldDiffer instanceof ILineDifferExtension)
((ILineDifferExtension) oldDiffer).resume();
return true;
}
// quick diff is showing with the wrong provider - ask the user whether he wants to switch
IPreferenceStore store = EditorsUI.getPreferenceStore();
if (oldDiffer != null && !store.getString(REVISION_ASK_BEFORE_QUICKDIFF_SWITCH_KEY).equals(MessageDialogWithToggle.ALWAYS)) {
MessageDialogWithToggle toggleDialog = MessageDialogWithToggle.openOkCancelConfirm(fViewer.getTextWidget().getShell(), RulerMessages.AbstractDecoratedTextEditor_revision_quickdiff_switch_title, RulerMessages.AbstractDecoratedTextEditor_revision_quickdiff_switch_message, RulerMessages.AbstractDecoratedTextEditor_revision_quickdiff_switch_rememberquestion, true, store, REVISION_ASK_BEFORE_QUICKDIFF_SWITCH_KEY);
if (toggleDialog.getReturnCode() != Window.OK)
return false;
}
IAnnotationModelExtension modelExtension = (IAnnotationModelExtension) annotationModel;
modelExtension.removeAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID);
modelExtension.addAnnotationModel(IChangeRulerColumn.QUICK_DIFF_MODEL_ID, newDiffer);
if (fDelegate instanceof IChangeRulerColumn)
// picks up the new model attachment
((IChangeRulerColumn) fDelegate).setModel(annotationModel);
return true;
}
Aggregations