Is there a way to enforce indent tabs instead of s...
# detekt
c
Is there a way to enforce indent tabs instead of spaces? EDIT --- So far my research on the matter shows that there is no way to use tabs with either detekt or ktlint. I get the desire to avoid indent-wars, but this effectively prevents teams with existing code bases and style guides based on tabs to use either detekt or ktlint, or forces them to disable all the relevant rule(set)s and write their own. Plainly that sucks 😞
m
You're bumping up against the Kotlin Official Style Guide, and the fact these tools are written against that... It says "spaces, never tabs." https://kotlinlang.org/docs/reference/coding-conventions.html#formatting Unfortunately, I wouldn't expect anything to change with these tools.
c
IntelliJ provides the setting against their own style guide, because they are pragmatic about it, I don't see why detekt and ktlint don't/aren't.
We have several million LOC across company and all that code uses tabs. And now when we want to add kotlin static analysis we suddenly need to switch, because another company's style guide says so...
m
IntelliJ provides options against all languages, yes. As do all other tools. BUT the officially published standards for Kotlin do state "spaces, not tabs". Detekt/ktlint are focusing on the majority, and enforcing the published standard, so I would guess it's not a priority for them, and they haven't had enough users ask for it. I know it's technically feasible, and DEFINITELY not arguing about tabs vs spaces. I prefer consistency over a specific standard. The best I can suggest is creating a ticket against the tools (if one doesn't already exist), and building support behind it. Or contributing the code required to make it work with tabs. As I use spaces, I'm not familiar with the types of issues you're running into. I assume the existence of rules to enforce spaces, and them being turned on by default? And perhaps absence of enforcing tabs? So couldn't you create a custom config that you share across your projects that turns off space specific rules, and contribute a 'tabs only' rule?
I have a custom config as my teams don't agree with all the default settings of Detekt, and we propogate it across projects...
c
That is my problem exactly, I slightly prefer tabs myself, but I'm okay with whatever is the standard in the team/company, it just happens to be that since 2014 when first line of code was written, the standard in the company I work for has been tabs. As for creating tickets, they've existed almost since conception of these libraries and tools.
Also it is not fair to say that IntelliJ provides tabs/spaces config because it provides the config for other languages. In reality in kotlin style configuration they have removed a lot of stuff which is available for java, to enforce rules they felt strongly about, but tab/spaces was left in place, because it just makes sense not to ask companies to switch their mature code bases or create a special case for Kotlin.
m
Does my suggestion work for you? Or is there something deeper I'm missing? I suspect IntelliJ still has tabs/spaces for Kotlin because it's a very contentious topic. Also because the Kotlin coding convention came AFTER the initial Kotlin plugin for IntelliJ, and the original default in IntelliJ isn't the same as the now current Kotlin standard. IntelliJ does support different indentation for different codes, so one could have tabs in other languages and spaces in Kotlin. But I think that would just be weird, so wouldn't actually do it 😉 So, is a custom config and a rule that enforces tabs sufficient for your needs? I'm not a dev on Detekt or anything, so just trying to help you with something that's not too onerous, and that works.
c
Your suggestion is a possibility, but it requries a little bit uncomfortable level of effort, thing is there is a set of rules which depend on indentation rule. So to use detekt for example, we'd have to do following: contribute tabs support to ktlint indent rule and all rules that depend on it, contribute support of the update ktlint support to detekt, which entails finding all rules detekt has which would break because of changed indent rule in ktlint, contribute fixes for that. All of this also needs to pass through "we don't want it because we adhere to JB style guide" argument somehow.
Second option: disable all those rules implement them as custom rules - we'll probably choose this one because it is much less time consuming. Because it allows us to deploy detekt right away with those rules disabled and add custom rules as we go. Thing is, it is suboptimal in grand scheme of things, would've been much better if it was part of core functionality
m
I'm curious. What do you use ktlint for that Detekt doesn't bring to the table? I use Detekt, but not ktlint, so curious. Also, I'm doing server side, and not Android. Thank you for explaining. I see where you're coming from, but given that ktlint/detekt don't support tabs, and if it doesn't appear on their roadmap, then unfortunately, you must be in the minority. So the teams would rather focus on new features. I'm sure you get this. Supporting both would increase effort for all changes, right?
c
Yes, I do. From the comments on the issues though I see that effort is not the problem, will is. I'm talking about ktlint because detekt uses ktlint internally (even its documentation refers to ktlint rules description)
m
Hmm, I'll have to dig into that, then (ktlint integration/usage within Detekt). Makes sense for them to leverage another library where possible, although I thought ktlint was used only for the formatting rules, and not any others.
c
yeah, probably you're right, and maybe switching ktlint rules to tabs won't break detekt... I'll have to check.
s
@Czar I recommend using the .editorconfig file. IntelliJ automatically picks this up, if you have it in the root of your project. Moreover, I suggest to deactivate all rules concerning the
spaces over tabs
issue. After that, I'd write a single custom rule in either detekt or ktlint that prevents using spaces over tabs. Perhaps you also want to correct spaces and transform that to tabs. The effort needed is really small. Both tools support custom rules very well. Please take a look at the following link for a guide on how to write custom rules in detekt. https://arturbosch.github.io/detekt/extensions.html HTH Markus
t
Detekt itself uses tabs, no? I've seen that community contributions had mixed style in the past
s
@taso not anymore. It changed with RC12.
a
Ha this is quite funny, as I was a Tab-guy and started detekt with tabs 😉. I stopped when copy-pasting code into tools like GitHub broke formatting. Also at my company everything is enfornced with spaces now. Unfortunately we can't "just" support a tabs rule next to the spaces rule from KtLint. There is the "failfast" options which just turns all rules on. These two rules can;t live together. So best would be really to open source a new rule set with the tab rule and users can configure it.
👍 1
t
Thanks for sharing 😊
273 Views