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

ktlint incompatibilities with Android Studio autoformatter #1321

Closed
spyderrsh opened this issue Dec 22, 2021 · 5 comments · Fixed by #1682
Closed

ktlint incompatibilities with Android Studio autoformatter #1321

spyderrsh opened this issue Dec 22, 2021 · 5 comments · Fixed by #1682
Labels
bug conflict-with-default-intellij-formatting Code produced by KtLint is not accepted by the IntelliJ default formatter indentation rule
Milestone

Comments

@spyderrsh
Copy link

spyderrsh commented Dec 22, 2021

Expected Behavior

I expect that if I run ktlint -F <src_dir> and then run the reformat code on the same code (with it configured to ktlint style) that things should remain the same.

Observed Behavior

ktlint has some battles with intellij

Steps to Reproduce

For the following blocks of code, run ktlint format and then AS format. They don't agree.

ktlint format

fun `ktlint doesn't like indenting a lambda in a complex boolean expression`() {
        val featureSupported = Any() == true &&
            connectionManager.deviceOS?.let { version ->
            true
        } == true
    }

vs intellij

    lateinit var connectionManager: Any

    private val Any.deviceOS: String? get() = null

    fun `ktlint doesn't like indenting a lambda in a complex boolean expression`() {
        val featureSupported = Any() == true &&
            connectionManager.deviceOS?.let { version ->
                true // <-- indent exception here
            } == true // <-- indent exception here
    }

and

ktlint

true.let { hasFocus ->
            if (hasFocus && (
                true ||
                    false
                )
            ) {
                /* no-op */
            }
        }

vs intellij

fun `IDE and ktlint have different ideas about indenting`() {
        true.let { hasFocus ->
            if (hasFocus && (
                    true || // <-- indent exception here
                        false // <-- indent exception here
                    ) // <-- indent exception here
            ) {
                /* no-op */
            }
        }
    }

Your Environment

  • Version of ktlint used: 0.43.2
  • Commands run: ktlint -F
  • Operating System and version: Windows 11
@paul-dingemans
Copy link
Collaborator

Both examples can be reproduced and should be fixed in ktlint.

@jaworskim
Copy link

I can confirm that issue still exists on v0.44.0. Issue seems to affect all cases of braces inside of a if condiition. In our case, we noticed it while calling a high order function inside of a if statement.

We were unable to find any workaround for this by using custom code style settings in Intellij.

@paul-dingemans paul-dingemans added this to the 0.46.0 milestone Mar 17, 2022
@paul-dingemans paul-dingemans modified the milestones: 0.46.0, 0.47.0 Jun 1, 2022
@paul-dingemans
Copy link
Collaborator

For the first example, I agree that the IntelliJ IDEA / Android Studio default formatter is superior. Ktlint should format the code like:

lateinit var connectionManager: Any

private val Any.deviceOS: String? get() = null

fun `ktlint doesn't like indenting a lambda in a complex boolean expression`() {
    val featureSupported = Any() == true &&
        connectionManager.deviceOS?.let { version ->
            true // <-- indent exception here
        } == true // <-- indent exception here
}

In the second example, I do agree that it conflicts with the IntelliJ IDEA / Android Studio default formatter but I am not convinced that the format is better than ktlint's formatting. With respect to discussion in #1526 I would favor that example to be formatted as currently is done by ktlint:

true.let { hasFocus ->
    if (hasFocus && (
        true ||
            false
        )
    ) {
        /* no-op */
    }
}

@paul-dingemans paul-dingemans added the conflict-with-default-intellij-formatting Code produced by KtLint is not accepted by the IntelliJ default formatter label Jun 27, 2022
@paul-dingemans paul-dingemans modified the milestones: 0.47.0, 0.48.0 Aug 14, 2022
@paul-dingemans
Copy link
Collaborator

Both problems are solved by #1682

@paul-dingemans
Copy link
Collaborator

Closed by #1682

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug conflict-with-default-intellij-formatting Code produced by KtLint is not accepted by the IntelliJ default formatter indentation rule
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants