default -> companion: replace all mentions of default and default object

This commit is contained in:
Pavel V. Talanov
2015-03-16 14:56:06 +03:00
parent a0783757e8
commit 06916d98c6
1019 changed files with 2468 additions and 2469 deletions
@@ -389,7 +389,7 @@ usageType.type.definition = Type definition
usageType.is = Target type of 'is' operation
usageType.as = Target type of 'as' operation
usageType.class.object = Nested class/object
usageType.default.object = Default object
usageType.companion.object = Companion object
usageType.instantiation = Instantiation
usageType.function.call = Function call
usageType.implicit.get = Implicit 'get'
@@ -66,7 +66,7 @@ private class ClassClsStubBuilder(
supertypeIds
}
}
private val defaultObjectName = if (classProto.hasDefaultObjectName()) c.nameResolver.getName(classProto.getDefaultObjectName()) else null
private val companionObjectName = if (classProto.hasCompanionObjectName()) c.nameResolver.getName(classProto.getCompanionObjectName()) else null
private val classOrObjectStub = createClassOrObjectStubAndModifierListStub()
@@ -94,14 +94,14 @@ private class ClassClsStubBuilder(
val additionalModifiers = when (classKind) {
ProtoBuf.Class.Kind.ENUM_CLASS -> listOf(JetTokens.ENUM_KEYWORD)
ProtoBuf.Class.Kind.ANNOTATION_CLASS -> listOf(JetTokens.ANNOTATION_KEYWORD)
ProtoBuf.Class.Kind.CLASS_OBJECT -> listOf(JetTokens.DEFAULT_KEYWORD)
ProtoBuf.Class.Kind.CLASS_OBJECT -> listOf(JetTokens.COMPANION_KEYWORD)
else -> listOf<JetModifierKeywordToken>()
}
return createModifierListStubForDeclaration(parent, classProto.getFlags(), relevantFlags, additionalModifiers)
}
private fun doCreateClassOrObjectStub(): StubElement<out PsiElement> {
val isDefaultObject = classKind == ProtoBuf.Class.Kind.CLASS_OBJECT
val isCompanionObject = classKind == ProtoBuf.Class.Kind.CLASS_OBJECT
val fqName = outerContext.containerFqName.child(classId.getRelativeClassName().shortName())
val shortName = fqName.shortName()?.ref()
val superTypeRefs = supertypeIds.filter {
@@ -113,7 +113,7 @@ private class ClassClsStubBuilder(
KotlinObjectStubImpl(
parentStub, shortName, fqName, superTypeRefs,
isTopLevel = !classId.isNestedClass(),
isDefault = isDefaultObject,
isDefault = isCompanionObject,
isLocal = false,
isObjectLiteral = false
)
@@ -164,19 +164,19 @@ private class ClassClsStubBuilder(
private fun createClassBodyAndMemberStubs() {
val classBody = KotlinPlaceHolderStubImpl<JetClassBody>(classOrObjectStub, JetStubElementTypes.CLASS_BODY)
createDefaultObjectStub(classBody)
createCompanionObjectStub(classBody)
createEnumEntryStubs(classBody)
createCallableMemberStubs(classBody)
createInnerAndNestedClasses(classBody)
}
private fun createDefaultObjectStub(classBody: KotlinPlaceHolderStubImpl<JetClassBody>) {
if (defaultObjectName == null) {
private fun createCompanionObjectStub(classBody: KotlinPlaceHolderStubImpl<JetClassBody>) {
if (companionObjectName == null) {
return
}
val defaultObjectId = classId.createNestedClassId(defaultObjectName)
createNestedClassStub(classBody, defaultObjectId)
val companionObjectId = classId.createNestedClassId(companionObjectName)
createNestedClassStub(classBody, companionObjectId)
}
private fun createEnumEntryStubs(classBody: KotlinPlaceHolderStubImpl<JetClassBody>) {
@@ -213,7 +213,7 @@ private class ClassClsStubBuilder(
private fun createInnerAndNestedClasses(classBody: KotlinPlaceHolderStubImpl<JetClassBody>) {
classProto.getNestedClassNameList().forEach { id ->
val nestedClassName = c.nameResolver.getName(id)
if (nestedClassName != defaultObjectName) {
if (nestedClassName != companionObjectName) {
val nestedClassId = classId.createNestedClassId(nestedClassName)
createNestedClassStub(classBody, nestedClassId)
}
@@ -210,7 +210,7 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
}
val typeConstraintListStub = KotlinPlaceHolderStubImpl<JetTypeConstraintList>(parent, JetStubElementTypes.TYPE_CONSTRAINT_LIST)
for ((name, type) in protosForTypeConstraintList) {
val typeConstraintStub = KotlinTypeConstraintStubImpl(typeConstraintListStub, isDefaultObjectConstraint = false)
val typeConstraintStub = KotlinTypeConstraintStubImpl(typeConstraintListStub, isCompanionObjectConstraint = false)
KotlinNameReferenceExpressionStubImpl(typeConstraintStub, name.ref())
createTypeReferenceStub(typeConstraintStub, type)
}
@@ -149,18 +149,18 @@ private fun buildDecompiledText(packageFqName: FqName, descriptors: List<Declara
builder.append(" {\n")
var firstPassed = false
val subindent = indent + " "
val defaultObject = descriptor.getDefaultObjectDescriptor()
if (defaultObject != null) {
val companionObject = descriptor.getCompanionObjectDescriptor()
if (companionObject != null) {
firstPassed = true
builder.append(subindent)
appendDescriptor(defaultObject, subindent)
appendDescriptor(companionObject, subindent)
}
val allDescriptors = descriptor.secondaryConstructors + descriptor.getDefaultType().getMemberScope().getDescriptors()
for (member in allDescriptors) {
if (member.getContainingDeclaration() != descriptor) {
continue
}
if (member == defaultObject) {
if (member == companionObject) {
continue
}
if (member is CallableMemberDescriptor
@@ -209,8 +209,8 @@ public object UsageTypeUtils {
return when (descriptor) {
is ClassifierDescriptor -> when {
// Treat object accesses as variables to simulate the old behaviour (when variables were created for objects)
DescriptorUtils.isNonDefaultObject(descriptor), DescriptorUtils.isEnumEntry(descriptor) -> getVariableUsageType()
DescriptorUtils.isDefaultObject(descriptor) -> DEFAULT_OBJECT_ACCESS
DescriptorUtils.isNonCompanionObject(descriptor), DescriptorUtils.isEnumEntry(descriptor) -> getVariableUsageType()
DescriptorUtils.isCompanionObject(descriptor) -> COMPANION_OBJECT_ACCESS
else -> getClassUsageType()
}
is PackageViewDescriptor -> {
@@ -232,7 +232,7 @@ enum class UsageTypeEnum {
TYPE_DEFINITION
IS
CLASS_OBJECT_ACCESS
DEFAULT_OBJECT_ACCESS
COMPANION_OBJECT_ACCESS
EXTENSION_RECEIVER_TYPE
SUPER_TYPE_QUALIFIER
@@ -99,7 +99,7 @@ private object DeclarationKindDetector : JetVisitor<AnnotationHostKind?, Unit?>(
override fun visitParameter(d: JetParameter, _: Unit?) = detect(d, "parameter", newLineNeeded = false)
override fun visitObjectDeclaration(d: JetObjectDeclaration, _: Unit?): AnnotationHostKind? {
if (d.isDefault()) return detect(d, "default object", name = "${d.getName()} of ${d.getStrictParentOfType<JetClass>()?.getName()}")
if (d.isCompanion()) return detect(d, "companion object", name = "${d.getName()} of ${d.getStrictParentOfType<JetClass>()?.getName()}")
if (d.getParent() is JetObjectLiteralExpression) return null
return detect(d, "object")
}
@@ -51,12 +51,12 @@ public fun PsiReference.matchesTarget(candidateTarget: PsiElement): Boolean {
if (this is JetReference) {
return targets.any {
it is PsiMethod && it.isConstructor() && it.getContainingClass() == unwrappedCandidate
|| it is JetObjectDeclaration && it.isDefault() && it.getNonStrictParentOfType<JetClass>() == unwrappedCandidate
|| it is JetObjectDeclaration && it.isCompanion() && it.getNonStrictParentOfType<JetClass>() == unwrappedCandidate
}
}
if (this is PsiReferenceExpression && candidateTarget is JetObjectDeclaration && unwrappedTargets.size() == 1) {
val referredClass = unwrappedTargets.first()
if (referredClass is JetClass && candidateTarget in referredClass.getDefaultObjects()) {
if (referredClass is JetClass && candidateTarget in referredClass.getCompanionObjects()) {
val parentReference = getParent().getReference()
if (parentReference != null) {
return parentReference.unwrappedTargets.any {
@@ -45,15 +45,15 @@ public class KotlinReferencesSearcher extends QueryExecutorBase<PsiReference, Re
if (lightClass != null) {
searchNamedElement(queryParameters, lightClass, className);
if (element instanceof JetObjectDeclaration && ((JetObjectDeclaration) element).isDefault()) {
PsiField fieldForDefaultObject = ApplicationManager.getApplication().runReadAction(new Computable<PsiField>() {
if (element instanceof JetObjectDeclaration && ((JetObjectDeclaration) element).isCompanion()) {
PsiField fieldForCompanionObject = ApplicationManager.getApplication().runReadAction(new Computable<PsiField>() {
@Override
public PsiField compute() {
return LightClassUtil.getLightFieldForDefaultObject(element);
return LightClassUtil.getLightFieldForCompanionObject(element);
}
});
if (fieldForDefaultObject != null) {
searchNamedElement(queryParameters, fieldForDefaultObject);
if (fieldForCompanionObject != null) {
searchNamedElement(queryParameters, fieldForCompanionObject);
}
}
}
@@ -63,7 +63,7 @@ fun PsiNamedElement.getAccessorNames(readable: Boolean = true, writable: Boolean
return Collections.emptyList()
}
public fun PsiNamedElement.getClassNameForDefaultObject(): String? {
public fun PsiNamedElement.getClassNameForCompanionObject(): String? {
return if (this is JetObjectDeclaration) {
getNonStrictParentOfType<JetClass>()?.getName()
} else {
@@ -105,7 +105,7 @@ public abstract class UsagesSearchHelper<T : PsiNamedElement> {
protected open fun makeWordList(target: UsagesSearchTarget<T>): List<String> {
return with(target.element) {
getName().singletonOrEmptyList() + getAccessorNames() + getClassNameForDefaultObject().singletonOrEmptyList() + getSpecialNamesToSearch()
getName().singletonOrEmptyList() + getAccessorNames() + getClassNameForCompanionObject().singletonOrEmptyList() + getSpecialNamesToSearch()
}
}
@@ -61,9 +61,9 @@ public fun JetType.canBeReferencedViaImport(): Boolean {
return descriptor != null && descriptor.canBeReferencedViaImport()
}
// for cases when class qualifier refers default object treats it like reference to class itself
// for cases when class qualifier refers companion object treats it like reference to class itself
public fun JetReferenceExpression.getImportableTargets(bindingContext: BindingContext): Collection<DeclarationDescriptor> {
val targets = bindingContext[BindingContext.SHORT_REFERENCE_TO_DEFAULT_OBJECT, this]?.let { listOf(it) }
val targets = bindingContext[BindingContext.SHORT_REFERENCE_TO_COMPANION_OBJECT, this]?.let { listOf(it) }
?: bindingContext[BindingContext.REFERENCE_TARGET, this]?.let { listOf(it) }
?: bindingContext[BindingContext.AMBIGUOUS_REFERENCE_TARGET, this]
?: listOf()
@@ -150,7 +150,7 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options.
ShortenTypesVisitor(file, elementFilter, failedToImportDescriptors),
ShortenThisExpressionsVisitor(file, elementFilter, failedToImportDescriptors),
ShortenQualifiedExpressionsVisitor(file, elementFilter, failedToImportDescriptors),
RemoveExplicitDefaultObjectReferenceVisitor(file, elementFilter, failedToImportDescriptors)
RemoveExplicitCompanionObjectReferenceVisitor(file, elementFilter, failedToImportDescriptors)
)
val descriptorsToImport = visitors.flatMap { analyzeReferences(elementsToUse, it) }.toSet()
visitors.forEach { it.shortenElements(elementsToUse) }
@@ -409,7 +409,7 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options.
}
}
private class RemoveExplicitDefaultObjectReferenceVisitor(
private class RemoveExplicitCompanionObjectReferenceVisitor(
file: JetFile,
elementFilter: (PsiElement) -> FilterResult,
failedToImportDescriptors: Set<DeclarationDescriptor>
@@ -434,7 +434,7 @@ public class ShortenReferences(val options: (JetElement) -> Options = { Options.
val selectorExpression = qualifiedExpression.getSelectorExpression() ?: return false
val selectorTarget = selectorExpression.singleTarget(bindingContext) ?: return false
if (receiverTarget.getDefaultObjectDescriptor() != selectorTarget) return false
if (receiverTarget.getCompanionObjectDescriptor() != selectorTarget) return false
val selectorsSelector = (qualifiedExpression.getParent() as? JetDotQualifiedExpression)?.getSelectorExpression()
if (selectorsSelector == null) {