Search in sources :

Example 1 with RemoveParamFix

use of org.intellij.lang.xpath.xslt.quickfix.RemoveParamFix in project intellij-community by JetBrains.

the class TemplateInvocationInspection method checkTemplateInvocation.

private static void checkTemplateInvocation(XsltTemplateInvocation call, ProblemsHolder holder, boolean onTheFly) {
    final XsltWithParam[] arguments = call.getArguments();
    final Map<String, XsltWithParam> argNames = new HashMap<>();
    for (XsltWithParam arg : arguments) {
        final XmlAttribute attr = arg.getNameAttribute();
        if (attr != null) {
            final String name = attr.getValue();
            if (argNames.containsKey(name)) {
                final PsiElement token = arg.getNameIdentifier();
                assert token != null;
                holder.registerProblem(token, "Duplicate Argument '" + name + "'");
            }
            argNames.put(name, arg);
        }
    }
    if (call instanceof XsltCallTemplate) {
        final XsltCallTemplate ct = ((XsltCallTemplate) call);
        final PsiElement nameToken = ct.getNameIdentifier();
        final XsltTemplate template = ct.getTemplate();
        if (template != null) {
            if (nameToken != null) {
                final XsltParameter[] parameters = template.getParameters();
                for (XsltParameter parameter : parameters) {
                    if (!argNames.containsKey(parameter.getName()) && !parameter.hasDefault()) {
                        final LocalQuickFix fix = new AddWithParamFix(parameter, call.getTag()).createQuickFix(onTheFly);
                        holder.registerProblem(nameToken, "Missing template parameter: " + parameter.getName(), AbstractFix.createFixes(fix));
                    }
                }
            }
            for (String s : argNames.keySet()) {
                final XmlAttribute argAttribute = argNames.get(s).getNameAttribute();
                assert argAttribute != null;
                final XmlAttributeValue valueElement = argAttribute.getValueElement();
                final PsiElement valueToken = XsltSupport.getAttValueToken(argAttribute);
                if (valueToken != null && s.trim().length() > 0) {
                    if (template.getParameter(s) == null) {
                        final LocalQuickFix fix1 = new AddParameterFix(s, template).createQuickFix(onTheFly);
                        final LocalQuickFix fix2 = new RemoveParamFix(argNames.get(s).getTag(), s).createQuickFix(onTheFly);
                        holder.registerProblem(valueToken, "Undeclared template parameter: " + s, ProblemHighlightType.LIKE_UNKNOWN_SYMBOL, AbstractFix.createFixes(fix1, fix2));
                    }
                } else if (valueElement != null) {
                    holder.registerProblem(valueElement, "Parameter name expected");
                }
            }
        }
    }
}
Also used : XmlAttribute(com.intellij.psi.xml.XmlAttribute) RemoveParamFix(org.intellij.lang.xpath.xslt.quickfix.RemoveParamFix) HashMap(java.util.HashMap) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) XmlAttributeValue(com.intellij.psi.xml.XmlAttributeValue) AddParameterFix(org.intellij.lang.xpath.xslt.quickfix.AddParameterFix) AddWithParamFix(org.intellij.lang.xpath.xslt.quickfix.AddWithParamFix) PsiElement(com.intellij.psi.PsiElement)

Aggregations

LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)1 PsiElement (com.intellij.psi.PsiElement)1 XmlAttribute (com.intellij.psi.xml.XmlAttribute)1 XmlAttributeValue (com.intellij.psi.xml.XmlAttributeValue)1 HashMap (java.util.HashMap)1 AddParameterFix (org.intellij.lang.xpath.xslt.quickfix.AddParameterFix)1 AddWithParamFix (org.intellij.lang.xpath.xslt.quickfix.AddWithParamFix)1 RemoveParamFix (org.intellij.lang.xpath.xslt.quickfix.RemoveParamFix)1