Supporting both Kotlin and Java
I wrote a plugin that currently works with Kotlin files.
However the PSI elements that the plugin looks at (specific annotations, etc) can all be met in Java files as well.
If I want to have the same inspections / quick-fixes / etc apply to both Kotlin and Java files, do I essentially need to have twice the number of classes, or is there some mechanism of easing the process? I guess I could have everything accept PsiElement but that would be pretty bad in terms of type safety.
I've not seen this documented but it would be interesting to know how JetBrains tackle this issue. Do you treat Java and Kotlin as completely separate and don't bother with any of this?
Please sign in to leave a comment.
Kotlin and Java (and to some degree, Groovy) can be abstracted by using UAST-API. It is a cross-language facade that represents the common subset of the JVM languages. Central starting point is UElement, you can see a number of usages e.g. in Devkit Plugin inspections. See also https://youtrack.jetbrains.com/issue/IJSDK-540
Please feel free to ask further questions here.
This is great, did not know about UAST before. Thank you very much for the pointers, I'll play around with it!
Just rewrote the plugin using UAST. Everything went quite smoothly, but I have a couple of questions:
Actually not having access to nameIdentifier on UClass prompted me to write this utility method:
It might be useful to have it on UClass to avoid this.
1. Use ProblemHighlightType.GENERIC_ERROR_OR_WARNING in such case
2. Use PsiElement identifier = UElementKt.getSourcePsiElement(method.getUastAnchor());
> whether the inspection should only be used as a guide, or whether it telling you strictly means it is in fact possible to get what you need in some other way.
The second option. It should be possible to get what you need in another way, if it is not possible then it is our (JetBrains) inadvertency or maybe what you want couldn't be safely done in a language-abstract way.
Thanks very much for the info guys. I'll keep exploring the APIs to get rid of all inspection warnings :-)