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,9 +185,9 @@ fun thisExpressionItems(
}
fun returnExpressionItems(bindingContext: BindingContext, position: KtElement): Collection<LookupElement> {
val result = ArrayList<LookupElement>()
for (parent in position.parentsWithSelf) {
if (parent is KtDeclarationWithBody) {
val result = mutableListOf<LookupElement>()
for (parent in position.parentsWithSelf.filterIsInstance<KtDeclarationWithBody>()) {
val returnType = parent.returnType(bindingContext)
val isUnit = returnType == null || KotlinBuiltIns.isUnit(returnType)
if (parent is KtFunctionLiteral) {
@@ -201,30 +201,34 @@ fun returnExpressionItems(bindingContext: BindingContext, position: KtElement):
if (!InlineUtil.isInline(bindingContext[BindingContext.REFERENCE_TARGET, callee])) break // not inlined
} else {
if (parent.hasBlockBody()) {
result.add(createKeywordElementWithSpace("return", addSpaceAfter = !isUnit))
val blockBodyReturns = mutableListOf<LookupElement>()
blockBodyReturns.add(createKeywordElementWithSpace("return", addSpaceAfter = !isUnit))
if (returnType != null) {
if (returnType.nullability() == TypeNullability.NULLABLE) {
result.add(createKeywordElement("return null"))
blockBodyReturns.add(createKeywordElement("return null"))
}
fun emptyListShouldBeSuggested(): Boolean = KotlinBuiltIns.isCollectionOrNullableCollection(returnType)
|| KotlinBuiltIns.isListOrNullableList(returnType)
|| KotlinBuiltIns.isIterableOrNullableIterable(returnType)
if (KotlinBuiltIns.isBooleanOrNullableBoolean(returnType)) {
result.add(createKeywordElement("return true"))
result.add(createKeywordElement("return false"))
} else if (KotlinBuiltIns.isCollectionOrNullableCollection(returnType) || KotlinBuiltIns.isListOrNullableList(
returnType
) || KotlinBuiltIns.isIterableOrNullableIterable(returnType)
) {
result.add(createKeywordElement("return", tail = " emptyList()"))
blockBodyReturns.add(createKeywordElement("return true"))
blockBodyReturns.add(createKeywordElement("return false"))
} else if (emptyListShouldBeSuggested()) {
blockBodyReturns.add(createKeywordElement("return", tail = " emptyList()"))
} else if (KotlinBuiltIns.isSetOrNullableSet(returnType)) {
result.add(createKeywordElement("return", tail = " emptySet()"))
blockBodyReturns.add(createKeywordElement("return", tail = " emptySet()"))
}
}
result.addAll(blockBodyReturns)
}
break
}
}
}
return result
}