Obtain Inspection output for a given file

Hello everyone,

For the past few days I have been browsing and prototyping with the Inspection API, but to no avail. I am looking for an API to, given a file, obtain all Inspections that IntelliJ shows. In other words, unused imports, unused variables, etc...

I have tried several things thus far, with mixed results:

The following initialization code is available:

final InspectionManagerEx instance = (InspectionManagerEx) InspectionManager.getInstance(project);
GlobalInspectionContextImpl context = (GlobalInspectionContextImpl) instance.createNewGlobalContext(true);
final List<InspectionToolWrapper> inspectionToolWrappers = InspectionToolRegistrar.getInstance().get();
PsiFile file = PsiDocumentManager.getInstance(project).getPsiFile(editor.getDocument()).getOriginalFile();

I run all code snippets below in a block of the following:

ProgressManager.getInstance().runProcess(() -> {
DumbServiceImpl.getInstance(project).runReadActionInSmartMode(() -> {
<snippets here>
});
}, new EmptyProgressIndicator());

My first attempt was to retrieve the Inspections presentations from the context, however this always returns an empty list.

final List<CommonProblemDescriptor> problems = inspectionToolWrappers.stream()
.flatMap(wrapper -> {
final InspectionToolPresentation presentationOrNull = context.getPresentationOrNull(wrapper);
if (presentationOrNull == null) {
return Stream.empty();
}
return presentationOrNull.getProblemDescriptors().stream();
})
.collect(Collectors.toList());
System.out.println(problems.toString());

Then I tried to the code analyzer on the file, but again an empty list:

final DaemonCodeAnalyzerImpl codeAnalyzer = (DaemonCodeAnalyzerImpl) DaemonCodeAnalyzerImpl.getInstance(project);
final List<HighlightInfo> highlightInfos = codeAnalyzer.runMainPasses(file, editor.getDocument(), progressIndicator);
System.out.println(highlightInfos);

Then I invoked the LocalInspectionsPass directly, again an empty list:

 

final LocalInspectionsPass localInspectionsPass = new LocalInspectionsPass(file, editor.getDocument(), 0, editor.getDocument().getTextLength(), file.getTextRange(), true, HighlightInfoProcessor.getEmpty());
System.out.println(localInspectionsPass.getInfos());

My last attempt does return a list, but there are two issues: 1. It is extremely slow and locks up the UI Thread. Putting the logic in a separate Thread throws error of invalid access. 2. It does not seem to return information about the unused import and I do not understand how it maps to the yellow highlighting my editor actually shows. (In the file I tests, I have 2 yellow highlighted errors and 1 unused import, but my list returns like 10 issues)

final List<ProblemDescriptor> problems = inspectionToolWrappers.stream()
.flatMap(wrapper -> InspectionEngine.runInspectionOnFile(file, wrapper, context).stream())
.collect(Collectors.toList());
System.out.println(problems.toString());

The file I am testing the plugin on. Expected highlighted parts are the `if` and the `!!false` and the `import java.util.List` is grayed out.

import java.util.List;

public class Foo {

public static void main(String[] args) {
if (!!false) {

}
}
}

All in all, my issue seems rather trivial. After indexing and calculation of Inspections (at the moment they are shown in the UI), I would like to obtain these Inspections and the corresponding information.

Could someone help me out pointing to the correct API endpoint to obtain this information?

2 comments
Comment actions Permalink

In all your attempts you run the inspections yourself, I guess it's not what you want. On the other hand you may retrieve already collected data from the model, like:

final MarkupModel markup = DocumentMarkupModel.forDocument(document, project, true);//get markup model by document

RangeHighlighter[] allHighlighters = markup.getAllHighlighters(); //all highlight of the model

for (RangeHighlighter highlighter : allHighlighters) {

if (!highlighter.isValid()) continue;

Object tooltip = highlighter.getErrorStripeTooltip();

if (!(tooltip instanceof HighlightInfo)) {

continue;

}

final HighlightInfo info = (HighlightInfo)tooltip; //contains level of the highlighting (HighlightInfoType), ranges, fixes, etc

}

Please ensure that you don't leak the model and that you update the view you want to build according to the changes in the document (there are corresponding listeners)

Anna

 

0
Comment actions Permalink

Dear Anna,

Thank you for your response! I did not work during the Christmas vacation, thus my delayed answer.

I just tried it out and it produces the output I expect, much appreciated!

Cheers,

Tim

0

Please sign in to leave a comment.