KT-34644 Refactor CompletionUtils::returnExpressionItems

- Collecting `return` lookups to `blockBodyReturns` will later allow to modify them to increase priority in the completion
This commit is contained in:
Roman Golyshev
2019-11-07 17:54:07 +03:00
committed by Roman Golyshev
parent cf82efb49f
commit de11b4cb64
@@ -185,46 +185,50 @@ fun thisExpressionItems(
} }
fun returnExpressionItems(bindingContext: BindingContext, position: KtElement): Collection<LookupElement> { fun returnExpressionItems(bindingContext: BindingContext, position: KtElement): Collection<LookupElement> {
val result = ArrayList<LookupElement>() val result = mutableListOf<LookupElement>()
for (parent in position.parentsWithSelf) {
if (parent is KtDeclarationWithBody) {
val returnType = parent.returnType(bindingContext)
val isUnit = returnType == null || KotlinBuiltIns.isUnit(returnType)
if (parent is KtFunctionLiteral) {
val (label, call) = parent.findLabelAndCall()
if (label != null) {
result.add(createKeywordElementWithSpace("return", tail = label.labelNameToTail(), addSpaceAfter = !isUnit))
}
// check if the current function literal is inlined and stop processing outer declarations if it's not for (parent in position.parentsWithSelf.filterIsInstance<KtDeclarationWithBody>()) {
val callee = call?.calleeExpression as? KtReferenceExpression ?: break // not inlined val returnType = parent.returnType(bindingContext)
if (!InlineUtil.isInline(bindingContext[BindingContext.REFERENCE_TARGET, callee])) break // not inlined val isUnit = returnType == null || KotlinBuiltIns.isUnit(returnType)
} else { if (parent is KtFunctionLiteral) {
if (parent.hasBlockBody()) { val (label, call) = parent.findLabelAndCall()
result.add(createKeywordElementWithSpace("return", addSpaceAfter = !isUnit)) if (label != null) {
result.add(createKeywordElementWithSpace("return", tail = label.labelNameToTail(), addSpaceAfter = !isUnit))
}
if (returnType != null) { // check if the current function literal is inlined and stop processing outer declarations if it's not
if (returnType.nullability() == TypeNullability.NULLABLE) { val callee = call?.calleeExpression as? KtReferenceExpression ?: break // not inlined
result.add(createKeywordElement("return null")) if (!InlineUtil.isInline(bindingContext[BindingContext.REFERENCE_TARGET, callee])) break // not inlined
} } else {
if (parent.hasBlockBody()) {
val blockBodyReturns = mutableListOf<LookupElement>()
blockBodyReturns.add(createKeywordElementWithSpace("return", addSpaceAfter = !isUnit))
if (KotlinBuiltIns.isBooleanOrNullableBoolean(returnType)) { if (returnType != null) {
result.add(createKeywordElement("return true")) if (returnType.nullability() == TypeNullability.NULLABLE) {
result.add(createKeywordElement("return false")) blockBodyReturns.add(createKeywordElement("return null"))
} else if (KotlinBuiltIns.isCollectionOrNullableCollection(returnType) || KotlinBuiltIns.isListOrNullableList( }
returnType
) || KotlinBuiltIns.isIterableOrNullableIterable(returnType) fun emptyListShouldBeSuggested(): Boolean = KotlinBuiltIns.isCollectionOrNullableCollection(returnType)
) { || KotlinBuiltIns.isListOrNullableList(returnType)
result.add(createKeywordElement("return", tail = " emptyList()")) || KotlinBuiltIns.isIterableOrNullableIterable(returnType)
} else if (KotlinBuiltIns.isSetOrNullableSet(returnType)) {
result.add(createKeywordElement("return", tail = " emptySet()")) if (KotlinBuiltIns.isBooleanOrNullableBoolean(returnType)) {
} blockBodyReturns.add(createKeywordElement("return true"))
blockBodyReturns.add(createKeywordElement("return false"))
} else if (emptyListShouldBeSuggested()) {
blockBodyReturns.add(createKeywordElement("return", tail = " emptyList()"))
} else if (KotlinBuiltIns.isSetOrNullableSet(returnType)) {
blockBodyReturns.add(createKeywordElement("return", tail = " emptySet()"))
} }
} }
break
result.addAll(blockBodyReturns)
} }
break
} }
} }
return result return result
} }