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

6 comments
Comment actions Permalink

Register com.intellij.openapi.compiler.CompileTask as "after" task (see Javadoc), then check parameter com.intellij.openapi.compiler.CompileContext#getMessageCount for zero warnings/errors.

0
Comment actions Permalink

Thanks, looks like this is the thing I need,

However I've encountered some problems during implementation

I added 

<compiler.task execute="AFTER" implementation="xxx.CheckCompilePassTask"/>

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?

0
Comment actions Permalink

Did you try Build->Rebuild Project?

0
Comment actions Permalink

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)

0
Comment actions Permalink

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?

0
Comment actions Permalink

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.

0

Please sign in to leave a comment.