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

Clarify how to implement an API Consumer #1881

Merged
merged 2 commits into from
Mar 24, 2023
Merged

Conversation

paul-dingemans
Copy link
Collaborator

Description

Clarify how to implement an API Consumer by

  • Improving the ktlint-api-consumer module
  • Adding a logging framework and custom logging configuration
  • Adding documentation about how to create an API consumer

Checklist

  • PR description added
  • tests are added
  • KtLint has been applied on source code itself and violations are fixed
  • documentation is updated
  • CHANGELOG.md is updated

In case of adding a new rule:


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 =
Copy link
Contributor

@leinardi leinardi Mar 23, 2023

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?

Copy link
Collaborator Author

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(),
Copy link
Contributor

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.

Copy link
Collaborator Author

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.

Copy link
Contributor

@leinardi leinardi Mar 23, 2023

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?

Copy link
Collaborator Author

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.

docs/api/custom-integration.md Show resolved Hide resolved
@paul-dingemans paul-dingemans mentioned this pull request Mar 24, 2023
6 tasks
@paul-dingemans paul-dingemans merged commit 8757eb6 into master Mar 24, 2023
@paul-dingemans paul-dingemans deleted the api-consumer branch March 24, 2023 16:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants