From 9e814782b4be58b949f398124f44468ae605fb66 Mon Sep 17 00:00:00 2001 From: charbgr Date: Tue, 22 May 2018 16:44:00 +0300 Subject: [PATCH] EditorConfig per path --- .../kotlin/com/github/shyiko/ktlint/Main.kt | 40 +++++++-------- .../ktlint/internal/EditorConfigFinder.kt | 51 +++++++++++++++++++ 2 files changed, 69 insertions(+), 22 deletions(-) create mode 100644 ktlint/src/main/kotlin/com/github/shyiko/ktlint/internal/EditorConfigFinder.kt diff --git a/ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt b/ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt index ca2cf1a79e..1f0f7192ec 100644 --- a/ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt +++ b/ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt @@ -10,6 +10,7 @@ import com.github.shyiko.ktlint.core.RuleExecutionException import com.github.shyiko.ktlint.core.RuleSet import com.github.shyiko.ktlint.core.RuleSetProvider import com.github.shyiko.ktlint.internal.EditorConfig +import com.github.shyiko.ktlint.internal.EditorConfigFinder import com.github.shyiko.ktlint.internal.IntellijIDEAIntegration import com.github.shyiko.ktlint.internal.MavenDependencyResolver import com.github.shyiko.ktlint.test.DumpAST @@ -187,19 +188,12 @@ object Main { @Option(names = arrayOf("-y"), hidden = true) private var forceApply: Boolean = false - @Option(names = arrayOf("--editorconfig-path"), description = arrayOf("Specify folder path of .editorconfig")) - private var editorConfigDirParam: String? = null - private val editorConfigDir: String - get() { - val filePath = if (editorConfigDirParam == null) File(".") else File(editorConfigDirParam) - return filePath.canonicalPath - } - @Parameters(hidden = true) private var patterns = ArrayList() private val workDir = File(".").canonicalPath private fun File.location() = if (relative) this.toRelativeString(File(workDir)) else this.path + private val editConfigFinder = EditorConfigFinder(workDir, true) private fun usage() = ByteArrayOutputStream() @@ -268,27 +262,17 @@ object Main { ruleSetProviders.forEach { System.err.println("[DEBUG] Discovered ruleset \"${it.first}\"") } } val reporter = loadReporter(dependencyResolver) - // load .editorconfig - val userData = ( - EditorConfig.of(editorConfigDir) - ?.also { editorConfig -> - if (debug) { - System.err.println("[DEBUG] Discovered .editorconfig (${ - generateSequence(editorConfig) { it.parent }.map { it.path.parent.toFile().location() }.joinToString() - })") - System.err.println("[DEBUG] ${editorConfig.mapKeys { it.key }} loaded from .editorconfig") - } - } - ?: emptyMap() - ) + mapOf("android" to android.toString()) + val userData = mapOf("android" to android.toString()) + val tripped = AtomicBoolean() data class LintErrorWithCorrectionInfo(val err: LintError, val corrected: Boolean) fun process(fileName: String, fileContent: String): List { if (debug) { System.err.println("[DEBUG] Checking ${if (fileName != "") File(fileName).location() else fileName}") } + val editorConfig = editConfigFinder.getEditorConfig(fileName) val result = ArrayList() - val localUserData = if (fileName != "") userData + ("file_path" to fileName) else userData + val localUserData = editorConfig.safe() + if (fileName != "") userData + ("file_path" to fileName) else userData if (format) { val formattedFileContent = try { format(fileName, fileContent, ruleSetProviders.map { it.second.get() }, localUserData) { err, corrected -> @@ -686,4 +670,16 @@ object Main { executorService.shutdown() consumer.join() } + + private fun EditorConfig?.safe(): Map { + this ?: return emptyMap() + + if (debug) { + System.err.println("[DEBUG] Discovered .editorconfig (${ + generateSequence(this) { it.parent }.map { it.path.parent.toFile().location() }.joinToString() + })") + System.err.println("[DEBUG] ${this.mapKeys { it.key }} loaded from " + this.path) + } + return this + } } diff --git a/ktlint/src/main/kotlin/com/github/shyiko/ktlint/internal/EditorConfigFinder.kt b/ktlint/src/main/kotlin/com/github/shyiko/ktlint/internal/EditorConfigFinder.kt new file mode 100644 index 0000000000..5dd356c0ab --- /dev/null +++ b/ktlint/src/main/kotlin/com/github/shyiko/ktlint/internal/EditorConfigFinder.kt @@ -0,0 +1,51 @@ +package com.github.shyiko.ktlint.internal + +import java.io.File + +class EditorConfigFinder(private val workDir: String, private val debug: Boolean) { + + private val editorConfigs = mutableMapOf() + + init { + editorConfigs[workDir] = EditorConfig.of(workDir) + } + + @Synchronized + fun getEditorConfig(file: File): EditorConfig? = editorConfigs.getOrPut(file.key(), { + findParentEditorConfig(file) + }) + + @Synchronized + fun getEditorConfig(filePath: String): EditorConfig? = getEditorConfig(File(filePath)) + + private fun findParentEditorConfig(file: File): EditorConfig? { + var currentFile: File = if (file.isDirectory) file else file.parentFile + val missingPathsWithEditorConfig = mutableListOf() + + while (currentFile.canonicalPath != workDir) { + val currentPath = currentFile.canonicalPath + val editorConfigFile = File("$currentPath/.editorconfig") + if (editorConfigFile.exists() && editorConfigFile.isFile) { + if (debug) { + System.err.println("[DEBUG] Found .editorconfig for ${file.name} in ${editorConfigFile.canonicalPath}") + } + + return EditorConfig.of(currentPath)?.also { + missingPathsWithEditorConfig.forEach { path -> + editorConfigs[path] = it + } + } + } + + currentFile = currentFile.parentFile + } + + return editorConfigs[workDir]?.also { + missingPathsWithEditorConfig.forEach { path -> + editorConfigs[path] = it + } + } + } + + private fun File.key() = this.parent +}