Creating a Compiler Error from a plugin

Answered

I am trying to write a plugin that creates more errors for the Java compiler. For example, I want a red lightbulb error to appear given this code:

int x = (½) * base * height;

I know there is already a yellow warning for this code, but I want it to show as an error instead and I want to add other errors as well. I want to stop the code from compiling if these errors are present in the code, but it currently is not working. I am using an annotator to highlight the error in the code, and it is showing up as a red error in the editor, but it is still allowing the project to compile. 

Is there any way to prevent the project from building given these custom errors in my plugin?

1
5 comments

Hi,

Please clarify whether you mean cancelling the build on the IDE side, or on the Java compiler side (another process).

0

Hello!

I would prefer it to be on the Java compiler side so the errors print to the screen like regular errors in Java. However, either way would work.

Thank you!

1

Hi,

Please try using com.intellij.openapi.compiler.CompilerManager.addBeforeTask() and com.intellij.openapi.compiler.CompileTask.execute().

The compiler task should perform PSI analysis to find the errors you expect the compilation to break.

0

Hello,

I know very little about PSI analysis. Could you please provide some tips or resources to help me figure out how to implement that solution?

I greatly appreciate all of the help!!!

0

PSI analysis is what you did in your annotator.

In case of using a compile before task, I suggest analyzing PsiFile to find errors you want to fail the compilation for.

You can try adding a compiler task on a project open:

CompilerManager compilerManager = CompilerManager.getInstance(myProject);
if (compilerManager != null) {
  compilerManager.addBeforeTask(new MyPsiAnalysisPrecompileTask(myProject));
}

You run this code on project open: https://plugins.jetbrains.com/docs/intellij/plugin-components.html#project-open

The task should inspect PsiFiles in the project and fail the compilation if there are any errors.

PSI docs:

0

Please sign in to leave a comment.