K2: introduce & use (for annotation creation) smartPlus for lists

This commit is contained in:
Mikhail Glukhikh
2022-12-19 17:21:58 +01:00
committed by Space Team
parent f33f87e3d4
commit 761a0f8248
5 changed files with 16 additions and 24 deletions
@@ -2021,12 +2021,7 @@ class DeclarationsConverter(
}
for (modifierList in allTypeModifiers) {
if (modifierList.annotations.isNotEmpty()) {
val replacementAnnotations =
if (calculatedFirType.annotations.isNotEmpty()) calculatedFirType.annotations + modifierList.annotations
else modifierList.annotations
calculatedFirType.replaceAnnotations(replacementAnnotations)
}
calculatedFirType.replaceAnnotations(calculatedFirType.annotations.smartPlus(modifierList.annotations))
}
return calculatedFirType
}
@@ -451,10 +451,7 @@ class ExpressionsConverter(
val result = firExpression ?: buildErrorExpression(null, ConeNotAnnotationContainer("???"))
require(result is FirAnnotationContainer)
if (firAnnotationList.isNotEmpty()) {
val replacementAnnotations = if (result.annotations.isNotEmpty()) result.annotations + firAnnotationList else firAnnotationList
result.replaceAnnotations(replacementAnnotations)
}
result.replaceAnnotations(result.annotations.smartPlus(firAnnotationList))
return result
}
@@ -499,12 +499,7 @@ open class RawFirBuilder(
if (this != null) {
it.extractAnnotationsFrom(this)
}
val resultAnnotations = when {
accessorAnnotationsFromProperty.isEmpty() -> it.annotations
it.annotations.isEmpty() -> accessorAnnotationsFromProperty
else -> it.annotations + accessorAnnotationsFromProperty
}
it.replaceAnnotations(resultAnnotations)
it.replaceAnnotations(it.annotations.smartPlus(accessorAnnotationsFromProperty))
it.status = status
it.initContainingClassAttr()
}
@@ -704,7 +699,7 @@ open class RawFirBuilder(
val annotations = buildList {
addAll(container.annotations)
for (annotationEntry in annotationEntries) {
add(annotationEntry.convert<FirAnnotation>())
add(annotationEntry.convert())
}
}
container.replaceAnnotations(annotations)
@@ -958,13 +958,7 @@ abstract class BaseFirBuilder<T>(val baseSession: FirSession, val context: Conte
}
return if (operation == FirOperation.ASSIGN) {
val result = unwrappedLhs.convert()
if (annotations.isNotEmpty()) {
val annotationsToReplace = if (result.annotations.isEmpty()) annotations else mutableListOf<FirAnnotation>().apply {
addAll(result.annotations)
addAll(annotations)
}
result.replaceAnnotations(annotationsToReplace)
}
result.replaceAnnotations(result.annotations.smartPlus(annotations))
result.pullUpSafeCallIfNecessary()
} else {
val receiver = unwrappedLhs.convert()
@@ -197,3 +197,14 @@ value class MutableOrEmptyList<T>(val list: MutableList<T>?) : List<T> {
fun <T> empty(): MutableOrEmptyList<T> = EMPTY as MutableOrEmptyList<T>
}
}
fun <T> List<T>.smartPlus(other: List<T>): List<T> = when {
other.isEmpty() -> this
this.isEmpty() -> other
else -> {
val result = ArrayList<T>(this.size + other.size)
result.addAll(this)
result.addAll(other)
result
}
}