Minor refactoring

This commit is contained in:
Valentin Kipyatkov
2015-10-24 11:03:48 +03:00
parent 6252fb04c4
commit 3471667aa0
4 changed files with 26 additions and 31 deletions
@@ -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<VariableDescriptor> {
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<FunctionDescriptor> {
@@ -135,27 +135,19 @@ private class LexicalToKtScopeAdapter(lexicalScope: LexicalScope): KtScope {
override fun getLocalVariable(name: Name) = lexicalScope.getLocalVariable(name)
override fun getSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<PropertyDescriptor> {
return lexicalScope.collectAllFromMeAndParent {
(it as? ImportingScope)?.getSyntheticExtensionProperties(receiverTypes, name, location) ?: emptyList()
}
return lexicalScope.collectAllFromImportingScopes { it.getSyntheticExtensionProperties(receiverTypes, name, location) }
}
override fun getSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>, name: Name, location: LookupLocation): Collection<FunctionDescriptor> {
return lexicalScope.collectAllFromMeAndParent {
(it as? ImportingScope)?.getSyntheticExtensionFunctions(receiverTypes, name, location) ?: emptyList()
}
return lexicalScope.collectAllFromImportingScopes { it.getSyntheticExtensionFunctions(receiverTypes, name, location) }
}
override fun getSyntheticExtensionProperties(receiverTypes: Collection<KotlinType>): Collection<PropertyDescriptor> {
return lexicalScope.collectAllFromMeAndParent {
(it as? ImportingScope)?.getSyntheticExtensionProperties(receiverTypes) ?: emptyList()
}
return lexicalScope.collectAllFromImportingScopes { it.getSyntheticExtensionProperties(receiverTypes) }
}
override fun getSyntheticExtensionFunctions(receiverTypes: Collection<KotlinType>): Collection<FunctionDescriptor> {
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 <T: Any> LexicalScope.findFirstFromMeAndParent(fetch: (LexicalScope)
return null
}
inline fun <T: Any> LexicalScope.collectAllFromImportingScopes(
collect: (ImportingScope) -> Collection<T>
): Collection<T> {
return collectAllFromMeAndParent { if (it is ImportingScope) collect(it) else emptyList() }
}
inline fun <T: Any> 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)
@@ -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)
@@ -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<SyntheticJavaPropertyDescriptor>()
return importingScope.collectAllFromImportingScopes {
it.getSyntheticExtensionProperties(listOf(extensionReceiverType), descriptor.name, NoLookupLocation.FROM_IDE)
}.firstIsInstanceOrNull<SyntheticJavaPropertyDescriptor>()
}
private fun isSameAsSynthetic(declaration: KtProperty, syntheticProperty: SyntheticJavaPropertyDescriptor): Boolean {
@@ -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