下面列出了怎么用org.eclipse.ui.texteditor.spelling.SpellingAnnotation的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean canFix(Annotation annotation) {
if (annotation.isMarkedDeleted())
return false;
// non-persisted annotation
if (annotation instanceof XtextAnnotation) {
XtextAnnotation a = (XtextAnnotation) annotation;
return getResolutionProvider().hasResolutionFor(a.getIssueCode());
}
// persisted markerAnnotation
if (annotation instanceof MarkerAnnotation) {
MarkerAnnotation markerAnnotation = (MarkerAnnotation) annotation;
if (!markerAnnotation.isQuickFixableStateSet())
markerAnnotation.setQuickFixable(getResolutionProvider().hasResolutionFor(
issueUtil.getCode(markerAnnotation)));
return markerAnnotation.isQuickFixable();
}
if (annotation instanceof SpellingAnnotation) {
return true;
}
return false;
}
private HoverInfoWithSpellingAnnotation getSpellingHover(ITextViewer textViewer, IRegion hoverRegion) {
IAnnotationModel model= null;
if (textViewer instanceof ISourceViewerExtension2) {
model = ((ISourceViewerExtension2)textViewer).getVisualAnnotationModel();
} else if (textViewer instanceof SourceViewer) {
model= ((SourceViewer)textViewer).getAnnotationModel();
}
if (model != null) {
@SuppressWarnings("rawtypes")
Iterator e= model.getAnnotationIterator();
while (e.hasNext()) {
Annotation a= (Annotation) e.next();
if (a instanceof SpellingAnnotation) {
Position p= model.getPosition(a);
if (p != null && p.overlapsWith(hoverRegion.getOffset(), hoverRegion.getLength())) {
return new HoverInfoWithSpellingAnnotation((SpellingAnnotation)a, textViewer, p.getOffset());
}
}
}
}
return null;
}
/**
* @since 2.3
*/
protected List<ICompletionProposal> createQuickfixes(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations) {
List<ICompletionProposal> result = Lists.newArrayList();
ISourceViewer sourceViewer = invocationContext.getSourceViewer();
IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
IXtextDocument xtextDocument = xtextDocumentUtil.getXtextDocument(sourceViewer);
for(Annotation annotation : applicableAnnotations) {
if (annotation instanceof SpellingAnnotation) {
SpellingProblem spellingProblem = ((SpellingAnnotation) annotation).getSpellingProblem();
ICompletionProposal[] proposals = spellingProblem.getProposals();
if (proposals != null) {
result.addAll(asList(proposals));
}
} else {
final Issue issue = issueUtil.getIssueFromAnnotation(annotation);
Position pos = annotationModel.getPosition(annotation);
if (issue != null && pos != null) {
@SuppressWarnings("deprecation")
Iterable<IssueResolution> resolutions = getResolutions(issue, xtextDocument);
if (resolutions.iterator().hasNext()) {
for (IssueResolution resolution : resolutions) {
result.add(create(pos, resolution));
}
}
}
}
}
return result;
}
/**
* Check whether the given annotation type is supported, i.e. {@link #canFix(Annotation)} might return {@code true}.
* This could be made protected in a future release.
*/
private boolean isSupported(Annotation annotation) {
return !annotation.isMarkedDeleted()
&& (annotation instanceof XtextAnnotation || annotation instanceof MarkerAnnotation
|| annotation instanceof SpellingAnnotation);
}
public void accept(SpellingProblem problem) {
fAddAnnotations.put(new SpellingAnnotation(problem), new Position(problem.getOffset(), problem.getLength()));
}
public HoverInfoWithSpellingAnnotation(SpellingAnnotation spellingAnnotation, ITextViewer viewer, int offset) {
this.fSpellingAnnotation = spellingAnnotation;
this.fViewer = viewer;
this.fOffset = offset;
}
public void accept(SpellingProblem problem) {
fAddAnnotations.put(new SpellingAnnotation(problem), new Position(problem.getOffset(), problem.getLength()));
}
@Override
public void accept(SpellingProblem problem) {
fAddAnnotations
.put(new SpellingAnnotation(problem), new Position(problem.getOffset(), problem.getLength()));
}
@Override
public void endCollecting() {
List<Object> toRemove = new ArrayList<Object>();
Object fLockObject;
if (fAnnotationModel instanceof ISynchronizable) {
fLockObject = ((ISynchronizable) fAnnotationModel).getLockObject();
} else {
fLockObject = new Object();
}
//let other threads execute before getting the lock on the annotation model
Thread.yield();
Thread thread = Thread.currentThread();
int initiaThreadlPriority = thread.getPriority();
try {
//before getting the lock, let's execute with normal priority, to optimize the time that we'll
//retain that object locked (the annotation model is used on lots of places, so, retaining the lock
//on it on a minimum priority thread is not a good thing.
thread.setPriority(Thread.NORM_PRIORITY);
Iterator<Annotation> iter;
synchronized (fLockObject) {
iter = fAnnotationModel.getAnnotationIterator();
while (iter.hasNext()) {
Object n = iter.next();
if (n instanceof SpellingAnnotation) {
toRemove.add(n);
}
}
iter = null;
}
Annotation[] annotationsToRemove = toRemove.toArray(new Annotation[toRemove.size()]);
//let other threads execute before getting the lock (again) on the annotation model
Thread.yield();
synchronized (fLockObject) {
if (fAnnotationModel instanceof IAnnotationModelExtension) {
((IAnnotationModelExtension) fAnnotationModel).replaceAnnotations(annotationsToRemove,
fAddAnnotations);
} else {
for (int i = 0; i < annotationsToRemove.length; i++) {
fAnnotationModel.removeAnnotation(annotationsToRemove[i]);
}
for (iter = fAddAnnotations.keySet().iterator(); iter.hasNext();) {
Annotation annotation = iter.next();
fAnnotationModel.addAnnotation(annotation, fAddAnnotations.get(annotation));
}
}
}
} finally {
thread.setPriority(initiaThreadlPriority);
}
fAddAnnotations = null;
}