Search in sources :

Example 1 with IQuickAssistInvocationContext

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()]);
}
Also used : ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) OperationCanceledException(org.eclipse.core.runtime.OperationCanceledException) IQuickAssistInvocationContext(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) IDocument(org.eclipse.jface.text.IDocument) Annotation(org.eclipse.jface.text.source.Annotation) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) XtextAnnotation(org.eclipse.xtext.ui.editor.validation.XtextAnnotation) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation) BadLocationException(org.eclipse.jface.text.BadLocationException) IXtextDocument(org.eclipse.xtext.ui.editor.model.IXtextDocument)

Example 2 with IQuickAssistInvocationContext

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));
            }
        }
    }
}
Also used : IQuickAssistProcessor(org.eclipse.jface.text.quickassist.IQuickAssistProcessor) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Iterator(java.util.Iterator) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IQuickAssistInvocationContext(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext) Annotation(org.eclipse.jface.text.source.Annotation)

Example 3 with IQuickAssistInvocationContext

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;
            }
        }
    }
}
Also used : Position(org.eclipse.jface.text.Position) IQuickAssistInvocationContext(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) Annotation(org.eclipse.jface.text.source.Annotation) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) XtextAnnotation(org.eclipse.xtext.ui.editor.validation.XtextAnnotation) SpellingAnnotation(org.eclipse.ui.texteditor.spelling.SpellingAnnotation)

Aggregations

IQuickAssistInvocationContext (org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext)3 Annotation (org.eclipse.jface.text.source.Annotation)3 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)3 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)2 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)2 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)2 SpellingAnnotation (org.eclipse.ui.texteditor.spelling.SpellingAnnotation)2 XtextAnnotation (org.eclipse.xtext.ui.editor.validation.XtextAnnotation)2 Iterator (java.util.Iterator)1 OperationCanceledException (org.eclipse.core.runtime.OperationCanceledException)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 IDocument (org.eclipse.jface.text.IDocument)1 Position (org.eclipse.jface.text.Position)1 IQuickAssistProcessor (org.eclipse.jface.text.quickassist.IQuickAssistProcessor)1 IXtextDocument (org.eclipse.xtext.ui.editor.model.IXtextDocument)1