Search in sources :

Example 46 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class TypeCustomizerInspection method buildVisitor.

@NotNull
@Override
protected BaseInspectionVisitor buildVisitor() {
    return new BaseInspectionVisitor() {

        @Override
        public void visitFile(@NotNull GroovyFileBase file) {
            CompilerConfiguration configuration = CompilerConfiguration.getInstance(file.getProject());
            if (configuration != null && !configuration.isResourceFile(file.getVirtualFile()) && fileSeemsToBeTypeCustomizer(file)) {
                final LocalQuickFix[] fixes = { new AddToResourceFix(file) };
                final String message = GroovyInspectionBundle.message("type.customizer.is.not.marked.as.a.resource.file");
                registerError(file, message, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
            }
        }
    };
}
Also used : GroovyFileBase(org.jetbrains.plugins.groovy.lang.psi.GroovyFileBase) BaseInspectionVisitor(org.jetbrains.plugins.groovy.codeInspection.BaseInspectionVisitor) CompilerConfiguration(com.intellij.compiler.CompilerConfiguration) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 47 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class GroovyStaticTypeCheckVisitor method registerError.

@Override
protected void registerError(@NotNull final PsiElement location, @NotNull final String description, @Nullable final LocalQuickFix[] fixes, final ProblemHighlightType highlightType) {
    if (highlightType != ProblemHighlightType.GENERIC_ERROR)
        return;
    final List<IntentionAction> intentions = ContainerUtil.newArrayList();
    if (fixes != null) {
        for (final LocalQuickFix fix : fixes) {
            intentions.add(new IntentionAction() {

                @NotNull
                @Override
                public String getText() {
                    return fix.getName();
                }

                @NotNull
                @Override
                public String getFamilyName() {
                    return fix.getFamilyName();
                }

                @Override
                public boolean isAvailable(@NotNull Project project, Editor editor, PsiFile file) {
                    return true;
                }

                @Override
                public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
                    final InspectionManager manager = InspectionManager.getInstance(project);
                    final ProblemDescriptor descriptor = manager.createProblemDescriptor(location, description, fixes, highlightType, fixes.length == 1, false);
                    fix.applyFix(project, descriptor);
                }

                @Override
                public boolean startInWriteAction() {
                    return true;
                }
            });
        }
    }
    registerError(location, description, intentions.toArray(new IntentionAction[intentions.size()]), highlightType);
}
Also used : Project(com.intellij.openapi.project.Project) InspectionManager(com.intellij.codeInspection.InspectionManager) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) IntentionAction(com.intellij.codeInsight.intention.IntentionAction) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiFile(com.intellij.psi.PsiFile) IncorrectOperationException(com.intellij.util.IncorrectOperationException) Editor(com.intellij.openapi.editor.Editor) NotNull(org.jetbrains.annotations.NotNull)

Example 48 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix 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)

Example 49 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class VariableShadowingInspection method buildVisitor.

@NotNull
public PsiElementVisitor buildVisitor(@NotNull final ProblemsHolder holder, final boolean isOnTheFly) {
    if (!(holder.getFile() instanceof XmlFile))
        return PsiElementVisitor.EMPTY_VISITOR;
    return new XmlElementVisitor() {

        @Override
        public void visitXmlTag(final XmlTag tag) {
            final XmlAttribute nameAttr = tag.getAttribute("name", null);
            if (nameAttr == null || PsiTreeUtil.hasErrorElements(nameAttr))
                return;
            if (XsltSupport.isVariableOrParam(tag)) {
                final XmlTag shadowedVariable = DeclarationChecker.getInstance((XmlFile) tag.getContainingFile()).getShadowedVariable(tag);
                if (shadowedVariable != null) {
                    final String innerKind = XsltSupport.isParam(tag) ? "Parameter" : "Variable";
                    final String outerKind = XsltSupport.isParam(shadowedVariable) ? "parameter" : "variable";
                    final LocalQuickFix fix1 = new RenameVariableFix(tag, "local").createQuickFix(isOnTheFly);
                    final LocalQuickFix fix2 = new RenameVariableFix(shadowedVariable, "outer").createQuickFix(isOnTheFly);
                    final XmlAttribute name = tag.getAttribute("name");
                    assert name != null;
                    final PsiElement token = XsltSupport.getAttValueToken(name);
                    assert token != null;
                    holder.registerProblem(token, innerKind + " '" + name.getValue() + "' shadows " + outerKind, AbstractFix.createFixes(fix1, fix2));
                }
            }
        }
    };
}
Also used : XmlElementVisitor(com.intellij.psi.XmlElementVisitor) RenameVariableFix(org.intellij.lang.xpath.xslt.quickfix.RenameVariableFix) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) PsiElement(com.intellij.psi.PsiElement) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Example 50 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project intellij-community by JetBrains.

the class BaseInspectionVisitor method registerMethodCallError.

protected void registerMethodCallError(GrMethodCall method, Object... args) {
    if (method == null) {
        return;
    }
    final LocalQuickFix[] fixes = createFixes(method);
    final String description = StringUtil.notNullize(inspection.buildErrorString(args));
    final GrExpression invoked = method.getInvokedExpression();
    final PsiElement nameElement = ((GrReferenceExpression) invoked).getReferenceNameElement();
    assert nameElement != null;
    registerError(nameElement, description, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING);
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) PsiElement(com.intellij.psi.PsiElement) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)

Aggregations

LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)59 NotNull (org.jetbrains.annotations.NotNull)20 PsiElement (com.intellij.psi.PsiElement)16 Nullable (org.jetbrains.annotations.Nullable)11 IntentionAction (com.intellij.codeInsight.intention.IntentionAction)10 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)10 ASTNode (com.intellij.lang.ASTNode)6 Project (com.intellij.openapi.project.Project)6 VirtualFile (com.intellij.openapi.vfs.VirtualFile)6 XmlTag (com.intellij.psi.xml.XmlTag)6 PsiFile (com.intellij.psi.PsiFile)5 PsiReference (com.intellij.psi.PsiReference)5 ArrayList (java.util.ArrayList)5 PsiTreeUtil (com.intellij.psi.util.PsiTreeUtil)4 LocalQuickFixProvider (com.intellij.codeInspection.LocalQuickFixProvider)3 QuickFixWrapper (com.intellij.codeInspection.ex.QuickFixWrapper)3 Pair (com.intellij.openapi.util.Pair)3 TextRange (com.intellij.openapi.util.TextRange)3 XmlAttributeDescriptor (com.intellij.xml.XmlAttributeDescriptor)3 XmlElementDescriptor (com.intellij.xml.XmlElementDescriptor)3