Skip to main content
summaryrefslogtreecommitdiffstats
blob: 9ea533fef7dcc07ef1b2e3bc76f43a1790a3e7fc (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * - Mickael Istria (Red Hat Inc.)
 *******************************************************************************/
package org.eclipse.ui.genericeditor.examples.dotproject;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.eclipse.core.filebuffers.ITextFileBufferManager;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.jobs.Job;
import org.eclipse.jface.text.DocumentEvent;
import org.eclipse.jface.text.IDocumentListener;
import org.eclipse.jface.text.Position;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IAnnotationModelExtension;
import org.eclipse.ui.editors.text.EditorsUI;
import org.eclipse.ui.texteditor.spelling.ISpellingProblemCollector;
import org.eclipse.ui.texteditor.spelling.SpellingAnnotation;
import org.eclipse.ui.texteditor.spelling.SpellingContext;
import org.eclipse.ui.texteditor.spelling.SpellingProblem;
import org.eclipse.ui.texteditor.spelling.SpellingService;

public class SpellCheckDocumentListener implements IDocumentListener {
	
	Job lastJob = null;
	SpellingService service = EditorsUI.getSpellingService();

	@Override
	public void documentAboutToBeChanged(DocumentEvent event) {
	}

	@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) {
				IAnnotationModel model = ITextFileBufferManager.DEFAULT.getTextFileBuffer(event.getDocument()).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);
	}

}

Back to the top