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 EditorAccessHighlighter method addAnnotations.
private void addAnnotations(IAnnotationModel model, Map<Annotation, Position> annotationToPositionMap) {
if (model instanceof IAnnotationModelExtension) {
IAnnotationModelExtension ame = (IAnnotationModelExtension) model;
ame.replaceAnnotations(new Annotation[0], annotationToPositionMap);
} else {
for (Annotation element : annotationToPositionMap.keySet()) {
Position p = annotationToPositionMap.get(element);
model.addAnnotation(element, p);
}
}
}
use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.
the class EditorAccessHighlighter method removeAnnotations.
/*
* Removes annotations from the given annotation model. The default implementation works for editors that
* implement <code>ITextEditor</code>.
* Subclasses may override this method.
* @param annotations A set containing the annotations to be removed.
* @see Annotation
*/
private void removeAnnotations(IAnnotationModel model, Set<Annotation> annotations) {
if (model instanceof IAnnotationModelExtension) {
IAnnotationModelExtension ame = (IAnnotationModelExtension) model;
Annotation[] annotationArray = new Annotation[annotations.size()];
ame.replaceAnnotations(annotations.toArray(annotationArray), Collections.emptyMap());
} else {
for (Annotation element : annotations) {
model.removeAnnotation(element);
}
}
}
use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.
the class SpellingProblem method removeAllInActiveEditor.
/**
* Removes all spelling problems that are reported
* for the given <code>word</code> in the active editor.
* <p>
* <em>This a workaround to fix bug https://bugs.eclipse.org/bugs/show_bug.cgi?id=134338
* for 3.2 at the time where spelling still resides in JDT Text.
* Once we move the spell check engine along with its quick fixes
* down to Platform Text we need to provide the proposals with
* a way to access the annotation model.</em>
* </p>
*
* @param editor the text editor, if <code>null</code> this method does nothing
* @param word the word for which to remove the problems or <code>null</code> to remove all
* @since 3.3
* @deprecated As of 3.4, replaced by {@link #removeAll(ISourceViewer, String)}
*/
@Deprecated
public static void removeAllInActiveEditor(ITextEditor editor, String word) {
if (editor == null)
return;
IDocumentProvider documentProvider = editor.getDocumentProvider();
if (documentProvider == null)
return;
IEditorInput editorInput = editor.getEditorInput();
if (editorInput == null)
return;
IAnnotationModel model = documentProvider.getAnnotationModel(editorInput);
if (model == null)
return;
IDocument document = documentProvider.getDocument(editorInput);
if (document == null)
return;
boolean supportsBatchReplace = (model instanceof IAnnotationModelExtension);
List<Annotation> toBeRemovedAnnotations = new ArrayList<>();
Iterator<Annotation> iter = model.getAnnotationIterator();
while (iter.hasNext()) {
Annotation annotation = iter.next();
if (SpellingAnnotation.TYPE.equals(annotation.getType())) {
boolean doRemove = word == null;
if (word == null)
doRemove = true;
else {
String annotationWord = null;
Position pos = model.getPosition(annotation);
try {
annotationWord = document.get(pos.getOffset(), pos.getLength());
} catch (BadLocationException e) {
continue;
}
doRemove = word.equals(annotationWord);
}
if (doRemove) {
if (supportsBatchReplace)
toBeRemovedAnnotations.add(annotation);
else
model.removeAnnotation(annotation);
}
}
}
if (supportsBatchReplace && !toBeRemovedAnnotations.isEmpty()) {
Annotation[] annotationArray = toBeRemovedAnnotations.toArray(new Annotation[toBeRemovedAnnotations.size()]);
((IAnnotationModelExtension) model).replaceAnnotations(annotationArray, null);
}
}
use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.
the class DefaultWordHighlightStrategy method applyHighlights.
private void applyHighlights(int offset) {
if (sourceViewer == null || !enabled) {
removeOccurrenceAnnotations();
return;
}
String text = document.get();
offset = ((ITextViewerExtension5) sourceViewer).widgetOffset2ModelOffset(offset);
String word = findCurrentWord(text, offset);
if (word == null) {
removeOccurrenceAnnotations();
return;
}
Matcher m = WORD_PATTERN.matcher(text);
Map<Annotation, Position> annotationMap = new HashMap<>();
while (m.find()) {
if (m.group().equals(word)) {
annotationMap.put(new Annotation(ANNOTATION_TYPE, false, NLS.bind(Messages.DefaultWordHighlightStrategy_OccurrencesOf, word)), new Position(m.start(), m.end() - m.start()));
}
}
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()]);
}
}
Aggregations