Scala compiler plugin support in IDE
I'd like to know, if I write a compiler plugin that extends the codebase, will the IDE recognize what the compiler plugin generates?
I'll add the plugin using SBT configuration adding a scalac compiler option to use the compiler plugin jar.
Please sign in to leave a comment.
Hello!
By default IntelliJ IDEA will not recognise the generated code.
So if you e.g. add some new declarations or change existing code, IntelliJ IDEA won't detect it.
If you wrote a library which has some macro, you could write a library extension and bundle it with the library.
This “library extension” provides IntelliJ some hints which allows it recognising e.g. dynamically-added methods.
https://github.com/JetBrains/intellij-scala/wiki/Library-Extensions
But AFAIU it won't meet your requirements as you don't have a library.
So, the alternative could be writing a IntelliJ IDEA plugin based on Scala Plugin:
https://github.com/JetBrains/sbt-idea-example
You could use the same/similar classes mentioned in the page above.
For the examples you can look into inheritors of `org.jetbrains.plugins.scala.lang.psi.impl.toplevel.typedef.SyntheticMembersInjector`
(e.g. `org.jetbrains.plugins.scala.lang.psi.impl.toplevel.typedef.QuasiQuotesInjector`).
If you decide to go this way, please join our discord:
https://discord.com/channels/931170831139217469/931171260824707072
Hi! Thanks for looking into this.
I can try the mentioned solutions, though I'm wondering if there is an easier one that could work.
AFAIK IDEA supports SBT's unmanaged sources. What about making my compiler plugin not just generate additions to the AST but serving as a code generator targeting the unmanaged sources directory?
The IDE will recognize those files right away if I'm right.
Ah, maybe I misunderstood your intention originally.
If the plugin just generates extra sources in the unmanaged sources directory IntelliJ can pick them.
Though you first need to invoke source generation from sbt (usually `sbt unmanagedSources` or something like that)
Then, during next project reload SBT will report those directories to IntelliJ
The implementation details are volatile, it will most likely end up in a combination of the ideas we have put together.
I'll check how SBT internals work in terms of notifying IntelliJ about changes in generated source directories to further streamline the process, as it would be the most convenient if the compiler plugin could do this notification step.