diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/KotlinShortNamesCache.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/KotlinShortNamesCache.kt index a419c8aca4f..60d70dc8b4d 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/KotlinShortNamesCache.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/caches/KotlinShortNamesCache.kt @@ -25,6 +25,7 @@ import com.intellij.psi.PsiMethod import com.intellij.psi.search.GlobalSearchScope import com.intellij.psi.search.PsiShortNamesCache import com.intellij.psi.stubs.StubIndex +import com.intellij.util.ArrayUtil import com.intellij.util.Processor import com.intellij.util.Processors import com.intellij.util.containers.ContainerUtil @@ -61,9 +62,9 @@ class KotlinShortNamesCache(private val project: Project) : PsiShortNamesCache() * Return kotlin class names from project sources which should be visible from java. */ override fun getAllClassNames(): Array { - return CancelableArrayCollectProcessor().also { processor -> + return withArrayProcessor(ArrayUtil.EMPTY_STRING_ARRAY) { processor -> processAllClassNames(processor) - }.toArray(arrayOf()) + } } override fun processClassesWithName( @@ -119,9 +120,9 @@ class KotlinShortNamesCache(private val project: Project) : PsiShortNamesCache() * Return class names form kotlin sources in given scope which should be visible as Java classes. */ override fun getClassesByName(name: String, scope: GlobalSearchScope): Array { - return CancelableArrayCollectProcessor().also { processor -> + return withArrayProcessor(PsiClass.EMPTY_ARRAY) { processor -> processClassesWithName(name, processor, scope, null) - }.toArray(arrayOf()) + } } private fun kotlinDeclarationsVisibleFromJavaScope(scope: GlobalSearchScope): GlobalSearchScope { @@ -142,9 +143,9 @@ class KotlinShortNamesCache(private val project: Project) : PsiShortNamesCache() } override fun getAllMethodNames(): Array { - return CancelableArrayCollectProcessor().also { processor -> + return withArrayProcessor(ArrayUtil.EMPTY_STRING_ARRAY) { processor -> processAllMethodNames(processor) - }.toArray(arrayOf()) + } } private fun processAllMethodNames(processor: Processor): Boolean { @@ -208,15 +209,15 @@ class KotlinShortNamesCache(private val project: Project) : PsiShortNamesCache() } override fun getMethodsByName(name: String, scope: GlobalSearchScope): Array { - return CancelableArrayCollectProcessor().also { processor -> + return withArrayProcessor(PsiMethod.EMPTY_ARRAY) { processor -> processMethodsWithName(name, processor, scope, null) - }.toArray(arrayOf()) + } } override fun getMethodsByNameIfNotMoreThan(name: String, scope: GlobalSearchScope, maxCount: Int): Array { require(maxCount >= 0) - return CancelableArrayCollectProcessor().also { processor -> + return withArrayProcessor(PsiMethod.EMPTY_ARRAY) { processor -> processMethodsWithName( name, { psiMethod -> @@ -225,7 +226,7 @@ class KotlinShortNamesCache(private val project: Project) : PsiShortNamesCache() scope, null ) - }.toArray(PsiMethod.EMPTY_ARRAY) + } } override fun processMethodsWithName(name: String, scope: GlobalSearchScope, processor: Processor): Boolean = @@ -239,9 +240,9 @@ class KotlinShortNamesCache(private val project: Project) : PsiShortNamesCache() } override fun getAllFieldNames(): Array { - return CancelableArrayCollectProcessor().also { processor -> + return withArrayProcessor(ArrayUtil.EMPTY_STRING_ARRAY) { processor -> processAllFieldNames(processor) - }.toArray(arrayOf()) + } } private fun processAllFieldNames(processor: Processor): Boolean { @@ -270,15 +271,15 @@ class KotlinShortNamesCache(private val project: Project) : PsiShortNamesCache() } override fun getFieldsByName(name: String, scope: GlobalSearchScope): Array { - return CancelableArrayCollectProcessor().also { processor -> + return withArrayProcessor(PsiField.EMPTY_ARRAY) { processor -> processFieldsWithName(name, processor, scope, null) - }.toArray(arrayOf()) + } } override fun getFieldsByNameIfNotMoreThan(name: String, scope: GlobalSearchScope, maxCount: Int): Array { require(maxCount >= 0) - return CancelableArrayCollectProcessor().also { processor -> + return withArrayProcessor(PsiField.EMPTY_ARRAY) { processor -> processFieldsWithName( name, { psiField -> @@ -287,10 +288,19 @@ class KotlinShortNamesCache(private val project: Project) : PsiShortNamesCache() scope, null ) - }.toArray(PsiField.EMPTY_ARRAY) + } } //endregion + private inline fun withArrayProcessor( + result: Array, + process: (CancelableArrayCollectProcessor) -> Unit + ): Array { + return CancelableArrayCollectProcessor().also { processor -> + process(processor) + }.toArray(result) + } + private class CancelableArrayCollectProcessor : Processor { val troveSet = ContainerUtil.newTroveSet() private val processor = Processors.cancelableCollectProcessor(troveSet)