In a Kotlin Multiplatform project, how would I find all PSI elements matching a Kotlin top-level property via its FQN? I’d like to use this function from a custom SMTestLocator.
This is what I’m currently using:
private fun topLevelProperties(
project: Project,
scope: GlobalSearchScope,
topLevelQualifiedName: String
): List<PsiLocation<PsiElement>> {
val packageName = topLevelQualifiedName.substringBeforeLast('.')
val simpleName = topLevelQualifiedName.substringAfterLast('.')
val psiElementsWithMatchingSimpleName: MutableList<PsiElement> = mutableListOf()
PsiSearchHelper.getInstance(project).processElementsWithWord(
{ element, _ ->
psiElementsWithMatchingSimpleName += element
true
},
scope,
simpleName,
UsageSearchContext.IN_CODE,
false // caseSensitive
)
return psiElementsWithMatchingSimpleName.mapNotNull {
if (it !is KtProperty) return@mapNotNull null
if ((it.parent as? KtFile)?.packageFqName?.asString() != packageName) return@mapNotNull null
PsiLocation(project, it)
}
}