This is the second blog post for “Tips of creating Intellij plugin”.
This entry is focus on tips for creating intellij plugin in Kotlin. (But I still believe it is useful for Java developers.)
For more basic tips, please read Tips for Creating Intellij Plugin
Topics
- How to find parent
- How to find children
- How to find files
How to find parent
Finding parent is just calling PsiElement#getParent()
. However, in most case, I want to find a specific type of parent. In that case, using extension methods in psiUtils.kt
is the best way to do it. psiUtils.kt provides variety of useful extension methods.
For instance, if I want to find the function that the element belong,
1
|
|
This method return null
if there is no applicable element. (In the example, if the element is not located inside a function, it will be null.)
For Java developers, use PsiTreeUtil.getParentOfType()
How to find children
After finding psiUtils.kt
, I was expected to find children using something like getStrictChildrenOfType
or something. Guess what. I was wrong. There are more useful methods in psiUtils.kt. The reason why it is not getStrictChildrenOfType
is I guess it requires to recursive tree visiting.
To find all children of specific types, use collectDescendantsOfType
1
|
|
To find existance of children of specific type, use anyDescendantOfType
1
|
|
To find the one child of specific type, use findDescendantOfType
1
|
|
How to find files
When I want to iterate though all resource files in my project, following one liner archive it
1
|
|
(Watch out .idea/
folder. It is also include as “Project” so this line also grabs all xml files in “.idea”.)
This method find all files in specific types of specific scope. The above line finds all xml files in my project.