Cleanup: apply "lift out..." inspection (+ some others)

This commit is contained in:
Mikhail Glukhikh
2017-06-28 14:30:52 +03:00
committed by Mikhail Glukhikh
parent 0c41ceea9d
commit 9c06739594
52 changed files with 294 additions and 311 deletions
@@ -332,20 +332,20 @@ class BasicCompletionSession(
if (callTypeAndReceiver.receiver == null && prefix.isNotEmpty()) {
val classKindFilter: ((ClassKind) -> Boolean)?
val includeTypeAliases: Boolean
when (callTypeAndReceiver) {
includeTypeAliases = when (callTypeAndReceiver) {
is CallTypeAndReceiver.ANNOTATION -> {
classKindFilter = { it == ClassKind.ANNOTATION_CLASS }
includeTypeAliases = true
true
}
is CallTypeAndReceiver.DEFAULT, is CallTypeAndReceiver.TYPE -> {
classKindFilter = { it != ClassKind.ENUM_ENTRY }
includeTypeAliases = true
true
}
else -> {
classKindFilter = null
includeTypeAliases = false
false
}
}
@@ -728,24 +728,24 @@ class BasicCompletionSession(
private fun referenceScope(declaration: KtNamedDeclaration): KtElement? {
val parent = declaration.parent
when (parent) {
is KtParameterList -> return parent.parent as KtElement
return when (parent) {
is KtParameterList -> parent.parent as KtElement
is KtClassBody -> {
val classOrObject = parent.parent as KtClassOrObject
if (classOrObject is KtObjectDeclaration && classOrObject.isCompanion()) {
return classOrObject.containingClassOrObject
classOrObject.containingClassOrObject
}
else {
return classOrObject
classOrObject
}
}
is KtFile -> return parent
is KtFile -> parent
is KtBlockExpression -> return parent
is KtBlockExpression -> parent
else -> return null
else -> null
}
}
@@ -138,30 +138,32 @@ class BasicLookupElementFactory(
}
val lookupObject: DeclarationLookupObject
val name: String
if (descriptor is ConstructorDescriptor) {
// for constructor use name and icon of containing class
val classifierDescriptor = descriptor.containingDeclaration
lookupObject = object : DeclarationLookupObjectImpl(descriptor) {
override val psiElement by lazy { DescriptorToSourceUtilsIde.getAnyDeclaration(project, classifierDescriptor) }
override fun getIcon(flags: Int) = KotlinDescriptorIconProvider.getIcon(classifierDescriptor, psiElement, flags)
val name: String = when (descriptor) {
is ConstructorDescriptor -> {
// for constructor use name and icon of containing class
val classifierDescriptor = descriptor.containingDeclaration
lookupObject = object : DeclarationLookupObjectImpl(descriptor) {
override val psiElement by lazy { DescriptorToSourceUtilsIde.getAnyDeclaration(project, classifierDescriptor) }
override fun getIcon(flags: Int) = KotlinDescriptorIconProvider.getIcon(classifierDescriptor, psiElement, flags)
}
classifierDescriptor.name.asString()
}
name = classifierDescriptor.name.asString()
}
else if (descriptor is SyntheticJavaPropertyDescriptor) {
lookupObject = object : DeclarationLookupObjectImpl(descriptor) {
override val psiElement by lazy { DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptor.getMethod) }
override fun getIcon(flags: Int) = KotlinDescriptorIconProvider.getIcon(descriptor, null, flags)
is SyntheticJavaPropertyDescriptor -> {
lookupObject = object : DeclarationLookupObjectImpl(descriptor) {
override val psiElement by lazy { DescriptorToSourceUtilsIde.getAnyDeclaration(project, descriptor.getMethod) }
override fun getIcon(flags: Int) = KotlinDescriptorIconProvider.getIcon(descriptor, null, flags)
}
descriptor.name.asString()
}
name = descriptor.name.asString()
}
else {
lookupObject = object : DeclarationLookupObjectImpl(descriptor) {
override val psiElement: PsiElement?
get() = declarationLazy
override fun getIcon(flags: Int) = KotlinDescriptorIconProvider.getIcon(descriptor, psiElement, flags)
else -> {
lookupObject = object : DeclarationLookupObjectImpl(descriptor) {
override val psiElement: PsiElement?
get() = declarationLazy
override fun getIcon(flags: Int) = KotlinDescriptorIconProvider.getIcon(descriptor, psiElement, flags)
}
descriptor.name.asString()
}
name = descriptor.name.asString()
}
var element = LookupElementBuilder.create(lookupObject, name)
@@ -237,11 +237,11 @@ abstract class CompletionSession(
sorter = sorter.weighAfter("stats", VariableOrFunctionWeigher, ImportedWeigher(importableFqNameClassifier))
val preferContextElementsWeigher = PreferContextElementsWeigher(inDescriptor)
if (callTypeAndReceiver is CallTypeAndReceiver.SUPER_MEMBERS) { // for completion after "super." strictly prefer the current member
sorter = sorter.weighBefore("kotlin.deprecated", preferContextElementsWeigher)
sorter = if (callTypeAndReceiver is CallTypeAndReceiver.SUPER_MEMBERS) { // for completion after "super." strictly prefer the current member
sorter.weighBefore("kotlin.deprecated", preferContextElementsWeigher)
}
else {
sorter = sorter.weighBefore("kotlin.proximity", preferContextElementsWeigher)
sorter.weighBefore("kotlin.proximity", preferContextElementsWeigher)
}
sorter = sorter.weighBefore("middleMatching", PreferMatchingItemWeigher)
@@ -59,11 +59,11 @@ tailrec fun <T : Any> LookupElement.putUserDataDeep(key: Key<T>, value: T?) {
}
tailrec fun <T : Any> LookupElement.getUserDataDeep(key: Key<T>): T? {
if (this is LookupElementDecorator<*>) {
return getDelegate().getUserDataDeep(key)
return if (this is LookupElementDecorator<*>) {
getDelegate().getUserDataDeep(key)
}
else {
return getUserData(key)
getUserData(key)
}
}
@@ -298,10 +298,10 @@ fun breakOrContinueExpressionItems(position: KtElement, breakOrContinue: String)
fun BasicLookupElementFactory.createLookupElementForType(type: KotlinType): LookupElement? {
if (type.isError) return null
if (type.isFunctionType) {
return if (type.isFunctionType) {
val text = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)
val baseLookupElement = LookupElementBuilder.create(text).withIcon(KotlinIcons.LAMBDA)
return BaseTypeLookupElement(type, baseLookupElement)
BaseTypeLookupElement(type, baseLookupElement)
}
else {
val classifier = type.constructor.declarationDescriptor ?: return null
@@ -317,7 +317,7 @@ fun BasicLookupElementFactory.createLookupElementForType(type: KotlinType): Look
}
// if type is simply classifier without anything else, use classifier's lookup element to avoid duplicates (works after "as" in basic completion)
return if (typeLookupElement.fullText == IdeDescriptorRenderers.SOURCE_CODE.renderClassifierName(classifier))
if (typeLookupElement.fullText == IdeDescriptorRenderers.SOURCE_CODE.renderClassifierName(classifier))
baseLookupElement
else
typeLookupElement