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 forces single-line raw string to new line when used as named argument #930

Closed
thomasboyt opened this issue Sep 28, 2020 · 3 comments

Comments

@thomasboyt
Copy link

Expected Behavior

The following code should lint without complaints:

fun myFun(regexp: String) {}

fun exampleCall() {
    myFun(regexp = """^[^\s@]+@[^\s@]+\.[^\s@]+$""")
}

Observed Behavior

ktlint complains with the following error:

/path/to/Example.kt:4:19: Missing newline after "=" (indent)

ktlint format will then reformat the code to:

fun myFun(regexp: String) {}

fun exampleCall() {
    myFun(regexp =
            """^[^\s@]+@[^\s@]+\.[^\s@]+$""")
}

This is slightly annoying on its own, but then this bug seems to interact with other indentation issues (possibly #764 or #805) where IDEA and ktlint's formatting rules don't match each other. IntelliJ IDEA's formatter (with the default applyToIDEAProject settings) will reformat the file to:

fun myFun(regexp: String) {}

fun exampleCall() {
    myFun(
        regexp =
        """^[^\s@]+@[^\s@]+\.[^\s@]+$"""
    )
}

Which then causes another complaint from ktlint:

/path/to/Example.kt:6:1: Unexpected indentation (8) (should be 12) (indent)

Subsequent ktlint/IDEA formats will cycle between these two indentations.

Steps to Reproduce

The above code sample should suffice for reproduction.

Your Environment

  • Version of ktlint used: 0.38.1
  • Name and version (or code for custom task) of integration used): Gradle plugin 9.4.0
  • Version of Gradle used (if applicable): 5.6.2
  • Operating System and version: Mac OS X 10.15.6 x86_64

Related Issues

I know #764, #805, and some other issues cover bugs around the new indent rule. I couldn't tell immediately whether this issue should be folded into one of those or stand on its own. I think an important distinction is that, regardless of the bugs ktlint has around formatting multi-line raw strings, the main problem I want to cover with this ticket is that a short raw string should be allowed to be on a single line when used as a named argument.

Additionally, I think this bug is another good reason to prioritize #631. I wouldn't mind this bug nearly as much if I had a simple way to ignore it the indent rules for a given line or block.

@thomasboyt
Copy link
Author

thomasboyt commented Sep 28, 2020

While doing a workaround for this (extracting my raw string to a variable, so I could pass the variable instead of it being defined inline), I noticed ktlint also turns:

const val EMAIL_RE = """^[^\s@]+@[^\s@]+\.[^\s@]+$"""

Into:

const val EMAIL_RE =
    """^[^\s@]+@[^\s@]+\.[^\s@]+$"""

I'm not sure if this is a bug or intentional (I can't find any style guide reference indicating raw strings should be defined on their own line). Thankfully, IDEA doesn't try to further reformat that code, so it still works as a workaround for now.

@romtsn
Copy link
Collaborator

romtsn commented Sep 28, 2020

This was fixed in 0.39.0, please check out the latest version

@romtsn romtsn closed this as completed Sep 28, 2020
@thomasboyt
Copy link
Author

Ah, apologies for the duplicate; ktlint-gradle still defaults to 0.38.1 and I didn't think to check if it was the newest!

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