Compare commits

...

1 Commits

Author SHA1 Message Date
Valentin Kipyatkov
4461928a23 Fixed reference search of implicitly imported top-level functions 2020-04-24 19:16:17 +03:00
5 changed files with 30 additions and 3 deletions

View File

@@ -15,6 +15,8 @@ import com.intellij.psi.search.UsageSearchContext
import org.jetbrains.kotlin.fileClasses.javaFileFacadeFqName
import org.jetbrains.kotlin.idea.KotlinFileType
import org.jetbrains.kotlin.idea.search.excludeFileTypes
import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.isSubpackageOf
import org.jetbrains.kotlin.psi.KtCallableDeclaration
import org.jetbrains.kotlin.psi.KtFile
@@ -26,10 +28,13 @@ class KotlinReferenceScopeOptimizer : ScopeOptimizer {
return null
}
// package "kotlin" and some of its subpackages may be imported by default
private val kotlinPackage = FqName.fromSegments(listOf("kotlin"))
private fun getRestrictedScopeForTopLevelCallable(callable: KtCallableDeclaration): GlobalSearchScope? {
val useScope = callable.useScope as? GlobalSearchScope ?: return null
val file = callable.parent as KtFile
val packageName = file.packageFqName.takeUnless { it.isRoot } ?: return null
val packageName = file.packageFqName.takeUnless { it.isRoot || it.isSubpackageOf(kotlinPackage) } ?: return null
val project = file.project
val cacheManager = CacheManager.SERVICE.getInstance(project)
@@ -37,7 +42,6 @@ class KotlinReferenceScopeOptimizer : ScopeOptimizer {
val javaScope = GlobalSearchScope.getScopeRestrictedByFileTypes(useScope, JavaFileType.INSTANCE)
val restScope = useScope.excludeFileTypes(KotlinFileType.INSTANCE, JavaFileType.INSTANCE) as GlobalSearchScope
//TODO: use all components of package name?
val shortPackageName = packageName.shortName().identifier
val kotlinFiles = cacheManager.getVirtualFilesWithWord(shortPackageName, UsageSearchContext.IN_CODE, kotlinScope, true)

View File

@@ -23,10 +23,13 @@ class KotlinReferenceScopeOptimizer : ScopeOptimizer {
return null
}
// package "kotlin" and some of its subpackages may be imported by default
private val kotlinPackage = FqName.fromSegments(listOf("kotlin"))
private fun getRestrictedScopeForTopLevelCallable(callable: KtCallableDeclaration): GlobalSearchScope? {
val useScope = callable.useScope as? GlobalSearchScope ?: return null
val file = callable.parent as KtFile
val packageName = file.packageFqName.takeUnless { it.isRoot } ?: return null
val packageName = file.packageFqName.takeUnless { it.isRoot || it.isSubpackageOf(kotlinPackage) } ?: return null
val project = file.project
val searchHelper = PsiSearchHelper.getInstance(project)

View File

@@ -0,0 +1,13 @@
// PSI_ELEMENT: org.jetbrains.kotlin.psi.KtNamedFunction
// OPTIONS: usages
package sample
fun foo(list: List<String>) {
val s = list.<caret>joinToString()
}
fun bar(list: List<Int>) {
val s = list.joinToString()
}

View File

@@ -0,0 +1,2 @@
Function call 10 val s = list.joinToString()
Function call 6 val s = list.joinToString()

View File

@@ -754,6 +754,11 @@ public class FindUsagesTestGenerated extends AbstractFindUsagesTest {
runTest("idea/testData/findUsages/kotlin/findFunctionUsages/objectExpressionMemberInTopLevel.0.kt");
}
@TestMetadata("stdlibExtensionUsages.0.kt")
public void testStdlibExtensionUsages() throws Exception {
runTest("idea/testData/findUsages/kotlin/findFunctionUsages/stdlibExtensionUsages.0.kt");
}
@TestMetadata("synthesizedFunction.0.kt")
public void testSynthesizedFunction() throws Exception {
runTest("idea/testData/findUsages/kotlin/findFunctionUsages/synthesizedFunction.0.kt");