Skip to content

Commit

Permalink
Add multiline-loop changelog and documentation (#2298)
Browse files Browse the repository at this point in the history
  • Loading branch information
hanggrian committed Oct 11, 2023
1 parent 33686d6 commit 4fec107
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 25 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,9 @@ This project adheres to [Semantic Versioning](https://semver.org/).

* Add new experimental rule `function-type-modifier-spacing` rule - [#2216](https://github.com/pinterest/ktlint/pull/2216), by @t-kameyama

* Define `EditorConfigOverride` for dynamically loaded ruleset - [#2194](https://github.com/pinterest/ktlint/pull/2194), by @paul-dingemans
* Add new experimental rule `multiline-loop` rule - [#2298](https://github.com/pinterest/ktlint/pull/2298), by @hendraanggrian

* Define `EditorConfigOverride` for dynamically loaded ruleset - [#2194](https://github.com/pinterest/ktlint/pull/2194), by @paul-dingemans
The `EditorConfigOverride` parameter of the `KtlintRuleEngine` can be defined using the factory method `EditorConfigOverride.from(vararg properties: Pair<EditorConfigProperty<*>, *>)`. This requires the `EditorConfigProperty`'s to be available at compile time. Some common `EditorConfigProperty`'s are defined in `ktlint-rule-engine-core` which is loaded as transitive dependency of `ktlint-rule-engine` and as of that are available at compile.
If an `EditorConfigProperty` is defined in a `Rule` that is only provided via a runtime dependency, it gets a bit more complicated. The `ktlint-api-consumer` example has now been updated to show how the `EditorConfigProperty` can be retrieved from the `Rule`.

Expand Down
45 changes: 33 additions & 12 deletions documentation/snapshot/docs/rules/experimental.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Rule id: `binary-expression-wrapping` (`standard` rule set)

## Chain method continuation

In a multiline method chain, the chain operators (`.` or `?.`) have to be aligned with each other.
In a multiline method chain, the chain operators (`.` or `?.`) have to be aligned with each other.

Multiple chained methods on a single line are allowed as long as the maximum line length, and the maximum number of chain operators are not exceeded. Under certain conditions, it is allowed that the expression before the first and/or the expression after the last chain operator is a multiline expression.

Expand Down Expand Up @@ -113,7 +113,7 @@ This rule can be configured with `.editorconfig` property [`ktlint_chain_method_
?.map {
2 * it
}
val foo3 =
val foo3 =
foo()
.bar().map {
it.foobar()
Expand All @@ -140,36 +140,36 @@ The other code styles allow an infinite amount of parameters on the same line (a
```kotlin
// Assume that max_line_length is not exceeded when written as single line
class Foo0

class Foo1(
a: Any,
)

class Foo2(
a: Any,
b: Any,
)

class Foo3(
@Foo a: Any,
b: Any,
c: Any,
)

class Foo4(
a: Any,
b: Any,
c: Any,
) : FooBar(a, c)

class Foo5 :
FooBar(
"bar1",
"bar2",
) {
// body
}

class Foo6(
val bar1: Bar,
val bar2: Bar,
Expand All @@ -179,7 +179,7 @@ The other code styles allow an infinite amount of parameters on the same line (a
) {
// body
}

class Foo7(
val bar1: Bar,
val bar2: Bar,
Expand All @@ -191,7 +191,7 @@ The other code styles allow an infinite amount of parameters on the same line (a
BarFoo2 {
// body
}

class Foo8
constructor(
val bar1: Bar,
Expand Down Expand Up @@ -422,11 +422,11 @@ If the function literal contains multiple parameter and at least one parameter o
->
foo + bar
}

// Assume that the last allowed character is
// at the X character on the right X
val foobar7 =
barrrrrrrrrrrrrr {
barrrrrrrrrrrrrr {
fooooooooooooooo: Foo
->
foo.repeat(2)
Expand Down Expand Up @@ -480,3 +480,24 @@ Enforce a single whitespace between the modifier list and the function type.
```

Rule id: `function-type-modifier-spacing` (`standard` rule set)

## Multiline loop

Braces required for multiline for, while, and do statements.

=== "[:material-heart:](#) Ktlint"

```kotlin
for (i in 1..10) {
println(i)
}
```

=== "[:material-heart-off-outline:](#) Disallowed"

```kotlin
for (i in 1..10)
println(i)
```

Rule id: `multiline-loop` (`standard` rule set)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class MultiLineLoopRuleTest {
val code =
"""
fun foo() {
for (c in "Hello World") { return 0 }
for (i in 1..10) { return 0 }
while (true) { return 0 }
do { return 0 } while (true)
}
Expand All @@ -25,7 +25,7 @@ class MultiLineLoopRuleTest {
val code =
"""
fun foo() {
for (c in "Hello World") return 0
for (i in 1..10) return 0
while (true) return 0
do return 0 while (true)
}
Expand All @@ -38,7 +38,7 @@ class MultiLineLoopRuleTest {
val code =
"""
fun foo() {
for (c in "Hello World") {
for (i in 1..10) {
return 0
}
while (true) {
Expand All @@ -57,7 +57,7 @@ class MultiLineLoopRuleTest {
val code =
"""
fun foo() {
for (c in "Hello World")
for (i in 1..10)
return 0
while (true)
return 0
Expand All @@ -69,7 +69,7 @@ class MultiLineLoopRuleTest {
val formattedCode =
"""
fun foo() {
for (c in "Hello World") {
for (i in 1..10) {
return 0
}
while (true) {
Expand All @@ -94,7 +94,7 @@ class MultiLineLoopRuleTest {
val code =
"""
fun main() {
for (c in "Hello World")
for (i in 1..10)
while (true)
do
return 0
Expand All @@ -104,7 +104,7 @@ class MultiLineLoopRuleTest {
val formattedCode =
"""
fun main() {
for (c in "Hello World") {
for (i in 1..10) {
while (true) {
do {
return 0
Expand All @@ -127,7 +127,7 @@ class MultiLineLoopRuleTest {
"""
fun test(s: String?): Int {
val i = s.let {
for (c in s)
for (i in 1..10)
1
while (true)
2
Expand All @@ -142,7 +142,7 @@ class MultiLineLoopRuleTest {
"""
fun test(s: String?): Int {
val i = s.let {
for (c in s) {
for (i in 1..10) {
1
}
while (true) {
Expand Down Expand Up @@ -191,7 +191,7 @@ class MultiLineLoopRuleTest {
val code =
"""
fun foo() {
for (c in "Hello World") 25
for (i in 1..10) 25
.toString()
while (true) 50
.toString()
Expand All @@ -203,7 +203,7 @@ class MultiLineLoopRuleTest {
val formattedCode =
"""
fun foo() {
for (c in "Hello World") {
for (i in 1..10) {
25
.toString()
}
Expand All @@ -220,7 +220,7 @@ class MultiLineLoopRuleTest {
multiLineLoopRuleAssertThat(code)
.addAdditionalRuleProvider { IndentationRule() }
.hasLintViolations(
LintViolation(2, 30, "Missing { ... }"),
LintViolation(2, 22, "Missing { ... }"),
LintViolation(4, 18, "Missing { ... }"),
LintViolation(6, 8, "Missing { ... }"),
)
Expand Down

0 comments on commit 4fec107

Please sign in to comment.