This post is a suggestion for a Kotlin+IntelliJ feature. It tries to solve what I call the “lambda braces hell”. Scroll to the end of the following real-world demo of using kotlinx.hmtl to see the problem:
class RecentAdvertsTemplate {
fun render(model: RecentAdvertsModel): BODY.() -> Unit = {
h1 { +model.h1Label }
div(classes = "teaserBox") {
img(src = model.teaserImage.imgUrl)
p { +model.teaserText }
}
div(classes = "advertsList") {
model.adverts.forEach { advert ->
div(classes = "advertItem") {
h2 { link(advert.linkedSubject) }
if (advert.images.isNotEmpty()) {
div(classes = "advertImageBox") {
advert.images.forEach { image ->
image(image)
}
}
}
p { +advert.bodyTeaser }
div {
span(classes = "advertDate") {
+advert.date
}
advert.price?.let { price ->
span(classes = "advertPrice") {
+price
} //lambda braces hell starts here:
}
}
}
}
}
}
}
That’s not very nice to read. My suggestion is to let IntelliJ show function name hints at the position of closing lambda braces. Something like in the following code:
class RecentAdvertsTemplate {
fun render(model: RecentAdvertsModel): BODY.() -> Unit = {
h1 { +model.h1Label }
div(classes = "teaserBox") {
img(src = model.teaserImage.imgUrl)
p { +model.teaserText }
} //div
div(classes = "advertsList") {
model.adverts.forEach { advert ->
div(classes = "advertItem") {
h2 { link(advert.linkedSubject) }
if (advert.images.isNotEmpty()) {
div(classes = "advertImageBox") {
advert.images.forEach { image ->
image(image)
}
} //div
}
p { +advert.bodyTeaser }
div {
span(classes = "advertDate") {
+advert.date
} //span
advert.price?.let { price ->
span(classes = "advertPrice") {
+price
} //span
}
} //div
} //div
}
}//div
}
}
These hints could be displayed for functions that are marked with a DSL marker annotation. Or there could be an additional annotation that triggers these hints.