-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #874 from Tapchicoma/701/initial-editorconfig-inte…
…gration Initial implementation of IDE integration via editorconfig
- Loading branch information
Showing
16 changed files
with
695 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/api/OptInFeatures.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package com.pinterest.ktlint.core.api | ||
|
||
@RequiresOptIn( | ||
message = "This Ktlint feature is highly experimental, and most probably will change in the future releases.", | ||
level = RequiresOptIn.Level.ERROR | ||
) | ||
@Retention(AnnotationRetention.BINARY) | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) | ||
public annotation class FeatureInAlphaState | ||
|
||
@RequiresOptIn( | ||
message = "This Ktlint feature is experimental, and may change in the future releases.", | ||
level = RequiresOptIn.Level.WARNING | ||
) | ||
@Retention(AnnotationRetention.BINARY) | ||
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) | ||
public annotation class FeatureInBetaState |
52 changes: 52 additions & 0 deletions
52
ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/api/UsesEditorConfigProperties.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package com.pinterest.ktlint.core.api | ||
|
||
import org.ec4j.core.model.Property | ||
import org.ec4j.core.model.PropertyType | ||
|
||
/** | ||
* Indicates [com.pinterest.ktlint.core.Rule] uses properties loaded from `.editorconfig` file. | ||
* | ||
* This properties could be: | ||
* - universal `.editorconfig` properties defined | ||
* [here](https://github.com/editorconfig/editorconfig/wiki/EditorConfig-Properties#current-universal-properties) | ||
* - universal IntelliJ IDEA properties defined | ||
* [here](https://github.com/JetBrains/intellij-community/blob/master/platform/lang-api/src/com/intellij/psi/codeStyle/CommonCodeStyleSettings.java) | ||
* - Kotlin specific properties defined | ||
* [here](https://github.com/JetBrains/kotlin/blob/master/idea/formatter/src/org/jetbrains/kotlin/idea/core/formatter/KotlinCodeStyleSettings.java) | ||
* | ||
* In the best case rule should only use one property. | ||
* | ||
* See [com.pinterest.ktlint.core.KtLint.generateKotlinEditorConfigSection] documentation how to generate | ||
* `.editorconfig` based on [com.pinterest.ktlint.core.Rule]s with this interface implementations. | ||
*/ | ||
@FeatureInAlphaState | ||
public interface UsesEditorConfigProperties { | ||
|
||
/** | ||
* Provide a list of code style editorconfig properties, that rule uses in linting. | ||
*/ | ||
public val editorConfigProperties: List<EditorConfigProperty<*>> | ||
|
||
/** | ||
* Get the value of [EditorConfigProperty] based on loaded [EditorConfigProperties] content. | ||
*/ | ||
public fun <T> EditorConfigProperties.getEditorConfigValue(property: EditorConfigProperty<T>): T { | ||
return get(property.type.name)?.getValueAs() ?: property.defaultValue | ||
} | ||
|
||
/** | ||
* Supported `.editorconfig` property. | ||
* | ||
* @param type type of property. Could be one of default ones (see [PropertyType.STANDARD_TYPES]) or custom one. | ||
* @param defaultValue default value for property if it does not exist in loaded properties. | ||
*/ | ||
public data class EditorConfigProperty<T>( | ||
public val type: PropertyType<T>, | ||
public val defaultValue: T | ||
) | ||
} | ||
|
||
/** | ||
* Loaded [Property]s from `.editorconfig` files. | ||
*/ | ||
public typealias EditorConfigProperties = Map<String, Property> |
58 changes: 58 additions & 0 deletions
58
ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/EditorConfigGenerator.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.pinterest.ktlint.core.internal | ||
|
||
import com.pinterest.ktlint.core.Rule | ||
import com.pinterest.ktlint.core.api.FeatureInAlphaState | ||
import com.pinterest.ktlint.core.api.UsesEditorConfigProperties | ||
import java.nio.file.Path | ||
import org.ec4j.core.model.Property | ||
|
||
/** | ||
* Generates Kotlin section content for `.editorconfig` file. | ||
* | ||
* Rule should implement [UsesEditorConfigProperties] interface to support this. | ||
*/ | ||
@FeatureInAlphaState | ||
internal class EditorConfigGenerator( | ||
private val editorConfigLoader: EditorConfigLoader | ||
) { | ||
/** | ||
* Method loads merged `.editorconfig` content using [com.pinterest.ktlint.core.KtLint.Params.fileName] path, | ||
* and then, by querying rules from [com.pinterest.ktlint.core.KtLint.Params.ruleSets] | ||
* generates Kotlin section (default is `[*.{kt,kts}]`) content including expected default values. | ||
* | ||
* @return Kotlin section editorconfig content. For example: | ||
* ```properties | ||
* final-newline = true | ||
* indent-size = 4 | ||
* ``` | ||
*/ | ||
fun generateEditorconfig( | ||
filePath: Path, | ||
rules: Set<Rule>, | ||
debug: Boolean = false | ||
): String { | ||
val editorConfig: Map<String, Property> = editorConfigLoader.loadPropertiesForFile( | ||
filePath = filePath, | ||
rules = rules, | ||
debug = debug | ||
) | ||
|
||
return rules | ||
.fold(mutableMapOf<String, String?>()) { acc, rule -> | ||
if (rule is UsesEditorConfigProperties) { | ||
if (debug) println("Checking properties for '${rule.id}' rule") | ||
rule.editorConfigProperties.forEach { prop -> | ||
if (debug) println("Setting '${prop.type.name}' property value") | ||
acc[prop.type.name] = with(rule) { editorConfig.getEditorConfigValue(prop).toString() } | ||
} | ||
} | ||
|
||
acc | ||
} | ||
.filterValues { it != null } | ||
.map { | ||
"${it.key} = ${it.value}" | ||
} | ||
.joinToString(separator = System.lineSeparator()) | ||
} | ||
} |
Oops, something went wrong.