Hi guys! I am writing a Kotlin compiler plugin. I ...
# compiler
c
Hi guys! I am writing a Kotlin compiler plugin. I want to find all specific references and replace them at compile time. Is there an API that can help me get there quickly? At present, my idea is to use
IrGenerationExtension
to process all files, then access all expressions through
IrElementTransformer
, then find out the corresponding references and replace them, but I’m a little worried about its performance. I would be grateful if someone could give me a some advice! 🙏🏻
In addition, I don’t know whether it is appropriate to ask here. When I use
IrElementTransformer
to implement this, how should I highlight them in the IDEA? There is currently no IR in IDEA
m
use 
IrGenerationExtension
 to process all files, then access all expressions through 
IrElementTransformer
, then find out the corresponding references and replace them
Yup, that's how you do it, that's how the compiler does it either, many many times. As to performance, I do argue this is may not be the very most optimal way in general, but as for a usage in plugin you should not notice any problem (not even difference), unless you do many such passes.
how should I highlight them in the IDEA? There is currently no IR in IDEA
You can't. IR by it's principle has, nothing to do with IDE. What you do here is a backend plugin. There is also a concept of frontend plugins, which are under development and will be usable with the new FIR frontend, but AFAIK not soon. These, along with Kotlin IDEA plugin might allow to do that, but I don't know.
c
Thank you, I will try it.
You can’t. IR by it’s principle has, nothing to do with IDE.
So would it be better if I found all the calls in advance through the
AnalysisHandlerExtension
? In this way, it can share code with the idea, but it is a little difficult to turn
KtElement
into
IrElement
🥲