Skip to content

Commit

Permalink
Wrap multiple statements on the same line (#2065)
Browse files Browse the repository at this point in the history
Closes #1078
  • Loading branch information
atulgpt authored Jun 5, 2023
1 parent 8c00f6c commit ff940d3
Show file tree
Hide file tree
Showing 6 changed files with 692 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,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 expressions on same line separated by semicolon ([#1078](https://github.com/pinterest/ktlint/issues/1078))

### 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")
}

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 -> insertNewLineAfterSemi(node, autoCorrect, emit)
}
}

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

private fun insertNewLineAfterSemi(
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; }
return
}
if (noNewLineInClosedRange(previousCodeLeaf, nextCodeLeaf)) {
requireNewlineAfterLeaf(node, autoCorrect, emit, indent = previousCodeLeaf.indent())
}
}

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

0 comments on commit ff940d3

Please sign in to comment.