Search in sources :

Example 11 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.

the class ProjectionAnnotationHover method getProjectionTextAtLine.

private String getProjectionTextAtLine(ISourceViewer viewer, int line, int numberOfLines) {
    IAnnotationModel model = null;
    if (viewer instanceof ISourceViewerExtension2) {
        ISourceViewerExtension2 viewerExtension = (ISourceViewerExtension2) viewer;
        IAnnotationModel visual = viewerExtension.getVisualAnnotationModel();
        if (visual instanceof IAnnotationModelExtension) {
            IAnnotationModelExtension modelExtension = (IAnnotationModelExtension) visual;
            model = modelExtension.getAnnotationModel(ProjectionSupport.PROJECTION);
        }
    }
    if (model != null) {
        try {
            IDocument document = viewer.getDocument();
            Iterator<Annotation> e = model.getAnnotationIterator();
            while (e.hasNext()) {
                ProjectionAnnotation annotation = (ProjectionAnnotation) e.next();
                if (!annotation.isCollapsed())
                    continue;
                Position position = model.getPosition(annotation);
                if (position == null)
                    continue;
                if (isCaptionLine(position, document, line))
                    return getText(document, position.getOffset(), position.getLength(), numberOfLines);
            }
        } catch (BadLocationException x) {
        }
    }
    return null;
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IDocument(org.eclipse.jface.text.IDocument) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException) ISourceViewerExtension2(org.eclipse.jface.text.source.ISourceViewerExtension2)

Example 12 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project linuxtools by eclipse.

the class GcovAnnotationModel method clear.

public static void clear(ITextEditor editor) {
    IDocumentProvider provider = editor.getDocumentProvider();
    if (provider == null) {
        return;
    }
    IAnnotationModel model = provider.getAnnotationModel(editor.getEditorInput());
    if (!(model instanceof IAnnotationModelExtension)) {
        return;
    }
    IAnnotationModelExtension modelex = (IAnnotationModelExtension) model;
    IAnnotationModel coverageModel = modelex.getAnnotationModel(KEY);
    if (coverageModel instanceof GcovAnnotationModel) {
        ((GcovAnnotationModel) coverageModel).clear();
    }
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel)

Example 13 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project linuxtools by eclipse.

the class GcovAnnotationModel method attach.

/**
 * Attaches a coverage annotation model for the given editor if the editor
 * can be annotated. Does nothing if the model is already attached.
 *
 * @param editor Editor to which an annotation model should be attached
 */
public static void attach(ITextEditor editor) {
    IDocumentProvider provider = editor.getDocumentProvider();
    if (provider == null) {
        return;
    }
    IAnnotationModel model = provider.getAnnotationModel(editor.getEditorInput());
    if (!(model instanceof IAnnotationModelExtension)) {
        return;
    }
    IAnnotationModelExtension modelex = (IAnnotationModelExtension) model;
    IDocument document = provider.getDocument(editor.getEditorInput());
    GcovAnnotationModel coveragemodel = (GcovAnnotationModel) modelex.getAnnotationModel(KEY);
    if (coveragemodel == null) {
        coveragemodel = new GcovAnnotationModel(editor, document);
        modelex.addAnnotationModel(KEY, coveragemodel);
    } else {
        coveragemodel.updateAnnotations(false);
    }
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IDocument(org.eclipse.jface.text.IDocument)

Example 14 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project xtext-xtend by eclipse.

the class OverrideIndicatorModelListener method updateAnnotationModel.

private IStatus updateAnnotationModel(IProgressMonitor monitor) {
    if (xtextEditor == null || xtextEditor.getDocument() == null || xtextEditor.getInternalSourceViewer().getAnnotationModel() == null) {
        return Status.OK_STATUS;
    }
    IXtextDocument xtextDocument = xtextEditor.getDocument();
    IAnnotationModel annotationModel = xtextEditor.getInternalSourceViewer().getAnnotationModel();
    Map<Annotation, Position> annotationToPosition = xtextDocument.readOnly(new CancelableUnitOfWork<Map<Annotation, Position>, XtextResource>() {

        @Override
        public Map<Annotation, Position> exec(XtextResource xtextResource, CancelIndicator cancelIndicator) {
            if (xtextResource == null)
                return Collections.emptyMap();
            return createOverrideIndicatorAnnotationMap(xtextResource, cancelIndicator);
        }
    });
    if (monitor.isCanceled())
        return Status.CANCEL_STATUS;
    if (annotationModel instanceof IAnnotationModelExtension) {
        IAnnotationModelExtension annotationModelExtension = (IAnnotationModelExtension) annotationModel;
        Object lockObject = getLockObject(annotationModel);
        synchronized (lockObject) {
            annotationModelExtension.replaceAnnotations(overrideIndicatorAnnotations.toArray(new Annotation[overrideIndicatorAnnotations.size()]), annotationToPosition);
        }
        overrideIndicatorAnnotations = annotationToPosition.keySet();
    }
    return Status.OK_STATUS;
}
Also used : Position(org.eclipse.jface.text.Position) XtextResource(org.eclipse.xtext.resource.XtextResource) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) EObject(org.eclipse.emf.ecore.EObject) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) CancelIndicator(org.eclipse.xtext.util.CancelIndicator) Map(java.util.Map) Annotation(org.eclipse.jface.text.source.Annotation) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 15 with IAnnotationModelExtension

use of org.eclipse.jface.text.source.IAnnotationModelExtension in project eclipse.platform.text by eclipse.

the class SpellingProblem method removeAll.

/**
 * Removes all spelling problems that are reported
 * for the given <code>word</code> in the active editor.
 *
 * @param sourceViewer the source viewer
 * @param word the word for which to remove the problems or <code>null</code> to remove all
 * @since 3.4
 */
public static void removeAll(ISourceViewer sourceViewer, String word) {
    Assert.isNotNull(sourceViewer);
    IAnnotationModel model = sourceViewer.getAnnotationModel();
    if (model == null)
        return;
    IDocument document = sourceViewer.getDocument();
    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) IDocument(org.eclipse.jface.text.IDocument) Annotation(org.eclipse.jface.text.source.Annotation) BadLocationException(org.eclipse.jface.text.BadLocationException)

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