From 3471667aa0f70c7ddf1bd2f43e9dd66f583e09c6 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Sat, 24 Oct 2015 11:03:48 +0300 Subject: [PATCH] Minor refactoring --- .../kotlin/resolve/scopes/utils/ScopeUtils.kt | 30 ++++++++++--------- .../kotlin/idea/util/ShortenReferences.kt | 5 ++-- .../ConflictingExtensionPropertyInspection.kt | 12 +++----- .../RedundantSamConstructorInspection.kt | 10 +++---- 4 files changed, 26 insertions(+), 31 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt index 58620699393..f00c9887bb1 100644 --- a/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt +++ b/compiler/frontend/src/org/jetbrains/kotlin/resolve/scopes/utils/ScopeUtils.kt @@ -121,11 +121,11 @@ private class LexicalToKtScopeAdapter(lexicalScope: LexicalScope): KtScope { override fun getClassifier(name: Name, location: LookupLocation) = lexicalScope.getClassifier(name, location) override fun getPackage(name: Name): PackageViewDescriptor? { - return lexicalScope.findFirstFromMeAndParent { (it as? ImportingScope)?.getPackage(name) } + return lexicalScope.findFirstFromImportingScopes { it.getPackage(name) } } override fun getProperties(name: Name, location: LookupLocation): Collection { - return lexicalScope.collectAllFromMeAndParent { (it as? ImportingScope)?.getDeclaredVariables(name, location) ?: emptyList() } + return lexicalScope.collectAllFromImportingScopes { it.getDeclaredVariables(name, location) } } override fun getFunctions(name: Name, location: LookupLocation): Collection { @@ -135,27 +135,19 @@ private class LexicalToKtScopeAdapter(lexicalScope: LexicalScope): KtScope { override fun getLocalVariable(name: Name) = lexicalScope.getLocalVariable(name) override fun getSyntheticExtensionProperties(receiverTypes: Collection, name: Name, location: LookupLocation): Collection { - return lexicalScope.collectAllFromMeAndParent { - (it as? ImportingScope)?.getSyntheticExtensionProperties(receiverTypes, name, location) ?: emptyList() - } + return lexicalScope.collectAllFromImportingScopes { it.getSyntheticExtensionProperties(receiverTypes, name, location) } } override fun getSyntheticExtensionFunctions(receiverTypes: Collection, name: Name, location: LookupLocation): Collection { - return lexicalScope.collectAllFromMeAndParent { - (it as? ImportingScope)?.getSyntheticExtensionFunctions(receiverTypes, name, location) ?: emptyList() - } + return lexicalScope.collectAllFromImportingScopes { it.getSyntheticExtensionFunctions(receiverTypes, name, location) } } override fun getSyntheticExtensionProperties(receiverTypes: Collection): Collection { - return lexicalScope.collectAllFromMeAndParent { - (it as? ImportingScope)?.getSyntheticExtensionProperties(receiverTypes) ?: emptyList() - } + return lexicalScope.collectAllFromImportingScopes { it.getSyntheticExtensionProperties(receiverTypes) } } override fun getSyntheticExtensionFunctions(receiverTypes: Collection): Collection { - return lexicalScope.collectAllFromMeAndParent { - (it as? ImportingScope)?.getSyntheticExtensionFunctions(receiverTypes) ?: emptyList() - } + return lexicalScope.collectAllFromImportingScopes { it.getSyntheticExtensionFunctions(receiverTypes) } } override fun getContainingDeclaration() = lexicalScope.ownerDescriptor @@ -271,6 +263,16 @@ inline fun LexicalScope.findFirstFromMeAndParent(fetch: (LexicalScope) return null } +inline fun LexicalScope.collectAllFromImportingScopes( + collect: (ImportingScope) -> Collection +): Collection { + return collectAllFromMeAndParent { if (it is ImportingScope) collect(it) else emptyList() } +} + +inline fun LexicalScope.findFirstFromImportingScopes(fetch: (ImportingScope) -> T?): T? { + return findFirstFromMeAndParent { if (it is ImportingScope) fetch(it) else null } +} + fun LexicalScope.addImportScope(importScope: KtScope): LexicalScope { if (this is ImportingScope) { return importScope.memberScopeAsImportingScope(this) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt index 956efe98672..2b0b6a39cf4 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/util/ShortenReferences.kt @@ -40,9 +40,8 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getCalleeExpressionIfAny import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.tasks.ExplicitReceiverKind import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode -import org.jetbrains.kotlin.resolve.scopes.ImportingScope import org.jetbrains.kotlin.resolve.scopes.receivers.ThisReceiver -import org.jetbrains.kotlin.resolve.scopes.utils.findFirstFromMeAndParent +import org.jetbrains.kotlin.resolve.scopes.utils.findFirstFromImportingScopes import org.jetbrains.kotlin.resolve.scopes.utils.getClassifier import java.util.* @@ -281,7 +280,7 @@ public class ShortenReferences(val options: (KtElement) -> Options = { Options.D val targetByName = if (target is ClassifierDescriptor) scope.getClassifier(name, NoLookupLocation.FROM_IDE) else - scope.findFirstFromMeAndParent { (it as? ImportingScope)?.getPackage(name) } + scope.findFirstFromImportingScopes { it.getPackage(name) } val canShortenNow = targetByName?.asString() == target.asString() processQualifiedElement(type, target, canShortenNow) diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/ConflictingExtensionPropertyInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/ConflictingExtensionPropertyInspection.kt index 5231ea31630..6d1fae753a6 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/ConflictingExtensionPropertyInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/ConflictingExtensionPropertyInspection.kt @@ -50,7 +50,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess import org.jetbrains.kotlin.resolve.isAnnotatedAsHidden import org.jetbrains.kotlin.resolve.scopes.ImportingScope -import org.jetbrains.kotlin.resolve.scopes.utils.collectAllFromMeAndParent +import org.jetbrains.kotlin.resolve.scopes.utils.collectAllFromImportingScopes import org.jetbrains.kotlin.synthetic.SyntheticJavaPropertyDescriptor import org.jetbrains.kotlin.utils.addToStdlib.firstIsInstanceOrNull @@ -89,13 +89,9 @@ public class ConflictingExtensionPropertyInspection : AbstractKotlinInspection() private fun conflictingSyntheticExtension(descriptor: PropertyDescriptor, importingScope: ImportingScope): SyntheticJavaPropertyDescriptor? { val extensionReceiverType = descriptor.extensionReceiverParameter?.type ?: return null - return importingScope - .collectAllFromMeAndParent { - (it as? ImportingScope) - ?.getSyntheticExtensionProperties(listOf(extensionReceiverType), descriptor.name, NoLookupLocation.FROM_IDE) - ?: emptyList() - } - .firstIsInstanceOrNull() + return importingScope.collectAllFromImportingScopes { + it.getSyntheticExtensionProperties(listOf(extensionReceiverType), descriptor.name, NoLookupLocation.FROM_IDE) + }.firstIsInstanceOrNull() } private fun isSameAsSynthetic(declaration: KtProperty, syntheticProperty: SyntheticJavaPropertyDescriptor): Boolean { diff --git a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSamConstructorInspection.kt b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSamConstructorInspection.kt index ef9dcdf097b..675d0d2e44d 100644 --- a/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSamConstructorInspection.kt +++ b/idea/src/org/jetbrains/kotlin/idea/inspections/RedundantSamConstructorInspection.kt @@ -41,8 +41,7 @@ import org.jetbrains.kotlin.resolve.calls.callUtil.getResolvedCall import org.jetbrains.kotlin.resolve.calls.model.isReallySuccess import org.jetbrains.kotlin.resolve.calls.util.DelegatingCall import org.jetbrains.kotlin.resolve.lazy.BodyResolveMode -import org.jetbrains.kotlin.resolve.scopes.ImportingScope -import org.jetbrains.kotlin.resolve.scopes.utils.collectAllFromMeAndParent +import org.jetbrains.kotlin.resolve.scopes.utils.collectAllFromImportingScopes import org.jetbrains.kotlin.synthetic.SamAdapterExtensionFunctionDescriptor import org.jetbrains.kotlin.types.TypeUtils import org.jetbrains.kotlin.utils.addToStdlib.check @@ -180,12 +179,11 @@ public class RedundantSamConstructorInspection : AbstractKotlinInspection() { // SAM adapters for member functions val resolutionScope = functionCall.getResolutionScope(bindingContext, functionCall.getResolutionFacade()) - val syntheticExtensions = resolutionScope.collectAllFromMeAndParent { - (it as? ImportingScope)?.getSyntheticExtensionFunctions( + val syntheticExtensions = resolutionScope.collectAllFromImportingScopes { + it.getSyntheticExtensionFunctions( containingClass.defaultType.singletonList(), functionResolvedCall.resultingDescriptor.name, - NoLookupLocation.FROM_IDE - ) ?: emptyList() + NoLookupLocation.FROM_IDE) } for (syntheticExtension in syntheticExtensions) { val samAdapter = syntheticExtension as? SamAdapterExtensionFunctionDescriptor ?: continue