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

Indentation rule inconsistently identifies issues with multiline string closing quotes between functions and properties #1971

Closed
3flex opened this issue Apr 23, 2023 · 3 comments · Fixed by #1996
Milestone

Comments

@3flex
Copy link
Contributor

3flex commented Apr 23, 2023

Expected Behavior

Indentation rule treats properties and functions similarly when identifying issues with multiline string closing quotes.

Observed Behavior

The rule is identifying issues with multiline string closing quotes inconsistently, when the only difference is whether the multiline string is returned from a function or is used as a property.

Steps to Reproduce

Throws violation on last line, first char.

fun foo() = """
    <!--suppress XmlUnusedNamespaceDeclaration -->
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
""".trimIndent()

Throws violation on second last line, first char.

fun foo2(): String {
    return """
        <!--suppress XmlUnusedNamespaceDeclaration -->
        <manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
    """.trimIndent()
}

Does not throw. The only difference between this and the first case is the change from fun foo() to val foo.

val foo = """
    <!--suppress XmlUnusedNamespaceDeclaration -->
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
""".trimIndent()

Your Environment

  • Version of ktlint used: 0.49.0
  • Relevant parts of the .editorconfig settings: N/A, using detekt wrapper, but also verified by adding failing test cases to IndentationRuleTest
  • Name and version (or code for custom task) of integration used (Gradle plugin, Maven plugin, command line, custom Gradle task): Gradle
  • Version of Gradle used (if applicable): 8.1/8.1.1
  • Operating System and version: Windows 11 22H2
3flex added a commit to 3flex/detekt that referenced this issue Apr 23, 2023
This probably should have been changed previously, but it now seems
required to work around a bug in ktlint (pinterest/ktlint#1971)
3flex added a commit to 3flex/detekt that referenced this issue Apr 23, 2023
@paul-dingemans
Copy link
Collaborator

Throws violation on second last line, first char.

I can can not reproduce this finding on fun foo2. Like with fun foo the error is reported at the beginning of the line containing the closing quotes.

$ cat << EOF | ktlint-0.49.0 --stdin
> fun foo() = """
>     <!--suppress XmlUnusedNamespaceDeclaration -->
>     <manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
> """.trimIndent()
> 
> fun foo2(): String {
>     return """
>         <!--suppress XmlUnusedNamespaceDeclaration -->
>         <manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
>     """.trimIndent()
> }
> 
> val foo = """
>     <!--suppress XmlUnusedNamespaceDeclaration -->
>     <manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
> """.trimIndent()
> EOF
17:22:41.009 [main] INFO com.pinterest.ktlint.cli.internal.KtlintCommandLine - Enable default patterns [**/*.kt, **/*.kts]
<stdin>:4:1: Unexpected indent of multiline string closing quotes (standard:indent)
<stdin>:10:1: Unexpected indent of multiline string closing quotes (standard:indent)

Does not throw. The only difference between this and the first case is the change from fun foo() to val foo.

val foo = """
    <!--suppress XmlUnusedNamespaceDeclaration -->
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"/>
""".trimIndent()

This is indeed not consistent and needs to be fixed.

@paul-dingemans paul-dingemans added this to the 0.49.1 milestone Apr 23, 2023
@paul-dingemans
Copy link
Collaborator

@3flex Can you please respond on previous comment? It would be more convenient for me to know whether a problem is to be fixed with example of fun foo2().

paul-dingemans added a commit that referenced this issue May 6, 2023
…e to keep formatting of other code styles consistent with `0.48.x` and before

Closes #1971
@paul-dingemans
Copy link
Collaborator

Ktlint 0.48.2 and older allowed for an asymmetrical form of aligning the opening and closing quotes. E.g., if the opening quotes are after but on the same line as the assignment operator, and the closing quotes are on a next line, then the closing quotes may be aligned with the start position of the statement.

In 0.49.x the check of the closing quotes has accidentally been changed for all code styles while it should only have veen changed for the new ktlint_official code style only. As the ktlint_official code style will allways wrap multiline expression to start on a new line (via rule standard:multiline-expression-wrapping) the formatting will be consistent for both functions and properties.

paul-dingemans added a commit that referenced this issue May 7, 2023
#1996)

* Restrict indentation of closing quotes to `ktlint_official` code style to keep formatting of other code styles consistent with `0.48.x` and before

Closes #1971
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

Successfully merging a pull request may close this issue.

2 participants