ktlint
ktlint copied to clipboard
Provide an example of how to use ktlint programmatically (using API)
... but first improve the API.
Currently, in order to lint/format a file you need something like this:
//DEPS com.github.shyiko:ktlint:0.30.0
import com.github.shyiko.ktlint.core.KtLint
import com.github.shyiko.ktlint.internal.EditorConfig
import com.github.shyiko.ktlint.ruleset.standard.StandardRuleSetProvider
val file: File = ...
val editorConfig = EditorConfig.of(file.parentFile.canonicalPath)
?: emptyMap<String, String>()
val userData = editorConfig + mapOf("file_path" to file.absolutePath)
val ruleSets = listOf(StandardRuleSetProvider().get())
KtLint.lint(file.readText(), ruleSets, userData) { e ->
println("${e.line}:${e.col} ${e.detail} (${e.ruleId})")
}
Code above has numerous issues (aside from being too complicated):
- needless dependency on
com.github.shyiko:ktlint(cli module)
(com.github.shyiko:ktlint-coreshould be enough) EditorConfigis part of com.github.shyiko.ktlint.internal (which does not guarantee backward-compatibly)EditorConfig.of(...)result (basically.editorconfigcontent) is mixed with unrelated entries (e.g. "file_path")- error prone & poorly discoverable (magic
"file_path"value)
Any update?
Several open source projects have implemented an integration with the KtLint API. There does not seem to be much value in adding another example implementation as part of KtLint (documentation) itself.