Inspection with runForWholeFile=true: Clear existing problems
Hello,
I write a inspection which depends on multiple PsiElements which can be located somewhere in the PsiFile/PsiClass.
It is something similar like the FieldCanBeLocalInspection.
So my inspection returns true in com.intellij.codeInspection.LocalInspectionTool#runForWholeFile
Register problem is not my problem. The problem is that already registered problems are not cleared when necessary.
I try to explain my inspection and use-case a bit more:
My inspection calls ProblemsHolder.registerProblem for a specific PsiJavaToken element (a simple string literal element)
because the word 'test' does not exist in the whole file (PsiFile/PsiClass).
Now, the user edits somewhere in the same file a javadoc (PsiComment) and writes the word 'test'.
The already registered problems must now be cleared.
How I am doing that?
Both PsiElements can be located in completely different locations.
Thanks
Please sign in to leave a comment.
Please show your inspection's implementation. Did you also specify runForWholeFile=true in plugin.xml registration?
Hello,
No I have not specify runForWholeFile in the plugin.xml (seems also not possible since the property i not in com.intellij.codeInspection.InspectionEP).
Please find below a reproducer. This inspection will mark every java token with a warning.
Now type somehere the code the word 'test'. For exmaple in the file javadoc header.
The warings are not cleared.
Thanks
plugin.xml entry
Code
public class DemoInspection extends AbstractBaseJavaLocalInspectionTool {
@Override
public PsiElementVisitor buildVisitor( ProblemsHolder holder, boolean isOnTheFly ) {
return new JavaElementVisitor() {
public void visitClass( PsiClass aClass ) {
super.visitClass( aClass );
if ( aClass.getContainingFile().getText().toLowerCase( Locale.ROOT ).contains( "test" ) ) {
System.out.println( "File contains 'test'" );
return;
}
System.out.println( "Check file..." );
aClass.accept( new JavaRecursiveElementWalkingVisitor() {
@Override
public void visitJavaToken( final PsiJavaToken token ) {
super.visitJavaToken( token );
System.out.println( "Register problem" );
holder.registerProblem( token, "Demo Inspection: This token is reported because the file does not contain the literal 'test'", ProblemHighlightType.WARNING );
}
} );
}
};
}
@Override
public SuppressQuickFix[] getBatchSuppressActions( PsiElement element ) {
return SuppressQuickFix.EMPTY_ARRAY;
}
@Override
public boolean runForWholeFile() {
return true;
}
}
Thanks for the sample, reproduced. It works as expected after adding runForWholeFile="true" to <localInspection...> registration in plugin.xml. Investigating.