Search in sources :

Example 36 with ProblemDescriptor

use of com.intellij.codeInspection.ProblemDescriptor 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 37 with ProblemDescriptor

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

the class GroovyRangeTypeCheckInspection method buildFix.

@Override
protected GroovyFix buildFix(@NotNull PsiElement location) {
    final GrRangeExpression range = (GrRangeExpression) location;
    final PsiType type = range.getType();
    final List<GroovyFix> fixes = new ArrayList<>(3);
    if (type instanceof GrRangeType) {
        PsiType iterationType = ((GrRangeType) type).getIterationType();
        if (!(iterationType instanceof PsiClassType))
            return null;
        final PsiClass psiClass = ((PsiClassType) iterationType).resolve();
        if (!(psiClass instanceof GrTypeDefinition))
            return null;
        final GroovyResolveResult[] nexts = ResolveUtil.getMethodCandidates(iterationType, "next", range);
        final GroovyResolveResult[] previouses = ResolveUtil.getMethodCandidates(iterationType, "previous", range);
        final GroovyResolveResult[] compareTos = ResolveUtil.getMethodCandidates(iterationType, "compareTo", range, iterationType);
        if (countImplementations(psiClass, nexts) == 0) {
            fixes.add(GroovyQuickFixFactory.getInstance().createAddMethodFix("next", (GrTypeDefinition) psiClass));
        }
        if (countImplementations(psiClass, previouses) == 0) {
            fixes.add(GroovyQuickFixFactory.getInstance().createAddMethodFix("previous", (GrTypeDefinition) psiClass));
        }
        if (!InheritanceUtil.isInheritor(iterationType, CommonClassNames.JAVA_LANG_COMPARABLE) || countImplementations(psiClass, compareTos) == 0) {
            fixes.add(GroovyQuickFixFactory.getInstance().createAddClassToExtendsFix((GrTypeDefinition) psiClass, CommonClassNames.JAVA_LANG_COMPARABLE));
        }
        return new GroovyFix() {

            @Override
            protected void doFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) throws IncorrectOperationException {
                for (GroovyFix fix : fixes) {
                    fix.applyFix(project, descriptor);
                }
            }

            @NotNull
            @Override
            public String getName() {
                return GroovyInspectionBundle.message("fix.class", psiClass.getName());
            }

            @Nls
            @NotNull
            @Override
            public String getFamilyName() {
                return "Fix range class";
            }
        };
    }
    return null;
}
Also used : ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) ArrayList(java.util.ArrayList) GrRangeType(org.jetbrains.plugins.groovy.lang.psi.impl.GrRangeType) NotNull(org.jetbrains.annotations.NotNull) Project(com.intellij.openapi.project.Project) GroovyResolveResult(org.jetbrains.plugins.groovy.lang.psi.api.GroovyResolveResult) GrTypeDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrTypeDefinition) GrRangeExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression)

Example 38 with ProblemDescriptor

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

the class UnusedMessageFormatParameterInspection method checkFile.

@Nullable
public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionManager manager, boolean isOnTheFly) {
    if (!(file instanceof PropertiesFile))
        return null;
    PropertiesFile propertiesFile = (PropertiesFile) file;
    final List<IProperty> properties = propertiesFile.getProperties();
    List<ProblemDescriptor> problemDescriptors = new ArrayList<>();
    for (IProperty property : properties) {
        @NonNls String name = property.getName();
        if (name != null) {
            if (name.startsWith("log4j"))
                continue;
            if (name.startsWith(REGEXP + ".") || name.endsWith("." + REGEXP))
                continue;
        }
        String value = property.getValue();
        Set<Integer> parameters = new HashSet<>();
        if (value != null) {
            int index = value.indexOf('{');
            while (index != -1) {
                value = value.substring(index + 1);
                final int comma = value.indexOf(',');
                final int brace = value.indexOf('}');
                //misformatted string
                if (brace == -1)
                    break;
                if (comma == -1) {
                    index = brace;
                } else {
                    index = Math.min(comma, brace);
                }
                try {
                    parameters.add(new Integer(value.substring(0, index)));
                } catch (NumberFormatException e) {
                    break;
                }
                index = value.indexOf('{');
            }
            for (Integer integer : parameters) {
                for (int i = 0; i < integer.intValue(); i++) {
                    if (!parameters.contains(new Integer(i))) {
                        ASTNode[] nodes = property.getPsiElement().getNode().getChildren(null);
                        PsiElement valElement = nodes.length < 3 ? property.getPsiElement() : nodes[2].getPsi();
                        final String message = PropertiesBundle.message("unused.message.format.parameter.problem.descriptor", integer.toString(), Integer.toString(i));
                        final String propertyKey = property.getKey();
                        final LocalQuickFix[] fixes = isOnTheFly ? new LocalQuickFix[] { new RenameElementFix(((Property) property), propertyKey == null ? REGEXP : propertyKey + "." + REGEXP) } : null;
                        problemDescriptors.add(manager.createProblemDescriptor(valElement, message, isOnTheFly, fixes, ProblemHighlightType.GENERIC_ERROR_OR_WARNING));
                        break;
                    }
                }
            }
        }
    }
    return problemDescriptors.isEmpty() ? null : problemDescriptors.toArray(new ProblemDescriptor[problemDescriptors.size()]);
}
Also used : NonNls(org.jetbrains.annotations.NonNls) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) ArrayList(java.util.ArrayList) RenameElementFix(com.intellij.codeInsight.daemon.impl.quickfix.RenameElementFix) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) ASTNode(com.intellij.lang.ASTNode) PropertiesFile(com.intellij.lang.properties.psi.PropertiesFile) PsiElement(com.intellij.psi.PsiElement) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 39 with ProblemDescriptor

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

the class GroovyRangeTypeCheckTest method doTest.

public void doTest() {
    myFixture.configureByFile(getTestName(false) + ".groovy");
    final int offset = myFixture.getEditor().getCaretModel().getOffset();
    final PsiElement atCaret = myFixture.getFile().findElementAt(offset);
    final GrRangeExpression range = PsiTreeUtil.getParentOfType(atCaret, GrRangeExpression.class);
    final GroovyRangeTypeCheckInspection inspection = new GroovyRangeTypeCheckInspection();
    final GroovyFix fix = inspection.buildFix(range);
    LocalQuickFix[] fixes = { fix };
    final ProblemDescriptor descriptor = InspectionManager.getInstance(getProject()).createProblemDescriptor(range, "bla-bla", false, fixes, ProblemHighlightType.WEAK_WARNING);
    WriteCommandAction.runWriteCommandAction(null, () -> {
        fix.applyFix(myFixture.getProject(), descriptor);
        PostprocessReformattingAspect.getInstance(getProject()).doPostponedFormatting();
    });
    myFixture.checkResultByFile(getTestName(false) + "_after.groovy");
}
Also used : GroovyFix(org.jetbrains.plugins.groovy.codeInspection.GroovyFix) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) LocalQuickFix(com.intellij.codeInspection.LocalQuickFix) GrRangeExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.arithmetic.GrRangeExpression) PsiElement(com.intellij.psi.PsiElement)

Example 40 with ProblemDescriptor

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

the class XmlImportOptimizer method processFile.

@NotNull
@Override
public CollectingInfoRunnable processFile(final PsiFile file) {
    return new CollectingInfoRunnable() {

        int myRemovedNameSpaces = 0;

        @Override
        public void run() {
            XmlFile xmlFile = (XmlFile) file;
            Project project = xmlFile.getProject();
            HighlightDisplayKey key = HighlightDisplayKey.find(myInspection.getShortName());
            if (!InspectionProjectProfileManager.getInstance(project).getCurrentProfile().isToolEnabled(key, xmlFile))
                return;
            ProblemsHolder holder = new ProblemsHolder(InspectionManager.getInstance(project), xmlFile, false);
            final XmlElementVisitor visitor = (XmlElementVisitor) myInspection.buildVisitor(holder, false);
            new PsiRecursiveElementVisitor() {

                @Override
                public void visitElement(PsiElement element) {
                    if (element instanceof XmlAttribute) {
                        visitor.visitXmlAttribute((XmlAttribute) element);
                    } else {
                        super.visitElement(element);
                    }
                }
            }.visitFile(xmlFile);
            ProblemDescriptor[] results = holder.getResultsArray();
            List<ProblemDescriptor> list = ContainerUtil.filter(results, myCondition);
            Map<XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix, ProblemDescriptor> fixes = new LinkedHashMap<>();
            for (ProblemDescriptor result : list) {
                for (QuickFix fix : result.getFixes()) {
                    if (fix instanceof XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix) {
                        fixes.put((XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix) fix, result);
                    }
                }
            }
            SmartPsiElementPointer<XmlTag> pointer = null;
            for (Map.Entry<XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix, ProblemDescriptor> fix : fixes.entrySet()) {
                pointer = fix.getKey().doFix(project, fix.getValue(), false);
                myRemovedNameSpaces++;
            }
            if (pointer != null) {
                XmlUnusedNamespaceInspection.RemoveNamespaceDeclarationFix.reformatStartTag(project, pointer);
            }
        }

        @Nullable
        @Override
        public String getUserNotificationInfo() {
            return myRemovedNameSpaces > 0 ? "Removed " + myRemovedNameSpaces + " namespace" + (myRemovedNameSpaces > 1 ? "s" : "") : null;
        }
    };
}
Also used : QuickFix(com.intellij.codeInspection.QuickFix) XmlAttribute(com.intellij.psi.xml.XmlAttribute) XmlFile(com.intellij.psi.xml.XmlFile) HighlightDisplayKey(com.intellij.codeInsight.daemon.HighlightDisplayKey) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) XmlUnusedNamespaceInspection(com.intellij.codeInsight.daemon.impl.analysis.XmlUnusedNamespaceInspection) ProblemsHolder(com.intellij.codeInspection.ProblemsHolder) LinkedHashMap(java.util.LinkedHashMap) Project(com.intellij.openapi.project.Project) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) XmlTag(com.intellij.psi.xml.XmlTag) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)40 Project (com.intellij.openapi.project.Project)15 NotNull (org.jetbrains.annotations.NotNull)13 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)11 PsiElement (com.intellij.psi.PsiElement)10 Nullable (org.jetbrains.annotations.Nullable)8 CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)5 PsiAnnotation (com.intellij.psi.PsiAnnotation)5 LinkedList (java.util.LinkedList)5 InspectionManager (com.intellij.codeInspection.InspectionManager)4 PsiFile (com.intellij.psi.PsiFile)4 GlobalSearchScope (com.intellij.psi.search.GlobalSearchScope)4 QuickFix (com.intellij.codeInspection.QuickFix)3 PsiAnnotationMemberValue (com.intellij.psi.PsiAnnotationMemberValue)3 PsiClass (com.intellij.psi.PsiClass)3 PsiJavaCodeReferenceElement (com.intellij.psi.PsiJavaCodeReferenceElement)3 GroovyFix (org.jetbrains.plugins.groovy.codeInspection.GroovyFix)3 LocalInspectionToolWrapper (com.intellij.codeInspection.ex.LocalInspectionToolWrapper)2 ASTNode (com.intellij.lang.ASTNode)2 Editor (com.intellij.openapi.editor.Editor)2