Thomas
Thomas

Reputation: 64

Unexpected newline before '.' in chained method call

Given this code:

                tokenCredential
                    .getToken(
                        TokenRequestContext()
                            .addScopes("https://management.core.windows.net/.default"),
                    )
                    .block() != null

ktlint complains that "Unexpected newline before '.'" on the line with .block().

I've tried quite a lot of settings to try to avoid this. Neither of these seem to have any effect:

in .editorconfig (in the .kt/kts section):

ktlint_standard_chain-wrapping = multiline
ktlint_standard_chain-wrapping = enabled
ktlint_standard_chain-wrapping = disabled


in build.gradle.kts:

ktlint {
    additionalEditorconfig.set(
        mapOf(
            "ktlint_standard_chain-wrapping" to "disabled", // etc
        ),
    )
}

I was expecting this rule to be disabled by this (or rather the wrapping to be enabled). This has been happening since I upgraded to v1.5.0 of ktlint.

Upvotes: 1

Views: 68

Answers (2)

k314159
k314159

Reputation: 11100

In the current version of Ktlint, the rule is called Chain method continuation. Therefore, you need to use

mapOf("ktlint_standard_chain-method-continuation" to "disabled")

Upvotes: 0

The issue you're facing is that ktlint expects all dot operators "." to be on the same line as the closing parenthesis ")". This is part of ktlint's formatting rules. Here is the correct format according to ktlint:

tokenCredential.getToken(
    TokenRequestContext()
        .addScopes("https://management.core.windows.net/.default")
).block() != null

Notice that the dot operator "." after addScopes is on the same line as the closing parenthesis of the getToken method.

You could also format it in another way, which is still compliant with ktlint:

tokenCredential
    .getToken(TokenRequestContext().addScopes("https://management.core.windows.net/.default"))
    .block() != null

Remember that ktlint aims to make the code more readable.

Upvotes: -2

Related Questions