How to add import for my class with Kotlin ?
Answered
according to the doc https://plugins.jetbrains.com/docs/intellij/modifying-psi.html#whitespaces-and-imports but that works on Java source file. And i haven't find a kotlin code style manager to shorten my fully-qualified class. Any one can give some advise to approach it?
Please sign in to leave a comment.
https://intellij-support.jetbrains.com/hc/en-us/community/posts/360010025120-Add-new-parameter-into-kotlin-data-class-from-IDEA-plugin?page=1#community_comment_360002950760
Thanks for the reply. I have tried the ShortenReferences.DEFAULT.process() method, but that doesn't work.
My scenario is shorten a function's return value's FQN type. It looks something like:
" fun foo(): Observable<com.my.pkg.SomeType>". my type is inside a class-template(< >). I am afraid ShortenReferences does not deal that case, Maybe just shorten on a variable's type declaration. Now I have to add import for my type manually:(
David Smith hi! Can you please share how did you use ShortenReferences class? It should work in the case that you've described
Hi Roman, here is my code:
I know the target to shorten is a KtTypeRef, and I can't find a way to find it out, so i use ASTNode's finding way, at last i construct a KtTypeRef object.
This will most probably not work
You need to have your PSI in the place where it can be actually shortened - a full file with list of imports, for example. You create `KtTypeReference` in the air, it is not attached to the real file, so it will not be shortened
What I suggest doing is the following: insert FQN type to the original file, so it can be successfully resolved, and after that run `ShortenReferences.DEFAULT.process` on it. Could you give it a try?
Thanks for the suggestion. I took a light try: first time append new code to the file, then run second time, this time didn't write any, and find the PSI previously added, then use ShortenReferences.DEFAULT.process, but doesn't work as expected. Now I am working on other stuff. So newbie manually add imports as a workaround.