Skip to content
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

KtLint enforces multi-line conditional statements to put operators on previous line #1043

Closed
Ciatzo opened this issue Dec 29, 2020 · 1 comment

Comments

@Ciatzo
Copy link

Ciatzo commented Dec 29, 2020

Hey,

When formatting with KtLint, it enforces to put operators like && or || on the previous line.

Before KtLintFormat:
Screenshot 2020-12-29 at 17 35 19

which gives this error:
Screenshot 2020-12-29 at 17 23 25

And gets restructured to

Screenshot 2020-12-29 at 17 35 00

When running KtLint Format.

However, the official style guide for Kotlin does not indicate that multi-line conditions should be structured this way. It only states:

If the condition of an if or when statement is multiline, always use curly braces around the body of the statement. Indent each subsequent line of the condition by four spaces relative to statement begin. Put the closing parentheses of the condition together with the opening curly brace on a separate line: (from KotlinLang)

Although the attached codeBlock does put the operators on the previous line, it does not state whether operators should be on the previous or next line to conform to code style. Why does KtLint choose to format this way?

If I would want to enforce putting operators on the next line, would this be possible with KtLint?

@romtsn
Copy link
Collaborator

romtsn commented Dec 29, 2020

Please check #163 (comment) and all related links. In short: none of the styleguides strictly define how to wrap binary operators and we intentionally decided to break after all of them to avoid ambiguity. There's no way to invert behavior of the existing rule, so you have two options: either create your custom rule or disable the chain-wrapping rule

@romtsn romtsn closed this as completed Dec 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants