Allow passing a file path when using --stdin
When formatting using --stdin, you lose filename/path in any error messages (obviously). Additionally there's no way to use things like specific-directory editorconfig overrides, which depend on knowing the file location.
This affects some tools like git-format-staged, which work by passing files through stdin. To get around this, some other formatters allow passing a path to treat as the current file path e.g. --stdin-filepath in Prettier, or --stdinpath in SwiftFormat.
It'd be great if ktlint supported this too!
Additionally there's no way to use things like specific-directory editorconfig overrides, which depend on knowing the file location.
This additional reason is not true. When the editorconfig parameter is specified it overrides any .editorconfig found on the path to the file.
$ cat .editorconfig
[*.{kt,kts,gradle}]
indent_size = 4
$ ktlint-0.46.1 --stdin
class Foo {
fun foo() {}
}
<stdin>:2:1: Unexpected indentation (0) (should be 4) (indent)
$ cat ../xx/.editorconfig
[*.{kt,kts,gradle}]
indent_size = 8
$ ktlint-0.46.1 --editorconfig=../xx --stdin
class Foo {
fun foo() {}
}
<stdin>:2:1: Unexpected indentation (0) (should be 8) (indent)
$ cat ../yy/.editorconfig
[*.{kt,kts,gradle}]
indent_size = 2
$ ktlint-0.46.1 --editorconfig=../yy --stdin
class Foo {
fun foo() {}
}
<stdin>:2:1: Unexpected indentation (0) (should be 2) (indent)
Given above, it does not seem to have an added value to specify the file path explicitly.