How do people set up ktlint in their Android devel...
# ktlint
c
How do people set up ktlint in their Android development team workflows? My issue is that we now have ktlint added to my teams Android project, but everyone on my team loves to press cmd + shift + L (reformat code) and everyone uses like a slightly different version of intellij/Android Studio and so cmd shift L is always kind of messing things up. What's the best way to setup ktlint so that we always stay in sync? I know I've mentioned a few times above that I'm new at all of this code style/formatting (it's my first real team project) and so I'm lost to what the conventions are as to how to set this up.
s
For us, we run ktlint with -F as a pre commit step, so ktlint always wins
๐Ÿ‘ 1
If you have specific instances where ktlint and intelliJ disagree, feel free to file a bug and we can take a look
c
pre commit (excuse my ignorance) but does that happen locally?
I've also opened up https://github.com/JLLeitschuh/ktlint-gradle/issues/348 and https://github.com/pinterest/ktlint/issues/716 with the initial issues I hit. According to @tapchicomas comment in my issue, the source of this mess is https://github.com/pinterest/ktlint/issues/701
As you can probably tell, my 3 issues opened up across ktlint and ktlint-gradle just come from me trying to drop it into my teams project and just encountering issues. Very much noob level issues, but we're just trying to make sure that we do things in a smart way.
s
@Colton Idle yes that's correct -they run locally prior to putting up a pr
c
Thanks. Looks like I will ditch my effort to have cmd shift L re-order things in intellij correctly and instead move over to getting ktlint -F to run as a pre commit step. Out of scope at this point, but out of curiosity is there other things that teams typically run as pre commit steps? I'm assuming something like android lint is too excessive because it can take a while.
s
Yeah there's actually no longer a very good reason that we run it pre commit. Originally it was done to avoid having to reformat the entire codebase (since it runs only on the changed files) but we eventually did that anyway. And yes on some of our other repos we run, eg python linters. Static analysis is usually fast enough to run pre commit, but yeah something like Android lint would be too slow.
s
You can export your code style to editorconfig and commit that style to repo and ask everyone to use editorconfig setup for ide formatting
Don't forget ktlint is just a small subset of formatting rule for Kotlin but editor formatter has much more settings for every file type. You cannot expect ktlint to solve every formatting problem. We use editorconfig config to share style in the team and ktlint is just a helper.
๐Ÿ‘ 1
c
@Sinan Kozak not 100% sure what editorconfig is. Is this different than cmd shift L in Intellij?
s
That is a file that store formatter config and ide uses it whole cmd shift l. There is blog post about issue you posted from ktlint repo
c
Oh. I thought the point was that the formatter was limited from the issue I posted. I guess I'm confused again. ๐Ÿ˜ž
s
Yes that one. And remember ktlint only has some rule not everything
t
yep, I am also doing how @Sinan Kozak proposed - extracted all kotlin style to
.editorconfig
file and committed it to repo. But we also run pre-commit hook from
ktlint-gradle
as well. You could add a dependency to
clean
task to also run
addKtlintFormatGitPreCommitHook
task, so eventually your team will have up-to-date pre-commit hook
c
Oh great. Thank you @Sinan Kozak @tapchicoma and @Sha Sha Chu Looks like I will do the following: 1. Extract kotlin style to .editorconfig and put it on the repo (this will get cmd shift L working the same across team members) 2. Add the ktlint -f to run as a pre-commit hook (this will make sure that code passes the ktlint at time of a commit) @tapchicoma If I'm understanding correctly, you're basically saying that the only way to really "enforce" that every developer has the pre-commit hook setup is to add a new task to the clean task? I think this finally gives me a good solution for integrating ktlint with my team. I think the entire intellij plugin thing confused me because I thought that all of this was not possible basically.
t
It is not really enforcing, but adds easy way to install it. You could enforce by running ktlint on CI and fail the build on code style violations
c
Understood. I guess I was just asking if there's any way to add pre-commit hooks to my repository so that everyone on my team has the same pre-commit hook? It looks like there's no which is why you mention adding the task to the clean task, right?
t
Yes, you could not commit pre commit hook into git ๐Ÿ™‚
c
Ah yes. Thank you all so much. I'm so excited to add this! Finally my team will all be committed code with a unified style and formatting!
๐ŸŽ‰ 2
@tapchicoma @Sinan Kozak is there any chance you can share how to extract a valid editorconfig from ktlint settings? It's my first time using editorconfig and so now after reading up about it, I understand the concerns that @Sinan Kozak pointed out ("ktlint is just a small subset of formatting rule for Kotlin but editor formatter has much more settings for every file type.") I just want to get an editorconfig file that's compatible with ktlint... maybe compatible is the wrong word. If ktlint is based on official kotlin style guide, then I want a kotlin style guide version of editorconfig. That's where I'm having trouble. I'm not sure where to get this editorconfig.
s
ANdroidStudio or Intellij, open code style setting in preference. There is a menu at top. You can find export action there
there is also and import option that you can import official codestyle to your ide
I can also suggest commiting codestyle xml in .idea folder instead of editorconfig, if that is a hustle
c
We all use Android Studio so I think editorconfig will be perfect (as per your rec on twitter and here). I just didn't know how to take the official kotlin style + official android kotlin style + ktlint with android style enabled = editorconfig file ๐Ÿ˜„ If that makes sense at all @Sinan Kozak I tried last night to create an editor config and while it seemed to work, it was formatting differently when using cmd shift L vs ktlintFormat. which is why I asked this question today.
s
I suggest delete you codestyle xml
Apply kotlin standard and android code style from idea. Than export editor config
c
Thank you! Those steps make sense. official kotlin style and android code style are available directly in AS? I only thought that IntelliJ shipped with a slightly different default and didn't actually ship official kotlin style due to legacy reasons.
s
There is a menu on top right at code style page for importing defaults
c
Talking about this?
s
Yes
If you go xml page and java page there are other default
c
Nice. I didn't even realize that was there before. Do you know if there is a Android Kotlin Style Guide? Or am I missing it for some reason?
s
Nope but there ar suggestion
c
Got it. I might open up a bug report on google issuetracker to add the android kotlin style guide as a possibility here.
Maybe they'll add it. ๐Ÿ˜ƒ https://issuetracker.google.com/issues/155298994
โญ 2