I know PSI seems to be used in Upsource and other ...
# intellij
m
I know PSI seems to be used in Upsource and other web apps, and inspections can be run from the command line, so PSI must be somewhat re-usable. Just wondering if there's any pre-canned examples or docs on this anywhere.
👀 1
e
I’m not 100% sure but kotlin #scripting uses PSI (possibly) outside of the context of IDE. Maybe a customised script would fulfil your requirements instead?
m
Afaik (but not 100% sure either), SQLdelight uses psi from a gradle plugin to generate kotlin code.
👍 1
w
I expect browsing KtLint and Detekt sources might be helpful as well.
👍 1
m
Is there maybe a lower API than PSI to parse Kotlin files? PSI sounds overkill if you "just" want to parse Kotlin files to an AST?
m
Well, PSI basically is an AST, just with lots of other features (when backed by the IDE engine)
👍 1
But yeah if you just want the semantics, not sure. There's a so-called "light AST"
Thanks both, those were great suggestions.
They do indeed both use PSI, seems like by embedding (a) parts of the IntelliJ JARs or (b) the Kotlin "embeddable" compiler. Now, I wonder if this is the sort of PSI that can edit code as well as parse it ...
w
@mikehearn It is, in KtLint there’s an option to autocorrect some violations. For example I can see
<http://org.jetbrains.kotlin.com|org.jetbrains.kotlin.com>.intellij.psi.PsiElement.astReplace(newElement: PsiElement)
signature
m
Great.
Very helpful. Thanks all.
m
I've been looking at that today. Both the embeddable compiler and intellij jars are ~50MB (~150MB uncompressed...) 😕
Ah! I just bumped into https://github.com/kotlinx/ast that seems to be a way lighter dependency
Although both of them seem to focus on reading, not writing