Skip to content

Commit

Permalink
Merge pull request #207 from charbgr/charbgr/custom-editorconfig-path
Browse files Browse the repository at this point in the history
Add ability to specify editorconfig path
  • Loading branch information
shyiko authored Jun 14, 2018
2 parents 6bed7cb + 88beb05 commit 1360289
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 14 deletions.
32 changes: 18 additions & 14 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 @@ -192,6 +193,7 @@ object Main {

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 @@ -260,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(workDir)
?.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 @@ -678,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,52 @@
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 pathsWithMissingEditorConfig = 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 {
pathsWithMissingEditorConfig.forEach { path ->
editorConfigs[path] = it
}
}
}

pathsWithMissingEditorConfig.add(currentPath)
currentFile = currentFile.parentFile
}

return editorConfigs[workDir]?.also {
pathsWithMissingEditorConfig.forEach { path ->
editorConfigs[path] = it
}
}
}

private fun File.key() = this.parent
}

0 comments on commit 1360289

Please sign in to comment.