Finding a Kotlin top-level property by FQN

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)
    }
}

Found it: There is KotlinTopLevelPropertyFqnNameIndex.

Suggestion for improving the documentation: In Indexing and PSI Stubs, don’t delve into implementation details like “indexing framework”, “file-based” and “stub” indexes right away. Please start with what is relevant for a plugin developer, like:


The IDE provides built-in indexes to quickly locate PSI elements in large codebases. Widely used indexes include:

INDEX_NAME KEYS VALUES

(Please provide a relevant number of indexes, so that folks can easily see what’s there.)

You can explore existing indexes with the Index Viewer plugin.

In addition, you can create your own indexes.


Then proceed with the implementation details.

Please also provide usage information for the Index Viewer plugin. There is none, and it is not obvious that it provides just another tool window.