forked from pinterest/ktlint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "printAST" sub command. (pinterest#500)
Move old one (--print-ast) option to be a ktlint sub command - new syntax: `ktlint printAST`. Old one `--print-ast` is still working, but deprecated. Signed-off-by: Yahor Berdnikau <egorr.berd@gmail.com>
- Loading branch information
1 parent
64f3269
commit 0fef759
Showing
4 changed files
with
158 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
ktlint/src/main/kotlin/com/pinterest/ktlint/internal/FileUtils.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.pinterest.ktlint.internal | ||
|
||
import com.github.shyiko.klob.Glob | ||
import com.pinterest.ktlint.core.KtLint.lint | ||
import com.pinterest.ktlint.core.KtLint.lintScript | ||
import com.pinterest.ktlint.core.LintError | ||
import com.pinterest.ktlint.core.RuleSet | ||
import java.io.File | ||
import java.nio.file.Path | ||
import java.nio.file.Paths | ||
|
||
internal val workDir: String = File(".").canonicalPath | ||
|
||
internal fun List<String>.fileSequence(): Sequence<File> { | ||
val kotlinFiles = if (isEmpty()) { | ||
Glob.from("**/*.kt", "**/*.kts") | ||
.iterate( | ||
Paths.get(workDir), | ||
Glob.IterationOption.SKIP_HIDDEN | ||
) | ||
} else { | ||
val normalizedPatterns = map(::expandTilde).toTypedArray() | ||
Glob.from(*normalizedPatterns) | ||
.iterate(Paths.get(workDir)) | ||
} | ||
|
||
return kotlinFiles | ||
.asSequence() | ||
.map(Path::toFile) | ||
} | ||
|
||
// a complete solution would be to implement https://www.gnu.org/software/bash/manual/html_node/Tilde-Expansion.html | ||
// this implementation takes care only of the most commonly used case (~/) | ||
internal fun expandTilde(path: String): String = path.replaceFirst(Regex("^~"), System.getProperty("user.home")) | ||
|
||
internal fun File.location( | ||
relative: Boolean | ||
) = if (relative) this.toRelativeString(File(workDir)) else this.path | ||
|
||
/** | ||
* Run lint over common kotlin file or kotlin script file. | ||
*/ | ||
internal fun lintFile( | ||
fileName: String, | ||
fileContent: String, | ||
ruleSetList: List<RuleSet>, | ||
userData: Map<String, String> = emptyMap(), | ||
lintErrorCallback: (LintError) -> Unit = {} | ||
) { | ||
if (fileName.endsWith(".kt", ignoreCase = true)) { | ||
lint(fileContent, ruleSetList, userData, lintErrorCallback) | ||
} else { | ||
lintScript(fileContent, ruleSetList, userData, lintErrorCallback) | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
ktlint/src/main/kotlin/com/pinterest/ktlint/internal/PrintASTSubCommand.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.pinterest.ktlint.internal | ||
|
||
import com.pinterest.ktlint.KtlintCommandLine | ||
import com.pinterest.ktlint.core.ParseException | ||
import com.pinterest.ktlint.core.RuleSet | ||
import com.pinterest.ktlint.test.DumpAST | ||
import java.io.File | ||
import picocli.CommandLine | ||
|
||
@CommandLine.Command( | ||
description = [ | ||
"Print AST (useful when writing/debugging rules)", | ||
"Usage of \"--print-ast\" command line option is deprecated!" | ||
], | ||
aliases = ["--print-ast"], | ||
mixinStandardHelpOptions = true, | ||
versionProvider = KtlintVersionProvider::class | ||
) | ||
internal class PrintASTSubCommand : Runnable { | ||
@CommandLine.ParentCommand | ||
private lateinit var ktlintCommand: KtlintCommandLine | ||
|
||
@CommandLine.Spec | ||
private lateinit var commandSpec: CommandLine.Model.CommandSpec | ||
|
||
@CommandLine.Parameters( | ||
description = ["include all files under this .gitignore-like patterns"] | ||
) | ||
private var patterns = ArrayList<String>() | ||
|
||
@CommandLine.Option( | ||
names = ["--stdin"], | ||
description = ["Read file content from stdin"] | ||
) | ||
private var stdin: Boolean = false | ||
|
||
private val astRuleSet by lazy(LazyThreadSafetyMode.NONE) { | ||
listOf( | ||
RuleSet("debug", DumpAST(System.out, ktlintCommand.color)) | ||
) | ||
} | ||
|
||
override fun run() { | ||
commandSpec.commandLine().printHelpOrVersionUsage() | ||
|
||
if (stdin) { | ||
printAST(STDIN_FILE, String(System.`in`.readBytes())) | ||
} else { | ||
for (file in patterns.fileSequence()) { | ||
printAST(file.path, file.readText()) | ||
} | ||
} | ||
} | ||
|
||
private fun printAST( | ||
fileName: String, | ||
fileContent: String | ||
) { | ||
if (ktlintCommand.debug) { | ||
val fileLocation = if (fileName != STDIN_FILE) { | ||
File(fileName).location(ktlintCommand.relative) | ||
} else { | ||
"stdin" | ||
} | ||
println("[DEBUG] Analyzing $fileLocation") | ||
} | ||
|
||
try { | ||
lintFile(fileName, fileContent, astRuleSet) | ||
} catch (e: Exception) { | ||
if (e is ParseException) { | ||
throw ParseException(e.line, e.col, "Not a valid Kotlin file (${e.message?.toLowerCase()})") | ||
} | ||
throw e | ||
} | ||
} | ||
|
||
companion object { | ||
internal const val COMMAND_NAME = "printAST" | ||
private const val STDIN_FILE = "<stdin>" | ||
} | ||
} |