I am trying to reconfigure out ktlint settings for...
# getting-started
l
I am trying to reconfigure out ktlint settings for a project. For the
ktlint_standard_trailing-comma-on-declaration-site
setting, the documentation is… highly confusing. https://pinterest.github.io/ktlint/latest/rules/standard/#trailing-comma-on-declaration-site It’s not clear if enabled or disabled will say yes to trailing commas. But I tried both (in
.editorconfig
), and in both cases, ktlint is removing trailing commas from my code base. I want it to either leave them alone or enforce having them, but neither enabled nor disabled is having that effect. What am I missing here?
d
In that past, I've noticed that as well. It seems like it is enforced one way or the other. I wish it was a three-state: "allowed, required, forbidden"
From the docs:
Copy code
KtLint values consistent formatting more than a per-situation decision, and therefore uses this setting to enforce/disallow usage of trailing comma's in declarations.
l
Right. And I’d be happy with “enforce”, but there doesn’t seem to be a way to do anything but “disallow.” Which is the worst option, IMO.
d
enable will enforce it in ktlint/ktlintFormat.
l
That is not the behavior I am seeing.
d
IntelliJ doesn't follow the same rules as ktlint for that one. You might try using the ktlint plugin. Note, I don't use it, so I can't vouch for its effectiveness.
l
That’s… not relevant to what I’m saying?
d
You want to either enforce it, or leave it alone, right? If you install the ktlint plug in, it will at least follow the rule with autoformat when you force it.
l
When I set the “trailing comma” rules to “enabled”, ktlintFormat (on the command line) removes all my trailing commas. When I set it to “disabled”, it removes all my trailing commas. The docs seem to imply that one of them should add trailing commas instead, but it is very unclear which one it means, and neither is having that effect anyway.
d
When its enable, ktlintFormat should add, not remove the trailing commas.
l
Should, yes. That is not the behavior I am seeing, however.
d
(And it my experience, that is what happens)
Can you give a minimal example that reproduces this issue? include a .editorconfig and .kt file that don't meet expectations.
l
The following .editorconfig:
Copy code
root = true

[*]
charset = utf-8
indent_size = 4
indent_style = space
end_of_line = lf
insert_final_newline = true
trim_trailing_whitespace = true

[*.{kt,kts}]
ktlint_code_style = intellij_idea

ktlint_standard_trailing-comma-on-call-site = enabled
ktlint_standard_trailing-comma-on-declaration-site = enabled
Results in many diffs like this:
Copy code
<http://logger.info|logger.info>(
            String.format(
                "FulfillmentEventsListener - onFulfillmentEvent : " +
                    "processingOrderId = %s, customerid = %s, eventType = %s, eventPhase = %s",
                fulfillmentEvent.processingOrderId,
                fulfillmentEvent.customerId,
                fulfillmentEvent.eventType,
-                fulfillmentEvent.eventPhase,
+                fulfillmentEvent.eventPhase
            ),
        )
When I would expect it to result in the opposite: Adding commas in places where they’re not present.
d
Hmm, intellij complains that those properties aren't supported. When I change it to this, it seems to work in intellij. Haven't tried it with ktlint directly though:
Copy code
[*.{kt,kts}]
ij_kotlin_code_style_defaults = KOTLIN_OFFICIAL

ij_kotlin_allow_trailing_comma = true
ij_kotlin_allow_trailing_comma_on_call_site = true
Looking at that documentation link you started with. The configuration setting name is indeed
ij_kotlin_allow_trailing_comma
, even though the
suppress or disable rule
section lists the
ktlint_standard_trailing-comma-on-declaration-site = enable
l
🤔 I hadn’t even noticed that. Let me try swapping the prefix.
Ah ha! That does work with the gradle command on the CLi.
Though it’s doing the exact thing is said it would, which is add commas even in highly stupid places in the name of consistency… 🙄
d
Mystery solved at least.
l
So… is the docs site just completely wrong? The copyable lines are just broken?
d
Seems likely.
l
Lovely. I wonder how I report that…
Looks like the docs source is in the main ktlint repo, I think? So I guess just report it there and let them figure it out.
plus1 1
d
There is a #ktlint channel here too, not sure if the maintainers would be there though.
l
O’rly?
d
Yeah, and it does look like the ktlint contributors are in the channel (whether they check slack often is a different question). Wouldn't hurt to ping them there, but probably creating an issue in the repo is better.
l
I’ll make an issue, and probably weigh on the configurability thread that was just posted. 🙂
plus1 1