Search in sources :

Example 11 with TemporaryAnnotation

use of org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation in project webtools.sourceediting by eclipse.

the class HTMLSyntaxValidationQuickFixProcessor 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_ELEMENT_NAMES, HTMLCorePreferenceNames.IGNORE_ELEMENT_NAMES_DEFAULT, fLookupOrder);
                String ignoreList = fPreferenceService.getString(getPreferenceNodeQualifier(), HTMLCorePreferenceNames.ELEMENT_NAMES_TO_IGNORE, HTMLCorePreferenceNames.ELEMENT_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 = getElementName(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())) {
                    IgnoreElementNameCompletionProposal p = new IgnoreElementNameCompletionProposal(name.toLowerCase(), offset, NLS.bind(HTMLUIMessages.DoNotValidateElement, name), HTMLUIMessages.DoNotValidateElementAddInfo, 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;
                    IgnoreElementNameCompletionProposal p = new IgnoreElementNameCompletionProposal(namePattern.toString().toLowerCase(), offset, NLS.bind(HTMLUIMessages.DoNotValidateAllElements, namePattern.toString()), HTMLUIMessages.DoNotValidateAllElementsAddInfo, 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 12 with TemporaryAnnotation

use of org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation in project webtools.sourceediting by eclipse.

the class HTMLSyntaxValidationQuickFixProcessor method canFix.

/*
	 * @see org.eclipse.jface.text.quickassist.IQuickAssistProcessor#canFix(org.eclipse.jface.text.source.Annotation)
	 */
public boolean canFix(Annotation annotation) {
    boolean result = false;
    String text = null;
    if (annotation instanceof TemporaryAnnotation) {
        TemporaryAnnotation tempAnnotation = (TemporaryAnnotation) annotation;
        int problemID = tempAnnotation.getProblemID();
        text = tempAnnotation.getText();
        if (problemID == 0 && text != null)
            result = true;
    } else if (annotation instanceof MarkerAnnotation) {
        MarkerAnnotation markerAnnotation = (MarkerAnnotation) annotation;
        text = markerAnnotation.getText();
        IMarker marker = markerAnnotation.getMarker();
        IResource resource = marker == null ? null : marker.getResource();
        if (resource != null && resource.exists() && resource.isAccessible() && text != null) {
            result = true;
        }
    }
    result = (result && UNKNOWN_ELEMENT_NAME_MATCHER.match(text));
    return result;
}
Also used : MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) IMarker(org.eclipse.core.resources.IMarker) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IResource(org.eclipse.core.resources.IResource)

Example 13 with TemporaryAnnotation

use of org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation in project liferay-ide by liferay.

the class LiferayCustomXmlHover method getHoverRegion.

@Override
public IRegion getHoverRegion(ITextViewer textViewer, int offset) {
    IDocument document = textViewer.getDocument();
    if (document == null) {
        return null;
    }
    CompoundRegion compoundRegion = new CompoundRegion(textViewer, offset);
    // if there are MarkerRegion or TemporaryRegion, then don't add
    // InfoRegion
    boolean addInfoRegion = true;
    if (textViewer instanceof ISourceViewer) {
        ISourceViewer sourceViewer = (ISourceViewer) textViewer;
        IAnnotationModel model = sourceViewer.getAnnotationModel();
        if (model != null) {
            Iterator<Annotation> it = model.getAnnotationIterator();
            while (it.hasNext()) {
                Annotation annotation = it.next();
                if (annotation instanceof MarkerAnnotation) {
                    Position pos = sourceViewer.getAnnotationModel().getPosition(annotation);
                    if (pos.includes(offset)) {
                        compoundRegion.addRegion(new MarkerRegion(pos.getOffset(), pos.getLength(), (MarkerAnnotation) annotation));
                        addInfoRegion = false;
                    }
                } else if (annotation instanceof TemporaryAnnotation) {
                    TemporaryAnnotation temp = (TemporaryAnnotation) annotation;
                    if ((temp.getAttributes() != null) && (temp.getAttributes().get(LiferayBaseValidator.MARKER_QUERY_ID) != null)) {
                        Position pos = sourceViewer.getAnnotationModel().getPosition(annotation);
                        if (pos.includes(offset)) {
                            compoundRegion.addRegion(new TemporaryRegion(pos.getOffset(), pos.getLength(), (TemporaryAnnotation) annotation));
                            addInfoRegion = false;
                        }
                    }
                }
            }
        }
        if (addInfoRegion) {
            IRegion normalRegion = super.getHoverRegion(textViewer, offset);
            if (normalRegion != null) {
                String content = getHoverInfo(textViewer, normalRegion);
                if (content != null) {
                    compoundRegion.addRegion(new InfoRegion(normalRegion.getOffset(), normalRegion.getLength(), getHoverInfo(textViewer, normalRegion)));
                }
            }
        }
    }
    return compoundRegion.getRegions().size() > 0 ? compoundRegion : null;
}
Also used : Position(org.eclipse.jface.text.Position) IAnnotationModel(org.eclipse.jface.text.source.IAnnotationModel) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) Annotation(org.eclipse.jface.text.source.Annotation) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IRegion(org.eclipse.jface.text.IRegion) MarkerAnnotation(org.eclipse.ui.texteditor.MarkerAnnotation) ISourceViewer(org.eclipse.jface.text.source.ISourceViewer) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IDocument(org.eclipse.jface.text.IDocument)

Aggregations

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