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

Ignore line length in string literals #659

Closed
pkubowicz opened this issue Nov 26, 2019 · 13 comments
Closed

Ignore line length in string literals #659

pkubowicz opened this issue Nov 26, 2019 · 13 comments

Comments

@pkubowicz
Copy link

Let's have `.editorconfig'

[*.{kt,kts}]
max_line_length=40

and Kotlin file:

val string1 =
        "a very very very very very long string literal"

val string2 = "a very very very very very long string literal"

Ktlint 0.35.0 rejects both declarations:

LongLines.kt:2:1: Exceeded max line length (40) (cannot be auto-corrected) (max-line-length)
LongLines.kt:4:1: Exceeded max line length (40) (cannot be auto-corrected) (max-line-length)

I think at least line 2 should be accepted - it contains only whitespace and a string literal.

@Tapchicoma
Copy link
Collaborator

Why it should be accepted? 🤔

You could split string literal using + sign. If you still want to have it long, add disable block around it.

@pkubowicz
Copy link
Author

pkubowicz commented Nov 27, 2019

Since comments and raw strings are already not taken into account when checking line length.

Splitting string literal is risky - this string may be the expected value in a test and you may miss something if the string is contained in 2 lines:

assertThat(string).isEqualTo(
    "very very long" +
    "string but I missed space between lines"
)

Disabling blocks are very invasive as you need to open and close them, so you get 1:2 signal to noise ratio: 2 lines to satisfy ktlint in 1 line of code.

@Tapchicoma
Copy link
Collaborator

Another way - would be support disabling particular rules via @Suppress annotation.

@pkubowicz
Copy link
Author

Supporting @Suppres would be nice in this case.

@ansman
Copy link

ansman commented Dec 12, 2019

I agree, string in exceptions, deprecations etc should be allowed to be of any length as it's very inconvenient to split them.

@k1dbl4ck
Copy link

k1dbl4ck commented Jul 27, 2020

Would like to add that back ticked function names in tests should also be handled / ignored:

@Test
fun `Should return something when passing something and something and the other thing is in this or that state`() {...}

In this case it cannot be split.

@VishalNehra
Copy link

VishalNehra commented Oct 25, 2020

+1
Looking for a handly .editorconfig rule that I can include in disabled_rules

@florianmski
Copy link

Just adding my two cents for this issue that we hit in our current project.
We found out as mentioned here #659 (comment) that this is not practical for tests names and sometimes you do really need that extra 20 chars to have a clear test name.
I first set the max_line_length to off but it still seemed to auto format some of the code according to some hardcoded limit somewhere so I came up with the following which has been working great for the last few months or so, hope that helps:

in .editorconfig

[**/test/**.kt]
# setting this to `off` seems to make the IDE behave like there's a hard limit when auto-formatting
# which will wrap some assignments and methods calls, setting this up to a high value do the trick
max_line_length=1000

@mideuger
Copy link

I have the same problem, long string literals are common and splitting them is not a working solution and does not work with const values.

I've submitted 2 PR that aim to solve this problem in different ways :

I would much rather prefer the second approach but I'm glad if one of them is a accepted/improved or the issue taken down in some way 🙂

@marciogranzotto
Copy link

marciogranzotto commented Jun 11, 2021

Just adding my two cents for this issue that we hit in our current project.
We found out as mentioned here #659 (comment) that this is not practical for tests names and sometimes you do really need that extra 20 chars to have a clear test name.
I first set the max_line_length to off but it still seemed to auto format some of the code according to some hardcoded limit somewhere so I came up with the following which has been working great for the last few months or so, hope that helps:

in .editorconfig

[**/test/**.kt]
# setting this to `off` seems to make the IDE behave like there's a hard limit when auto-formatting
# which will wrap some assignments and methods calls, setting this up to a high value do the trick
max_line_length=1000

looks like you can define as max_line_length=-1 if you want to ignore that rule, at least on 0.31.0 which is the version that I'm using

@shawnthye-zalora
Copy link

@Tapchicoma what about Room database Query?

if we break like normal string, we lose the Query syntax support from Room 🤔

@paul-dingemans
Copy link
Collaborator

looks like you can define as max_line_length=-1 if you want to ignore that rule, at least on 0.31.0 which is the version that I'm using

Value -1 is not accepted, but you can set the value to off which will be translated by Ktlint to value -1.

@paul-dingemans
Copy link
Collaborator

String literals will not be ignored when checking whether the maximum length of the line is exceeded. Consider using a raw string literal instead, or the @Suppress("ktlint:standard:max-line-length").

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

10 participants