How Can I find KtClass at project scope by providing class's qualified name? Follow
Answered
I am seeking for a best way to find a KtClass by class (written by Kotlin) qualified name.
I have tried these code, but it same like not work in my expect.
fun String.findKtClass(project: Project): KtClass? {
// `this` is class qualified name
// like com.example.config.Config
val facade = JavaPsiFacade.getInstance(project)
val scope = GlobalSearchScope.allScope(project)
val psiClass = facade.findClass(this, scope)
return psiClass as? KtClass
}
When calling the code, I find that facade.findClass() return a KtUltraLightClass, Not KtClass
Is there any best way to find a class written by Kotlin in project scope? Thank you!
Please sign in to leave a comment.
Sorry for not describe the problem clearly:
For this data class:
How can I get the type canonicalName of KtParameter, which is declared in primary constructor?
And How can I get all super types (in this case it is AbstractBehavior and IConfig) corresponding KtClass?
Thank you and have a nice day :)
You can use PsiViewer to find out about underlying PSI Structure and then use provided PSI's method to obtain such information,
see https://plugins.jetbrains.com/docs/intellij/explore-api.html#31-use-internal-mode-and-psiviewer
If you plan on supporting not only Kotlin, consider using UAST instead of targeting Kotlin PSI only https://plugins.jetbrains.com/docs/intellij/uast.html
Yann Cebron Thank for your helpful answers!
I have installed IDEA Plugin: PsiViewer to find out about current selected file. But I have no idea to view the structure of PsiFile(in java) and KtFile(in Kotlin)'s parent PsiElement structure.
By viewing the structure of PsiFile's parent structure. I will be able to searching a class by given qualified name. How can I view the structure of a PsiFile' parent and PsiFile's parent's parent recursively?
Thank for your reading and answers. Have a nice day :)
Not sure I understand your question, if you want to locate a class by FQN use com.intellij.psi.JavaPsiFacade#findClass and specify resolve scope
Thank you Yann Cebron. Your answer is helpful for me. I use com.intellij.psi.JavaPsiFacade#findClass and it work for me.