Refactored createLookupElementForType changing rendering for function types
This commit is contained in:
@@ -26,6 +26,7 @@ import com.intellij.psi.PsiDocumentManager
|
|||||||
import com.intellij.util.PlatformIcons
|
import com.intellij.util.PlatformIcons
|
||||||
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
import org.jetbrains.kotlin.builtins.KotlinBuiltIns
|
||||||
import org.jetbrains.kotlin.descriptors.*
|
import org.jetbrains.kotlin.descriptors.*
|
||||||
|
import org.jetbrains.kotlin.idea.JetIcons
|
||||||
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
|
import org.jetbrains.kotlin.idea.caches.resolve.ResolutionFacade
|
||||||
import org.jetbrains.kotlin.idea.completion.handlers.CastReceiverInsertHandler
|
import org.jetbrains.kotlin.idea.completion.handlers.CastReceiverInsertHandler
|
||||||
import org.jetbrains.kotlin.idea.completion.handlers.WithTailInsertHandler
|
import org.jetbrains.kotlin.idea.completion.handlers.WithTailInsertHandler
|
||||||
@@ -342,14 +343,17 @@ fun LookupElementFactory.createBackingFieldLookupElement(
|
|||||||
|
|
||||||
fun LookupElementFactory.createLookupElementForType(type: JetType): LookupElement? {
|
fun LookupElementFactory.createLookupElementForType(type: JetType): LookupElement? {
|
||||||
if (type.isError()) return null
|
if (type.isError()) return null
|
||||||
val classifier = type.getConstructor().getDeclarationDescriptor() ?: return null
|
|
||||||
|
|
||||||
val lookupElement = createLookupElement(classifier, false)
|
if (KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(type)) {
|
||||||
var itemText = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)
|
val text = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)
|
||||||
val typeText = IdeDescriptorRenderers.SOURCE_CODE.renderType(type)
|
val baseLookupElement = LookupElementBuilder.create(text).setIcon(JetIcons.LAMBDA)
|
||||||
|
return BaseTypeLookupElement(type, baseLookupElement)
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
val classifier = type.getConstructor().getDeclarationDescriptor() ?: return null
|
||||||
|
val baseLookupElement = createLookupElement(classifier, false)
|
||||||
|
|
||||||
var packageName: FqName? = null
|
var packageName: FqName? = null
|
||||||
if (!KotlinBuiltIns.isExactFunctionOrExtensionFunctionType(type)) {
|
|
||||||
var container = classifier.getContainingDeclaration()
|
var container = classifier.getContainingDeclaration()
|
||||||
while (container is ClassDescriptor) {
|
while (container is ClassDescriptor) {
|
||||||
container = container.getContainingDeclaration()
|
container = container.getContainingDeclaration()
|
||||||
@@ -357,38 +361,38 @@ fun LookupElementFactory.createLookupElementForType(type: JetType): LookupElemen
|
|||||||
if (container is PackageFragmentDescriptor) {
|
if (container is PackageFragmentDescriptor) {
|
||||||
packageName = container.fqName
|
packageName = container.fqName
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
val insertHandler: InsertHandler<LookupElement> = object : InsertHandler<LookupElement> {
|
val itemText = IdeDescriptorRenderers.SOURCE_CODE_SHORT_NAMES_IN_TYPES.renderType(type)
|
||||||
override fun handleInsert(context: InsertionContext, item: LookupElement) {
|
|
||||||
context.getDocument().replaceString(context.getStartOffset(), context.getTailOffset(), typeText)
|
|
||||||
context.setTailOffset(context.getStartOffset() + typeText.length())
|
|
||||||
shortenReferences(context, context.getStartOffset(), context.getTailOffset())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
class TypeLookupElement : LookupElementDecorator<LookupElement>(lookupElement) {
|
return object : BaseTypeLookupElement(type, baseLookupElement) {
|
||||||
val typeText = typeText
|
override fun renderElement(presentation: LookupElementPresentation) {
|
||||||
|
super.renderElement(presentation)
|
||||||
|
presentation.setItemText(itemText)
|
||||||
|
|
||||||
override fun equals(other: Any?) = other is TypeLookupElement && typeText == other.typeText
|
presentation.clearTail()
|
||||||
override fun hashCode() = typeText.hashCode()
|
if (packageName != null) {
|
||||||
|
presentation.appendTailText(" ($packageName)", true)
|
||||||
override fun renderElement(presentation: LookupElementPresentation) {
|
}
|
||||||
getDelegate().renderElement(presentation)
|
|
||||||
presentation.setItemText(itemText)
|
|
||||||
|
|
||||||
presentation.clearTail()
|
|
||||||
if (packageName != null) {
|
|
||||||
presentation.appendTailText(" ($packageName)", true)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
override fun handleInsert(context: InsertionContext) {
|
private open class BaseTypeLookupElement(type: JetType, baseLookupElement: LookupElement) : LookupElementDecorator<LookupElement>(baseLookupElement) {
|
||||||
insertHandler.handleInsert(context, getDelegate())
|
private val fullText = IdeDescriptorRenderers.SOURCE_CODE.renderType(type)
|
||||||
}
|
|
||||||
|
override fun equals(other: Any?) = other is BaseTypeLookupElement && fullText == other.fullText
|
||||||
|
override fun hashCode() = fullText.hashCode()
|
||||||
|
|
||||||
|
override fun renderElement(presentation: LookupElementPresentation) {
|
||||||
|
getDelegate().renderElement(presentation)
|
||||||
}
|
}
|
||||||
|
|
||||||
return TypeLookupElement()
|
override fun handleInsert(context: InsertionContext) {
|
||||||
|
context.getDocument().replaceString(context.getStartOffset(), context.getTailOffset(), fullText)
|
||||||
|
context.setTailOffset(context.getStartOffset() + fullText.length())
|
||||||
|
shortenReferences(context, context.getStartOffset(), context.getTailOffset())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fun shortenReferences(context: InsertionContext, startOffset: Int, endOffset: Int) {
|
fun shortenReferences(context: InsertionContext, startOffset: Int, endOffset: Int) {
|
||||||
|
|||||||
Reference in New Issue
Block a user