ktlint-gradle
ktlint-gradle copied to clipboard
Confused about IntelliJ IDEA/Android Studio integration
I have a plain and simple configuration setup. In the root of my AS project I have:
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
ext.kotlin_version = "1.3.70"
repositories {
google()
jcenter()
maven {
url "https://plugins.gradle.org/m2/"
}
}
dependencies {
classpath "com.android.tools.build:gradle:4.0.0-beta02"
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
classpath "org.jlleitschuh.gradle:ktlint-gradle:9.2.1"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
subprojects {
apply plugin: "org.jlleitschuh.gradle.ktlint"
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: "org.jlleitschuh.gradle.ktlint"
I would love if my project could now have a codestyle set in intelliJ that allows me to cmd + shift + L to reformat as per the ktlint rules.
Looking at the readme I'm confused:
It says:
IntelliJ Idea Only Plugin Note: This plugin is automatically applied by the main ktlint plugin.
This plugin just adds special tasks that can generate IntelliJ IDEA codestyle rules using ktlint.
Idea plugin simple setup For all gradle versions:
Use the same buildscript logic as above, but with this instead of the above suggested apply line.
apply plugin: "org.jlleitschuh.gradle.ktlint-idea"
-
So it looks like for the IntelliJ plugin, I don't have to do anything since it's applied "by the main ktlint". I'm not sure if this is a gradle plugin or is this an IntelliJ IDEA plugin. If I go to the IntelliJ Plugin marketplace, I don't see anything about a plugin being installed
-
If "This plugin is automatically applied by the main ktlint plugin." then why does it say right below to " Use the same buildscript logic as above, but with this instead of the above suggested apply line.
apply plugin: "org.jlleitschuh.gradle.ktlint-idea""
Appreciate any clarification here or on the readme that get's me to my goal of cmd + shift + L to reformat as per the ktlint rules in my project, so that we can share via git in my teams project
It's a plugin for Gradle that will configure IJ to conform to the Ktlint format settings.
@JLLeitschuh So if I want to use ktlint in my intellij project, it akes sense that I would have BOTH plugins?
i.e.
apply plugin: "org.jlleitschuh.gradle.ktlint"
apply plugin: "org.jlleitschuh.gradle.ktlint-idea"
After I apply the idea plugin, when does it actually "configure IJ to conform to the Ktlint format settings". Every time I hit build in intellij?
See: https://github.com/JLLeitschuh/ktlint-gradle#additional-helper-tasks
Your IDE will only be configured if you run one of the two IDEA specific tasks.
Aha. That's where my confusion came in. When I read the docs for ktlintApplyToIdea I thought that I could run that task with only apply plugin: "org.jlleitschuh.gradle.ktlint", but it sounds like you're saying I still need the apply plugin: "org.jlleitschuh.gradle.ktlint-idea" line as well.
Then, I need to instruct everyone on my team to run ktlintApplyToIdea.
If I set it up right, "org.jlleitschuh.gradle.ktlint" should automatically apply the "org.jlleitschuh.gradle.ktlint-idea" plugin automatically.
Oh. So no need for me to apply two plugins? Only apply plugin: "org.jlleitschuh.gradle.ktlint"?
Not too clear from the docs, but I can try this out and let you know the behavior, now that you clarified what the intended behavior is.
I thought I had documented it at one point, @Tapchicoma what happened to the 'what plugin is applied' documentation? Did that get removed at some point?
@JLLeitschuh could not recall if I removed it or it was missing initially
@ColtonIdle this integration from ktlint side is quite limited, see https://github.com/pinterest/ktlint/issues/701
@Tapchicoma oh wait? Shit. Really?
This really sucks. I really want cmd + shift + L for my teammates to all do the same thing AND to adhere to ktlint.
From what it sounds like... this is not possible? Am I interpreting this correctly @Tapchicoma ?
@ColtonIdle sorry for a late reply.
Yep currently it is not possible. If linked ticket would be implemented - this should solve your issue.
@Tapchicoma thanks. I thought I was going crazy.
@ColtonIdle bit late to the party but what did your team decide to do? Did you end up using Ktlint in your project? Did you instruct everyone on your team to run ktlintApplyToIdea or did you figure out a better way? I would appreciate your insight and guidance.
@u-ways we moved to ktfmt. It's based off of googles java formatter and so it inherits any fixes from there, which makes for more frequent releases + a supported intellij plugin was perfect for our team.