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

Non chained calls should be on the same line if short enough #1077

Closed
hiqua opened this issue Feb 11, 2021 · 5 comments
Closed

Non chained calls should be on the same line if short enough #1077

hiqua opened this issue Feb 11, 2021 · 5 comments

Comments

@hiqua
Copy link

hiqua commented Feb 11, 2021

Expected Rule behavior

Right now, ktlint (with --experimental) does not complain with this snippet:

class MissingRules {
    fun f() {
        3

            .compareTo(2)
    }
}

In this case, since 3.compareTo(2) is shorter than the maximum line length and it does not consist of chained calls, and it should be written in one line. With long arguments, it should still look like 3.compareTo( with the arguments on the next lines.

This kind of superfluous / misleading line breaks can happen with automatic conversion from Java to Kotlin using Android Studio.

Additional information

  • Current version of ktlint: 0.40.0
  • Styleguide section: I don't think this is covered in the style guides, they mostly mention when the line should be broken rather than not.
@Tapchicoma
Copy link
Collaborator

I am not sure that such rule should exist as such style is subjective - in more complex scenarios some people tend to move .some_statement() to the next line.

Not related directly to the topic - KtLint didn't complain about empty line between 3 and .compareTo(2)? 🤔

@hiqua
Copy link
Author

hiqua commented Feb 27, 2021

KtLint didn't complain about empty line between 3 and .compareTo(2)?

No, when run like this: ktlint --experimental MissingRules.kt

Version:

ktlint --version
0.40.0

@paul-dingemans
Copy link
Collaborator

KtLint didn't complain about empty line between 3 and .compareTo(2)?

No, when run like this: ktlint --experimental MissingRules.kt

Version:

ktlint --version
0.40.0

Fixed in #1105

@hiqua
Copy link
Author

hiqua commented Mar 11, 2021

I'm still unsure about codebases where something like:

someLongVariable
    .someNonChainedCall()

would be normal.

Do you have such an example? I can think of things like

("some" + "string" + "which is concatenated making the line very long")
    .someStringMethod()

but even then I don't know if people would format it this way unless it breaks the line limit.

In any case free to close if you don't think it makes sense to implement this.

Tapchicoma added a commit that referenced this issue Jun 23, 2021
* Lint/format needless blank lines in dot qualified expression (#1077)

* Lint/format needless blank lines in dot qualified expression (#1077)

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
Co-authored-by: Yahor Berdnikau <egorr.berd@gmail.com>
@Tapchicoma
Copy link
Collaborator

It is very subjective and if you still want to have it - best option for you would be to implement custom rule.

romtsn added a commit to paul-dingemans/ktlint that referenced this issue Aug 8, 2021
* Lint/format needless blank lines in dot qualified expression (pinterest#1077)

* Lint/format needless blank lines in dot qualified expression (pinterest#1077)

Co-authored-by: Paul Dingemans <pdingemans@bol.com>
Co-authored-by: Roman Zavarnitsyn <rom4ek93@gmail.com>
Co-authored-by: Yahor Berdnikau <egorr.berd@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants