Trying out Ktlint via in a brand new project from Android Studio with https://github.com/jlleitschuh/ktlint-gradle
This is the code of one of the auto generated test from File > New Project in AS
package com.example.tryktlint
import org.junit.Assert.assertEquals
import org.junit.Test
/**
* Example local unit test, which will execute on the development machine (host).
*
* See [testing documentation](http://d.android.com/tools/testing).
*/
class ExampleUnitTest {
@Test
fun addition_isCorrect() {
assertEquals(4, 2 + 2)
}
}
Running ./gradlew ktlinCheck gives me the following errors
/Users/coltonidle/projects/tryktlint/app/src/test/java/com/example/tryktlint/ExampleUnitTest.kt:6:1: Unexpected indentation (2) (it should be 4) (cannot be auto-corrected)
/Users/coltonidle/projects/tryktlint/app/src/test/java/com/example/tryktlint/ExampleUnitTest.kt:11:1: Unexpected indentation (2) (it should be 4) (cannot be auto-corrected)
So it looks like the starting of the comment and the class ExampleUnitTest can't be auto-corrected.
I have the default settings in AS with regards to formatting code via cmd + shift + L and when I do that, it auto reformats everything correctly, and so it would seem that it can indeed be autoformatted?
Note: This bug came out of a convo on the #ktlint channel in the kotlin slack.
I have the same problem. Why can't ktlint figure out the indentation itself?
Same thing here. If the difference between what is and what should be is already there, why can it can not be auto-corrected?
Same problem
/home/luis/gogs/tmp/script.kt:1:23: Unexpected indentation (2) (it should be 4) (cannot be auto-corrected)
/home/luis/gogs/tmp/script.kt:2:24: Unexpected indentation (2) (it should be 4) (cannot be auto-corrected)
Why can't it figure this out? It seems it has the information, every time I get the "cannot be auto-corrected" I can post process it and correct it...
var a = "sdf"
var b = "with extra space"
val buildCommands = listOf(
"a",
"b",
"c"
)
I had the same problem.
After debugging I found that com.pinterest.ktlint.ruleset.experimental.IndentationRule returns immediately when .editorconfig has indent_style = tab or when indent_size = 1 or lower:
override fun visit(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit
) {
val editorConfig = node.getUserData(KtLint.EDITOR_CONFIG_USER_DATA_KEY)!!
if (editorConfig.indentStyle == EditorConfig.IndentStyle.TAB || editorConfig.indentSize <= 1) {
return
}
...
...
}
The reason for the console output the OP mentioned is because the indent rule in the standard rule set still executes, but that rule doesn't have the formatting feature.
In my case, it turned out I had an .editorconfig file in my user home directory with indent_style = tab, while the editorconfig file in my project root did not define any style. I got it to work by commenting out the indent_style in my user home (space seems to be the default). What also worked was explicitly setting it to space in either of the editor config files.
You can see which editor configs are evaluated by enabling ktlint's debug mode and inspecting the log output.
P.S. Experimental rules (--experimental) and formatting (--format) also need to be enabled for this to work.
I don't have .editorconfig simply
./ktlint script.kt --experimental --format
worked for me
Yes, so in summary: the default behavior without any .editorconfig file is that it works as expected.
But when you have such a file (in project root, user home, etc) and ktlint detects it, then you need to make sure that indent_style is absent or set explicitly to space. And that indent_size is absent or set to 2 or higher.
On May 11, 2020 at 11:00 PM,
I don't have .editorconfig simply
./ktlint script.kt --experimental --format
worked for me—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub (https://github.com/pinterest/ktlint/issues/716#issuecomment-626960528), or unsubscribe (https://github.com/notifications/unsubscribe-auth/AAJTRYKYRYT3LOQSO75PZ3DRRBRNTANCNFSM4LPPNQBA).
Yes, so in summary: the default behavior without any
.editorconfigfile is that it works as expected.
if you use --experimental
@shashachu probably we should move experimental "ident" rule to standard one.
Should be fixed now that the experimental indentation rule has been promoted to standard. (Releasing with 0.37)
I'm using 0.40 and I run into the same issue (Running Ktlint through Spotless). Is there anything I'm missing? See:

@cesards please open new issue with steps to reproduce your problem (would be nice if you also check it against snapshot build from master)
Most helpful comment
Same thing here. If the difference between what is and what should be is already there, why can it can not be auto-corrected?