Search in sources :

Example 91 with IAnnotationModel

use of org.eclipse.jface.text.source.IAnnotationModel 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);
}
Also used : IStatus(org.eclipse.core.runtime.IStatus) Position(org.eclipse.jface.text.Position) ISpellingProblemCollector(org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector) IAnnotationModelExtension(org.eclipse.jface.text.source.IAnnotationModelExtension) SpellingProblem(org.eclipse.ui.texteditor.spelling.SpellingProblem) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) SpellingContext(org.eclipse.ui.texteditor.spelling.SpellingContext) IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ITextFileBuffer(org.eclipse.core.filebuffers.ITextFileBuffer) Region(org.eclipse.jface.text.Region) Job(org.eclipse.core.runtime.jobs.Job) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Example 92 with IAnnotationModel

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

the class HighlightTest method clearAnnotations.

private void clearAnnotations() {
    editor.selectAndReveal(0, 0);
    IAnnotationModel annotationModel = getAnnotationModel();
    List<Annotation> annotations = getAnnotationsFromAnnotationModel();
    for (Annotation annotation : annotations) {
        annotationModel.removeAnnotation(annotation);
    }
}
Also used : IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Example 93 with IAnnotationModel

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

the class HighlightTest method getAnnotationModel.

private IAnnotationModel getAnnotationModel() {
    IDocumentProvider dp = editor.getDocumentProvider();
    IAnnotationModel am = dp.getAnnotationModel(editor.getEditorInput());
    return am;
}
Also used : IDocumentProvider(org.eclipse.ui.texteditor.IDocumentProvider) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel)

Example 94 with IAnnotationModel

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

the class MarkerAnnotationHover method getHoverRegion.

@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
    if (!(textViewer instanceof ISourceViewerExtension2)) {
        return null;
    }
    ISourceViewerExtension2 viewer = (ISourceViewerExtension2) textViewer;
    List<MarkerAnnotation> annotations = findMarkerAnnotations(viewer, new Region(offset, 0));
    if (annotations.isEmpty()) {
        return null;
    }
    // find intersection of regions
    int highestOffsetStart = 0;
    int lowestOffsetEnd = Integer.MAX_VALUE;
    IAnnotationModel annotationModel = viewer.getVisualAnnotationModel();
    for (Annotation annotation : annotations) {
        Position position = annotationModel.getPosition(annotation);
        highestOffsetStart = Math.max(highestOffsetStart, position.getOffset());
        lowestOffsetEnd = Math.min(lowestOffsetEnd, position.getOffset() + position.getLength());
    }
    return new Region(highestOffsetStart, Math.max(0, lowestOffsetEnd - highestOffsetStart));
}
Also used : MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) Position(org.eclipse.jface.text.Position) Region(org.eclipse.jface.text.Region) IRegion(org.eclipse.jface.text.IRegion) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) ISourceViewerExtension2(org.eclipse.jface.text.source.ISourceViewerExtension2)

Example 95 with IAnnotationModel

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

the class MarkerAnnotationHover method findMarkerAnnotations.

private static List<MarkerAnnotation> findMarkerAnnotations(ISourceViewerExtension2 viewer, IRegion region) {
    List<MarkerAnnotation> res = new ArrayList<>();
    IAnnotationModel annotationModel = viewer.getVisualAnnotationModel();
    annotationModel.getAnnotationIterator().forEachRemaining(annotation -> {
        if (isIncluded(annotation)) {
            Position position = annotationModel.getPosition(annotation);
            if (region.getOffset() >= position.getOffset() && region.getOffset() + region.getLength() <= position.getOffset() + position.getLength()) {
                res.add((MarkerAnnotation) annotation);
            }
        }
    });
    return res;
}
Also used : MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel)

Aggregations

IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)148 Annotation (org.eclipse.jface.text.source.Annotation)71 Position (org.eclipse.jface.text.Position)58 IDocument (org.eclipse.jface.text.IDocument)41 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)26 Iterator (java.util.Iterator)23 ArrayList (java.util.ArrayList)20 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)20 IDocumentProvider (org.eclipse.ui.texteditor.IDocumentProvider)19 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)19 BadLocationException (org.eclipse.jface.text.BadLocationException)18 IFile (org.eclipse.core.resources.IFile)17 ITextEditor (org.eclipse.ui.texteditor.ITextEditor)16 IEditorInput (org.eclipse.ui.IEditorInput)13 Test (org.junit.Test)13 HashMap (java.util.HashMap)12 List (java.util.List)12 CoreException (org.eclipse.core.runtime.CoreException)11 IMarker (org.eclipse.core.resources.IMarker)10 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)10