-
Notifications
You must be signed in to change notification settings - Fork 509
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix indentation rule has problems with ignoring braces (#1646)
* fix indentation wrong if suppress single line contains braces * Rename PreparedCode to RuleExecutionContext as changing the suppressionLocator in a Context feels less strange than in PreparedCode which sounds more immutable. * Extract SuppressHandler so that the logic that alters the autoCorrect and emit variables is in one place. Closes #1644 Co-authored-by: yuejunyu <yuejunyu.0@bytedance.com> Co-authored-by: paul-dingemans <paul-dingemans@users.noreply.github.com>
- Loading branch information
1 parent
65f1cd0
commit f50bb5e
Showing
6 changed files
with
118 additions
and
42 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
34 changes: 34 additions & 0 deletions
34
ktlint-core/src/main/kotlin/com/pinterest/ktlint/core/internal/SuppressHandler.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,34 @@ | ||
package com.pinterest.ktlint.core.internal | ||
|
||
import org.jetbrains.kotlin.com.intellij.lang.ASTNode | ||
|
||
internal class SuppressHandler( | ||
private val suppressionLocator: SuppressionLocator, | ||
private val rootNode: ASTNode, | ||
private val autoCorrect: Boolean, | ||
private val emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit, | ||
) { | ||
fun handle( | ||
node: ASTNode, | ||
ruleId: String, | ||
function: (Boolean, (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit) -> Unit, | ||
) { | ||
val suppress = suppressionLocator( | ||
node.startOffset, | ||
ruleId, | ||
node == rootNode, | ||
) | ||
val autoCorrect = this.autoCorrect && !suppress | ||
val emit = if (suppress) { | ||
suppressEmit | ||
} else { | ||
this.emit | ||
} | ||
function(autoCorrect, emit) | ||
} | ||
|
||
private companion object { | ||
// Swallow violation so that it is not emitted | ||
val suppressEmit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit = { _, _, _ -> } | ||
} | ||
} |
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