Hi, I'm new here and new to ktlint. I like the rul...
# ktlint
m
Hi, I'm new here and new to ktlint. I like the rules and don't mind it's opinionated, however when using (data) classes with fields and annotations, these classes become unreadable...
Copy code
@Schema(
    name = "Country",
    description = "Representation of a country.",
)
data class CountryDto(
    @field:Schema(
        description = "The unique identifier for the country.",
    )
    val id: String,
    @field:Schema(
        description = "The unique ISO 3166-1 alpha-2 code of the country.",
        minLength = 2,
        maxLength = 2,
        pattern = "^[A-Z]{2}$",
    )
    val alpha2Code: String,
    @field:Schema(
        description = "The unique ISO 3166-1 alpha-3 code of the country.",
        minLength = 3,
        maxLength = 3,
        pattern = "^[A-Z]{3}$",
    )
    val alpha3Code: String,
    @field:Schema(
        description = "The name of the country.",
    )
    val name: String,
    @field:Schema(
        types = ["string", "null"],
        description = "The demonym for citizens of the country.",
        required = true,
    )
    val demonym: String?,
    @field:Schema(
        description = "The identifier for the continent where the country is located.",
    )
    val continentId: String,
)
a blank link between the fields would help a lot, but this does not seem to be possible... is that correct, or am I missing something?
p
Indeed there is no configuration option for that.
m
Thx for confirming.. would have been quite nice for readability.
p
Yes, but when implementing features like that it always turns out to be more difficult. In what cases would the blank line need to be added, and when not? Probalby it should not be added before the first field.
m
I full understand, in this case I wouldn't mind if there is space before the first field, as long as the others become more readable. I would say whenever there is Javadoc or an annotation, if there is no space it becomes a readability issue.
p
Sorry that I was not clear. I did not intend to communicate that I am considering a configuration option for this. I was just trying to clarify that when adding a rule like this, that there are too many options to consider that would make everyone (reasonably) happy. I think that I even have tried at one moment to add this, and was very unhappy with the result.
d
Have you tried asking chatgpt? 😄 just kidding, spaces are a must, that sample looks ugly
p
Yep, I do agree that it does look horrible. The real problem is to come up with an algorithm that does not have any other side effects that would ask for another exception. People often ask me to add an exception for one specific case, without taking into account all other things that can go wrong. So what can you do: 1) disable the offending rule per class, file, directory or entire project 2) use a EOL comment without text instead of blank line (ugly as well) 3) ask chatgpt to suggest an algorithm, or even better an implementation that just works out of the box 🤣
m
note there is already this rule: Blank line between declarations with annotations (https://pinterest.github.io/ktlint/latest/rules/standard/#blank-line-between-declarations-with-annotations) but it seems to apply for this case with fields in a (data) class. Maybe option 1) is now the best option for me, maybe per class or directory... Which rule should I disable to avoid removing these spaces?
p
With
@file:Suppress("ktlint:standard:no-blank-line-in-list")
you can suppress the rule for all classes in a file. Or just
@Suppress("ktlint:standard:no-blank-line-in-list")
per per class. See https://pinterest.github.io/ktlint/latest/faq/ for more information about how to suppress rules.
👍 2