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));
}
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);
}
});
}
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);
}
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) };
}
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) };
}
Aggregations