Minor code refactoring

This commit is contained in:
Valentin Kipyatkov
2015-05-14 20:35:49 +03:00
parent 7641c3cb84
commit c8b1dd6425
10 changed files with 48 additions and 59 deletions
@@ -142,10 +142,10 @@ abstract class CompletionSessionBase(protected val configuration: CompletionSess
receiverTypes = receiverTypes.map { it.makeNotNullable() }
}
LookupElementFactory(receiverTypes)
LookupElementFactory(resolutionFacade, receiverTypes)
}
else {
LookupElementFactory(emptyList())
LookupElementFactory(resolutionFacade, emptyList())
}
}
@@ -358,7 +358,7 @@ fun LookupElementFactory.createBackingFieldLookupElement(
val bindingContext = resolutionFacade.analyze(declaration)
if (!bindingContext[BindingContext.BACKING_FIELD_REQUIRED, property]) return null
val lookupElement = createLookupElement(resolutionFacade, property, true)
val lookupElement = createLookupElement(property, true)
return object : LookupElementDecorator<LookupElement>(lookupElement) {
override fun getLookupString() = "$" + super.getLookupString()
override fun getAllLookupStrings() = setOf(getLookupString())
@@ -20,7 +20,6 @@ import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.codeInsight.lookup.LookupElementDecorator
import com.intellij.patterns.ElementPattern
import com.intellij.patterns.PlatformPatterns.psiElement
import com.intellij.patterns.StandardPatterns
import com.intellij.util.ProcessingContext
@@ -78,14 +77,14 @@ class KDocNameCompletionSession(parameters: CompletionParameters,
.filter { it.getName().asString() !in documentedParameters }
descriptors.forEach {
resultSet.addElement(lookupElementFactory.createLookupElement(resolutionFacade, it, false))
resultSet.addElement(lookupElementFactory.createLookupElement(it, false))
}
}
private fun addLinkCompletions(declarationDescriptor: DeclarationDescriptor) {
val scope = getResolutionScope(resolutionFacade, declarationDescriptor)
scope.getDescriptors(nameFilter = prefixMatcher.asNameFilter()).forEach {
val element = lookupElementFactory.createLookupElement(resolutionFacade, it, false)
val element = lookupElementFactory.createLookupElement(it, false)
resultSet.addElement(object: LookupElementDecorator<LookupElement>(element) {
override fun handleInsert(context: InsertionContext?) {
// insert only plain name here, no qualifier/parentheses/etc.
@@ -17,34 +17,30 @@
package org.jetbrains.kotlin.idea.completion
import com.intellij.codeInsight.completion.InsertHandler
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.codeInsight.lookup.LookupElementBuilder
import com.intellij.codeInsight.lookup.*
import com.intellij.codeInsight.lookup.impl.LookupCellRenderer
import com.intellij.openapi.util.Iconable
import com.intellij.psi.PsiClass
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.asJava.KotlinLightClass
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.JetDescriptorIconProvider
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.completion.handlers.*
import org.jetbrains.kotlin.idea.util.TypeNullability
import org.jetbrains.kotlin.idea.util.nullability
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.idea.JetDescriptorIconProvider
import org.jetbrains.kotlin.idea.completion.handlers.*
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import com.intellij.psi.PsiClass
import org.jetbrains.kotlin.asJava.KotlinLightClass
import com.intellij.codeInsight.lookup.LookupElementDecorator
import com.intellij.codeInsight.lookup.LookupElementPresentation
import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
import com.intellij.codeInsight.lookup.DefaultLookupItemRenderer
import org.jetbrains.kotlin.types.TypeUtils
import com.intellij.codeInsight.lookup.impl.LookupCellRenderer
import org.jetbrains.kotlin.idea.util.nullability
import org.jetbrains.kotlin.idea.util.TypeNullability
public class LookupElementFactory(
private val resolutionFacade: ResolutionFacade,
private val receiverTypes: Collection<JetType>
) {
public fun createLookupElement(
resolutionFacade: ResolutionFacade,
descriptor: DeclarationDescriptor,
boldImmediateMembers: Boolean
): LookupElement {
@@ -52,7 +48,7 @@ public class LookupElementFactory(
DescriptorUtils.unwrapFakeOverride(descriptor)
else
descriptor
var element = createLookupElement(resolutionFacade, _descriptor, DescriptorToSourceUtils.descriptorToDeclaration(_descriptor))
var element = createLookupElement(_descriptor, DescriptorToSourceUtils.descriptorToDeclaration(_descriptor))
val weight = callableWeight(descriptor)
if (weight != null) {
@@ -127,7 +123,6 @@ public class LookupElementFactory(
}
private fun createLookupElement(
resolutionFacade: ResolutionFacade,
descriptor: DeclarationDescriptor,
declaration: PsiElement?
): LookupElement {
@@ -71,7 +71,7 @@ class LookupElementsCollector(
private fun addDescriptorElements(descriptor: DeclarationDescriptor, suppressAutoInsertion: Boolean, withReceiverCast: Boolean) {
run {
var lookupElement = lookupElementFactory.createLookupElement(resolutionFacade, descriptor, true)
var lookupElement = lookupElementFactory.createLookupElement(descriptor, true)
if (withReceiverCast) {
lookupElement = lookupElement.withReceiverCast()
@@ -97,7 +97,7 @@ class LookupElementsCollector(
if (KotlinBuiltIns.isFunctionOrExtensionFunctionType(parameterType)) {
val parameterCount = KotlinBuiltIns.getParameterTypeProjectionsFromFunctionType(parameterType).size()
if (parameterCount > 1) {
var lookupElement = lookupElementFactory.createLookupElement(resolutionFacade, descriptor, true)
var lookupElement = lookupElementFactory.createLookupElement(descriptor, true)
lookupElement = object : LookupElementDecorator<LookupElement>(lookupElement) {
override fun renderElement(presentation: LookupElementPresentation) {
@@ -16,14 +16,16 @@
package org.jetbrains.kotlin.idea.completion
import com.intellij.codeInsight.completion.*
import com.intellij.codeInsight.completion.CompletionParameters
import com.intellij.codeInsight.completion.CompletionResultSet
import com.intellij.codeInsight.completion.PlainPrefixMatcher
import com.intellij.openapi.progress.ProcessCanceledException
import com.intellij.patterns.PlatformPatterns
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetPackageDirective
import org.jetbrains.kotlin.idea.caches.resolve.*
import org.jetbrains.kotlin.idea.caches.resolve.getResolutionFacade
import org.jetbrains.kotlin.idea.codeInsight.ReferenceVariantsHelper
import org.jetbrains.kotlin.idea.references.JetSimpleNameReference
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetPackageDirective
/**
* Performs completion in package directive. Should suggest only packages and avoid showing fake package produced by
@@ -52,7 +54,7 @@ object PackageDirectiveCompletion {
val variants = ReferenceVariantsHelper(bindingContext, { true }).getPackageReferenceVariants(ref.expression, prefixMatcher.asNameFilter())
for (variant in variants) {
val lookupElement = LookupElementFactory(listOf()).createLookupElement(resolutionFacade, variant, false)
val lookupElement = LookupElementFactory(resolutionFacade, listOf()).createLookupElement(variant, false)
if (!lookupElement.getLookupString().contains(DUMMY_IDENTIFIER)) {
result.addElement(lookupElement)
}
@@ -36,7 +36,6 @@ import org.jetbrains.kotlin.idea.util.*
import org.jetbrains.kotlin.lexer.JetTokens
import org.jetbrains.kotlin.psi.*
import org.jetbrains.kotlin.psi.psiUtil.getReceiverExpression
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.types.JetType
@@ -149,7 +148,7 @@ class SmartCompletion(
val infoClassifier = { expectedInfo: ExpectedInfo -> types.classifyExpectedInfo(expectedInfo) }
result.addLookupElements(descriptor, expectedInfos, infoClassifier, noNameSimilarityForReturnItself = receiver == null) { descriptor ->
lookupElementFactory.createLookupElement(descriptor, resolutionFacade, bindingContext, true)
lookupElementFactory.createLookupElement(descriptor, bindingContext, true)
}
if (descriptor is PropertyDescriptor) {
@@ -172,7 +171,7 @@ class SmartCompletion(
.addTo(additionalItems, inheritanceSearchers, expectedInfos)
if (expression is JetSimpleNameExpression) {
StaticMembers(bindingContext, resolutionFacade, lookupElementFactory).addToCollection(additionalItems, expectedInfos, expression, itemsToSkip)
StaticMembers(bindingContext, lookupElementFactory).addToCollection(additionalItems, expectedInfos, expression, itemsToSkip)
}
additionalItems.addThisItems(expression, expectedInfos)
@@ -330,7 +329,7 @@ class SmartCompletion(
val matchedExpectedInfos = functionExpectedInfos.filter { functionType.isSubtypeOf(it.type) }
if (matchedExpectedInfos.isEmpty()) return null
var lookupElement = lookupElementFactory.createLookupElement(descriptor, resolutionFacade, bindingContext, true)
var lookupElement = lookupElementFactory.createLookupElement(descriptor, bindingContext, true)
val text = "::" + (if (descriptor is ConstructorDescriptor) descriptor.getContainingDeclaration().getName() else descriptor.getName())
lookupElement = object: LookupElementDecorator<LookupElement>(lookupElement) {
override fun getLookupString() = text
@@ -430,7 +429,7 @@ class SmartCompletion(
val types = descriptor.fuzzyTypes(smartCastTypes)
fun createLookupElement(): LookupElement {
return lookupElementFactory.createLookupElement(descriptor, resolutionFacade, bindingContext, true)
return lookupElementFactory.createLookupElement(descriptor, bindingContext, true)
}
if (types.any { typeFilter(it) }) {
@@ -451,7 +450,7 @@ class SmartCompletion(
if (jetType.isError()) return null
val classifier = jetType.getConstructor().getDeclarationDescriptor() ?: return null
val lookupElement = lookupElementFactory.createLookupElement(classifier, resolutionFacade, bindingContext, false)
val lookupElement = lookupElementFactory.createLookupElement(classifier, bindingContext, false)
val lookupString = lookupElement.getLookupString()
val typeArgs = jetType.getArguments()
@@ -16,29 +16,27 @@
package org.jetbrains.kotlin.idea.completion.smart
import com.intellij.codeInsight.completion.InsertionContext
import com.intellij.codeInsight.lookup.LookupElement
import org.jetbrains.kotlin.types.TypeUtils
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.resolve.DescriptorUtils
import com.intellij.codeInsight.lookup.LookupElementDecorator
import com.intellij.codeInsight.lookup.LookupElementPresentation
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import com.intellij.codeInsight.completion.InsertionContext
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.completion.ExpectedInfo
import org.jetbrains.kotlin.idea.completion.LookupElementFactory
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.idea.core.isVisible
import org.jetbrains.kotlin.idea.util.IdeDescriptorRenderers
import org.jetbrains.kotlin.idea.util.fuzzyReturnType
import org.jetbrains.kotlin.psi.JetSimpleNameExpression
import org.jetbrains.kotlin.idea.completion.ExpectedInfo
import org.jetbrains.kotlin.idea.core.isVisible
import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.TypeUtils
// adds java static members, enum members and members from companion object
class StaticMembers(
val bindingContext: BindingContext,
val resolutionFacade: ResolutionFacade,
val lookupElementFactory: LookupElementFactory
) {
public fun addToCollection(collection: MutableCollection<LookupElement>,
@@ -100,7 +98,7 @@ class StaticMembers(
}
private fun createLookupElement(memberDescriptor: DeclarationDescriptor, classDescriptor: ClassDescriptor): LookupElement {
val lookupElement = lookupElementFactory.createLookupElement(memberDescriptor, resolutionFacade, bindingContext, false)
val lookupElement = lookupElementFactory.createLookupElement(memberDescriptor, bindingContext, false)
val qualifierPresentation = classDescriptor.getName().asString()
val qualifierText = IdeDescriptorRenderers.SOURCE_CODE.renderClassifierName(classDescriptor)
@@ -131,7 +129,7 @@ class StaticMembers(
var text = qualifierText + "." + IdeDescriptorRenderers.SOURCE_CODE.renderName(memberDescriptor.getName())
context.getDocument().replaceString(context.getStartOffset(), context.getTailOffset(), text)
context.setTailOffset(context.getStartOffset() + text.length)
context.setTailOffset(context.getStartOffset() + text.length())
if (memberDescriptor is FunctionDescriptor) {
getDelegate().handleInsert(context)
@@ -122,7 +122,7 @@ class TypeInstantiationItems(
typeArgs: List<TypeProjection>,
tail: Tail?
): LookupElement? {
var lookupElement = lookupElementFactory.createLookupElement(classifier, resolutionFacade, bindingContext, false)
var lookupElement = lookupElementFactory.createLookupElement(classifier, bindingContext, false)
if (DescriptorUtils.isNonCompanionObject(classifier)) {
return lookupElement.addTail(tail)
@@ -252,7 +252,7 @@ class TypeInstantiationItems(
val samConstructor = scope.getFunctions(`class`.getName())
.filterIsInstance<SamConstructorDescriptor>()
.singleOrNull() ?: return
val lookupElement = lookupElementFactory.createLookupElement(samConstructor, resolutionFacade, bindingContext, false)
val lookupElement = lookupElementFactory.createLookupElement(samConstructor, bindingContext, false)
.assignSmartCompletionPriority(SmartCompletionItemPriority.INSTANTIATION)
.addTail(tail)
collection.add(lookupElement)
@@ -24,14 +24,11 @@ import com.intellij.codeInsight.lookup.LookupElementPresentation
import com.intellij.openapi.util.Key
import com.intellij.psi.PsiDocumentManager
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
import org.jetbrains.kotlin.idea.completion.*
import org.jetbrains.kotlin.idea.completion.handlers.WithExpressionPrefixInsertHandler
import org.jetbrains.kotlin.idea.completion.handlers.WithTailInsertHandler
import org.jetbrains.kotlin.idea.util.*
import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.psi.JetValueArgument
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType
import org.jetbrains.kotlin.resolve.BindingContext
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.types.JetType
@@ -264,11 +261,10 @@ fun functionType(function: FunctionDescriptor): JetType? {
fun LookupElementFactory.createLookupElement(
descriptor: DeclarationDescriptor,
resolutionFacade: ResolutionFacade,
bindingContext: BindingContext,
boldImmediateMembers: Boolean
): LookupElement {
var element = createLookupElement(resolutionFacade, descriptor, boldImmediateMembers)
var element = createLookupElement(descriptor, boldImmediateMembers)
if (descriptor is FunctionDescriptor && descriptor.getValueParameters().isNotEmpty()) {
element = element.keepOldArgumentListOnTab()