Skip to content

Commit

Permalink
EditorConfig per path
Browse files Browse the repository at this point in the history
  • Loading branch information
charbgr committed May 23, 2018
1 parent e593dcf commit 9e81478
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
40 changes: 18 additions & 22 deletions ktlint/src/main/kotlin/com/github/shyiko/ktlint/Main.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<String>()

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()
Expand Down Expand Up @@ -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<String, String>()
) + 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<LintErrorWithCorrectionInfo> {
if (debug) {
System.err.println("[DEBUG] Checking ${if (fileName != "<text>") File(fileName).location() else fileName}")
}
val editorConfig = editConfigFinder.getEditorConfig(fileName)
val result = ArrayList<LintErrorWithCorrectionInfo>()
val localUserData = if (fileName != "<text>") userData + ("file_path" to fileName) else userData
val localUserData = editorConfig.safe() + if (fileName != "<text>") userData + ("file_path" to fileName) else userData
if (format) {
val formattedFileContent = try {
format(fileName, fileContent, ruleSetProviders.map { it.second.get() }, localUserData) { err, corrected ->
Expand Down Expand Up @@ -686,4 +670,16 @@ object Main {
executorService.shutdown()
consumer.join()
}

private fun EditorConfig?.safe(): Map<String, String> {
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
}
}
Original file line number Diff line number Diff line change
@@ -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<String, EditorConfig?>()

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<String>()

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
}

0 comments on commit 9e81478

Please sign in to comment.