How to get the last known code analysis state (contains errors or not) on a specified file?

Answered

Hello, 

I am using the IntelliJ Open API to do some analyses on PSI Elements and would like to know if a specific .java file is currently "compileable" (does not contain any errors, no red underlines). 

Is there an API event which is triggered when the validation of a file is completed by the IDE or some kind of property where I can get the last known state of the file without restarting the validation?

Things I have tried:

  • CodeSmellDetector

I want to avoid calling the CodeSmellDetector as it seems to re-analyze the file but actually I'm just interested in the last known state of the file. I don't want to redo the validation if it is not necessary.

List<VirtualFile> files = new ArrayList<>();
List<CodeSmellInfo> infos = CodeSmellDetector.getInstance(currentProject).findCodeSmells(files);

for (CodeSmellInfo codeSmellInfo : infos) {
if (codeSmellInfo.getSeverity() == HighlightSeverity.ERROR) {
// PSI Tree in file is invalid
break;
}
}
  • com.intellij.psi.PsiTreeChangeListener

By checking if an PsiErrorElement has been added, I can partially determine the last state of the file but it isn't as accurate as I hoped it would be.

PsiElement element = event.getChild();

if(element instanceof PsiErrorElement){
// PSI Tree in file is invalid
}
0
1 comment

See com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx#processHighlights(), most simple case is com.intellij.codeInsight.daemon.impl.DaemonCodeAnalyzerEx#hasErrors

0

Please sign in to leave a comment.