Moved method

This commit is contained in:
Valentin Kipyatkov
2015-08-26 18:04:22 +03:00
parent 363295f6af
commit 6e6e80cc2a
2 changed files with 35 additions and 39 deletions
@@ -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>(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
@@ -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>(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,