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

Move wrapping on semicolon from wrapping rule to statement-wrapping rule #2222

Merged
merged 3 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -115,6 +115,7 @@ If an `EditorConfigProperty` is defined in a `Rule` that is only provided via a
* Update dependency `io.github.oshai:kotlin-logging-jvm` to `v5.1.0` ([#2174](https://github.com/pinterest/ktlint/pull/2174))
* Update kotlin monorepo to v1.9.10 [#2197](https://github.com/pinterest/ktlint/issues/2197)
* Update dependency info.picocli:picocli to v4.7.5 [#2215](https://github.com/pinterest/ktlint/pull/2215)
* Move wrapping on semicolon from `wrapping` rule to `statement-wrapping` rule [#2220](https://github.com/pinterest/ktlint/issue/2220)
* Update dependency org.jetbrains.dokka:dokka-gradle-plugin to v1.9.0 [#2221](https://github.com/pinterest/ktlint/pull/2221)

## [0.50.0] - 2023-06-29
Expand Down
6 changes: 5 additions & 1 deletion ktlint-ruleset-standard/api/ktlint-ruleset-standard.api
Original file line number Diff line number Diff line change
Expand Up @@ -764,12 +764,16 @@ public final class com/pinterest/ktlint/ruleset/standard/rules/SpacingBetweenFun
public static final fun getSPACING_BETWEEN_FUNCTION_NAME_AND_OPENING_PARENTHESIS_RULE_ID ()Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;
}

public final class com/pinterest/ktlint/ruleset/standard/rules/StatementWrappingRule : com/pinterest/ktlint/ruleset/standard/StandardRule, com/pinterest/ktlint/rule/engine/core/api/Rule$Experimental {
public final class com/pinterest/ktlint/ruleset/standard/rules/StatementWrappingRule : com/pinterest/ktlint/ruleset/standard/StandardRule {
public fun <init> ()V
public fun beforeFirstNode (Lcom/pinterest/ktlint/rule/engine/core/api/editorconfig/EditorConfig;)V
public fun beforeVisitChildNodes (Lorg/jetbrains/kotlin/com/intellij/lang/ASTNode;ZLkotlin/jvm/functions/Function3;)V
}

public final class com/pinterest/ktlint/ruleset/standard/rules/StatementWrappingRuleKt {
public static final fun getSTATEMENT_WRAPPING_RULE_ID ()Lcom/pinterest/ktlint/rule/engine/core/api/RuleId;
}

public final class com/pinterest/ktlint/ruleset/standard/rules/StringTemplateIndentRule : com/pinterest/ktlint/ruleset/standard/StandardRule, com/pinterest/ktlint/rule/engine/core/api/Rule$OfficialCodeStyle {
public static final field RAW_STRING_LITERAL_QUOTES Ljava/lang/String;
public fun <init> ()V
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package com.pinterest.ktlint.ruleset.standard.rules

import com.pinterest.ktlint.rule.engine.core.api.ElementType
import com.pinterest.ktlint.rule.engine.core.api.ElementType.ARROW
import com.pinterest.ktlint.rule.engine.core.api.ElementType.BLOCK
import com.pinterest.ktlint.rule.engine.core.api.ElementType.CLASS_BODY
import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_LITERAL
import com.pinterest.ktlint.rule.engine.core.api.ElementType.LBRACE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.RBRACE
import com.pinterest.ktlint.rule.engine.core.api.ElementType.SEMICOLON
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST
import com.pinterest.ktlint.rule.engine.core.api.ElementType.WHEN
import com.pinterest.ktlint.rule.engine.core.api.IndentConfig
import com.pinterest.ktlint.rule.engine.core.api.Rule
import com.pinterest.ktlint.rule.engine.core.api.RuleId
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.EXPERIMENTAL
import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE
import com.pinterest.ktlint.rule.engine.core.api.children
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY
import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.indent
import com.pinterest.ktlint.rule.engine.core.api.lastChildLeafOrSelf
import com.pinterest.ktlint.rule.engine.core.api.nextCodeLeaf
import com.pinterest.ktlint.rule.engine.core.api.noNewLineInClosedRange
import com.pinterest.ktlint.rule.engine.core.api.prevCodeLeaf
Expand All @@ -28,7 +33,8 @@ import org.jetbrains.kotlin.com.intellij.psi.PsiElement
import org.jetbrains.kotlin.psi.KtClass
import org.jetbrains.kotlin.utils.addToStdlib.applyIf

@SinceKtlint("1.0", EXPERIMENTAL)
@SinceKtlint("0.50", EXPERIMENTAL)
@SinceKtlint("1.0", STABLE)
public class StatementWrappingRule :
StandardRule(
"statement-wrapping",
Expand All @@ -37,8 +43,7 @@ public class StatementWrappingRule :
INDENT_SIZE_PROPERTY,
INDENT_STYLE_PROPERTY,
),
),
Rule.Experimental {
) {
private var indentConfig = IndentConfig.DEFAULT_INDENT_CONFIG

override fun beforeFirstNode(editorConfig: EditorConfig) {
Expand All @@ -65,6 +70,9 @@ public class StatementWrappingRule :

CLASS_BODY, WHEN ->
visitBlock(node, emit, autoCorrect)

SEMICOLON ->
visitSemiColon(node, autoCorrect, emit)
}
}

Expand Down Expand Up @@ -98,7 +106,7 @@ public class StatementWrappingRule :
}?.let { lbraceOrArrow ->
val nextCodeLeaf = lbraceOrArrow.nextCodeLeaf()
if (nextCodeLeaf != null && noNewLineInClosedRange(lbraceOrArrow, nextCodeLeaf)) {
emit(nextCodeLeaf.startOffset, "Expected new line after '${lbraceOrArrow.text}'", true)
emit(nextCodeLeaf.startOffset, "Missing newline after '${lbraceOrArrow.text}'", true)
if (autoCorrect) {
if (node.elementType == WHEN) {
lbraceOrArrow.upsertWhitespaceAfterMe(lbraceOrArrow.indentAsChild)
Expand All @@ -113,7 +121,7 @@ public class StatementWrappingRule :
?.let { rbrace ->
val prevCodeLeaf = rbrace.prevCodeLeaf()
if (prevCodeLeaf != null && noNewLineInClosedRange(prevCodeLeaf, rbrace)) {
emit(rbrace.startOffset, "Expected new line before '}'", true)
emit(rbrace.startOffset, "Missing newline before '}'", true)
if (autoCorrect) {
rbrace.upsertWhitespaceBeforeMe(rbrace.indentAsParent)
}
Expand Down Expand Up @@ -161,4 +169,26 @@ public class StatementWrappingRule :

private inline val ASTNode.indentAsParent: String
get() = treeParent.indent()

private fun visitSemiColon(
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 == ElementType.ENUM_ENTRY && nextCodeLeaf.elementType == RBRACE) {
// Allow
// enum class INDEX2 { ONE, TWO, THREE; }
return
}
if (noNewLineInClosedRange(previousCodeLeaf, nextCodeLeaf)) {
emit(node.startOffset + 1, """Missing newline after '${node.text}'""", true)
if (autoCorrect) {
node.upsertWhitespaceAfterMe(previousCodeLeaf.indent())
}
}
}
}

public val STATEMENT_WRAPPING_RULE_ID: RuleId = StatementWrappingRule().ruleId
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ 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 @@ -27,7 +26,6 @@ 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 @@ -69,7 +67,6 @@ 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 @@ -143,7 +140,6 @@ 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 @@ -542,34 +538,6 @@ 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, previousCodeLeaf.indent())
node
.treeParent
.takeIf { it.elementType == BLOCK }
?.let { block ->
beforeVisitBlock(block, autoCorrect, emit)
block
.treeParent
.takeIf { it.elementType == FUNCTION_LITERAL }
?.findChildByType(ARROW)
?.let { arrow -> rearrangeArrow(arrow, autoCorrect, emit) }
}
}
}

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