use of org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext in project xtext-eclipse by eclipse.
the class XtextQuickAssistProcessor method computeQuickAssistProposals.
@Override
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
ISourceViewer sourceViewer = invocationContext.getSourceViewer();
if (sourceViewer == null)
return new ICompletionProposal[0];
if (invocationContext instanceof QuickAssistInvocationContext) {
if (((QuickAssistInvocationContext) invocationContext).isCancelled()) {
return new ICompletionProposal[0];
}
}
final IDocument document = sourceViewer.getDocument();
if (!(document instanceof IXtextDocument))
return new ICompletionProposal[0];
final IXtextDocument xtextDocument = (IXtextDocument) document;
final IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
List<ICompletionProposal> result = Lists.newArrayList();
try {
Set<Annotation> applicableAnnotations = getApplicableAnnotations(xtextDocument, annotationModel, invocationContext.getOffset());
result = createQuickfixes(invocationContext, applicableAnnotations);
selectAndRevealQuickfix(invocationContext, applicableAnnotations, result);
} catch (BadLocationException e) {
errorMessage = e.getMessage();
} catch (OperationCanceledException e) {
return new ICompletionProposal[0];
}
sortQuickfixes(result);
return result.toArray(new ICompletionProposal[result.size()]);
}
use of org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext in project egit by eclipse.
the class SpellcheckableMessageArea method addProposals.
private void addProposals(final SubMenuManager quickFixMenu) {
IAnnotationModel sourceModel = sourceViewer.getAnnotationModel();
if (sourceModel == null) {
return;
}
Iterator annotationIterator = sourceModel.getAnnotationIterator();
while (annotationIterator.hasNext()) {
Annotation annotation = (Annotation) annotationIterator.next();
boolean isDeleted = annotation.isMarkedDeleted();
boolean isIncluded = !isDeleted && includes(sourceModel.getPosition(annotation), getTextWidget().getCaretOffset());
boolean isFixable = isIncluded && sourceViewer.getQuickAssistAssistant().canFix(annotation);
if (isFixable) {
IQuickAssistProcessor processor = sourceViewer.getQuickAssistAssistant().getQuickAssistProcessor();
IQuickAssistInvocationContext context = sourceViewer.getQuickAssistInvocationContext();
ICompletionProposal[] proposals = processor.computeQuickAssistProposals(context);
for (ICompletionProposal proposal : proposals) {
quickFixMenu.add(createQuickFixAction(proposal));
}
}
}
}
use of org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext in project xtext-eclipse by eclipse.
the class XtextQuickAssistProcessor method selectAndRevealQuickfix.
/**
* @since 2.3
*/
protected void selectAndRevealQuickfix(IQuickAssistInvocationContext invocationContext, Set<Annotation> applicableAnnotations, List<ICompletionProposal> completionProposals) {
if (completionProposals.isEmpty()) {
return;
}
if (!(invocationContext instanceof QuickAssistInvocationContext && ((QuickAssistInvocationContext) invocationContext).isSuppressSelection())) {
ISourceViewer sourceViewer = invocationContext.getSourceViewer();
IAnnotationModel annotationModel = sourceViewer.getAnnotationModel();
Iterator<Annotation> iterator = applicableAnnotations.iterator();
while (iterator.hasNext()) {
Position pos = annotationModel.getPosition(iterator.next());
if (pos != null) {
sourceViewer.setSelectedRange(pos.getOffset(), pos.getLength());
sourceViewer.revealRange(pos.getOffset(), pos.getLength());
break;
}
}
}
}
Aggregations