I've seen similar issues but the consensus seems to be this works now. But in my experience, it doesn't.
Formatting a class using the built-in formatter of Android Studio should produce ktlint-compatible code (as long as the IDE uses the official Kotlin style)
Multiple violations reported by ktlint, even after reformatting the whole project from Android Studio.
It seems that ktlint and the Kotlin guys have different ideas about indentation in some special cases (see example).
P.S. I would not be surprised if ktlint sees it correctly, but there's a bug in Android Studio. However, ktlint seems inconsistent (compare the two examples).
Violation for ktlint:
override fun actionProcessor():
ObservableTransformer<in SomeVeryVeryLongNameOverHereAction, out SomeVeryVeryLongNameOverHereResult> =
ObservableTransformer { actions ->
// ...
}
In the code above ktlint reports Some.kt:103:1: Unexpected indentation (8) (should be 12). (also reports indentation problems for the following lines, since they're from the same block)
Line 103 is the one with ObservableTransformer { actions ->.
In the snippet above there is a new line after actionProcessor(): in order to break the line that'd be too long otherwise. If I break it in a different way though, the problem disappears (even though the indentation is the same):
Works:
override fun actionProcessor(): ObservableTransformer<in SomeVeryVeryLongNameOverHereAction,
out SomeVeryVeryLongNameOverHereResult> =
ObservableTransformer { actions ->
// ...
}
Customize maximum line length to 120 (probably not relevant)
Use the code I've provided.
Format with Android Studio
Run ktlint and see errors
@romtsn worth to recheck this when you will address main issue
I have a similar issue, but would like to confirm that this issue is actually equivalent to this:
fun generateGooooooooooooooooogle():
Gooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogle {
return Gooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogle()
}
Because the return type cannot fit in with the function name, I have to move the return type into the next line.
I am using the "Kotlin Coding Convention" in the code style and applied the recommended IDE settings from ktlint readme as well. Reformatting in Android Studio does not change the code at all.
In ktlint:0.37.0, there are new violations detected:
/Users/cazhang/TestKtLint/app/src/main/java/com/chaozhang/testktlint/Main.kt:7:1: Unexpected indentation (4) (should be 8)
/Users/cazhang/TestKtLint/app/src/main/java/com/chaozhang/testktlint/Main.kt:8:1: Unexpected indentation (0) (should be 4)
To make it pass, the code needs to be formatted into the following:
fun generateGooooooooooooooooogle():
Gooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogle {
return Gooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooogle()
}
Reformatting in Android Studio changes the code back to the expected behavior.
I'm experiencing the same behavior when using ktlint, here are some cases:
if (records.containsKey(AdvRecord.TYPE_UUID128) && Arrays.equals(
Config.MYOWN_GATT_SERVICE_RAW, // <-- Unexpected indentation (expected 12, actual 16)
AdvRecord.getServiceUUID128(records[AdvRecord.TYPE_UUID128]!!) // <-- Same as above
) // <-- Unexpected indentation (expected 8, actual 12)
) {
...
}
if (TextUtils.isEmpty(oldContext.aaaaaaaaaaaaaaaaaaa) || !Bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb.ccccccccccccccc(
oldContext.aaaaaaaaaaaaaaaaaaa // <-- Unexpected indentation (expected 24, actual 28)
) // <-- Unexpected indentation (expected 20, actual 24)
) {
...
}
if (!customBleService.enableCharacteristicNotification(
myCharacteristic, // <-- Unexpected indentation (expected 24, actual 28)
true // <-- Unexpected indentation (expected 24, actual 28)
) // <-- Unexpected indentation (expected 20, actual 24)
) {
...
}
However, if I change manually to the expected result, _ktlintCheck_ task complains about the same lines to be changed into the previous code indentation again. I don't understand anything. What I'm trying to achieve is Android Studio IDE code reformat fits with ktlint criteria, and somehow they're unsynced now.
I have even tried to run ktlint --android applyToIDEAProject from command line to apply project specific code style, but the problem persist. Any idea how to solve this guys?
@XabierGoros my temporary workaround is one of these:
// @formatter:off and // @formatter:on around the problematic code
Most helpful comment
I'm experiencing the same behavior when using ktlint, here are some cases:
Case 1:
Case 2
Case 3
However, if I change manually to the expected result, _ktlintCheck_ task complains about the same lines to be changed into the previous code indentation again. I don't understand anything. What I'm trying to achieve is Android Studio IDE code reformat fits with ktlint criteria, and somehow they're unsynced now.
I have even tried to run
ktlint --android applyToIDEAProjectfrom command line to apply project specific code style, but the problem persist. Any idea how to solve this guys?My environment