I'm having a ktlint issue where ktlint is fighting...
# ktlint
d
I'm having a ktlint issue where ktlint is fighting with the AS formatter - the following is how AS formats and I think this looks correct (aesthetically)
Copy code
@Composable
private fun Buttons() {
    Row {
        FooButton(onClick = { }) {
            Text("Primary")
        }
        BarOutlineButton(onClick = { }) {
            Text("PrimaryOutline")
        }
        FooTextButton(onClick = { }) {
            Text("PrimaryText")
        }
    }
    Row {
        FooButton(onClick = { }, enabled = false) {
            Text("Primary")
        }
        BarOutlineButton(onClick = { }, enabled = false) {
            Text("PrimaryOutline")
        }
        FooTextButton(onClick = { }, enabled = false) {
            Text("PrimaryText")
        }
    }
}
ktlint will fail with:
Copy code
> Task :app:ktlintDebugSourceSetCheck FAILED
/Users/darrankelinske/source/consumer-android-ui/app/src/debug/java/co/well/wellapp/DesignSystem.kt:3:1 Imports must be ordered in lexicographic order without any empty lines in-between
/Users/darrankelinske/source/consumer-android-ui/app/src/debug/java/co/well/wellapp/DesignSystem.kt:291:1 Unexpected indentation (12) (should be 8)
/Users/darrankelinske/source/consumer-android-ui/app/src/debug/java/co/well/wellapp/DesignSystem.kt:292:1 Unexpected indentation (8) (should be 4)
/Users/darrankelinske/source/consumer-android-ui/app/src/debug/java/co/well/wellapp/DesignSystem.kt:294:1 Unexpected indentation (12) (should be 8)
/Users/darrankelinske/source/consumer-android-ui/app/src/debug/java/co/well/wellapp/DesignSystem.kt:295:1 Unexpected indentation (8) (should be 4)
/Users/darrankelinske/source/consumer-android-ui/app/src/debug/java/co/well/wellapp/DesignSystem.kt:297:1 Unexpected indentation (12) (should be 8)
/Users/darrankelinske/source/consumer-android-ui/app/src/debug/java/co/well/wellapp/DesignSystem.kt:298:1 Unexpected indentation (8) (should be 4)
and this is how ktlint formats it
Copy code
@Composable
private fun Buttons() {
    Row {
        FooButton(onClick = { }) {
            Text("Primary")
        }
        BarOutlineButton(onClick = { }) {
            Text("PrimaryOutline")
        }
        FooTextButton(onClick = { }) {
            Text("PrimaryText")
        }
    }
    Row {
        FooButton(onClick = { }, enabled = false) {
        Text("Primary")
    }
        BarOutlineButton(onClick = { }, enabled = false) {
        Text("PrimaryOutline")
    }
        FooTextButton(onClick = { }, enabled = false) {
        Text("PrimaryText")
    }
    }
}
It looks like ktlint is formatting the functions inconsistently and in a way that doesn't really render as it should be read (in a compose kind of mindset)
now the checks will pass with ktlint formatting
Copy code
darrankelinske@W2DWT39JN9 consumer-android-ui % ./gradlew ktlintDebugSourceSetCheck 
Configuration on demand is an incubating feature.

BUILD SUCCESSFUL in 550ms
16 actionable tasks: 3 executed, 13 up-to-date
@David Corrado fyi
e
d
thank you - it looks like it is coming out in .46
p
Code snippet does not throw lint errors for me with latest ktlint (0.45.2). Can you please create an issue and specify details about ktlint version and editorconfig. Also please run ktlint with option “-v”.
d
i dont have any .editorconfig defined
Copy code
darrankelinske@W2DWT39JN9 consumer-android-ui % ./gradlew ktlint -v

------------------------------------------------------------
Gradle 7.2
------------------------------------------------------------

Build time:   2021-08-17 09:59:03 UTC
Revision:     a773786b58bb28710e3dc96c4d1a7063628952ad

Kotlin:       1.5.21
Groovy:       3.0.8
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          11.0.12 (Homebrew 11.0.12+0)
OS:           Mac OS X 12.3.1 aarch64
I am using ktlint from
id("org.jlleitschuh.gradle.ktlint") version Versions.ktlint
Copy code
const val ktlint = "10.2.1"
p
d
hmm - it looks like the PR to update the version is still in progress and the maintainer is looking for help...
e
d
yep - might try kotlinter - have you tried it @ephemient
e
I currently use kotlinter and detekt in various projects
d
it looks like you can set a particular version on both of the plugins through different means
going to try that
Copy code
ktlint {
    version.set("0.45.2")
    android.set(true)
    reporters {
        reporter(ReporterType.HTML)
    }
    outputColorName.set("RED")
    disabledRules.set(setOf("no-wildcard-imports"))
}
I am hitting another difference between intellij formatting and ktlint (even after I have run applyToIDEAProject) https://github.com/pinterest/ktlint/issues/1237#issuecomment-1103135680
if I install the ktlint intellij plugin everything works as expected - does anyone know why the ktlint intellij plugin solves all the problems?
p
Probably it is the difference in the ktlint version. It would be best if you first verify whether your problem also occur with latest ktlint version (0.45.2) when running from the command line.
d
How do I tell which version is running from the command line? It looks like -v doesn't dump the ktlint version
just the system stats
p
`ktlint --help`will tell it all 😉
e
you are being recommended to run ktlint directly, not via a Gradle plugin or IDE plugin
p
But seriously,
ktlint --version
is the command you are looking for,
d
I know and that is what I prefer, but my teammates like to use the Android studio UI to format
Copy code
darrankelinske@W2DWT39JN9 consumer-android-ui-2 % ./gradlew ktlint --version

------------------------------------------------------------
Gradle 7.2
------------------------------------------------------------

Build time:   2021-08-17 09:59:03 UTC
Revision:     a773786b58bb28710e3dc96c4d1a7063628952ad

Kotlin:       1.5.21
Groovy:       3.0.8
Ant:          Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM:          11.0.12 (Homebrew 11.0.12+0)
OS:           Mac OS X 12.3.1 aarch64
so I need to determine the version by the hash?
p
No, you should install ktlint as CLI tool and run it from the command line.
d
i have that installed too
I am primarily trying to find solutions to make it easier for my teammates to use "Reformat Code" in Android Studio UI in a way that jives with ktlint
i am a terminal / command line person, but not everyone is
Copy code
darrankelinske@W2DWT39JN9 consumer-android-ui-2 % ktlint --version
0.45.2
p
I want you to use the CLI tool, to verify whether it is a Ktlint or a gradle pf a plugin problem,
I can only help with ktlint problem itself. I do not maintain the other repo’s / projects/
e
if ktlint works and gradle ktlint doesn't, it's narrowed down to a plugin issue
d
got it - now I'm following
134 Views