Search in sources :

Example 1 with XsltParameter

use of org.intellij.lang.xpath.xslt.psi.XsltParameter in project intellij-community by JetBrains.

the class XsltValidator method checkUnusedVariable.

public static void checkUnusedVariable(XsltVariable variable, ProblemsHolder holder) {
    if (variable instanceof XsltParameter) {
        if (((XsltParameter) variable).isAbstract()) {
            return;
        }
    } else {
        if (variable.isVoid()) {
            return;
        }
    }
    final XmlTag tag = variable.getTag();
    final XmlTag templateTag = XsltCodeInsightUtil.getTemplateTag(tag, false);
    if (templateTag == null) {
        return;
    }
    final XmlAttribute attribute = tag.getAttribute("name");
    if (attribute == null) {
        return;
    }
    final PsiElement token = XsltSupport.getAttValueToken(attribute);
    if (token == null) {
        return;
    }
    final SearchScope scope = new LocalSearchScope(templateTag);
    final Query<PsiReference> refs = ReferencesSearch.search(variable, scope, false);
    if (isUnused(variable, refs)) {
        final String name = variable.getName();
        assert name != null;
        final LocalQuickFix[] fixes;
        if (variable instanceof XsltParameter) {
            fixes = new LocalQuickFix[] { new DeleteUnusedParameterFix(name, (XsltParameter) variable) };
        } else {
            fixes = new LocalQuickFix[] { new DeleteUnusedVariableFix(name, variable) };
        }
        holder.registerProblem(token, ((DeleteUnusedElementBase) fixes[0]).getType() + " '" + name + "' is never used", ProblemHighlightType.LIKE_UNUSED_SYMBOL, fixes);
    }
}
Also used : LocalSearchScope(com.intellij.psi.search.LocalSearchScope) XmlAttribute(com.intellij.psi.xml.XmlAttribute) DeleteUnusedVariableFix(org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedVariableFix) PsiReference(com.intellij.psi.PsiReference) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) DeleteUnusedParameterFix(org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedParameterFix) SearchScope(com.intellij.psi.search.SearchScope) LocalSearchScope(com.intellij.psi.search.LocalSearchScope) PsiElement(com.intellij.psi.PsiElement) DeleteUnusedElementBase(org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedElementBase) XmlTag(com.intellij.psi.xml.XmlTag)

Example 2 with XsltParameter

use of org.intellij.lang.xpath.xslt.psi.XsltParameter in project intellij-community by JetBrains.

the class XsltValidator method isUnused.

private static boolean isUnused(PsiElement obj, Query<PsiReference> query) {
    if (obj instanceof XsltParameter) {
        final Collection<PsiReference> references = query.findAll();
        int n = references.size();
        for (PsiReference reference : references) {
            final PsiElement element = reference.getElement();
            if (element instanceof XmlAttributeValue) {
                final XmlAttribute parent = (XmlAttribute) element.getParent();
                if ("name".equals(parent.getName())) {
                    final XmlTag tag = parent.getParent();
                    if (tag != null && "with-param".equals(tag.getLocalName())) {
                        n--;
                    }
                }
            }
        }
        return n == 0;
    } else {
        return query.findFirst() == null;
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) PsiReference(com.intellij.psi.PsiReference) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag)

Example 3 with XsltParameter

use of org.intellij.lang.xpath.xslt.psi.XsltParameter in project intellij-community by JetBrains.

the class XsltVariableContext method isReferenceTo.

public boolean isReferenceTo(PsiElement element, XPathVariableReference reference) {
    if (element instanceof XsltParameter) {
        final XsltTemplate template = XsltCodeInsightUtil.getTemplate(element, false);
        if (template == null || template.getMatchExpression() == null)
            return false;
        final XPathVariable t = reference.resolve();
        final PsiReference[] references = element.getReferences();
        for (PsiReference r : references) {
            if (r.isReferenceTo(t))
                return true;
        }
    }
    return false;
}
Also used : XPathVariable(org.intellij.lang.xpath.psi.XPathVariable) PsiReference(com.intellij.psi.PsiReference) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) XsltTemplate(org.intellij.lang.xpath.xslt.psi.XsltTemplate)

Example 4 with XsltParameter

use of org.intellij.lang.xpath.xslt.psi.XsltParameter in project intellij-community by JetBrains.

the class XsltTemplateImpl method getParameters.

@NotNull
public XsltParameter[] getParameters() {
    final PsiElement[] elements = ResolveUtil.collect(new ParamMatcher(getTag(), null));
    final XsltParameter[] xsltParameters = new XsltParameter[elements.length];
    //noinspection SuspiciousSystemArraycopy
    System.arraycopy(elements, 0, xsltParameters, 0, elements.length);
    return xsltParameters;
}
Also used : ParamMatcher(org.intellij.lang.xpath.xslt.util.ParamMatcher) XsltParameter(org.intellij.lang.xpath.xslt.psi.XsltParameter) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

XsltParameter (org.intellij.lang.xpath.xslt.psi.XsltParameter)4 PsiElement (com.intellij.psi.PsiElement)3 PsiReference (com.intellij.psi.PsiReference)3 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XmlTag (com.intellij.psi.xml.XmlTag)2 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 LocalSearchScope (com.intellij.psi.search.LocalSearchScope)1 SearchScope (com.intellij.psi.search.SearchScope)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 XPathVariable (org.intellij.lang.xpath.psi.XPathVariable)1 XsltTemplate (org.intellij.lang.xpath.xslt.psi.XsltTemplate)1 DeleteUnusedElementBase (org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedElementBase)1 DeleteUnusedParameterFix (org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedParameterFix)1 DeleteUnusedVariableFix (org.intellij.lang.xpath.xslt.quickfix.DeleteUnusedVariableFix)1 ParamMatcher (org.intellij.lang.xpath.xslt.util.ParamMatcher)1 NotNull (org.jetbrains.annotations.NotNull)1