Smart completion: special items with "!!" and "?:" for nullable members
This commit is contained in:
@@ -61,7 +61,7 @@ public object ShortenReferences {
|
||||
}
|
||||
|
||||
private fun process(elements: Iterable<JetElement>, elementFilter: (PsiElement) -> FilterResult) {
|
||||
for ((file, fileElements) in elements.groupBy { element -> element.getContainingFile() as JetFile }) {
|
||||
for ((file, fileElements) in elements.groupBy { element -> element.getContainingJetFile() }) {
|
||||
// first resolve all qualified references - optimization
|
||||
val referenceToContext = JetFileReferencesResolver.resolve(file, fileElements, visitShortNames = false)
|
||||
|
||||
|
||||
+25
-9
@@ -2,11 +2,14 @@ package org.jetbrains.jet.plugin.completion.handlers
|
||||
|
||||
import com.intellij.codeInsight.completion.*
|
||||
import com.intellij.codeInsight.lookup.LookupElement
|
||||
import com.intellij.openapi.editor.*
|
||||
import com.intellij.openapi.editor.event.*
|
||||
import com.intellij.openapi.util.TextRange
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
|
||||
class WithTailInsertHandler(val tailChar: Char, val spaceAfter: Boolean) : InsertHandler<LookupElement> {
|
||||
abstract class WithTailInsertHandlerBase : InsertHandler<LookupElement> {
|
||||
protected abstract fun insertTail(document: Document, offset: Int): Int
|
||||
|
||||
override fun handleInsert(context: InsertionContext, item: LookupElement) {
|
||||
val document = context.getDocument()
|
||||
val caretModel = context.getEditor().getCaretModel()
|
||||
@@ -37,21 +40,34 @@ class WithTailInsertHandler(val tailChar: Char, val spaceAfter: Boolean) : Inser
|
||||
}
|
||||
|
||||
val moveCaret = caretModel.getOffset() == maxChangeOffset
|
||||
val endTailOffset = insertTail(document, maxChangeOffset)
|
||||
if (moveCaret) {
|
||||
caretModel.moveToOffset(endTailOffset)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
class WithTailCharInsertHandler(val tailChar: Char, val spaceAfter: Boolean) : WithTailInsertHandlerBase() {
|
||||
override fun insertTail(document: Document, offset: Int): Int {
|
||||
fun isCharAt(offset: Int, c: Char) = offset < document.getTextLength() && document.getText(TextRange(offset, offset + 1))[0] == c
|
||||
|
||||
if (isCharAt(maxChangeOffset, tailChar)) {
|
||||
document.deleteString(maxChangeOffset, maxChangeOffset + 1)
|
||||
if (isCharAt(offset, tailChar)) {
|
||||
document.deleteString(offset, offset + 1)
|
||||
|
||||
if (spaceAfter && isCharAt(maxChangeOffset, ' ')) {
|
||||
document.deleteString(maxChangeOffset, maxChangeOffset + 1)
|
||||
if (spaceAfter && isCharAt(offset, ' ')) {
|
||||
document.deleteString(offset, offset + 1)
|
||||
}
|
||||
}
|
||||
|
||||
val textToInsert = if (spaceAfter) tailChar + " " else tailChar.toString()
|
||||
document.insertString(maxChangeOffset, textToInsert)
|
||||
if (moveCaret) {
|
||||
caretModel.moveToOffset(maxChangeOffset + textToInsert.length)
|
||||
}
|
||||
document.insertString(offset, textToInsert)
|
||||
return offset + textToInsert.length
|
||||
}
|
||||
}
|
||||
|
||||
class WithTailStringInsertHandler(val tail: String) : WithTailInsertHandlerBase() {
|
||||
override fun insertTail(document: Document, offset: Int): Int {
|
||||
document.insertString(offset, tail)
|
||||
return offset + tail.length
|
||||
}
|
||||
}
|
||||
@@ -29,7 +29,7 @@ class LambdaItems(val project: Project) {
|
||||
val offerNoParametersLambda = singleSignatureLength == 0 || singleSignatureLength == 1
|
||||
if (offerNoParametersLambda) {
|
||||
val lookupElement = createLookupElement("{...}", "{ ", " }", shortenRefs = false)
|
||||
collection.add(addTailToLookupElement(lookupElement, functionExpectedInfos))
|
||||
collection.add(lookupElement.addTail(functionExpectedInfos))
|
||||
}
|
||||
|
||||
if (singleSignatureLength != 0) {
|
||||
@@ -44,7 +44,7 @@ class LambdaItems(val project: Project) {
|
||||
val lookupElement = LookupElementBuilder.create(lookupString)
|
||||
.withInsertHandler(LambdaInsertHandler(functionType, useExplicitTypes))
|
||||
.suppressAutoInsertion()
|
||||
collection.add(addTailToLookupElement(lookupElement, functionExpectedInfos.filter { it.`type` == functionType }))
|
||||
collection.add(lookupElement.addTail(functionExpectedInfos.filter { it.`type` == functionType }))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -55,13 +55,16 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
|
||||
for (descriptor in referenceVariants) {
|
||||
if (itemsToSkip.contains(descriptor)) continue
|
||||
|
||||
val matchedExpectedInfos = expectedInfos.filter { expectedInfo ->
|
||||
typesWithAutoCasts(descriptor).any { it.isSubtypeOf(expectedInfo.`type`) }
|
||||
}
|
||||
if (matchedExpectedInfos.isNotEmpty()) {
|
||||
val lookupElement = DescriptorLookupConverter.createLookupElement(resolveSession, bindingContext, descriptor)
|
||||
result.add(addTailToLookupElement(lookupElement, matchedExpectedInfos))
|
||||
val types = typesWithAutoCasts(descriptor)
|
||||
val nonNullTypes = types.map { TypeUtils.makeNotNullable(it) }
|
||||
val classifier = { (expectedInfo: ExpectedInfo) ->
|
||||
when {
|
||||
types.any { it.isSubtypeOf(expectedInfo.`type`) } -> ExpectedInfoClassification.MATCHES
|
||||
nonNullTypes.any { it.isSubtypeOf(expectedInfo.`type`) } -> ExpectedInfoClassification.MAKE_NOT_NULLABLE
|
||||
else -> ExpectedInfoClassification.NOT_MATCHES
|
||||
}
|
||||
}
|
||||
result.addLookupElements(expectedInfos, classifier, { DescriptorLookupConverter.createLookupElement(resolveSession, bindingContext, descriptor) })
|
||||
|
||||
if (receiver == null) {
|
||||
toFunctionReferenceLookupElement(descriptor, functionExpectedInfos)?.let { result.add(it) }
|
||||
@@ -129,7 +132,7 @@ class SmartCompletion(val expression: JetSimpleNameExpression,
|
||||
}
|
||||
}
|
||||
|
||||
return addTailToLookupElement(lookupElement, matchedExpectedInfos)
|
||||
return lookupElement.addTail(matchedExpectedInfos)
|
||||
}
|
||||
|
||||
if (descriptor is SimpleFunctionDescriptor) {
|
||||
|
||||
@@ -21,6 +21,7 @@ import com.intellij.codeInsight.completion.InsertionContext
|
||||
import org.jetbrains.jet.plugin.project.ResolveSessionForBodies
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext
|
||||
import org.jetbrains.jet.lang.psi.JetExpression
|
||||
import java.util.ArrayList
|
||||
|
||||
// adds java static members, enum members and members from class object
|
||||
class StaticMembers(val bindingContext: BindingContext, val resolveSession: ResolveSessionForBodies) {
|
||||
@@ -45,15 +46,27 @@ class StaticMembers(val bindingContext: BindingContext, val resolveSession: Reso
|
||||
fun processMember(descriptor: DeclarationDescriptor) {
|
||||
if (descriptor is DeclarationDescriptorWithVisibility && !Visibilities.isVisible(descriptor, scope.getContainingDeclaration())) return
|
||||
|
||||
val matchedExpectedInfos = expectedInfos.filter {
|
||||
expectedInfo ->
|
||||
descriptor is CallableDescriptor && descriptor.getReturnType()?.let { it.isSubtypeOf(expectedInfo.`type`) } ?: false
|
||||
|| DescriptorUtils.isEnumEntry(descriptor) /* we do not need to check type of enum entry because it's taken from proper enum */
|
||||
val classifier: (ExpectedInfo) -> ExpectedInfoClassification
|
||||
if (descriptor is CallableDescriptor) {
|
||||
val returnType = descriptor.getReturnType()
|
||||
if (returnType == null) return
|
||||
classifier = {
|
||||
expectedInfo ->
|
||||
when {
|
||||
returnType.isSubtypeOf(expectedInfo.`type`) -> ExpectedInfoClassification.MATCHES
|
||||
returnType.isNullable() && TypeUtils.makeNotNullable(returnType).isSubtypeOf(expectedInfo.`type`) -> ExpectedInfoClassification.MAKE_NOT_NULLABLE
|
||||
else -> ExpectedInfoClassification.NOT_MATCHES
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (DescriptorUtils.isEnumEntry(descriptor)) {
|
||||
classifier = { ExpectedInfoClassification.MATCHES } /* we do not need to check type of enum entry because it's taken from proper enum */
|
||||
}
|
||||
else{
|
||||
return
|
||||
}
|
||||
if (matchedExpectedInfos.isEmpty()) return
|
||||
|
||||
val lookupElement = createLookupElement(descriptor, classDescriptor)
|
||||
collection.add(addTailToLookupElement(lookupElement, matchedExpectedInfos))
|
||||
collection.addLookupElements(expectedInfos, classifier, { createLookupElement(descriptor, classDescriptor) })
|
||||
}
|
||||
|
||||
if (classDescriptor is JavaClassDescriptor) {
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.jetbrains.jet.lang.psi.JetValueArgumentList
|
||||
import org.jetbrains.jet.lang.psi.JetCallExpression
|
||||
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression
|
||||
import org.jetbrains.jet.lang.resolve.BindingContext
|
||||
import org.jetbrains.jet.lang.types.TypeUtils
|
||||
|
||||
class ThisItems(val bindingContext: BindingContext) {
|
||||
public fun addToCollection(collection: MutableCollection<LookupElement>, context: JetExpression, expectedInfos: Collection<ExpectedInfo>) {
|
||||
@@ -27,15 +28,21 @@ class ThisItems(val bindingContext: BindingContext) {
|
||||
for (i in 0..receivers.size - 1) {
|
||||
val receiver = receivers[i]
|
||||
val thisType = receiver.getType()
|
||||
val matchedExpectedInfos = expectedInfos.filter { thisType.isSubtypeOf(it.`type`) }
|
||||
if (matchedExpectedInfos.notEmpty) {
|
||||
//TODO: use this code when KT-4258 fixed
|
||||
//val expressionText = if (i == 0) "this" else "this@" + (thisQualifierName(receiver, bindingContext) ?: continue)
|
||||
val qualifier = if (i == 0) null else thisQualifierName(receiver) ?: continue
|
||||
val expressionText = if (qualifier == null) "this" else "this@" + qualifier
|
||||
val lookupElement = LookupElementBuilder.create(expressionText).withTypeText(DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(thisType))
|
||||
collection.add(addTailToLookupElement(lookupElement, matchedExpectedInfos))
|
||||
val classifier = { (expectedInfo: ExpectedInfo) ->
|
||||
when {
|
||||
thisType.isSubtypeOf(expectedInfo.`type`) -> ExpectedInfoClassification.MATCHES
|
||||
thisType.isNullable() && TypeUtils.makeNotNullable(thisType).isSubtypeOf(expectedInfo.`type`) -> ExpectedInfoClassification.MAKE_NOT_NULLABLE
|
||||
else -> ExpectedInfoClassification.NOT_MATCHES
|
||||
}
|
||||
}
|
||||
fun lookupElementFactory(): LookupElement? {
|
||||
//TODO: use this code when KT-4258 fixed
|
||||
//val expressionText = if (i == 0) "this" else "this@" + (thisQualifierName(receiver, bindingContext) ?: return null)
|
||||
val qualifier = if (i == 0) null else (thisQualifierName(receiver) ?: return null)
|
||||
val expressionText = if (qualifier == null) "this" else "this@" + qualifier
|
||||
return LookupElementBuilder.create(expressionText).withTypeText(DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(thisType))
|
||||
}
|
||||
collection.addLookupElements(expectedInfos, classifier, ::lookupElementFactory)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -101,6 +101,6 @@ class TypeInstantiationItems(val bindingContext: BindingContext, val resolveSess
|
||||
}
|
||||
}
|
||||
|
||||
collection.add(addTailToLookupElement(lookupElement, tail))
|
||||
collection.add(lookupElement.addTail(tail))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,17 +5,18 @@ import com.intellij.codeInsight.lookup.LookupElement
|
||||
import com.intellij.codeInsight.completion.InsertionContext
|
||||
import com.intellij.psi.PsiDocumentManager
|
||||
import org.jetbrains.jet.lang.psi.JetFile
|
||||
import com.intellij.psi.util.PsiTreeUtil
|
||||
import org.jetbrains.jet.lang.psi.JetElement
|
||||
import org.jetbrains.jet.plugin.codeInsight.ShortenReferences
|
||||
import java.util.HashSet
|
||||
import com.intellij.codeInsight.lookup.LookupElementDecorator
|
||||
import org.jetbrains.jet.plugin.completion.handlers.WithTailInsertHandler
|
||||
import org.jetbrains.jet.plugin.completion.handlers.WithTailCharInsertHandler
|
||||
import com.intellij.codeInsight.lookup.AutoCompletionPolicy
|
||||
import org.jetbrains.jet.lang.descriptors.FunctionDescriptor
|
||||
import org.jetbrains.jet.lang.types.JetType
|
||||
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns
|
||||
import org.jetbrains.jet.lang.types.checker.JetTypeChecker
|
||||
import com.intellij.codeInsight.lookup.LookupElementPresentation
|
||||
import org.jetbrains.jet.plugin.completion.handlers.WithTailStringInsertHandler
|
||||
import java.util.ArrayList
|
||||
|
||||
class ArtificialElementInsertHandler(
|
||||
val textBeforeCaret: String, val textAfterCaret: String, val shortenRefs: Boolean) : InsertHandler<LookupElement>{
|
||||
@@ -42,29 +43,94 @@ fun mergeTails(tails: Collection<Tail?>): Tail? {
|
||||
return if (HashSet(tails).size == 1) tails.first() else null
|
||||
}
|
||||
|
||||
fun addTailToLookupElement(lookupElement: LookupElement, tail: Tail?): LookupElement {
|
||||
fun LookupElement.addTail(tail: Tail?): LookupElement {
|
||||
return when (tail) {
|
||||
null -> lookupElement
|
||||
null -> this
|
||||
|
||||
Tail.COMMA -> object: LookupElementDecorator<LookupElement>(lookupElement) {
|
||||
Tail.COMMA -> object: LookupElementDecorator<LookupElement>(this) {
|
||||
override fun handleInsert(context: InsertionContext) {
|
||||
WithTailInsertHandler(',', true /*TODO: use code style option*/).handleInsert(context, lookupElement)
|
||||
WithTailCharInsertHandler(',', true /*TODO: use code style option*/).handleInsert(context, getDelegate())
|
||||
}
|
||||
}
|
||||
|
||||
Tail.PARENTHESIS -> object: LookupElementDecorator<LookupElement>(lookupElement) {
|
||||
Tail.PARENTHESIS -> object: LookupElementDecorator<LookupElement>(this) {
|
||||
override fun handleInsert(context: InsertionContext) {
|
||||
WithTailInsertHandler(')', false).handleInsert(context, lookupElement)
|
||||
WithTailCharInsertHandler(')', false).handleInsert(context, getDelegate())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun addTailToLookupElement(lookupElement: LookupElement, matchedExpectedInfos: Collection<ExpectedInfo>): LookupElement
|
||||
= addTailToLookupElement(lookupElement, mergeTails(matchedExpectedInfos.map { it.tail }))
|
||||
fun LookupElement.addTail(matchedExpectedInfos: Collection<ExpectedInfo>): LookupElement
|
||||
= addTail(mergeTails(matchedExpectedInfos.map { it.tail }))
|
||||
|
||||
fun LookupElement.suppressAutoInsertion() = AutoCompletionPolicy.NEVER_AUTOCOMPLETE.applyPolicy(this)
|
||||
|
||||
enum class ExpectedInfoClassification {
|
||||
MATCHES
|
||||
MAKE_NOT_NULLABLE
|
||||
NOT_MATCHES
|
||||
}
|
||||
|
||||
fun MutableCollection<LookupElement>.addLookupElements(expectedInfos: Collection<ExpectedInfo>,
|
||||
infoClassifier: (ExpectedInfo) -> ExpectedInfoClassification,
|
||||
lookupElementFactory: () -> LookupElement?) {
|
||||
val matchedInfos = ArrayList<ExpectedInfo>()
|
||||
val matchedInfosNotNullable = ArrayList<ExpectedInfo>()
|
||||
for (info in expectedInfos) {
|
||||
when (infoClassifier(info)) {
|
||||
ExpectedInfoClassification.MATCHES -> matchedInfos.add(info)
|
||||
ExpectedInfoClassification.MAKE_NOT_NULLABLE -> matchedInfosNotNullable.add(info)
|
||||
}
|
||||
}
|
||||
|
||||
if (matchedInfos.isNotEmpty()) {
|
||||
val lookupElement = lookupElementFactory()
|
||||
if (lookupElement != null) {
|
||||
add(lookupElement.addTail(matchedInfos))
|
||||
}
|
||||
}
|
||||
else if (matchedInfosNotNullable.isNotEmpty()) {
|
||||
addLookupElementsForNullable(lookupElementFactory, matchedInfosNotNullable)
|
||||
}
|
||||
}
|
||||
|
||||
fun MutableCollection<LookupElement>.addLookupElementsForNullable(factory: () -> LookupElement?, matchedInfos: Collection<ExpectedInfo>) {
|
||||
run {
|
||||
var lookupElement = factory()
|
||||
if (lookupElement != null) {
|
||||
lookupElement = object: LookupElementDecorator<LookupElement>(lookupElement!!) {
|
||||
override fun renderElement(presentation: LookupElementPresentation) {
|
||||
super.renderElement(presentation)
|
||||
presentation.setItemText("!! " + presentation.getItemText())
|
||||
}
|
||||
override fun handleInsert(context: InsertionContext) {
|
||||
WithTailStringInsertHandler("!!").handleInsert(context, getDelegate())
|
||||
}
|
||||
}
|
||||
lookupElement = lookupElement!!.suppressAutoInsertion()
|
||||
add(lookupElement!!.addTail(matchedInfos))
|
||||
}
|
||||
}
|
||||
|
||||
run {
|
||||
var lookupElement = factory()
|
||||
if (lookupElement != null) {
|
||||
lookupElement = object: LookupElementDecorator<LookupElement>(lookupElement!!) {
|
||||
override fun renderElement(presentation: LookupElementPresentation) {
|
||||
super.renderElement(presentation)
|
||||
presentation.setItemText("?: " + presentation.getItemText())
|
||||
}
|
||||
override fun handleInsert(context: InsertionContext) {
|
||||
WithTailStringInsertHandler(" ?: ").handleInsert(context, getDelegate()) //TODO: code style
|
||||
}
|
||||
}
|
||||
lookupElement = lookupElement!!.suppressAutoInsertion()
|
||||
add(lookupElement!!.addTail(matchedInfos))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fun functionType(function: FunctionDescriptor): JetType? {
|
||||
return KotlinBuiltIns.getInstance().getKFunctionType(function.getAnnotations(),
|
||||
null,
|
||||
|
||||
Reference in New Issue
Block a user