Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

These two formatters both suffer from the flaw that what intellij does when it autoformats is different from what either of those two do and can only be controlled through plugins. Especially when it comes to controlling imports this stuff is just broken in Intellij. The settings for this are not actually part of the code formatting settings.

Most sane code formatters & code standards: "don't use import wild cards!" Intellij's default since forever: "wildcards all over the place; please jump through hoops to get rid of them".

I've given up on having code formatters in my kotlin builds for this reason. Just not worth the hassle of trying to make both sides just do the same thing by default so I don't have to think about it. And then having to explain to every new team member how they have to customize their intellij setup so it isn't broken. I just gave up on it. These days I tell people, auto format your code, leave the settings alone. Try to only format the code you modified to avoid endless formatting related diffs.

The fix would be for Jetbrains to finally fix their IDE to have proper formatting that includes the behavior of organize imports and is perfectly in sync with whatever your build file says is the code style rather than whatever manual or built in stuff. Any IDE settings for this should IMHO just be removed as they are completely and utterly wrong in the presence of a build file that says how things should be formatted. Just default to whatever your build file says is the code standard with some sane default that just comes with the kotlin compiler. This should not be optional. You want to override it: do it in your build file.

Auto format on save in intellij. CI build verifies that nothing is mis-formatted because the IDE did it right because it does whatever the build file specifies needs doing without second guessing that. Have a githook to format before commit (optional). Zero manual fixing of anything ever related to formatting. It should not have a chance to go wrong and break the build unless somebody forgot to format or have a commit hook to do so. Just import the project and everything does the right thing. That would be the goal. IMHO it's not technically hard but it would require addressing what is likely to be quite a bit of technical debt related to this in intellij. And it's about time they did that.



Not sure what's preventing you from setting up Intellij to do what you want, even without .editorconfig. I setup intellij editor format preferences to follow the formatting I setup with my build-based linter/formatter. I just change the IDE settings so that they match my formatter, use it as the project setting, and then commit the codestyle files in .idea folder so that it's always there when I import or open a project. Never have to touch it again, autoformat in the IDE works and is 100% compatible with my ktfmt, ktlint, checkstyle, spotless or whatever. Imports, order, line wraps, indentation, etc... it 'just works' for me.

Yes, it's a minor additional step at first, but it's like 15 minutes tops, and after the first time you do it, you simply copy the xml from project to project if you want to use the same settings.


What you are suggesting here is a lot of manual work. What I'm complaining about is that there is no sane/easy way to avoid having to do all of that and getting everybody on the team to do the exact same things.

This should be as simple as "import the project". Done. There should not be any extra steps. No settings fiddling. No magic files to copy. No plugin installations to intellij. Etc.


You wrote this entire comment but ktlint can generate a .editorconfig (and in fact used to generate a IntelliJ specific config file) which is a universal way of solving this exact problem.

Pretty sure the FB equivalent also has a matching .editorconfig

Also you could just setup a prepush hook so they run before your CI does which afaik both give you examples of.


I use pre-commit/pre-push hooks to apply formatters like Spotless/ktfmt etc, it's too much work to try to fight Intellij's formatting.

There's a ktfmt IntelliJ plugin that does work most of the time IME, if you're willing to give it a shot.


My auto-generated config has been pretty solid

The only time the pre-push hook fails is if a line is too long and there's no "canonical" way to fix it (so it'll create a method chain for example, but it won't do string concatenation since there's N ways to break a string across lines)




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: