Search in sources :

Example 1 with SpellingContext

use of org.eclipse.ui.texteditor.spelling.SpellingContext 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)

Aggregations

HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ITextFileBuffer (org.eclipse.core.filebuffers.ITextFileBuffer)1 IProgressMonitor (org.eclipse.core.runtime.IProgressMonitor)1 IStatus (org.eclipse.core.runtime.IStatus)1 Job (org.eclipse.core.runtime.jobs.Job)1 Position (org.eclipse.jface.text.Position)1 Region (org.eclipse.jface.text.Region)1 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)1 IAnnotationModelExtension (org.eclipse.jface.text.source.IAnnotationModelExtension)1 ISpellingProblemCollector (org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector)1 SpellingAnnotation (org.eclipse.ui.texteditor.spelling.SpellingAnnotation)1 SpellingContext (org.eclipse.ui.texteditor.spelling.SpellingContext)1 SpellingProblem (org.eclipse.ui.texteditor.spelling.SpellingProblem)1