Ktlint: Indentation is not flagged correctly when using use

Created on 25 Jan 2018  路  5Comments  路  Source: pinterest/ktlint

I'm in a 2 spaces project with the following code.

class ReaderFacade {
  fun run() {
    val output: Output = CsvOutput()

    Okio.buffer(Okio.sink(File("output.${output.fileEnding}"))).use {
        it.writeUtf8(stringOutput)
      }
  }
}

I'd expect it to get flagged since the line in use as well as the } is not indented correctly. Instead it should look like this:

-        it.writeUtf8(stringOutput)
-      }
+      it.writeUtf8(stringOutput)
+    }

I'm running version 0.15.0. Didn't try the snapshot out as it should only add quick fixing in case it gets reported. Correct me if I'm wrong.

Most helpful comment

This is due to two "limitations" of IndentionRule

  1. Rule expects that there is always an indent. Regular one or continuation. In that case, there is no indent at all. Of course, there is "parent indent" for entire block, but inside that block, there is no additional indent for }
  2. Rule is not strict enough in detecting violations. There is condition used
    (line.length - previousIndent) % expectedIndentSize != 0
    As result, not all violations are detected. This is also a reason why rule doesn't fail everywhere for }.

Ideally, I'd like to make condition more strict (line.length - previousIndent) != expectedIndentSize. But for that we have to teach rule about all other elements (} etc).

I'm happy to work on that, but it will take some time.

All 5 comments

This is due to two "limitations" of IndentionRule

  1. Rule expects that there is always an indent. Regular one or continuation. In that case, there is no indent at all. Of course, there is "parent indent" for entire block, but inside that block, there is no additional indent for }
  2. Rule is not strict enough in detecting violations. There is condition used
    (line.length - previousIndent) % expectedIndentSize != 0
    As result, not all violations are detected. This is also a reason why rule doesn't fail everywhere for }.

Ideally, I'd like to make condition more strict (line.length - previousIndent) != expectedIndentSize. But for that we have to teach rule about all other elements (} etc).

I'm happy to work on that, but it will take some time.

Have there been any updates on this issue?

@Octogonapus Sorry, I didn't really made any progress here. But I'm still interested in that topic. Hopefully, I'll be able to come back soon.

Ok. Let me know if there is something I can do to help.

Moved to #338.

Was this page helpful?
0 / 5 - 0 ratings