-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support partial formatting (range) in given code
Allows API consumers like the 'ktlint-intellij-plugin' to format a block of code inside the given code (for example a file) without autocorrect the given code entirely. The start offset of the node on which a rule can detect a lint violation should be inside the range which is to be formatted. This has some unexpected side effects for some rules. In most cases it is to be expected that the user won't notice those side effects. And if it happens, the most likely way the user responds is widening the range which is to be formatted, and try to format again. For example, the given code might contain the when-statement below: ``` // code with lint violations when(foobar) { FOO -> "Single line" BAR -> """ Multi line """.trimIndent() else -> null } // more code with lint violations ``` The `blank-line-between-when-conditions` rule requires blank lines to be added between the conditions. If the when-keyword above is included in the range which is to be formatted then the blank lines before the conditions are added. If only the when-conditions itself are selected, but not the when-keyword, then the blank lines are not added. This unexpected behavior is a side effect of the way the partial formatting is implemented currently. The side effects can be prevented by delaying the decision to autocorrect as late as possible and the exact offset of the error is known. This however would cause a breaking change, and needs to wait until Ktlint V2.x. Closes #2629
- Loading branch information
1 parent
c9a97af
commit fd083b6
Showing
5 changed files
with
139 additions
and
9 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
24 changes: 24 additions & 0 deletions
24
...le-engine/src/main/kotlin/com/pinterest/ktlint/rule/engine/internal/AutoCorrectHandler.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,24 @@ | ||
package com.pinterest.ktlint.rule.engine.internal | ||
|
||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
||
/** | ||
* Handler which determines whether autocorrect should be enabled or disabled for the given offset. | ||
*/ | ||
internal sealed interface AutoCorrectHandler { | ||
fun autoCorrect(node: ASTNode): Boolean | ||
} | ||
|
||
internal data object AutoCorrectDisabledHandler : AutoCorrectHandler { | ||
override fun autoCorrect(node: ASTNode) = false | ||
} | ||
|
||
internal data object AutoCorrectEnabledHandler : AutoCorrectHandler { | ||
override fun autoCorrect(node: ASTNode) = true | ||
} | ||
|
||
internal class AutoCorrectOffsetRangeHandler( | ||
private val autoCorrectOffsetRange: IntRange, | ||
) : AutoCorrectHandler { | ||
override fun autoCorrect(node: ASTNode) = node.startOffset in autoCorrectOffsetRange | ||
} |
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