How to programmatically determine inspection results for a source file via plugin?
Hello,
I would like to be able to get the inspection results for a group of source files from within my plugin. I have tried looking in many of the *Inspection*, *Inspections*, *Problem*, and *Result* classes that appeared related.
I have created an algorithm (see below), but it is heavy and it doesn't appear to work correctly.
What is the preferred way to get the inspection results for a group of source files? Filtering by inspection level/severity would be a bonus.
My initial object is a CompileContext.
Thanks,
Todd
private static Optional<Navigatable> getInspectionError(
final @NotNull CompileContext compileContext,
final VirtualFile virtualFile)
{
final Project project = compileContext.getProject();
// Get PsiFile
final PsiFile psiFile = PsiManager.getInstance(project).findFile(virtualFile);
if (null == psiFile)
{
return Optional.empty();
}
// Get InspectionToolWrapper
final ProjectInspectionProfileManager projectInspectionProfileManager =
new ProjectInspectionProfileManager(project);
final InspectionProfile inspectionProfile =
projectInspectionProfileManager.getCurrentProfile();
final List<InspectionToolWrapper<?, ?>> toolWrappers =
inspectionProfile.getInspectionTools(psiFile);
// Get GlobalInspectionContext
final InspectionManager manager = InspectionManager.getInstance(project);
final GlobalInspectionContext inspectionContext = manager.createNewGlobalContext();
final Set<ProblemDescriptor> problemDescriptors = new HashSet<>();
for (final InspectionToolWrapper<?, ?> toolWrapper : toolWrappers)
{
if (!toolWrapper.getTool().isSuppressedFor(psiFile))
{
problemDescriptors.addAll(
InspectionEngine.runInspectionOnFile(psiFile, toolWrapper, inspectionContext));
}
}
for (final ProblemDescriptor problemDescriptor : problemDescriptors)
{
final ProblemHighlightType problemHighlightType = problemDescriptor.getHighlightType();
if (problemHighlightType == ProblemHighlightType.ERROR
|| problemHighlightType == ProblemHighlightType.GENERIC_ERROR
|| problemHighlightType == ProblemHighlightType.GENERIC_ERROR_OR_WARNING)
{
final ProblemGroup problemGroup =
problemDescriptor.getProblemGroup();
if (null == problemGroup)
{
return Optional.empty();
}
final String problemName = problemGroup.getProblemName();
if (null == problemName)
{
return Optional.empty();
}
return getNavigatable(project, problemName);
}
}
return Optional.empty();
}
Please sign in to leave a comment.
Could you please explain your usecase/scenario, from where and why do you need these results for?
Hello Yann Cebron,
Thanks for getting back to me. I am trying to identify all Inspections that return a pre-selected severity, such as Error. I would like to be able to get a list of those inspection results and the documents in which they occurred, so that I can put my own CompilerContext message in the Build view with the link to the inspection.
I would like to get the results from a document that has been subjected to inspection, whether that is a currently edited document or the result of a code analysis.
I also need a way to determine if a document has undergone inspection, so as to not waste too much time processing something I don't need.
I don't have a preferred way of collecting the data I need, though it seems to me that if I were able to register listeners to inspections, that might work, assuming I can get a list of all active inspections in a project. I understand that for BulkFileListeners that I have to check whether the event is for a document in the current project, so that might be similar.
I have had some limited success with a combination of MarkupModel, RangeHighlighter, and HighlightInfo on Documents, but it is somewhat sequence dependent, meaning background processes, such as inspections, need to be completed before I get valid information.
Is there a way to know when the background process of inspection has been completed?
I know I have asked more questions, but I hope I have given you enough information to help me out.
Thanks again for your time and assistance.
Sorry for delay. It seems it might be easier to use builtin EP com.intellij.openapi.compiler.util.InspectionValidator and provide logic from there what files and what inspections to run. Note that this has to enabled in Preferences | Build, Execution, Deployment | Compiler | Validation
Thanks, Yann Cebron. I'll look into that