nemi
12/23/2020, 8:14 AMinteface Greeting {
fun greet()
}
Would be transformed into a class replacing the interface:
class Greeting {
fun geet() {
println("Hello, World!")
}
}
When trying to figure out whether I can to this and mostly how, I looked at [arrow-meta](https://github.com/arrow-kt/arrow-meta). I see they accomplish replacing/renaming pieces of kotlin code in a AnalysisHandlerExtension
.
They also build their own AST and have their own writer to write the modified tree into a file.
So I though instead of relying on arrow-meta I thought I'd use the PSI api to copy, modify and get the text of modified files.
However it seems that in the compiler some pieces of PSI is not available. Most importantly when I try to delete and element from the tree I get the following exception:
e: java.lang.NullPointerException
at org.jetbrains.kotlin.com.intellij.psi.impl.source.codeStyle.CodeEditUtil.saveWhitespacesInfo(CodeEditUtil.java:122)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.codeStyle.CodeEditUtil.removeChildren(CodeEditUtil.java:134)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.codeStyle.CodeEditUtil.removeChild(CodeEditUtil.java:49)
at org.jetbrains.kotlin.com.intellij.psi.impl.source.tree.CompositeElement.deleteChildInternal(CompositeElement.java:451)
at org.jetbrains.kotlin.com.intellij.extapi.psi.ASTDelegatePsiElement.deleteChildInternal(ASTDelegatePsiElement.java:346)
at org.jetbrains.kotlin.com.intellij.extapi.psi.ASTDelegatePsiElement.delete(ASTDelegatePsiElement.java:330)
Looking at CodeEditUtil
I know that the NullPointerException
is raised in this part
public static void saveWhitespacesInfo(ASTNode first) {
...
setOldIndentation((TreeElement)first, IndentHelper.getInstance().getIndent(file, first));
}
Because IdentnHelper.getInstance()
returns null.
public abstract class IndentHelper {
public static IndentHelper getInstance() {
return ApplicationManager.getApplication().getService(IndentHelper.class);
}
...
}
And indeed trying to get IndentHelper
as service returns null in my plugin.
So now I'm wondering if it is possible to install an instance if this service out side of the IDE to be able to manipulate PSI in a kotlin compiler plugin.
Alternatively I'd be also grateful is someone could provide me with the highlights on an alternative approach to achieve my goal.dmitriy.novozhilov
12/23/2020, 8:20 AMraulraja
12/25/2020, 10:56 AMGreeting()
with the wrong error messages and potentially wrong suggestions too.
We hope that once FIR has some kind of compiler hooks for what metaprogramming looks like, it includes the builtin IDE support so compiler plugin authors are not forced to write and market IDEA plugins to get meta-programming support in Kotlin.nemi
01/11/2021, 12:28 AMnemi
01/11/2021, 6:34 AMIn your example the Kotlin plugin would still think that is an interface and not a class and would red line use site calls for@raulraja yes this definitely would be a PITA, however in my case it is not an issue, as these classes would never be instantiated directly. And I do not need to reflect the changes in the IDE either 🤔 so what meta is doing right now cloud for my use casewith the wrong error messages and potentially wrong suggestions too.Greeting()
raulraja
01/11/2021, 9:00 AM