Skip to content

Commit

Permalink
Add reporter for format summary (#1536)
Browse files Browse the repository at this point in the history
* Call callback on all errors
* Add reporter for a format summary of one line per file.

Closes #1491
Closes #821
  • Loading branch information
paul-dingemans committed Jul 24, 2022
1 parent 2f1b672 commit 7ae1126
Show file tree
Hide file tree
Showing 15 changed files with 744 additions and 236 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Expand Up @@ -114,6 +114,8 @@ The callback function provided as parameter to the format function is now called

### Added

* Add `format` reporter. This reporter prints a one-line-summary of the formatting status per file. ([#621](https://github.com/pinterest/ktlint/issue/621)).

### Fixed

* Fix cli argument "--disabled_rules" ([#1520](https://github.com/pinterest/ktlint/issue/1520)).
Expand Down Expand Up @@ -169,12 +171,16 @@ If your project did not run with the `experimental` ruleset enabled before, you

### API Changes & RuleSet providers

If you are not an API user nor a RuleSet provider, then you can safely skip this section. Otherwise, please read below carefully and upgrade your usage of ktlint. In this and coming releases, we are changing and adapting important parts of our API in order to increase maintainability and flexibility for future changes. Please avoid skipping a releases as that will make it harder to migrate.
If you are not an API consumer nor a RuleSet provider, then you can safely skip this section. Otherwise, please read below carefully and upgrade your usage of ktlint. In this and coming releases, we are changing and adapting important parts of our API in order to increase maintainability and flexibility for future changes. Please avoid skipping a releases as that will make it harder to migrate.

#### Lint and formatting functions

The lint and formatting changes no longer accept parameters of type `Params` but only `ExperimentalParams`. Also, the VisitorProvider parameter has been removed. Because of this, your integration with KtLint breaks. Based on feedback with ktlint 0.45.x, we now prefer to break at compile time instead of trying to keep the interface backwards compatible. Please raise an issue, in case you help to convert to the new API.

#### Format callback

The callback function provided as parameter to the format function is now called for all errors regardless whether the error has been autocorrected. Existing consumers of the format function should now explicitly check the `autocorrected` flag in the callback result and handle it appropriately (in most case this will be ignoring the callback results for which `autocorrected` has value `true`).

#### Use of ".editorconfig" properties & userData

The interface `UsesEditorConfigProperties` provides method `getEditorConfigValue` to retrieve a named `.editorconfig` property for a given ASTNode. When implementing this interface, the value `editorConfigProperties` needs to be overridden. Previously it was not checked whether a retrieved property was actually recorded in this list. Now, retrieval of unregistered properties results in an exception.
Expand Down Expand Up @@ -237,6 +243,7 @@ An AssertJ style API for testing KtLint rules ([#1444](https://github.com/pinter
- Fix indentation of property getter/setter when the property has an initializer on a separate line `indent` ([#1335](https://github.com/pinterest/ktlint/issues/1335))
- When `.editorconfig` setting `indentSize` is set to value `tab` then return the default tab width as value for `indentSize` ([#1485](https://github.com/pinterest/ktlint/issues/1485))
- Allow suppressing all rules or a list of specific rules in the entire file with `@file:Suppress(...)` ([#1029](https://github.com/pinterest/ktlint/issues/1029))
- Invoke callback on `format` function for all errors including errors that are autocorrected ([#1491](https://github.com/pinterest/ktlint/issues/1491))


### Changed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -20,7 +20,7 @@
While this might sound extreme, keep in mind that `ktlint` tries to capture (reflect) **official code style**[*](https://github.com/pinterest/ktlint/issues/284#issuecomment-425177186) from [kotlinlang.org](https://kotlinlang.org/docs/reference/coding-conventions.html) and [Android Kotlin Style Guide](https://android.github.io/kotlin-guides/style.html)
(+ [we respect your .editorconfig](#editorconfig) and support additional [ruleset](#creating-a-ruleset)|s).
- **Built-in formatter.** So that you wouldn't have to fix all style violations by hand.
- **Customizable output.** `plain` (+ `plain?group_by_file`), `json`, `html` and `checkstyle` reporters are available out-of-the-box.
- **Customizable output.** `plain` (+ `plain?group_by_file`), `format`, `json`, `html` and `checkstyle` reporters are available out-of-the-box.
It's also [easy to create your own](#creating-a-reporter).
- **A single executable jar with all dependencies included.**

Expand Down
45 changes: 30 additions & 15 deletions ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/Reporter.kt
Expand Up @@ -6,42 +6,57 @@ package com.pinterest.ktlint.core
* on the same thread).
* @see ReporterProvider
*/
interface Reporter {

public interface Reporter {
/**
* Called once, before the processing begins
* (regardless of whether any files matching the pattern are (going to) found).
* It's guarantied to be called before any of the other [Reporter]s methods.
*/
fun beforeAll() {}
public fun beforeAll() {}

/**
* Called when file (matching the pattern) is found but before it's parsed.
*/
fun before(file: String) {}
fun onLintError(file: String, err: LintError, corrected: Boolean)
public fun before(file: String) {}

/**
* Called once for each lint violation that is found. The [corrected] flag indicated whether the violations has been
* fixed automatically.
*/
public fun onLintError(file: String, err: LintError, corrected: Boolean)

/**
* Called after ktlint is done with the file.
*/
fun after(file: String) {}
public fun after(file: String) {}

/**
* Called once, after all the files (if any) have been processed.
* It's guarantied to be called after all other [Reporter]s methods.
*/
fun afterAll() {}
public fun afterAll() {}

public companion object {
/**
* Aggregates multiple [Reporter]s into a single reporter. Each action called on the aggregation will be invoked
* on all individual [Reporter]s.
*/
public fun from(vararg reporters: Reporter): Reporter =
object : Reporter {
override fun beforeAll() =
reporters.forEach(Reporter::beforeAll)

override fun before(file: String) =
reporters.forEach { it.before(file) }

companion object {
fun from(vararg reporters: Reporter): Reporter {
return object : Reporter {
override fun beforeAll() { reporters.forEach(Reporter::beforeAll) }
override fun before(file: String) { reporters.forEach { it.before(file) } }
override fun onLintError(file: String, err: LintError, corrected: Boolean) =
reporters.forEach { it.onLintError(file, err, corrected) }
override fun after(file: String) { reporters.forEach { it.after(file) } }
override fun afterAll() { reporters.forEach(Reporter::afterAll) }

override fun after(file: String) =
reporters.forEach { it.after(file) }

override fun afterAll() =
reporters.forEach(Reporter::afterAll)
}
}
}
}

0 comments on commit 7ae1126

Please sign in to comment.