Search in sources :

Example 31 with LocalQuickFix

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

the class ActionScriptFunctionSignatureChecker method checkConstructorCall.

@Override
public void checkConstructorCall(@NotNull JSCallExpression node, @NotNull JSClass target) {
    if (node instanceof JSNewExpression || node.getMethodExpression() instanceof JSSuperExpression) {
        final JSArgumentList argumentList = node.getArgumentList();
        final JSExpression[] expressions = argumentList != null ? argumentList.getArguments() : JSExpression.EMPTY_ARRAY;
        if (expressions.length > 0) {
            final ActionScriptCreateConstructorFix fix = ActionScriptCreateConstructorFix.createIfApplicable(node);
            registerProblem(node, JSBundle.message("javascript.invalid.number.of.parameters", "0"), fix != null ? new LocalQuickFix[] { fix } : LocalQuickFix.EMPTY_ARRAY);
        }
    } else {
        reportProblemIfNotExpectedCountOfParameters(node, 1, "one");
    }
}
Also used : ActionScriptCreateConstructorFix(com.intellij.lang.javascript.validation.fixes.ActionScriptCreateConstructorFix) JSSuperExpression(com.intellij.lang.javascript.psi.ecmal4.JSSuperExpression) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix)

Example 32 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project android by JetBrains.

the class ExternalAnnotationsSupport method checkAnnotationsJarAttached.

// Based on similar code in MagicConstantInspection
@SuppressWarnings("ALL")
private static void checkAnnotationsJarAttached(@NotNull PsiFile file, @NotNull ProblemsHolder holder) {
    // Not yet used
    if (false) {
        final Project project = file.getProject();
        PsiClass actionBar = JavaPsiFacade.getInstance(project).findClass("android.app.ActionBar", GlobalSearchScope.allScope(project));
        if (actionBar == null) {
            // no sdk to attach
            return;
        }
        PsiMethod[] methods = actionBar.findMethodsByName("getNavigationMode", false);
        if (methods.length != 1) {
            // no sdk to attach
            return;
        }
        PsiMethod getModifiers = methods[0];
        ExternalAnnotationsManager annotationsManager = ExternalAnnotationsManager.getInstance(project);
        PsiAnnotation annotation = annotationsManager.findExternalAnnotation(getModifiers, MagicConstant.class.getName());
        if (annotation != null) {
            return;
        }
        final VirtualFile virtualFile = PsiUtilCore.getVirtualFile(getModifiers);
        if (virtualFile == null) {
            // no sdk to attach
            return;
        }
        final List<OrderEntry> entries = ProjectRootManager.getInstance(project).getFileIndex().getOrderEntriesForFile(virtualFile);
        Sdk sdk = null;
        for (OrderEntry orderEntry : entries) {
            if (orderEntry instanceof JdkOrderEntry) {
                sdk = ((JdkOrderEntry) orderEntry).getJdk();
                if (sdk != null) {
                    break;
                }
            }
        }
        if (sdk == null) {
            // no sdk to attach
            return;
        }
        final Sdk finalSdk = sdk;
        String path = finalSdk.getHomePath();
        String text = "No IDEA annotations attached to the Android SDK " + finalSdk.getName() + (path == null ? "" : " (" + FileUtil.toSystemDependentName(path) + ")") + ", some issues will not be found";
        holder.registerProblem(file, text, ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new LocalQuickFix() {

            @NotNull
            @Override
            public String getName() {
                return "Attach annotations";
            }

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

            @Override
            public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
                ApplicationManager.getApplication().runWriteAction(new Runnable() {

                    @Override
                    public void run() {
                        SdkModificator modifier = finalSdk.getSdkModificator();
                        attachJdkAnnotations(modifier);
                        modifier.commitChanges();
                    }
                });
            }
        });
    }
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) MagicConstant(org.intellij.lang.annotations.MagicConstant) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) SdkModificator(com.intellij.openapi.projectRoots.SdkModificator) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) Sdk(com.intellij.openapi.projectRoots.Sdk) ExternalAnnotationsManager(com.intellij.codeInsight.ExternalAnnotationsManager)

Example 33 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoUnresolvedReferenceInspection method buildGoVisitor.

@NotNull
@Override
protected GoVisitor buildGoVisitor(@NotNull ProblemsHolder holder, @NotNull LocalInspectionToolSession session) {
    return new GoVisitor() {

        @Override
        public void visitFieldName(@NotNull GoFieldName o) {
            super.visitFieldName(o);
            PsiElement resolve = o.resolve();
            if (resolve == null) {
                PsiElement id = o.getIdentifier();
                holder.registerProblem(id, "Unknown field <code>#ref</code> in struct literal #loc", LIKE_UNKNOWN_SYMBOL);
            }
        }

        @Override
        public void visitReferenceExpression(@NotNull GoReferenceExpression o) {
            super.visitReferenceExpression(o);
            GoReference reference = o.getReference();
            GoReferenceExpression qualifier = o.getQualifier();
            GoReference qualifierRef = qualifier != null ? qualifier.getReference() : null;
            PsiElement qualifierResolve = qualifierRef != null ? qualifierRef.resolve() : null;
            if (qualifier != null && qualifierResolve == null)
                return;
            ResolveResult[] results = reference.multiResolve(false);
            PsiElement id = o.getIdentifier();
            String name = id.getText();
            if (results.length > 1) {
                holder.registerProblem(id, "Ambiguous reference " + "'" + name + "'", GENERIC_ERROR_OR_WARNING);
            } else if (reference.resolve() == null) {
                LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY;
                if (isProhibited(o, qualifier)) {
                    fixes = createImportPackageFixes(o, reference, holder.isOnTheFly());
                } else if (holder.isOnTheFly()) {
                    boolean canBeLocal = PsiTreeUtil.getParentOfType(o, GoBlock.class) != null;
                    List<LocalQuickFix> fixesList = ContainerUtil.newArrayList(new GoIntroduceGlobalVariableFix(id, name));
                    if (canBeLocal) {
                        fixesList.add(new GoIntroduceLocalVariableFix(id, name));
                    }
                    PsiElement parent = o.getParent();
                    if (o.getReadWriteAccess() == ReadWriteAccessDetector.Access.Read) {
                        fixesList.add(new GoIntroduceGlobalConstantFix(id, name));
                        if (canBeLocal) {
                            fixesList.add(new GoIntroduceLocalConstantFix(id, name));
                        }
                    } else if (canBeLocal) {
                        PsiElement grandParent = parent.getParent();
                        if (grandParent instanceof GoAssignmentStatement) {
                            fixesList.add(new GoReplaceAssignmentWithDeclarationQuickFix(grandParent));
                        } else if (parent instanceof GoRangeClause || parent instanceof GoRecvStatement) {
                            fixesList.add(new GoReplaceAssignmentWithDeclarationQuickFix(parent));
                        }
                    }
                    if (parent instanceof GoCallExpr && PsiTreeUtil.getParentOfType(o, GoConstDeclaration.class) == null) {
                        fixesList.add(new GoIntroduceFunctionFix(parent, name));
                    }
                    fixes = fixesList.toArray(new LocalQuickFix[fixesList.size()]);
                }
                holder.registerProblem(id, "Unresolved reference " + "'" + name + "'", LIKE_UNKNOWN_SYMBOL, fixes);
            }
        }

        @Override
        public void visitImportSpec(@NotNull GoImportSpec o) {
            if (o.isCImport())
                return;
            GoImportString string = o.getImportString();
            if (string.getTextLength() < 2)
                return;
            PsiReference[] references = string.getReferences();
            for (PsiReference reference : references) {
                if (reference instanceof FileReference) {
                    ResolveResult[] resolveResults = ((FileReference) reference).multiResolve(false);
                    if (resolveResults.length == 0) {
                        ProblemHighlightType type = reference.getRangeInElement().isEmpty() ? GENERIC_ERROR_OR_WARNING : LIKE_UNKNOWN_SYMBOL;
                        holder.registerProblem(reference, ProblemsHolder.unresolvedReferenceMessage(reference), type);
                    }
                }
            }
        }

        @Override
        public void visitLabelRef(@NotNull GoLabelRef o) {
            PsiReference reference = o.getReference();
            String name = o.getText();
            if (reference.resolve() == null) {
                holder.registerProblem(o, "Unresolved label " + "'" + name + "'", LIKE_UNKNOWN_SYMBOL);
            }
        }

        @Override
        public void visitTypeReferenceExpression(@NotNull GoTypeReferenceExpression o) {
            super.visitTypeReferenceExpression(o);
            PsiReference reference = o.getReference();
            GoTypeReferenceExpression qualifier = o.getQualifier();
            PsiElement qualifierResolve = qualifier != null ? qualifier.resolve() : null;
            if (qualifier != null && qualifierResolve == null)
                return;
            if (reference.resolve() == null) {
                PsiElement id = o.getIdentifier();
                String name = id.getText();
                boolean isProhibited = isProhibited(o, qualifier);
                LocalQuickFix[] fixes = LocalQuickFix.EMPTY_ARRAY;
                if (isProhibited) {
                    fixes = createImportPackageFixes(o, reference, holder.isOnTheFly());
                } else if (holder.isOnTheFly()) {
                    fixes = new LocalQuickFix[] { new GoIntroduceTypeFix(id, name) };
                }
                holder.registerProblem(id, "Unresolved type " + "'" + name + "'", LIKE_UNKNOWN_SYMBOL, fixes);
            }
        }
    };
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) NotNull(org.jetbrains.annotations.NotNull) ResolveResult(com.intellij.psi.ResolveResult) PsiElement(com.intellij.psi.PsiElement) PsiReference(com.intellij.psi.PsiReference) GoReference(com.goide.psi.impl.GoReference) FileReference(com.intellij.psi.impl.source.resolve.reference.impl.providers.FileReference) ProblemHighlightType(com.intellij.codeInspection.ProblemHighlightType) NotNull(org.jetbrains.annotations.NotNull)

Example 34 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoMissingReturnInspection method check.

private static void check(@Nullable GoSignature signature, @Nullable GoBlock block, @NotNull ProblemsHolder holder) {
    if (block == null)
        return;
    GoResult result = signature != null ? signature.getResult() : null;
    if (result == null || result.isVoid() || isTerminating(block))
        return;
    PsiElement brace = block.getRbrace();
    holder.registerProblem(brace == null ? block : brace, "Missing return at end of function", brace == null ? new LocalQuickFix[] {} : new LocalQuickFix[] { new AddReturnFix(block) });
}
Also used : LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) LocalQuickFixAndIntentionActionOnPsiElement(com.intellij.codeInspection.LocalQuickFixAndIntentionActionOnPsiElement) PsiElement(com.intellij.psi.PsiElement)

Example 35 with LocalQuickFix

use of com.intellij.codeInspection.LocalQuickFix in project go-lang-idea-plugin by go-lang-plugin-org.

the class GoMultiplePackagesInspection method checkFile.

@Override
protected void checkFile(@NotNull GoFile file, @NotNull ProblemsHolder problemsHolder) {
    if (((ScratchFileType) ScratchFileType.INSTANCE).isMyFileType(file.getVirtualFile()))
        return;
    GoPackageClause packageClause = file.getPackage();
    if (packageClause != null) {
        String packageName = file.getPackageName();
        if (packageName == null || packageName.equals(GoConstants.DOCUMENTATION))
            return;
        PsiDirectory dir = file.getContainingDirectory();
        Collection<String> packages = GoPackageUtil.getAllPackagesInDirectory(dir, null, true);
        packages.remove(GoConstants.DOCUMENTATION);
        if (packages.size() > 1) {
            Collection<LocalQuickFix> fixes = ContainerUtil.newArrayList();
            if (problemsHolder.isOnTheFly()) {
                fixes.add(new GoMultiplePackagesQuickFix(packageClause, packageName, packages, true));
            } else {
                for (String name : packages) {
                    fixes.add(new GoMultiplePackagesQuickFix(packageClause, name, packages, false));
                }
            }
            problemsHolder.registerProblem(packageClause, "Multiple packages in directory", fixes.toArray(new LocalQuickFix[fixes.size()]));
        }
    }
}
Also used : PsiDirectory(com.intellij.psi.PsiDirectory) GoPackageClause(com.goide.psi.GoPackageClause) ScratchFileType(com.intellij.ide.scratch.ScratchFileType) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) GoMultiplePackagesQuickFix(com.goide.quickfix.GoMultiplePackagesQuickFix)

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