diff --git a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt index c7dc9c14d6..a14ed20b3a 100644 --- a/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt +++ b/ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt @@ -309,7 +309,6 @@ internal class KtlintCommandLine : CliktCommand(name = "ktlint") { lintStdin( ktLintRuleEngine, aggregatedReporter, - stdinPath, ) } else { lintFiles( @@ -433,17 +432,18 @@ internal class KtlintCommandLine : CliktCommand(name = "ktlint") { private fun lintStdin( ktLintRuleEngine: KtLintRuleEngine, reporter: ReporterV2, - stdinpath: String?, ) { - val expandedStdinPath = - stdinpath + val code = + stdinPath ?.expandTildeToFullPath() ?.let { path -> Paths.get(path) } + ?.let { Code.fromStdin(it) } + ?: Code.fromStdin() report( KtLintRuleEngine.STDIN_FILE, process( ktLintRuleEngine = ktLintRuleEngine, - code = Code.fromStdin(expandedStdinPath), + code = code, baselineLintErrors = emptyList(), ), reporter, diff --git a/ktlint-rule-engine/api/ktlint-rule-engine.api b/ktlint-rule-engine/api/ktlint-rule-engine.api index b6a0da969d..0fe4c7d1eb 100644 --- a/ktlint-rule-engine/api/ktlint-rule-engine.api +++ b/ktlint-rule-engine/api/ktlint-rule-engine.api @@ -17,6 +17,7 @@ public final class com/pinterest/ktlint/rule/engine/api/Code$Companion { public static synthetic fun fromSnippet$default (Lcom/pinterest/ktlint/rule/engine/api/Code$Companion;Ljava/lang/String;ZILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/api/Code; public final fun fromSnippetWithPath (Ljava/lang/String;Ljava/nio/file/Path;)Lcom/pinterest/ktlint/rule/engine/api/Code; public static synthetic fun fromSnippetWithPath$default (Lcom/pinterest/ktlint/rule/engine/api/Code$Companion;Ljava/lang/String;Ljava/nio/file/Path;ILjava/lang/Object;)Lcom/pinterest/ktlint/rule/engine/api/Code; + public final fun fromStdin ()Lcom/pinterest/ktlint/rule/engine/api/Code; public final fun fromStdin (Ljava/nio/file/Path;)Lcom/pinterest/ktlint/rule/engine/api/Code; } diff --git a/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/api/Code.kt b/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/api/Code.kt index 7371174bf2..762f892365 100644 --- a/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/api/Code.kt +++ b/ktlint-rule-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/api/Code.kt @@ -115,6 +115,13 @@ public class Code private constructor( * filesystem are ignored as the snippet is not associated with a file path. Use [Code.fromFile] for scanning a file while at the * same time respecting the '.editorconfig' files on the path to the file. */ - public fun fromStdin(stdinPath: Path?): Code = fromSnippetWithPath(String(System.`in`.readBytes()), virtualPath = stdinPath) + public fun fromStdin(): Code = fromSnippetWithPath(String(System.`in`.readBytes())) + + /** + * Create [Code] by reading the snippet from 'stdin'. The code snippet is associated with the given path. The actual file does not + * need to exist, neither do the contents of the actual file have to match with the content specified via 'stdin'. The + * '.editorconfig' files on the [virtualPath] are respected. + */ + public fun fromStdin(virtualPath: Path): Code = fromSnippetWithPath(String(System.`in`.readBytes()), virtualPath = virtualPath) } }