diff --git a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt index a6c97049c6..f64ce31322 100644 --- a/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt +++ b/ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/KtLint.kt @@ -44,6 +44,7 @@ object KtLint { val ANDROID_USER_DATA_KEY = Key("ANDROID") val FILE_PATH_USER_DATA_KEY = Key("FILE_PATH") val DISABLED_RULES = Key>("DISABLED_RULES") + const val STDIN_FILE = "" private val psiFileFactory: PsiFileFactory private val nullSuppression = { _: Int, _: String, _: Boolean -> false } @@ -191,7 +192,7 @@ object KtLint { return emptyMap() } - if (fileName == "") { + if (fileName == STDIN_FILE) { return workdirUserData.value } return ( diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt index ca425a2b62..dad62959bb 100644 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt @@ -1,6 +1,7 @@ @file:JvmName("Main") package com.pinterest.ktlint +import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.LintError import com.pinterest.ktlint.core.ParseException import com.pinterest.ktlint.core.Reporter @@ -241,8 +242,6 @@ class KtlintCommandLine { @Parameters(hidden = true) private var patterns = ArrayList() - private val workDir = File(".").canonicalPath - fun run() { if (apply || applyToProject) { applyToIDEA() @@ -279,7 +278,7 @@ class KtlintCommandLine { val userData = mapOf("android" to android.toString()) fun process(fileName: String, fileContent: String): List { if (debug) { - val fileLocation = if (fileName != "") File(fileName).location(relative) else fileName + val fileLocation = if (fileName != KtLint.STDIN_FILE) File(fileName).location(relative) else fileName System.err.println("[DEBUG] Checking $fileLocation") } val result = ArrayList() @@ -347,7 +346,7 @@ class KtlintCommandLine { } reporter.beforeAll() if (stdin) { - report("", process("", String(System.`in`.readBytes()))) + report(KtLint.STDIN_FILE, process(KtLint.STDIN_FILE, String(System.`in`.readBytes()))) } else { patterns.fileSequence() .takeWhile { errorNumber.get() < limit } diff --git a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/PrintASTSubCommand.kt b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/PrintASTSubCommand.kt index ab6e151eb4..d6e87f50dd 100644 --- a/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/PrintASTSubCommand.kt +++ b/ktlint/src/main/kotlin/com/pinterest/ktlint/internal/PrintASTSubCommand.kt @@ -1,6 +1,7 @@ package com.pinterest.ktlint.internal import com.pinterest.ktlint.KtlintCommandLine +import com.pinterest.ktlint.core.KtLint import com.pinterest.ktlint.core.ParseException import com.pinterest.ktlint.core.RuleSet import com.pinterest.ktlint.test.DumpAST @@ -44,7 +45,7 @@ internal class PrintASTSubCommand : Runnable { commandSpec.commandLine().printHelpOrVersionUsage() if (stdin) { - printAST(STDIN_FILE, String(System.`in`.readBytes())) + printAST(KtLint.STDIN_FILE, String(System.`in`.readBytes())) } else { for (file in patterns.fileSequence()) { printAST(file.path, file.readText()) @@ -57,7 +58,7 @@ internal class PrintASTSubCommand : Runnable { fileContent: String ) { if (ktlintCommand.debug) { - val fileLocation = if (fileName != STDIN_FILE) { + val fileLocation = if (fileName != KtLint.STDIN_FILE) { File(fileName).location(ktlintCommand.relative) } else { "stdin" @@ -66,7 +67,7 @@ internal class PrintASTSubCommand : Runnable { } try { - lintFile(fileName, fileContent, astRuleSet) + lintFile(fileName, fileContent, astRuleSet, debug = ktlintCommand.debug) } catch (e: Exception) { if (e is ParseException) { throw ParseException(e.line, e.col, "Not a valid Kotlin file (${e.message?.toLowerCase()})") @@ -77,6 +78,5 @@ internal class PrintASTSubCommand : Runnable { companion object { internal const val COMMAND_NAME = "printAST" - private const val STDIN_FILE = "" } }