Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to specify editorconfig path #207

Merged
merged 2 commits into from
Jun 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}