diff --git a/sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightReferenceContributor.kt b/sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightReferenceContributor.kt index 56b13ef320d..8576be4b789 100644 --- a/sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightReferenceContributor.kt +++ b/sqldelight-idea-plugin/src/main/kotlin/app/cash/sqldelight/intellij/SqlDelightReferenceContributor.kt @@ -64,7 +64,7 @@ internal class SqlDelightReferenceContributor : PsiReferenceContributor() { val scope = module.getModuleWithDependenciesAndLibrariesScope(false) val classNameIndex = KotlinFullClassNameIndex.getInstance() val ktClass = { classNameIndex[qName, project, scope].firstOrNull() } - val typeAliasFqNameIndex = KotlinTopLevelTypeAliasFqNameIndex.getInstance() + val typeAliasFqNameIndex = getKotlinTopLevelTypeAliasFqNameIndex() val typeAlias = { typeAliasFqNameIndex[qName, project, scope].firstOrNull() } val javaPsiFacade = JavaPsiFacade.getInstance(project) val javaClass = { javaPsiFacade.findClass(qName, scope) } @@ -74,3 +74,17 @@ internal class SqlDelightReferenceContributor : PsiReferenceContributor() { private fun typeForThisPackage(file: SqlDelightFile) = "${file.packageName}.${element.text}" } } + +private fun getKotlinTopLevelTypeAliasFqNameIndex(): KotlinTopLevelTypeAliasFqNameIndex { + // read the INSTANCE variable reflectively first (newer Kotlin plugins) + try { + val instanceField = KotlinTopLevelTypeAliasFqNameIndex::class.java.getField("INSTANCE") + val instance = instanceField.get(null) + if (instance is KotlinTopLevelTypeAliasFqNameIndex) { + return instance + } + } catch (e: Exception) { + /* intentionally empty, fall back to getInstance() call in case of errors */ + } + return KotlinTopLevelTypeAliasFqNameIndex.getInstance() +}