Hey everyone, Does anyone know if it's possible t...
# intellij
s
Hey everyone, Does anyone know if it's possible to leverage PSI/KtElement and IDEA to script refactoring? Arrow is migrating runtimes towards 2.x.x, and it involves changing some imports and add some imports depending on usage. The sources are compatible. If we can inspect usages through PSI, and add import accordingly, Arrow can offer users automatic migration scripts.
w
Structural Search & Replace operates on PSI I think, but it’s not simple and I’m not sure about replace facilities (for search you can write scripts). You can also leverage e.g. ktlint and its fixing API, and tbh it probably wouldn’t be too difficult to write your own small tool to go over the files, parse them and write the replacement, as long as you only need PSI
b
There's also LivePlugin and Flora to make "light plugins" that ca be shared, but I haven't used them, just know they exist 🙂 (plus they also need the user to install a plugin so it might not solve the issue of making this as simple as possible to the user)
s
@wasyl do you know of any examples I could look at?
tbh it probably wouldn’t be too difficult to write your own small tool to go over the files
Someone told me this should be possible from a KtScript, but I am unsure where to start. Al tools like Detekt, and KtLint have so much build around it requires some time to dig through it.
w
I can’t find a simple example, sorry 😕 Initialisation doesn’t seem that straighforward though, you’re right (looking at e.g. https://github.com/pinterest/ktlint/blob/master/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/KotlinPsiFileFactory.kt). I’d personally start with Ktlint — it’s a known tool in Kotlin ecosystem, and you can just let the users download a binary and run it with your custom rule set jar. It won’t be more complex than a fully custom tool. Or if you want a Kotlin script that the users can paste and execute, perhaps you can import Ktlint dependency and use its APIs ?
👍 1
s
Or if you want a Kotlin script that the users can paste and execute, perhaps you can import Ktlint dependency and use its APIs ?
🤔 That is probably my best bet then to make it as simple as possible.
👍 1
b
Please share your findings, as I'm interested in this too! 🙏
e
Using
@Deprecated
can add imports as quick fix resolution if you use the
ReplaceWith
properly.
Copy code
@Deprecated("message", ReplaceWith("new code", "import 1", "import 2"), level = DeprecationLevel.WARNING)
s
Sadly, it's not sufficient for what we need 😞
m
If you only need package migrations, you could maybe use IntelliJ IDEA's Migrate refactoring? https://www.jetbrains.com/help/idea/migrate.html
If you need more code changes, you may want to look into writing an OpenRewrite recipe for your users?https://docs.openrewrite.org/
b
I had the impression from another thread that OpenRewrite didn't support Kotlin?
m
Ah correct, not yet.
Afaik they are working on it
👍 1