Search in sources :

Example 31 with ProblemDescriptor

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

the class ProblemDescriptionNode method calculatePresentableName.

@NotNull
@Override
protected String calculatePresentableName() {
    CommonProblemDescriptor descriptor = getDescriptor();
    if (descriptor == null)
        return "";
    PsiElement element = descriptor instanceof ProblemDescriptor ? ((ProblemDescriptor) descriptor).getPsiElement() : null;
    return XmlStringUtil.stripHtml(ProblemDescriptorUtil.renderDescriptionMessage(descriptor, element, TRIM_AT_TREE_END));
}
Also used : CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 32 with ProblemDescriptor

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

the class SuppressableInspectionTreeNode method getSuppressContent.

@NotNull
public final Pair<PsiElement, CommonProblemDescriptor> getSuppressContent() {
    RefEntity refElement = getElement();
    CommonProblemDescriptor descriptor = getDescriptor();
    PsiElement element = descriptor instanceof ProblemDescriptor ? ((ProblemDescriptor) descriptor).getPsiElement() : refElement instanceof RefElement ? ((RefElement) refElement).getElement() : null;
    return Pair.create(element, descriptor);
}
Also used : RefElement(com.intellij.codeInspection.reference.RefElement) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) RefEntity(com.intellij.codeInspection.reference.RefEntity) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) ProblemDescriptor(com.intellij.codeInspection.ProblemDescriptor) PsiElement(com.intellij.psi.PsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 33 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 34 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 35 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)

Aggregations

ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)41 Project (com.intellij.openapi.project.Project)15 NotNull (org.jetbrains.annotations.NotNull)13 LocalQuickFix (com.intellij.codeInspection.LocalQuickFix)11 PsiElement (com.intellij.psi.PsiElement)11 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