This is continuation of post Scala macros part II- macro annotations where we’ve implemented @TalkingAnimalSpell macro annotation. As a reminder: This macro annotation add new method before compilation to annotated class, and this method was invisible for Intellij Idea because its coding assistance is based on static code analysis and it is not aware of AST changes. In this post we will try to create plugin to solve this problem. You will need Intellij Idea 15. Code as always is on my Github:
In October 2015, Jetbrains team published this post. According to this blog let’s clone example repo for building intellij idea macros plugins.
And open Injector.scala:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
Building plugin which will support your macro annotations is about creating class which extends SyntheticMembersInjector class and implementing one of SyntheticMembersInjector functions:
If you want to add methods to any class, object or trait you will have to implement this method:
def injectFunctions(source: ScTypeDefinition): Seq[String]
We will implement this method later for our plugin, and example implementation you have also in this cloned from Intellij sbt-plugin-example project.
If you would like to custom inner class or object to any class, object or trait you would have to implement this method:
def injectInners(source: ScTypeDefinition): Seq[String]
Returning types for those 2 methods is Seq[String], it is just collection of texts which will appear on code completion menu when you will operate on proper object.
SyntheticMembersInjector has also third method:
def needsCompanionObject(source: ScTypeDefinition): Boolean = false
And according to sources: “Use this method to mark class or trait, that it requires companion object.”
Actually, using this api for building macro support reminds me writing macros. Look, argument of, for example, injectFunction is weird ScTypeDefinition, so it sounds a little bit like one of the c.Tree subclasses. Then you’ve got ScValue, we can check if it has some annotations, so we are operating, not on Tree types of course, but some types provided by IntelliJ but they are just some code tokens. So let’s look how to use this api for out purposes.
III. Plugin for @TalkingAnimalSpell
This is our implementation of Injector:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
I hope that this code is pretty straightforward. If source, is class and has annotation called “TalkingAnimalSpell” then return seq with text corresponding to sayHello method signature. And that’s all :)
Now inside your plugin directory run:
This command will create .jar with your plugin. Go to Idea settings, Plugins section, then click on install plugin from disc, and choose this .jar, restart Idea, open project from part II, and you should have this effect:
IV. Fun with the IntelliJ API
I encourage you to play this api a little bit, check what methods are available and what they do. For example we can slightly change behavior of this plugin and provide code completion only when annotated class extends proper Animal trait. Possible solution for this would be:
1 2 3 4 5 6 7
In this blog series I tried to make some gentle introduction to scala macros. In Javeo I used them to build library which reduced huge amount of boilerplate code. You will find code of Gridzzly(That’s the name of our beast) on Javeo’s github, I will update repo in few days. The last missing factor was Intellij idea support for macros, but now nothing can stop you from writing your own library using scala macros ;) By the way, Gridzzly is also Macro Annotation so you probably want plugin for it ;) It’s here.
Thank you for reading this, feel free to comment and… have a great day!