Skip to content

Commit

Permalink
Move disabled_rules into its own userdata key
Browse files Browse the repository at this point in the history
  • Loading branch information
shashachu committed Jul 3, 2019
1 parent 9c959bd commit 20c7758
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.pinterest.ktlint.core

/**
* @see [EditorConfig](http://editorconfig.org/)
*
* This class is injected into the user data, so it is available to rules via [KtLint.EDITOR_CONFIG_USER_DATA_KEY]
*/
interface EditorConfig {

Expand All @@ -12,7 +14,6 @@ interface EditorConfig {
val tabWidth: Int
val maxLineLength: Int
val insertFinalNewline: Boolean
val disabledRules: Set<String>
fun get(key: String): String?

companion object {
Expand All @@ -27,14 +28,12 @@ interface EditorConfig {
val tabWidth = map["indent_size"]?.toIntOrNull()
val maxLineLength = map["max_line_length"]?.toIntOrNull() ?: -1
val insertFinalNewline = map["insert_final_newline"]?.toBoolean() ?: true
val disabledRules = map["disabled_rules"]?.split(",")?.toSet() ?: emptySet()
return object : EditorConfig {
override val indentStyle = indentStyle
override val indentSize = indentSize
override val tabWidth = tabWidth ?: indentSize
override val maxLineLength = maxLineLength
override val insertFinalNewline = insertFinalNewline
override val disabledRules: Set<String> = disabledRules
override fun get(key: String): String? = map[key]
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ object KtLint {
val EDITOR_CONFIG_USER_DATA_KEY = Key<EditorConfig>("EDITOR_CONFIG")
val ANDROID_USER_DATA_KEY = Key<Boolean>("ANDROID")
val FILE_PATH_USER_DATA_KEY = Key<String>("FILE_PATH")
val DISABLED_RULES = Key<Set<String>>("DISABLED_RULES")

private val psiFileFactory: PsiFileFactory
private val nullSuppression = { _: Int, _: String, _: Boolean -> false }
Expand Down Expand Up @@ -227,6 +228,7 @@ object KtLint {
node.putUserData(FILE_PATH_USER_DATA_KEY, userData["file_path"])
node.putUserData(EDITOR_CONFIG_USER_DATA_KEY, EditorConfig.fromMap(editorConfigMap - "android" - "file_path"))
node.putUserData(ANDROID_USER_DATA_KEY, android)
node.putUserData(DISABLED_RULES, userData["disabled_rules"]?.split(",")?.toSet() ?: emptySet())
}

private fun visitor(
Expand Down Expand Up @@ -295,7 +297,7 @@ object KtLint {
}

private fun filterDisabledRules(rootNode: ASTNode, fqRuleId: String): Boolean {
return rootNode.getUserData(EDITOR_CONFIG_USER_DATA_KEY)?.disabledRules?.contains(fqRuleId) == false
return rootNode.getUserData(DISABLED_RULES)?.contains(fqRuleId) == false
}

private fun calculateLineColByOffset(text: String): (offset: Int) -> Pair<Int, Int> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import java.util.Properties
import java.util.concurrent.CompletableFuture
import java.util.concurrent.ConcurrentHashMap

/**
* This class handles traversing the filetree and parsing and merging the contents of any discovered .editorconfig files
*/
class EditorConfigInternal private constructor (
val parent: EditorConfigInternal?,
val path: Path,
Expand Down

0 comments on commit 20c7758

Please sign in to comment.