My Journey of Fixing Kotlin Code Formatter

Sinan Kozak
3 min readApr 17, 2020

Since Jetbrain released the official Kotlin code style guideline, we have adjusted our own code style for ImmoScout24 app. Luckily we didn't have to change lots of existing rules.

Photo by Bernard Hermant on Unsplash

One thing we adjusted is Minimum Blank Line: After class header option. We set it to 1. After all, the guideline suggests having an empty line after the multiline class header. And personally, having an empty line between different layers of code help readability. For example; an empty line between methods or an empty line before the companion object.

For the IDEA formatter, there is no difference between single and multiline class header. That is why we want to have a minimum of 1 empty line after every class header.

Right after we changed the code style, we noticed that the formatter doesn't add a new line after the class header. After a quick search, I found out that it’s an already reported issue. KT-24750

I starred the issue and expected a fix but there wasn't any change in the ticket status for some time. Therefore, I recently asked about the KT-24750 in the Kotlin Slack channel and apparently it is not something planned for the near future. https://kotlinlang.slack.com/archives/C0B8H786P/p1585045546095300

Besides IDEA’s formatter, we also use ktlint to check our code on CI builds. However, ktlint doesn't support Minimum Blank Line: After class header rule.

So at that point, I thought that since Kotlin is open source, I can fix the formatter issue myself. I spend half of a Sunday setting up the Kotlin project locally. I found the responsible classes. And started playing with the code. For checking my changes, I updated the test of Minimum Blank Line option.

Then I run tests of full Kotlin project to see if I break anything. There were some random test fails. I spend lots of time to understand how a change in the formatter can break other parts. Again I asked in Kotlin Slack, and I learned that there are some failing and flaky tests in the repository. :(

At the end of that Sunday, I was able to open a pull request to Kotlin repository about formatter issue. It contains new tests with the fix. Now, someone is assigned to that pull request and ticket (KT-24750) is targetted to Kotlin 1.4. That is awesome. :) https://github.com/JetBrains/kotlin/pull/3225

I expect the pull request to get some reviews and maybe a request change, but that will happen in an uncertain future. Until then we will still have the same issue with formatter in our project.

In addition to my Kotlin formatter pull request, I consider opening another pull request to ktlint, After all, even with fixed code, the formatter won’t provide any strict checks. ktlint can check for the empty line but that code style could be opinionated for other users of ktlint. That is why I decided to write a custom ktlint rule library instead of adding rule to ktlint.

Currently, there is only one rule; one-empty-line-after-class-header. It checks the code and fails if there isn’t one empty line after the class header.

One huge benefit of ktlint rule, it can automatically apply correct changes. With the help of the new rule, I updated more than 1000 files in our codebase and added missing empty lines. Moreover, from now on, every new class will have empty line thanks to the new rule. Otherwise ktlint will fail on CI build.

I hope my pull request to Kotlin formatter plugin will also get some reviews and get merged to help rest of the community. But until then if you like to follow Kotlin guideline suggestions and add a new line after the class header, you can check this custom rule.

Happy coding :)

Thanks to Said Tahsin Dane, ashdavies ™ and Francesco Noya for their review.

--

--

Sinan Kozak

Staff Android Engineer at Delivery Hero in Infra team. Making sure food delivery applications won't crash and run butter smooth.