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

Fixes/1078/statement wrap after semi #2065

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ At this point in time, it is not yet decided what the next steps will be. Ktlint
* Extract rule `no-single-line-block-comment` from `comment-wrapping` rule. The `no-single-line-block-comment` rule is added as experimental rule to the `ktlint_official` code style, but it can be enabled explicitly for the other code styles as well. ([#1980](https://github.com/pinterest/ktlint/issues/1980))
* Clean-up unwanted logging dependencies ([#1998](https://github.com/pinterest/ktlint/issues/1998))
* Fix directory traversal for patterns referring to paths outside of current working directory or any of it child directories ([#2002](https://github.com/pinterest/ktlint/issues/2002))
* Prevent multiple expression in same line separated by semicolon ([#1078](https://github.com/pinterest/ktlint/issues/1078))
atulgpt marked this conversation as resolved.
Show resolved Hide resolved

### Changed

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ internal class ReporterAggregator(
val stream =
when {
reporterConfiguration.output != null -> {
File(reporterConfiguration.output).parentFile?.mkdirsOrFail(); PrintStream(reporterConfiguration.output, "UTF-8")
File(reporterConfiguration.output).parentFile?.mkdirsOrFail()
PrintStream(reporterConfiguration.output, "UTF-8")
atulgpt marked this conversation as resolved.
Show resolved Hide resolved
}

stdin -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ internal class RangeTree(seq: List<Int> = emptyList()) {

init {
if (arr.isNotEmpty()) {
arr.reduce { p, n -> require(p <= n) { "Input must be sorted" }; n }
arr.reduce { p, n ->
require(p <= n) { "Input must be sorted" }
n
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ENUM_ENTRY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.OBJECT_KEYWORD
import com.pinterest.ktlint.rule.engine.core.api.ElementType.SEMICOLON
import com.pinterest.ktlint.rule.engine.core.api.Rule.VisitorModifier.RunAfterRule
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpace
import com.pinterest.ktlint.rule.engine.core.api.lastChildLeafOrSelf
Expand All @@ -24,7 +25,17 @@ import org.jetbrains.kotlin.psi.KtIfExpression
import org.jetbrains.kotlin.psi.KtLoopExpression
import org.jetbrains.kotlin.psi.psiUtil.getStrictParentOfType

public class NoSemicolonsRule : StandardRule("no-semi") {
public class NoSemicolonsRule :
StandardRule(
id = "no-semi",
visitorModifiers =
setOf(
RunAfterRule(
ruleId = WRAPPING_RULE_ID,
mode = RunAfterRule.Mode.REGARDLESS_WHETHER_RUN_AFTER_RULE_IS_LOADED_OR_DISABLED,
),
),
) {
override fun beforeVisitChildNodes(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.COMMA
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CONDITION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.DESTRUCTURING_DECLARATION
import com.pinterest.ktlint.rule.engine.core.api.ElementType.DOT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ENUM_ENTRY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.EOL_COMMENT
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_LITERAL
Expand All @@ -26,6 +27,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.OBJECT_LITERAL
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RBRACE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RBRACKET
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RPAR
import com.pinterest.ktlint.rule.engine.core.api.ElementType.SEMICOLON
import com.pinterest.ktlint.rule.engine.core.api.ElementType.STRING_TEMPLATE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.SUPER_TYPE_CALL_ENTRY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.SUPER_TYPE_ENTRY
Expand Down Expand Up @@ -64,6 +66,7 @@ import com.pinterest.ktlint.rule.engine.core.api.nextCodeLeaf
import com.pinterest.ktlint.rule.engine.core.api.nextCodeSibling
import com.pinterest.ktlint.rule.engine.core.api.nextLeaf
import com.pinterest.ktlint.rule.engine.core.api.nextSibling
import com.pinterest.ktlint.rule.engine.core.api.noNewLineInClosedRange
import com.pinterest.ktlint.rule.engine.core.api.prevCodeLeaf
import com.pinterest.ktlint.rule.engine.core.api.prevLeaf
import com.pinterest.ktlint.rule.engine.core.api.prevSibling
Expand Down Expand Up @@ -137,6 +140,7 @@ public class WrappingRule :
ARROW -> rearrangeArrow(node, autoCorrect, emit)
WHITE_SPACE -> line += node.text.count { it == '\n' }
CLOSING_QUOTE -> rearrangeClosingQuote(node, autoCorrect, emit)
SEMICOLON -> insertNewLineBeforeSemi(node, autoCorrect, emit)
}
}

Expand Down Expand Up @@ -529,6 +533,23 @@ public class WrappingRule :
}
}

private fun insertNewLineBeforeSemi(
node: ASTNode,
autoCorrect: Boolean,
emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> Unit,
) {
val previousCodeLeaf = node.prevCodeLeaf()?.lastChildLeafOrSelf() ?: return
val nextCodeLeaf = node.nextCodeLeaf()?.firstChildLeafOrSelf() ?: return
if (previousCodeLeaf.treeParent.elementType == ENUM_ENTRY && nextCodeLeaf.elementType == RBRACE) {
// Allow
// enum class INDEX2 { ONE, TWO, THREE; }
paul-dingemans marked this conversation as resolved.
Show resolved Hide resolved
return
}
if (noNewLineInClosedRange(previousCodeLeaf, nextCodeLeaf)) {
requireNewlineAfterLeaf(node, autoCorrect, emit, indent = previousCodeLeaf.indent())
paul-dingemans marked this conversation as resolved.
Show resolved Hide resolved
}
}

private fun requireNewlineBeforeLeaf(
node: ASTNode,
autoCorrect: Boolean,
Expand Down
Loading