Search in sources :

Example 1 with IAnnotationModelExtension2

use of org.eclipse.jface.text.source.IAnnotationModelExtension2 in project xtext-eclipse by eclipse.

the class XtextQuickAssistProcessor method getApplicableAnnotations.

protected Set<Annotation> getApplicableAnnotations(final IXtextDocument document, final IAnnotationModel annotationModel, final int offset) throws BadLocationException {
    final int line = document.getLineOfOffset(offset);
    final String delim = document.getLineDelimiter(line);
    final int delimLength = delim != null ? delim.length() : 0;
    final int lineLength = document.getLineLength(line) - delimLength;
    final int lineOffset = document.getLineOffset(line);
    Iterator<?> iterator;
    if (annotationModel instanceof IAnnotationModelExtension2)
        iterator = ((IAnnotationModelExtension2) annotationModel).getAnnotationIterator(lineOffset, lineLength, true, true);
    else
        iterator = annotationModel.getAnnotationIterator();
    Map<Annotation, Position> possibleAnnotations = Maps.newLinkedHashMap();
    boolean hasIncludingAnnotation = false;
    int smallestAnnotationDistance = Integer.MAX_VALUE;
    int offsetOfFirstAnnotation = Integer.MAX_VALUE;
    while (iterator.hasNext()) {
        Object key = iterator.next();
        if (key instanceof Annotation) {
            Annotation annotation = (Annotation) key;
            if (isSupported(annotation)) {
                Position pos = annotationModel.getPosition(annotation);
                // Consider all annotations that are on the same line as the cursor
                if (pos != null && pos.overlapsWith(lineOffset, lineLength)) {
                    possibleAnnotations.put(annotation, pos);
                    if (pos.includes(offset)) {
                        hasIncludingAnnotation = true;
                    } else if (offset >= pos.getOffset()) {
                        int annotationDistance = offset - (pos.getOffset() + pos.getLength());
                        if (annotationDistance < smallestAnnotationDistance)
                            smallestAnnotationDistance = annotationDistance;
                    } else if (pos.getOffset() < offsetOfFirstAnnotation) {
                        offsetOfFirstAnnotation = pos.getOffset();
                    }
                }
            }
        }
    }
    if (possibleAnnotations.isEmpty()) {
        return Collections.emptySet();
    }
    // There is an annotation that includes the cursor, so accept all including annotations
    if (hasIncludingAnnotation) {
        Set<Annotation> includingAnnotations = Sets.newLinkedHashSetWithExpectedSize(possibleAnnotations.size());
        for (Map.Entry<Annotation, Position> entry : possibleAnnotations.entrySet()) {
            if (entry.getValue().includes(offset) && canFix(entry.getKey()))
                includingAnnotations.add(entry.getKey());
        }
        return includingAnnotations;
    }
    // There is an annotation that is left of the cursor, so accept the nearest annotations on the left
    if (smallestAnnotationDistance != Integer.MAX_VALUE) {
        Set<Annotation> nearestAnnotations = Sets.newLinkedHashSetWithExpectedSize(possibleAnnotations.size());
        for (Map.Entry<Annotation, Position> entry : possibleAnnotations.entrySet()) {
            Position pos = entry.getValue();
            if (offset >= pos.getOffset() && offset - (pos.getOffset() + pos.getLength()) == smallestAnnotationDistance && canFix(entry.getKey()))
                nearestAnnotations.add(entry.getKey());
        }
        return nearestAnnotations;
    }
    // Accept the nearest annotations on the right
    Set<Annotation> nearestAnnotations = Sets.newLinkedHashSetWithExpectedSize(possibleAnnotations.size());
    for (Map.Entry<Annotation, Position> entry : possibleAnnotations.entrySet()) {
        if (entry.getValue().getOffset() == offsetOfFirstAnnotation && canFix(entry.getKey()))
            nearestAnnotations.add(entry.getKey());
    }
    return nearestAnnotations;
}
Also used : IAnnotationModelExtension2(org.eclipse.jface.text.source.IAnnotationModelExtension2) Position(org.eclipse.jface.text.Position) Map(java.util.Map) 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)

Example 2 with IAnnotationModelExtension2

use of org.eclipse.jface.text.source.IAnnotationModelExtension2 in project webtools.sourceediting by eclipse.

the class SourceValidationQuickAssistProcessor method computeQuickAssistProposals.

public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext quickAssistContext) {
    ISourceViewer viewer = quickAssistContext.getSourceViewer();
    int documentOffset = quickAssistContext.getOffset();
    int length = viewer != null ? viewer.getSelectedRange().y : 0;
    IAnnotationModel model = viewer.getAnnotationModel();
    if (model == null)
        return null;
    List allProposals = new ArrayList();
    if (model instanceof IAnnotationModelExtension2) {
        Iterator iter = ((IAnnotationModelExtension2) model).getAnnotationIterator(documentOffset, length, true, true);
        while (iter.hasNext()) {
            List processors = new ArrayList();
            Annotation anno = (Annotation) iter.next();
            if (canFix(anno)) {
                // fix processor attached to it
                if (anno instanceof TemporaryAnnotation) {
                    Object o = ((TemporaryAnnotation) anno).getAdditionalFixInfo();
                    if (o instanceof IQuickAssistProcessor) {
                        processors.add(o);
                    }
                }
                // get all relevant quick fixes for this annotation
                QuickFixRegistry registry = QuickFixRegistry.getInstance();
                processors.addAll(Arrays.asList(registry.getQuickFixProcessors(anno)));
                // set up context
                Map attributes = null;
                if (anno instanceof TemporaryAnnotation) {
                    attributes = ((TemporaryAnnotation) anno).getAttributes();
                }
                Position pos = model.getPosition(anno);
                StructuredTextInvocationContext sseContext = new StructuredTextInvocationContext(viewer, pos.getOffset(), pos.getLength(), attributes);
                // call each processor
                for (int i = 0; i < processors.size(); ++i) {
                    List proposals = new ArrayList();
                    collectProposals((IQuickAssistProcessor) processors.get(i), anno, sseContext, proposals);
                    if (proposals.size() > 0) {
                        allProposals.addAll(proposals);
                    }
                }
            }
        }
    }
    if (allProposals.isEmpty())
        return null;
    return (ICompletionProposal[]) allProposals.toArray(new ICompletionProposal[allProposals.size()]);
}
Also used : Position(org.eclipse.jface.text.Position) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IQuickFixableAnnotation(org.eclipse.jface.text.quickassist.IQuickFixableAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IQuickAssistProcessor(org.eclipse.jface.text.quickassist.IQuickAssistProcessor) IAnnotationModelExtension2(org.eclipse.jface.text.source.IAnnotationModelExtension2) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) Map(java.util.Map) StructuredTextInvocationContext(org.eclipse.wst.sse.ui.StructuredTextInvocationContext)

Example 3 with IAnnotationModelExtension2

use of org.eclipse.jface.text.source.IAnnotationModelExtension2 in project webtools.sourceediting by eclipse.

the class HTMLAttributeValidationQuickFixProcessor method computeQuickAssistProposals.

/*
	 * @see org.eclipse.jface.text.quickassist.IQuickAssistProcessor#computeQuickAssistProposals(org.eclipse.jface.text.quickassist.IQuickAssistInvocationContext)
	 */
public ICompletionProposal[] computeQuickAssistProposals(IQuickAssistInvocationContext invocationContext) {
    ISourceViewer viewer = invocationContext.getSourceViewer();
    int documentOffset = invocationContext.getOffset();
    int length = viewer != null ? viewer.getSelectedRange().y : 0;
    IAnnotationModel model = viewer.getAnnotationModel();
    if (model == null)
        return null;
    List proposals = new ArrayList();
    if (model instanceof IAnnotationModelExtension2) {
        Iterator iter = ((IAnnotationModelExtension2) model).getAnnotationIterator(documentOffset, length, true, true);
        while (iter.hasNext()) {
            Annotation anno = (Annotation) iter.next();
            if (canFix(anno)) {
                int offset = -1;
                if (anno instanceof TemporaryAnnotation) {
                    offset = ((TemporaryAnnotation) anno).getPosition().getOffset();
                } else if (anno instanceof MarkerAnnotation) {
                    offset = ((MarkerAnnotation) anno).getMarker().getAttribute(IMarker.CHAR_START, -1);
                }
                if (offset == -1)
                    continue;
                IDOMNode node = (IDOMNode) ContentAssistUtils.getNodeAt(viewer, offset);
                if (!(node instanceof Element))
                    continue;
                Object adapter = (node instanceof IAdaptable ? ((IAdaptable) node).getAdapter(IResource.class) : null);
                IProject project = (adapter instanceof IResource ? ((IResource) adapter).getProject() : null);
                IScopeContext[] fLookupOrder = new IScopeContext[] { new InstanceScope(), new DefaultScope() };
                if (project != null) {
                    ProjectScope projectScope = new ProjectScope(project);
                    if (projectScope.getNode(getPreferenceNodeQualifier()).getBoolean(getProjectSettingsKey(), false))
                        fLookupOrder = new IScopeContext[] { projectScope, new InstanceScope(), new DefaultScope() };
                }
                boolean ignore = fPreferenceService.getBoolean(getPreferenceNodeQualifier(), HTMLCorePreferenceNames.IGNORE_ATTRIBUTE_NAMES, HTMLCorePreferenceNames.IGNORE_ATTRIBUTE_NAMES_DEFAULT, fLookupOrder);
                String ignoreList = fPreferenceService.getString(getPreferenceNodeQualifier(), HTMLCorePreferenceNames.ATTRIBUTE_NAMES_TO_IGNORE, HTMLCorePreferenceNames.ATTRIBUTE_NAMES_TO_IGNORE_DEFAULT, fLookupOrder);
                Set result = new HashSet();
                if (ignoreList.trim().length() > 0) {
                    // $NON-NLS-1$
                    String[] names = ignoreList.split(",");
                    for (int i = 0; names != null && i < names.length; i++) {
                        String name = names[i] == null ? null : names[i].trim();
                        if (name != null && name.length() > 0)
                            result.add(name.toLowerCase());
                    }
                }
                String name = getAttributeName(node, offset);
                if (name == null)
                    continue;
                // If ignore == false. then show a quick fix anyway (due to allow to turn 'ignore' option on)
                if (!ignore || shouldShowQuickFix(result, name.toLowerCase())) {
                    IgnoreAttributeNameCompletionProposal p = new IgnoreAttributeNameCompletionProposal(name.toLowerCase(), offset, NLS.bind(HTMLUIMessages.DoNotValidateAttribute, name), HTMLUIMessages.DoNotValidateAttributeAddInfo, node);
                    if (!proposals.contains(p))
                        proposals.add(p);
                }
                int dashIndex = name.indexOf('-');
                while (dashIndex != -1) {
                    StringBuffer namePattern = new StringBuffer(name.substring(0, dashIndex + 1)).append('*');
                    // a more common pattern is already created
                    if (ignore && result.contains(namePattern.toString().toLowerCase()))
                        break;
                    IgnoreAttributeNameCompletionProposal p = new IgnoreAttributeNameCompletionProposal(namePattern.toString().toLowerCase(), offset, NLS.bind(HTMLUIMessages.DoNotValidateAllAttributes, namePattern.toString()), HTMLUIMessages.DoNotValidateAllAttributesAddInfo, node);
                    if (!proposals.contains(p))
                        proposals.add(p);
                    dashIndex = name.indexOf('-', dashIndex + 1);
                }
            }
        }
    }
    if (proposals.isEmpty())
        return null;
    return (ICompletionProposal[]) proposals.toArray(new ICompletionProposal[proposals.size()]);
}
Also used : ProjectScope(org.eclipse.core.resources.ProjectScope) IAdaptable(org.eclipse.core.runtime.IAdaptable) HashSet(java.util.HashSet) Set(java.util.Set) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) IScopeContext(org.eclipse.core.runtime.preferences.IScopeContext) IDOMNode(org.eclipse.wst.xml.core.internal.provisional.document.IDOMNode) ICompletionProposal(org.eclipse.jface.text.contentassist.ICompletionProposal) Iterator(java.util.Iterator) InstanceScope(org.eclipse.core.runtime.preferences.InstanceScope) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet) DefaultScope(org.eclipse.core.runtime.preferences.DefaultScope) Annotation(org.eclipse.jface.text.source.Annotation) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IProject(org.eclipse.core.resources.IProject) IAnnotationModelExtension2(org.eclipse.jface.text.source.IAnnotationModelExtension2) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IResource(org.eclipse.core.resources.IResource)

Example 4 with IAnnotationModelExtension2

use of org.eclipse.jface.text.source.IAnnotationModelExtension2 in project webtools.sourceediting by eclipse.

the class SpellcheckStrategy method getSpellingAnnotationsToRemove.

private TemporaryAnnotation[] getSpellingAnnotationsToRemove(IRegion region) {
    List toRemove = new ArrayList();
    IAnnotationModel annotationModel = getAnnotationModel();
    // can be null when closing the editor
    if (annotationModel != null) {
        Iterator i = null;
        boolean annotationOverlaps = false;
        if (annotationModel instanceof IAnnotationModelExtension2) {
            i = ((IAnnotationModelExtension2) annotationModel).getAnnotationIterator(region.getOffset(), region.getLength(), true, true);
            annotationOverlaps = true;
        } else {
            i = annotationModel.getAnnotationIterator();
        }
        while (i.hasNext()) {
            Object obj = i.next();
            if (!(obj instanceof TemporaryAnnotation))
                continue;
            TemporaryAnnotation annotation = (TemporaryAnnotation) obj;
            ReconcileAnnotationKey key = (ReconcileAnnotationKey) annotation.getKey();
            // partition type
            if (key != null && key.equals(fReconcileAnnotationKey)) {
                if (key.getScope() == ReconcileAnnotationKey.PARTIAL && (annotationOverlaps || annotation.getPosition().overlapsWith(region.getOffset(), region.getLength()))) {
                    toRemove.add(annotation);
                } else if (key.getScope() == ReconcileAnnotationKey.TOTAL) {
                    toRemove.add(annotation);
                }
            }
        }
    }
    return (TemporaryAnnotation[]) toRemove.toArray(new TemporaryAnnotation[toRemove.size()]);
}
Also used : IAnnotationModelExtension2(org.eclipse.jface.text.source.IAnnotationModelExtension2) ReconcileAnnotationKey(org.eclipse.wst.sse.ui.internal.reconcile.ReconcileAnnotationKey) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)

Example 5 with IAnnotationModelExtension2

use of org.eclipse.jface.text.source.IAnnotationModelExtension2 in project eclipse.platform.text by eclipse.

the class InlinedAnnotationSupport method getInlinedAnnotationAtOffset.

/**
 * Returns the {@link AbstractInlinedAnnotation} from the given offset and null otherwise.
 *
 * @param viewer the source viewer
 * @param offset the start position of the region, must be >= 0
 * @param length the length of the region, must be >= 0
 * @return the {@link AbstractInlinedAnnotation} from the given offset and null otherwise.
 */
private static AbstractInlinedAnnotation getInlinedAnnotationAtOffset(ISourceViewer viewer, int offset, int length) {
    if (viewer == null) {
        return null;
    }
    IAnnotationModel annotationModel = viewer.getAnnotationModel();
    if (annotationModel == null) {
        return null;
    }
    Iterator<Annotation> iter = (annotationModel instanceof IAnnotationModelExtension2) ? ((IAnnotationModelExtension2) annotationModel).getAnnotationIterator(offset, length, true, true) : annotationModel.getAnnotationIterator();
    while (iter.hasNext()) {
        Annotation ann = iter.next();
        if (ann instanceof AbstractInlinedAnnotation) {
            Position p = annotationModel.getPosition(ann);
            if (p != null) {
                if (p.overlapsWith(offset, length)) {
                    return (AbstractInlinedAnnotation) ann;
                }
            }
        }
    }
    return null;
}
Also used : IAnnotationModelExtension2(org.eclipse.jface.text.source.IAnnotationModelExtension2) Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) Annotation(org.eclipse.jface.text.source.Annotation)

Aggregations

IAnnotationModelExtension2 (org.eclipse.jface.text.source.IAnnotationModelExtension2)8 Annotation (org.eclipse.jface.text.source.Annotation)7 IAnnotationModel (org.eclipse.jface.text.source.IAnnotationModel)6 Iterator (java.util.Iterator)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 Position (org.eclipse.jface.text.Position)4 MarkerAnnotation (org.eclipse.ui.texteditor.MarkerAnnotation)4 TemporaryAnnotation (org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)4 ICompletionProposal (org.eclipse.jface.text.contentassist.ICompletionProposal)3 ISourceViewer (org.eclipse.jface.text.source.ISourceViewer)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Set (java.util.Set)2 IProject (org.eclipse.core.resources.IProject)2 IResource (org.eclipse.core.resources.IResource)2 ProjectScope (org.eclipse.core.resources.ProjectScope)2 IAdaptable (org.eclipse.core.runtime.IAdaptable)2 DefaultScope (org.eclipse.core.runtime.preferences.DefaultScope)2 IScopeContext (org.eclipse.core.runtime.preferences.IScopeContext)2