Search in sources :

Example 1 with IAnnotationModelExtension

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;
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IEditorInput(org.eclipse.ui.IEditorInput)

Example 2 with IAnnotationModelExtension

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);
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) Annotation(org.eclipse.jface.text.source.Annotation)

Example 3 with IAnnotationModelExtension

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);
        }
    }
}
Also used : IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) Annotation(org.eclipse.jface.text.source.Annotation)

Example 4 with IAnnotationModelExtension

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);
    }
}
Also used : Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation) IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IEditorInput(org.eclipse.ui.IEditorInput) IDocument(org.eclipse.jface.text.IDocument) BadLocationException(org.eclipse.jface.text.BadLocationException)

Example 5 with IAnnotationModelExtension

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()]);
    }
}
Also used : Entry(java.util.Map.Entry) Matcher(java.util.regex.Matcher) Position(org.eclipse.jface.text.Position) HashMap(java.util.HashMap) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Aggregations

IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)30 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)20 Annotation (org.eclipse.jface.text.source.Annotation)14 Position (org.eclipse.jface.text.Position)12 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)6 HashMap (java.util.HashMap)5 IDocument (org.eclipse.jface.text.IDocument)5 Entry (java.util.Map.Entry)4 BadLocationException (org.eclipse.jface.text.BadLocationException)4 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)4 IEditorInput (org.eclipse.ui.IEditorInput)4 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Matcher (java.util.regex.Matcher)2 IPreferenceStore (org.eclipse.jface.preference.IPreferenceStore)2 IRegion (org.eclipse.jface.text.IRegion)2 ITextViewer (org.eclipse.jface.text.ITextViewer)2 AnnotationModel (org.eclipse.jface.text.source.AnnotationModel)2 IChangeRulerColumn (org.eclipse.jface.text.source.IChangeRulerColumn)2 ILineDifferExtension (org.eclipse.jface.text.source.ILineDifferExtension)2