aboutsummaryrefslogtreecommitdiff
path: root/ktlint-test/src/main/kotlin/com
diff options
context:
space:
mode:
authorAurimas Liutikas <aurimas@google.com>2018-05-25 16:15:10 -0700
committerAurimas Liutikas <aurimas@google.com>2018-05-25 16:15:10 -0700
commit3432676ef4ed599933a9a05a9f4e712e7e657f1b (patch)
tree8aa62a48ff99be9923c5f57521ba855b763eb8aa /ktlint-test/src/main/kotlin/com
parent946fa4217826656ac2e82c101f63c5c471ee5df0 (diff)
parent326cdc53aa67dc3e8d68eb24fcc08b3e4a7b9bde (diff)
downloadktlint-3432676ef4ed599933a9a05a9f4e712e7e657f1b.tar.gz
Merge commit '326cdc53aa67dc3e8d68eb24fcc08b3e4a7b9bde'HEADmastermain
Update to ktlint 0.23.1 https://github.com/shyiko/ktlint/tree/0.23.1 Test: None
Diffstat (limited to 'ktlint-test/src/main/kotlin/com')
-rw-r--r--ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/DumpAST.kt94
-rw-r--r--ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/RuleExtension.kt28
-rw-r--r--ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/package.kt27
3 files changed, 117 insertions, 32 deletions
diff --git a/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/DumpAST.kt b/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/DumpAST.kt
new file mode 100644
index 00000000..a22223ee
--- /dev/null
+++ b/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/DumpAST.kt
@@ -0,0 +1,94 @@
+package com.github.shyiko.ktlint.test
+
+import com.andreapivetta.kolor.Color
+import com.andreapivetta.kolor.Kolor
+import com.github.shyiko.ktlint.core.Rule
+import org.jetbrains.kotlin.com.intellij.lang.ASTNode
+import org.jetbrains.kotlin.com.intellij.openapi.util.TextRange
+import org.jetbrains.kotlin.com.intellij.psi.util.PsiTreeUtil
+import org.jetbrains.kotlin.diagnostics.DiagnosticUtils
+import org.jetbrains.kotlin.psi.stubs.elements.KtStubElementTypes
+import java.io.PrintStream
+
+val debugAST = {
+ (System.getProperty("ktlintDebug") ?: System.getenv("KTLINT_DEBUG") ?: "")
+ .toLowerCase().split(",").contains("ast")
+}
+
+class DumpAST @JvmOverloads constructor(
+ private val out: PrintStream = System.err,
+ private val color: Boolean = false
+) : Rule("dump") {
+
+ private var lineNumberColumnLength: Int = 0
+ private var lastNode: ASTNode? = null
+
+ override fun visit(
+ node: ASTNode,
+ autoCorrect: Boolean,
+ emit: (offset: Int, errorMessage: String, corrected: Boolean) -> Unit
+ ) {
+ if (node.elementType == KtStubElementTypes.FILE) {
+ lineNumberColumnLength = (location(PsiTreeUtil.getDeepestLast(node.psi).node)?.line ?: 1)
+ .let { var v = it; var c = 0; while (v > 0) { c++; v /= 10 }; c }
+ lastNode = lastChildNodeOf(node)
+ }
+ var level = -1
+ var parent: ASTNode? = node
+ do {
+ level++
+ parent = parent?.treeParent
+ } while (parent != null)
+ out.println((
+ location(node)
+ ?.let { String.format("%${lineNumberColumnLength}s: ", it.line).gray() }
+ // should only happen when autoCorrect=true and other rules mutate AST in a way that changes text length
+ ?: String.format("%${lineNumberColumnLength}s: ", "?").gray()
+ ) +
+ " ".repeat(level).gray() +
+ colorClassName(node.psi.className) +
+ " (".gray() + colorClassName(node.elementType.className) + "." + node.elementType + ")".gray() +
+ if (node.getChildren(null).isEmpty()) " \"" + node.text.escape().yellow() + "\"" else "")
+ if (lastNode == node) {
+ out.println()
+ out.println(" ".repeat(lineNumberColumnLength) +
+ " format: <line_number:> <node.psi::class> (<node.elementType>) \"<node.text>\"".gray())
+ out.println(" ".repeat(lineNumberColumnLength) +
+ " legend: ~ = org.jetbrains.kotlin, c.i.p = com.intellij.psi".gray())
+ out.println()
+ }
+ }
+
+ private tailrec fun lastChildNodeOf(node: ASTNode): ASTNode? =
+ if (node.lastChildNode == null) node else lastChildNodeOf(node.lastChildNode)
+
+ private fun location(node: ASTNode) =
+ node.psi.containingFile?.let { psiFile ->
+ try {
+ DiagnosticUtils.getLineAndColumnInPsiFile(
+ psiFile,
+ TextRange(node.startOffset, node.startOffset)
+ )
+ } catch (e: Exception) {
+ null // DiagnosticUtils has no knowledge of mutated AST
+ }
+ }
+
+ private fun colorClassName(className: String): String {
+ val name = className.substringAfterLast(".")
+ return className.substring(0, className.length - name.length).gray() + name
+ }
+
+ private fun String.yellow() =
+ if (color) Kolor.foreground(this, Color.YELLOW) else this
+ private fun String.gray() =
+ if (color) Kolor.foreground(this, Color.DARK_GRAY) else this
+
+ private val Any.className
+ get() = this.javaClass.name
+ .replace("org.jetbrains.kotlin.", "~.")
+ .replace("com.intellij.psi.", "c.i.p.")
+
+ private fun String.escape() =
+ this.replace("\\", "\\\\").replace("\n", "\\n").replace("\t", "\\t").replace("\r", "\\r")
+}
diff --git a/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/RuleExtension.kt b/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/RuleExtension.kt
index 29117900..3914705b 100644
--- a/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/RuleExtension.kt
+++ b/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/RuleExtension.kt
@@ -6,10 +6,11 @@ import com.github.shyiko.ktlint.core.Rule
import com.github.shyiko.ktlint.core.RuleSet
import java.util.ArrayList
-fun Rule.lint(text: String, userData: Map<String, String> = emptyMap()): List<LintError> {
+fun Rule.lint(text: String, userData: Map<String, String> = emptyMap(), script: Boolean = false): List<LintError> {
val res = ArrayList<LintError>()
val debug = debugAST()
- KtLint.lint(text, (if (debug) listOf(RuleSet("debug", DumpAST())) else emptyList()) +
+ val f: L = if (script) KtLint::lintScript else KtLint::lint
+ f(text, (if (debug) listOf(RuleSet("debug", DumpAST())) else emptyList()) +
listOf(RuleSet("standard", this@lint)), userData) { e ->
if (debug) {
System.err.println("^^ lint error")
@@ -19,10 +20,27 @@ fun Rule.lint(text: String, userData: Map<String, String> = emptyMap()): List<Li
return res
}
+private typealias L = (
+ text: String,
+ ruleSets: Iterable<RuleSet>,
+ userData: Map<String, String>,
+ cb: (e: LintError) -> Unit
+) -> Unit
+
fun Rule.format(
text: String,
userData: Map<String, String> = emptyMap(),
- cb: (e: LintError, corrected: Boolean) -> Unit = { _, _ -> }
-): String =
- KtLint.format(text, (if (debugAST()) listOf(RuleSet("debug", DumpAST())) else emptyList()) +
+ cb: (e: LintError, corrected: Boolean) -> Unit = { _, _ -> },
+ script: Boolean = false
+): String {
+ val f: F = if (script) KtLint::formatScript else KtLint::format
+ return f(text, (if (debugAST()) listOf(RuleSet("debug", DumpAST())) else emptyList()) +
listOf(RuleSet("standard", this@format)), userData, cb)
+}
+
+private typealias F = (
+ text: String,
+ ruleSets: Iterable<RuleSet>,
+ userData: Map<String, String>,
+ cb: (e: LintError, corrected: Boolean) -> Unit
+) -> String
diff --git a/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/package.kt b/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/package.kt
deleted file mode 100644
index a3c6b837..00000000
--- a/ktlint-test/src/main/kotlin/com/github/shyiko/ktlint/test/package.kt
+++ /dev/null
@@ -1,27 +0,0 @@
-package com.github.shyiko.ktlint.test
-
-import com.github.shyiko.ktlint.core.Rule
-import org.jetbrains.kotlin.com.intellij.lang.ASTNode
-
-val debugAST = {
- (System.getProperty("ktlintDebug") ?: System.getenv("KTLINT_DEBUG") ?: "")
- .toLowerCase().split(",").contains("ast")
-}
-
-class DumpAST : Rule("dump") {
-
- override fun visit(node: ASTNode, autoCorrect: Boolean,
- emit: (offset: Int, errorMessage: String, corrected: Boolean) -> Unit) {
- var level = -1
- var parent: ASTNode? = node
- do {
- level++
- parent = parent?.treeParent
- } while (parent != null)
- System.err.println(" ".repeat(level) + node.psi.javaClass.name + " (${node.elementType})" +
- (if (node.getChildren(null).isEmpty()) " | \"" + node.text.escape() + "\"" else ""))
- }
-
- private fun String.escape() =
- this.replace("\\", "\\\\").replace("\n", "\\n").replace("\t", "\\t").replace("\r", "\\r")
-}