-
Notifications
You must be signed in to change notification settings - Fork 506
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
"Unexpected indentation" on wrapped function declarations #26
Comments
Hi. This is because Code Style for Kotlin hasn't been applied ("Code Style" -> "Kotlin" -> "Wrapping and Braces" -> "Method declaration parameters" -> "Align when multiline" is on while it should be off). Right now indentation logic is simple: 4 spaces everywhere (which makes rules about how to align method parameters, function call arguments, etc. consistent and easy to remember). The code above can be written (reformater can do it for you provided aforementioned flag is off) as fun reformat(
str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' '
) {
...
} Kotlin compiler source is quite inconsistent in term of code style (it was breaking pretty much every rule last time I tried ktlint on it) so it's not really the best place to look. There is a relevant discussion on vertical alignment at Kotlin/kotlin-style-guide#23 (comment). So far, ktlint tried to stay away from any kind of vertical alignment just to keep things simple. |
I don't think that discussion is talking about exactly the same thing, though they are related. They're talking more about alignment of elements within the line, like infix operators or parameters to calls of the same function, by adding multiple internal spaces. I do sympathize with @yole's comment, re: "The problem with any kind of vertical alignment", and I think the problems he mentions do apply in this case (though perhaps to a lesser degree than in some others). Interestingly, in Kotlin/kotlin-style-guide#15 (comment) he indicates that he thinks it's important that parameters align, and doesn't like being forced to have a line break after the open paren. Inserting the newline leads to some somewhat ugly cases in my code:
Relying on parameter alignment to fix these does seem kind of ugly, though, so I'm inclined to agree with you that this sort of alignment isn't such a great idea. So now I'm reformatting my code like this:
and this:
I also egrepped for '<fun>.*([^)]+$' in my code and found a few other instance of this that just happened to fall on multiples of 4. It looks the recommended indentation for functions hasn't really been nailed down yet, but if ktlint is going to complain about non-multiple-of-4 indents, then perhaps it should also complain about function (or constructor) declarations that have multi-line parameter lists with a parameter on the first line. |
Yeah, I didn't mean that discussion is about the exact same problem. Vertical alignment comes in many different forms and fun reformat(str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' ') {
...
} is just one of them. Imagine that you decided to rename fun format(str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' ') {
...
} multiple-of-4 rule eliminates this kind of problem. fun reformat(
str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' '
) {
...
} stays fun format(
str: String,
normalizeCase: Boolean = true,
upperCaseFirstLetter: Boolean = true,
divideByCamelHumps: Boolean = false,
wordSeparator: Char = ' '
) {
...
} Regarding a newline before first parameter in multi-line parameter declaration, personally I feel like it would be a good thing (standardjs for example has a similar rule for objects (object-property-newline) which states that "object properties must go on a new line if they aren't all on the same line") but I'm not convinced (yet) that this should be enforced by ktlint. It all depends on the outcome of Kotlin/kotlin-style-guide#15 (comment). |
Starting from 0.5.0 ktlint accepts both fun f(
val a: Any,
val b: Any,
val c: Any
) {
} and fun f(val a: Any,
val b: Any,
val c: Any) {
} as valid. 🎈 |
An interesting fact I just discovered about Android Studio is that it sets
The way to fix this is opening After doing this, when reformatting code, ktlint won't complain about indents :-) (I'm using version |
|
The following function appears in the Kotlin Reference:
If you copy this to a source file (and remove the ellipsis),
ktlint
will complain:This style also appears numerous times within the Kotlin compiler source, and when asked to reformat the code, this is the indentation style used by IntelliJ, so I think this error is incorrect.
The text was updated successfully, but these errors were encountered: