From 6e6e80cc2a6a9078005ea3834f0a39f2bb0ad9d1 Mon Sep 17 00:00:00 2001 From: Valentin Kipyatkov Date: Wed, 26 Aug 2015 18:04:22 +0300 Subject: [PATCH] Moved method --- .../kotlin/idea/completion/CompletionUtils.kt | 40 +------------------ .../idea/completion/LookupElementFactory.kt | 34 +++++++++++++++- 2 files changed, 35 insertions(+), 39 deletions(-) diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionUtils.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionUtils.kt index f78a24ff6b5..30b71575c1c 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionUtils.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/CompletionUtils.kt @@ -23,13 +23,12 @@ import com.intellij.openapi.util.Key import com.intellij.patterns.ElementPattern import com.intellij.patterns.StandardPatterns import com.intellij.psi.PsiDocumentManager -import com.intellij.util.PlatformIcons import org.jetbrains.kotlin.builtins.KotlinBuiltIns -import org.jetbrains.kotlin.descriptors.* +import org.jetbrains.kotlin.descriptors.CallableDescriptor +import org.jetbrains.kotlin.descriptors.ReceiverParameterDescriptor import org.jetbrains.kotlin.idea.JetIcons import org.jetbrains.kotlin.idea.completion.handlers.CastReceiverInsertHandler import org.jetbrains.kotlin.idea.completion.handlers.WithTailInsertHandler -import org.jetbrains.kotlin.idea.resolve.ResolutionFacade import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers import org.jetbrains.kotlin.idea.util.ShortenReferences import org.jetbrains.kotlin.idea.util.findLabelAndCall @@ -40,8 +39,6 @@ import org.jetbrains.kotlin.psi.psiUtil.parentsWithSelf import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.renderer.render import org.jetbrains.kotlin.resolve.BindingContext -import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils -import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.inline.InlineUtil import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.typeUtil.TypeNullability @@ -293,39 +290,6 @@ fun breakOrContinueExpressionItems(position: JetElement, breakOrContinue: String return result } -fun LookupElementFactory.createBackingFieldLookupElement( - property: PropertyDescriptor, - useReceiverTypes: Boolean, - inDescriptor: DeclarationDescriptor, - resolutionFacade: ResolutionFacade -): LookupElement? { - val insideAccessor = inDescriptor is PropertyAccessorDescriptor && inDescriptor.getCorrespondingProperty() == property - if (!insideAccessor) { - val container = property.getContainingDeclaration() - if (container !is ClassDescriptor || !DescriptorUtils.isAncestor(container, inDescriptor, false)) return null // backing field not accessible - } - - val declaration = (DescriptorToSourceUtils.descriptorToDeclaration(property) as? JetProperty) ?: return null - - val accessors = declaration.getAccessors() - if (accessors.all { it.getBodyExpression() == null }) return null // makes no sense to access backing field - it's the same as accessing property directly - - val bindingContext = resolutionFacade.analyze(declaration) - if (!bindingContext[BindingContext.BACKING_FIELD_REQUIRED, property]!!) return null - - val lookupElement = createLookupElement(property, useReceiverTypes) - return object : LookupElementDecorator(lookupElement) { - override fun getLookupString() = "$" + super.getLookupString() - override fun getAllLookupStrings() = setOf(getLookupString()) - - override fun renderElement(presentation: LookupElementPresentation) { - super.renderElement(presentation) - presentation.setItemText("$" + presentation.getItemText()) - presentation.setIcon(PlatformIcons.FIELD_ICON) //TODO: special icon - } - }.assignPriority(ItemPriority.BACKING_FIELD) -} - fun LookupElementFactory.createLookupElementForType(type: JetType): LookupElement? { if (type.isError()) return null diff --git a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/LookupElementFactory.kt b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/LookupElementFactory.kt index a89a58800eb..998fa83fba1 100644 --- a/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/LookupElementFactory.kt +++ b/idea/idea-completion/src/org/jetbrains/kotlin/idea/completion/LookupElementFactory.kt @@ -36,8 +36,11 @@ import org.jetbrains.kotlin.idea.util.fuzzyReturnType import org.jetbrains.kotlin.load.java.JvmAbi import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name +import org.jetbrains.kotlin.psi.JetProperty import org.jetbrains.kotlin.psi.psiUtil.parents import org.jetbrains.kotlin.renderer.DescriptorRenderer +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils import org.jetbrains.kotlin.resolve.DescriptorUtils import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension import org.jetbrains.kotlin.synthetic.SamAdapterExtensionFunctionDescriptor @@ -91,7 +94,7 @@ public class LookupElementFactory( } if (descriptor is PropertyDescriptor && inDescriptor != null) { - var backingFieldElement = createBackingFieldLookupElement(descriptor, useReceiverTypes, inDescriptor, resolutionFacade) + var backingFieldElement = createBackingFieldLookupElement(descriptor, useReceiverTypes) if (backingFieldElement != null) { if (context == Context.STRING_TEMPLATE_AFTER_DOLLAR) { backingFieldElement = backingFieldElement.withBracesSurrounding() @@ -141,6 +144,35 @@ public class LookupElementFactory( return lookupElement } + private fun createBackingFieldLookupElement(property: PropertyDescriptor, useReceiverTypes: Boolean): LookupElement? { + if (inDescriptor == null) return null + val insideAccessor = inDescriptor is PropertyAccessorDescriptor && inDescriptor.getCorrespondingProperty() == property + if (!insideAccessor) { + val container = property.getContainingDeclaration() + if (container !is ClassDescriptor || !DescriptorUtils.isAncestor(container, inDescriptor, false)) return null // backing field not accessible + } + + val declaration = (DescriptorToSourceUtils.descriptorToDeclaration(property) as? JetProperty) ?: return null + + val accessors = declaration.getAccessors() + if (accessors.all { it.getBodyExpression() == null }) return null // makes no sense to access backing field - it's the same as accessing property directly + + val bindingContext = resolutionFacade.analyze(declaration) + if (!bindingContext[BindingContext.BACKING_FIELD_REQUIRED, property]!!) return null + + val lookupElement = createLookupElement(property, useReceiverTypes) + return object : LookupElementDecorator(lookupElement) { + override fun getLookupString() = "$" + super.getLookupString() + override fun getAllLookupStrings() = setOf(getLookupString()) + + override fun renderElement(presentation: LookupElementPresentation) { + super.renderElement(presentation) + presentation.setItemText("$" + presentation.getItemText()) + presentation.setIcon(PlatformIcons.FIELD_ICON) //TODO: special icon + } + }.assignPriority(ItemPriority.BACKING_FIELD) + } + public fun createLookupElement( descriptor: DeclarationDescriptor, useReceiverTypes: Boolean,