Listen to code changes that compiles.
Answered
Hi,
Is there a way to listen to code changes that can successfully compile?
I am using `DocumentListener` and `PsiTreeChangeListener` to listen to either text changes or AST-level changes.
But how I may know the compilation status for the current changes since I only want to respond to code changes that successfully compile?
Many thanks
Yanze Li
Please sign in to leave a comment.
Register com.intellij.openapi.compiler.CompileTask as "after" task (see Javadoc), then check parameter com.intellij.openapi.compiler.CompileContext#getMessageCount for zero warnings/errors.
Thanks, looks like this is the thing I need,
However I've encountered some problems during implementation
I added
to my Plugin.xml.
And I wrote a simple implementation for it
public class CheckCompilePassTask implements CompileTask {
public static int errorCount = 0;
@Override
public boolean execute(CompileContext context) {
errorCount = context.getMessageCount(CompilerMessageCategory.ERROR);
return true;
}
}
Those are the instructions provided in Javadoc.
But the `execute` method seems never to be called, `CheckCompilePassTask.errorCount` is always 0.
Can you give me some hints on any mistake I could have made?
Did you try Build->Rebuild Project?
The CompileTask now works, but I find it not exactly does what I want.
Seems like the CompileTask is only executed after a "Build" process.
Is there a way to check the error count from the Analysis result? (like the picture below)
Sorry I didn't make myself clear in the first place,
What I want is to check if my code is syntactically and semantically correct from IntelliJ's code inspection, every time after a change is made.
Is it possible?
When IDEA finds an error/clears syntax error in the file it calls
`com.intellij.problems.ProblemListener`
you can also query
`com.intellij.problems.WolfTheProblemSolver#isProblemFile` which return true if the file contains syntax errors.