https://kotlinlang.org logo
#feed
Title
# feed
c

Carmi Grushko

03/28/2021, 7:01 PM
Hi! Wanted to share https://facebookincubator.github.io/ktfmt/, which allows you to check out
ktfmt
, a predictable Kotlin code formatter, inside the browser. ktfmt is distinctive in that it always produces the same output, regardless of how the code looks initially. It is based on google-java-format, if you're familiar with it from Java. (The website is powered by an AWS Lambda which is notorious for slow cold-startup times for JVM programs, so it might take a few seconds the first time to get a response. When running locally, it is fast enough to run as a save hook.)
🤘 3
K 4
b

Big Chungus

03/28/2021, 7:49 PM
Strongly recommend ktfmt to everyone. Tried everything there is (ktlint, prettier, detekt...) and loved ktfmt simplicity and consistency most.
❤️ 2
d

Dmitry Kandalov

03/28/2021, 8:38 PM
“It is by design non-customizable in order to promote consistency.”
e

Emil Kantis

03/28/2021, 9:54 PM
Could you explain how to run it as a save hook? 🙂
g

gammax

03/28/2021, 10:00 PM
Side note: there is also a #ktfmt channel to talk about it blob grin
👍 1
k

kqr

03/29/2021, 7:42 AM
shouldn't be indents 2 spaces?
c

Carmi Grushko

03/29/2021, 7:43 AM
We never got to it - https://github.com/facebookincubator/ktfmt/issues/60 (Internally, we use a different plug-in that does this, so I can appreciate how convenient it is)
“Kotlinlang” style is 4/4 indents, “Java-like” is 2/4 spaces. (Block / continuation respectively). There’s another style, “google” which I didn’t expose in the website but is available in ktfmt, that does 2/2 spaces.
k

kqr

03/29/2021, 7:47 AM
ah I thought google one is default
b

Big Chungus

03/29/2021, 7:48 AM
Am I the only one hating the fact that "official" kotlin style chose 4 space indent? Given the number of nesting in kotlin that puts most of the code to far right
c

Carmi Grushko

03/29/2021, 7:58 AM
“Google” here doesn’t refer to the android guidelines, but to an internal style that may be used internally at google. I should rename it at some point to reduce confusion. Internally at Facebook we use the “Java -like” style because it’s easier to interop with Java like that, but I appreciate that different organizations really want different things. I had hoped ktfmt would help standardize things but maybe not at this point.
b

Big Chungus

03/29/2021, 7:59 AM
Is java-like the default, though?
c

Carmi Grushko

03/29/2021, 8:00 AM
Java-like is the default, correct.
a

Alan B

03/29/2021, 12:59 PM
I just wish we didn’t use 4 spaces. I don’t understand why, I never have. I’ve standardized on 2 spaces for JS and Kotlin, I won’t go back and change Java. 4 uses up too much whitespace, and I’m sick of horizontal scrolling 4 is just ridiculous, remember this is all all MY opinion, not a flame war. No other written language construct uses up so much whitespace between statements, sentences, paragraphs, etc… well, unless you write screenplays as I do.
@Big Chungus I totally agree. I just stated I loathe 4 spaces, it’s a ridiculous default. I don’t get the “interop with java”. It makes no sense. We don’t write Java in Kotlin code nor the other way around. Even if you have a mixed project, a simple
.editorconfig
solves this problem. I am in the 2 space camp and will always be. I also don’t use
;
in my javascript, nor does my team. 😄 If we want kotlin to look like Java, the write Java. Or, start using semicolons on everything. Don’t use type inference, don’t use external functions or extension functions, or Kotlin features. If you want the Java look and feel, use Java. The worst was the Sun Java coding conventions. Go look at that original source from 1.1. That’s a hot mess.
Example here. That
fillMode
assignment here is broken up into yet another line because it’s at column 20 where it could have been at 10. That makes things harder to read. Not to mention that code should be extracted. Too many if statements in a loop. Too hard to test. That’s off topic. But anyway. all just my opinion.
d

Dmitry Kandalov

03/29/2021, 1:31 PM
Or maybe it’s a good constraint to avoid code which is too nested? 🤔
b

Big Chungus

03/29/2021, 1:32 PM
In that case I'd rely on some linter to scream at me rather than leaving it to the formatter
c

CLOVIS

03/29/2021, 2:02 PM
@Dmitry Kandalov that's why Linux kernel development uses tabs of size 8.
👍 1
Anyway, the solution is to enforce tabs. Using tabs everyone can have the size they prefer.
t

tddmonkey

03/29/2021, 3:11 PM
4 vs 2 - this is why tabs are superior - you can set the tab stop to be whatever you want. Tabs are also more accessibility friendly
n

nkiesel

03/29/2021, 4:12 PM
What exactly is covered by kfmt? I added `{}`to the last one-liner
if/else
and the formatter did not reverse that. So clearly such braces are not covered in "how the code looks initially". Is it really exclusively adjusting whitespace?
c

Carmi Grushko

03/29/2021, 4:24 PM
@nkiesel that’s correct. Apart from removing semicolons and unused imports, we’ve been reluctant to change code (eg {} into = Unit). We believe this fits better in a linter or entirely forbidden at the compiler level. We might revisit this decision in the future, of course.
n

nkiesel

03/29/2021, 4:33 PM
"removing imports": does it sort imports as well or really just removes unused ones?
c

Carmi Grushko

03/29/2021, 4:50 PM
@nkiesel it also sorts them. You can check it out at https://facebookincubator.github.io/ktfmt/
n

nkiesel

03/29/2021, 6:52 PM
Hmm. So is there a definite list of what kfmt does?. So far I have • normalizes whitespace • removes unnecessary semicolons • removes unnecessary imports • sorts imports Anything else? (I checked the docs but could not find this information). I also think that while you might have good (internal) reasons to default to 2 spaces, the official Kotlin coding guideline clearly states: "Use four spaces for indentation". So calling this the "Dropbox style" seems misleading at best. I'd rather see that be the default and you should try to convince the Kotlin designers to adopt your "2 spaces" convention. (P.S.: Overall very nice; don't take my comments as any indication that I'm not grateful for this!)
c

Carmi Grushko

03/29/2021, 7:00 PM
@nkiesel that list sounds comprehensive. Ktfmt is first and foremost a code formatter, taken in the meaning of normalizing whitespace; with some minor extras. There are currently 4 supported styles - • Facebook aka Java-like : 2/4 • Kotlinlang: reflects the Kotlin language guidelines, 4/4 • Google internal (not android guidelines ) - identical to kotlinlang but using 2/2 • Dropbox - deprecated - identical to Facebook but using 4/4 @Javier already suggested we switch the default to kotlinlang, but that doesn’t seem like a pressing issue because it’s trivial to set the preferred style.
b

Big Chungus

03/29/2021, 7:04 PM
Please don't switch the default to kotlinlang 😀
n

nkiesel

03/29/2021, 7:08 PM
Perhaps adjust the GitHub README.md then, because that - in the "from the command line" - only mentions
--dropbox-style
. And I do not see
facebook-style
in the source code...
👍 1
j

Javier

03/29/2021, 7:15 PM
IMO Kotlinlang should be the default one because I think it is more used and a lot of people will find for it. At the end, it is the official format. Personally I don't like 2 indents (I have a big line height too because I would like to see easily everything instead a condensed block), but I can understand it should be the default if Kotlin had it.
n

nkiesel

03/29/2021, 7:16 PM
Another place is the FAQ which also only talks about "--dropbox-style". And briefly looking at the kfmt source code is seems kotlinlang style is a variation of google style and not dropbox style. Which brings up the question: what else besides 2/4 for indentation and continuation is different between the google/kotlinlang and facebook/dropbox styles?
c

Carmi Grushko

03/29/2021, 7:42 PM
Which brings up the question: ...
There are additional changes related to whitespace arrangement, such as where closing parenthesis / braces are placed. Easiest is to play around with the https://facebookincubator.github.io/ktfmt/ and see what you like best. Alternatively, compare https://github.com/facebookincubator/ktfmt/blob/master/core/src/test/java/com/facebook/ktfmt/GoogleStyleFormatterKtTest.kt to https://github.com/facebookincubator/ktfmt/blob/master/core/src/test/java/com/facebook/ktfmt/FormatterKtTest.kt
j

Jilles van Gurp

03/30/2021, 12:24 PM
From the point of view of minimizing diffs, 4 spaces is a sane default that avoids modifying every single indented line in most projects you unleash this on. Close to 100% of the Java and Kotlin projects I've worked on in the last 25 years used that as a default. Most IDEs have that as a default and you have to go out of your way to deviate from the defaults (change settings basically and insist everyone does so). It's encoded in many coding standards. Including Jetbrains' official coding guidelines for kotlin, intellij defaults, Android Studio defaults, implementing that, etc. The vast majority of kotlin/java projects out there on Github use that (because it is the default).
👆 1
n

Nikky

03/30/2021, 3:49 PM
if everyone uses idea and a gradle plugin is used to force the formatting settings to use ktfmt "somehow" then standardization would be less of a issue, but i am not sure if it is even possible to hook ktfmt up to run upon hitting the idea autformat shortcut
c

Carmi Grushko

03/30/2021, 3:51 PM
@Nikky the ktfmt IntelliJ plug-in does exactly this.
❤️ 2
n

Nikky

03/30/2021, 3:52 PM
but there is no sane way to have per-project plugins synced via git yet.. is there ? enabling it per project might be good enough though
c

Carmi Grushko

03/30/2021, 3:53 PM
That I don’t know.
j

Javier

03/30/2021, 4:09 PM
@Nikky you can share the ktfmt config in git, it is inside of the .idea folder (it will appear if you have enabled ktfmt idea plugin). But I am not a fan of sharing something inside .idea to git. Maybe if exists an .editorconfig in the project, the plugin could detect which format should use. What do you think @Carmi Grushko?
Sharing the ktfmt config file in git has another advantage, when you clone the project if you have the plugin installed or you install it, it will be enabled for that project
👍 1
I don't know if that behavior should be replicated if a .editorconfig exists and ktfmt read it to detect which format should use. Probably it is not a good idea
m

Michal Harakal

03/31/2021, 7:29 PM
Do you have any experience with KMP projects? I've got replaced `import kotlin.jvm.Throws`with
import kotlin.Throws
in actual JVM implementation file.
17 Views