-
Notifications
You must be signed in to change notification settings - Fork 508
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
Clarify how to implement an API Consumer #1881
Conversation
|
||
The `KtLintRuleEngine` must be configured with at least one `RuleProvider`. A `RuleProvider` is a lambda which upon request of the `KtLintRuleEngine` provides a new instance of a specific rule. You can either provide any of the standard rules provided by KtLint or with your own custom rules, or with a combination of both. | ||
```kotlin title="Creating a set of RuleProviders" | ||
val KTLINT_API_CONSUMER_RULE_PROVIDERS = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I get this right, this example shows how to define a set of rules that can have custom rules or rules from KtLint rulesets. But to me it is not clear how can I just use the use all the standard rules from KtLint rulesets.
In Slack I was recommended to do the following:
private val ruleProviders = buildSet {
ServiceLoader.load(RuleSetProviderV2::class.java).flatMapTo(this) { it.getRuleProviders() }
}
But I'm not sure what this is providing. Is this providing all the standard rules from KtLint rulesets?
What about the experimental rulesets? How can I be able to add those as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To get just the RuleProviders from Ktlint, it is more readable to use StandardRuleSetProvider().getRuleProviders()
(see https://github.com/pinterest/ktlint/blob/0.48.2/ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/StandardRuleSetProvider.kt).
The code block above retrieves all rules which are defined in the project or in any of the dependencies that are included in the project. Standard rules will then only be retrieved in case the Ktlint dependency is included in which the Standard rules are defined.
In '0.48.x', experimental rules can be retrieved in a similar way as the standard rules. Note that in 0.49.x
the experimental rules will be included in the standard rules. See changelog for more information.
ruleProviders = KTLINT_API_CONSUMER_RULE_PROVIDERS, | ||
editorConfigDefaults = EditorConfigDefaults.load( | ||
path = Paths.get("/some/path/to/editorconfig/file/or/directory"), | ||
propertyTypes = KTLINT_API_CONSUMER_RULE_PROVIDERS.propertyTypes(), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a confirmation: this is only valid for 0.49.x
, right? The current stable version of EditorConfigDefaults
doesn't have any propertyTypes
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That is correct. The function without the propertyType
parameter is deprecated in 0.49.x
and will be removed in the next release after that.
ktlint-api-consumer/build.gradle.kts
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry if it's a stupid question but what's the difference between ktlint-api-consimer
and ktlint-core
? Right now I'm using ktlint-code
, should I use instead ktlint-api-consumer
? If yes, why?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ktlint-api-consumer
is just meant as an example of how an API Consumer can be implemented. It is not meant to be used as a kind of parent dependency. I will add a README.md
file to that module to explain that.
Description
Clarify how to implement an API Consumer by
Checklist
CHANGELOG.md
is updatedIn case of adding a new rule: