Search in sources :

Example 26 with CommonProblemDescriptor

use of com.intellij.codeInspection.CommonProblemDescriptor 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 27 with CommonProblemDescriptor

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

the class ProblemPreviewEditorPresentation method appendFoldings.

private void appendFoldings(CommonProblemDescriptor[] descriptors) {
    List<UsageInfo> usages = Arrays.stream(descriptors).filter(ProblemDescriptorBase.class::isInstance).map(ProblemDescriptorBase.class::cast).map(d -> {
        final PsiElement psi = d.getPsiElement();
        if (psi == null) {
            return null;
        }
        final TextRange range = d.getTextRangeInElement();
        return range == null ? new UsageInfo(psi) : new UsageInfo(psi, range.getStartOffset(), range.getEndOffset());
    }).collect(Collectors.toList());
    boolean isUpdated = false;
    for (UsageInfo usage : usages) {
        if (usage == null) {
            return;
        }
        isUpdated |= appendFoldings(usage.getSegment());
    }
    if (isUpdated) {
        updateFoldings();
    }
    List<UsageInfo> validUsages = usages.stream().filter(Objects::nonNull).collect(Collectors.toList());
    PsiDocumentManager.getInstance(myView.getProject()).performLaterWhenAllCommitted(() -> {
        if (!myEditor.isDisposed()) {
            myView.invalidate();
            myView.validate();
            UsagePreviewPanel.highlight(validUsages, myEditor, myView.getProject(), false, HighlighterLayer.SELECTION);
            if (validUsages.size() == 1) {
                final PsiElement element = validUsages.get(0).getElement();
                if (element != null) {
                    final DocumentEx document = myEditor.getDocument();
                    final int offset = Math.min(element.getTextRange().getEndOffset() + VIEW_ADDITIONAL_OFFSET, document.getLineEndOffset(document.getLineNumber(element.getTextRange().getEndOffset())));
                    myEditor.getScrollingModel().scrollTo(myEditor.offsetToLogicalPosition(offset), ScrollType.CENTER);
                    return;
                }
            }
            myEditor.getScrollingModel().scrollTo(myEditor.offsetToLogicalPosition(0), ScrollType.CENTER_UP);
        }
    });
}
Also used : ProblemDescriptorBase(com.intellij.codeInspection.ProblemDescriptorBase) java.util(java.util) ProblemDescriptorBase(com.intellij.codeInspection.ProblemDescriptorBase) FoldRegion(com.intellij.openapi.editor.FoldRegion) DiffDrawUtil(com.intellij.diff.util.DiffDrawUtil) UsageInfo(com.intellij.usageView.UsageInfo) TextRange(com.intellij.openapi.util.TextRange) CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) Collectors(java.util.stream.Collectors) ScrollType(com.intellij.openapi.editor.ScrollType) HighlighterLayer(com.intellij.openapi.editor.markup.HighlighterLayer) FoldingModelSupport(com.intellij.diff.tools.util.FoldingModelSupport) PsiElement(com.intellij.psi.PsiElement) UsagePreviewPanel(com.intellij.usages.impl.UsagePreviewPanel) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) EditorEx(com.intellij.openapi.editor.ex.EditorEx) Segment(com.intellij.openapi.util.Segment) Logger(com.intellij.openapi.diagnostic.Logger) PsiDocumentManager(com.intellij.psi.PsiDocumentManager) DocumentEx(com.intellij.openapi.editor.ex.DocumentEx) TextRange(com.intellij.openapi.util.TextRange) PsiElement(com.intellij.psi.PsiElement) UsageInfo(com.intellij.usageView.UsageInfo)

Example 28 with CommonProblemDescriptor

use of com.intellij.codeInspection.CommonProblemDescriptor 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 29 with CommonProblemDescriptor

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

the class FieldCanBeMovedToSubclassInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefField)) {
        return null;
    }
    final RefField refField = (RefField) refEntity;
    final PsiField field = refField.getElement();
    if (field == null) {
        return null;
    }
    final PsiType type = field.getType();
    if (!type.equals(PsiType.BOOLEAN)) {
        return null;
    }
    final RefClass fieldClass = refField.getOwnerClass();
    final Collection<RefElement> inReferences = refField.getInReferences();
    final RefJavaUtil refUtil = RefJavaUtil.getInstance();
    final Set<RefClass> classesUsed = new HashSet<>();
    for (RefElement inReference : inReferences) {
        final RefClass referringClass = refUtil.getOwnerClass(inReference);
        if (referringClass == null) {
            return null;
        }
        if (referringClass.equals(fieldClass)) {
            return null;
        }
        classesUsed.add(referringClass);
        if (classesUsed.size() > 1) {
            return null;
        }
    }
    if (classesUsed.size() != 1) {
        return null;
    }
    final RefClass referencingClass = classesUsed.iterator().next();
    //TODO: check that referencing class is a subclass of the field class
    final String errorString = "Field " + refEntity.getName() + " is only accessed in subclass " + referencingClass.getName();
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiField(com.intellij.psi.PsiField) PsiType(com.intellij.psi.PsiType) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Example 30 with CommonProblemDescriptor

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

the class StaticFieldCanBeMovedToUseInspection method checkElement.

@Override
@Nullable
public CommonProblemDescriptor[] checkElement(@NotNull RefEntity refEntity, @NotNull AnalysisScope analysisScope, @NotNull InspectionManager inspectionManager, @NotNull GlobalInspectionContext globalInspectionContext) {
    if (!(refEntity instanceof RefField)) {
        return null;
    }
    final RefField refField = (RefField) refEntity;
    final PsiField field = refField.getElement();
    if (field == null) {
        return null;
    }
    final PsiType type = field.getType();
    if (!type.equals(PsiType.BOOLEAN)) {
        return null;
    }
    final RefClass fieldClass = refField.getOwnerClass();
    final Collection<RefElement> inReferences = refField.getInReferences();
    final RefJavaUtil refUtil = RefJavaUtil.getInstance();
    final Set<RefClass> classesUsed = new HashSet<>();
    for (RefElement inReference : inReferences) {
        final RefClass referringClass = refUtil.getOwnerClass(inReference);
        if (referringClass == null) {
            return null;
        }
        if (referringClass.equals(fieldClass)) {
            return null;
        }
        classesUsed.add(referringClass);
        if (classesUsed.size() > 1) {
            return null;
        }
    }
    if (classesUsed.size() != 1) {
        return null;
    }
    final RefClass referencingClass = classesUsed.iterator().next();
    final String errorString = "Static field " + refEntity.getName() + " is only accessed in subclass " + referencingClass.getName();
    return new CommonProblemDescriptor[] { inspectionManager.createProblemDescriptor(errorString) };
}
Also used : CommonProblemDescriptor(com.intellij.codeInspection.CommonProblemDescriptor) PsiField(com.intellij.psi.PsiField) PsiType(com.intellij.psi.PsiType) HashSet(java.util.HashSet) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

CommonProblemDescriptor (com.intellij.codeInspection.CommonProblemDescriptor)30 Nullable (org.jetbrains.annotations.Nullable)20 RefClass (com.intellij.codeInspection.reference.RefClass)13 RefEntity (com.intellij.codeInspection.reference.RefEntity)12 RefPackage (com.intellij.codeInspection.reference.RefPackage)9 PsiClass (com.intellij.psi.PsiClass)9 PsiElement (com.intellij.psi.PsiElement)7 HashSet (java.util.HashSet)6 ProblemDescriptor (com.intellij.codeInspection.ProblemDescriptor)5 RefModule (com.intellij.codeInspection.reference.RefModule)4 PsiIdentifier (com.intellij.psi.PsiIdentifier)4 NotNull (org.jetbrains.annotations.NotNull)4 QuickFix (com.intellij.codeInspection.QuickFix)2 Project (com.intellij.openapi.project.Project)2 TextRange (com.intellij.openapi.util.TextRange)2 PsiField (com.intellij.psi.PsiField)2 PsiType (com.intellij.psi.PsiType)2 TreePath (javax.swing.tree.TreePath)2 BatchQuickFix (com.intellij.codeInspection.BatchQuickFix)1 ProblemDescriptorBase (com.intellij.codeInspection.ProblemDescriptorBase)1