Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Raw strings forced on a separate line by indent rule #711

Closed
lwasyl opened this issue Mar 5, 2020 · 4 comments · Fixed by #849
Closed

Raw strings forced on a separate line by indent rule #711

lwasyl opened this issue Mar 5, 2020 · 4 comments · Fixed by #849

Comments

@lwasyl
Copy link

lwasyl commented Mar 5, 2020

KtLint 0.36.0

Raw strings don't seem to play well with indent experimental rule. For example this:

description = """
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lacus arcu, blandit non semper elementum,
    """.trimIndent(),

reports an error: [experimental:indent] Missing newline after "="

This (weird) formatting:

description =
"""
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lacus arcu, blandit non semper elementum,
    """.trimIndent(),

reports wrong indentation for the first """, however this:

description =
    """
    Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris lacus arcu, blandit non semper elementum,
    """.trimIndent(),

is automatically reformatted by IntellIJ to the previous variant (with first """ not indented)

@Tapchicoma Tapchicoma added the bug label May 25, 2020
@henrikbarium
Copy link

This behavior is especially annoying when using single-line raw strings, e.g. as function arguments. (Using named parameters makes it even worse.)

data class Example(val string: String)
val e = Example(string = """a"b"c""")

The above code snippet is auto-formatted by ktlint into the following.

data class Example(val string: String)
val e = Example(string =
        """a"b"c""")

However, this is not valid according to ktlint, so it also includes some errors.

  • Missing newline before ")"
  • Missing newline after "("

Running ktlint with auto formatting again then gives the following.

data class Person(val name: String)
val p = Person(
    name =
        """a"b"c"""
)

This is quite a lot worse than the initial code example, especially if we add more arguments (which may even contain raw strings).

@Tapchicoma Tapchicoma added this to the 0.38.0 milestone Jun 22, 2020
@adwsingh
Copy link

I was planning to work on this, if nobody has already started.

@romtsn
Copy link
Collaborator

romtsn commented Jun 28, 2020

@MehWhatever would be awesome, most likely it's just commenting out these lines:

// force """ to be on a separate line
if (!node.nextCodeLeaf().isRawString()) {
return
}

But I'm not sure, definitely needs some testing.

charleskorn added a commit to batect/batect that referenced this issue Jul 14, 2020
This isn't the most recent version available, but the most recent
version has pinterest/ktlint#711 which
causes raw strings to be formatted weirdly.
@cortinico
Copy link

Adding another example to this issue. The following line:

val html = """<span style="color:red;">my span</span>"""

Gets reformatted by ktlint v0.37.2 to this:

val html =
    """<span style="color:red;">my span</span>"""

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants