Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly handle --stdin flag for printAST command #529

Merged
merged 2 commits into from
Jul 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ object KtLint {
val ANDROID_USER_DATA_KEY = Key<Boolean>("ANDROID")
val FILE_PATH_USER_DATA_KEY = Key<String>("FILE_PATH")
val DISABLED_RULES = Key<Set<String>>("DISABLED_RULES")
const val STDIN_FILE = "<stdin>"

private val psiFileFactory: PsiFileFactory
private val nullSuppression = { _: Int, _: String, _: Boolean -> false }
Expand Down Expand Up @@ -191,7 +192,7 @@ object KtLint {
return emptyMap()
}

if (fileName == "<text>") {
if (fileName == STDIN_FILE) {
return workdirUserData.value
}
return (
Expand Down
7 changes: 3 additions & 4 deletions ktlint/src/main/kotlin/com/pinterest/ktlint/Main.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -241,8 +242,6 @@ class KtlintCommandLine {
@Parameters(hidden = true)
private var patterns = ArrayList<String>()

private val workDir = File(".").canonicalPath

fun run() {
if (apply || applyToProject) {
applyToIDEA()
Expand Down Expand Up @@ -279,7 +278,7 @@ class KtlintCommandLine {
val userData = mapOf("android" to android.toString())
fun process(fileName: String, fileContent: String): List<LintErrorWithCorrectionInfo> {
if (debug) {
val fileLocation = if (fileName != "<text>") 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<LintErrorWithCorrectionInfo>()
Expand Down Expand Up @@ -347,7 +346,7 @@ class KtlintCommandLine {
}
reporter.beforeAll()
if (stdin) {
report("<text>", process("<text>", String(System.`in`.readBytes())))
report(KtLint.STDIN_FILE, process(KtLint.STDIN_FILE, String(System.`in`.readBytes())))
} else {
patterns.fileSequence()
.takeWhile { errorNumber.get() < limit }
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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())
Expand All @@ -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"
Expand All @@ -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()})")
Expand All @@ -77,6 +78,5 @@ internal class PrintASTSubCommand : Runnable {

companion object {
internal const val COMMAND_NAME = "printAST"
private const val STDIN_FILE = "<stdin>"
}
}