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

Configuration to ignore max-line-length for strings #2453

Closed
mklnwt opened this issue Dec 21, 2023 · 3 comments · Fixed by #2480
Closed

Configuration to ignore max-line-length for strings #2453

mklnwt opened this issue Dec 21, 2023 · 3 comments · Fixed by #2480

Comments

@mklnwt
Copy link

mklnwt commented Dec 21, 2023

Add a configuration to allow for long strings exceeding max-line-length.
Splitting long lines of strings over multiple lines can obstruct readability of code.

Optional: differentiation between single (") and multiline (""") strings.

// Assume that the last allowed character is
// at the X character on the right                         X
log.info {
    "Long line of logging you might want to keep in one line"
}

Expected Behavior

Rule should ignore the long string if it's the only code in line or if the line could not be broken down otherwise.

Current Behavior

Exceeded max line length (?) (cannot be auto-corrected) (standard:max-line-length)

Additional information

  • Current version of ktlint: 1.1.0
@paul-dingemans
Copy link
Collaborator

Raw string literals ("""...""") are already excluded from max line length violations as they are typically used in multiline contexts like:

log.info {
    """
    Long line of logging you might want to keep in one line
    """
}

Strings on the other hand are often used in combination with other elements on the same line. It then becomes unclear what to do if that line exceeds the maximum. Should it be accepted because the line contains a string anywhere, or only when it is the last element on the line? And what if it is the last element, but followed by an EOL comment.

But it might be reasonable to allow lines that only contain on single string literal to exceed the max line length. This is something that can be considered for the next minor release as this is not a regression bug.

@mklnwt
Copy link
Author

mklnwt commented Dec 23, 2023

Last element on the line is the most important for now (i.e. my logging example).

A bit trickier would be:

log.info {
    "Long line of logging with a variable ${foo.bar} you might want to keep in one line"
}

I would want to still keep that in one line.

Let me think a little deeper about other cases.

@paul-dingemans
Copy link
Collaborator

log.info {
"Long line of logging with a variable ${foo.bar} you might want to keep in one line"
}

From the parser perspective this log statement still contains a single string. Following would however not be accepted:

log.info {
    "Long line of logging with a variable " + foo.bar + " you might want to keep in one line"
}```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants