|
| 1 | +package com.pinterest.ktlint.ruleset.standard.rules |
| 2 | + |
| 3 | +import com.pinterest.ktlint.rule.engine.core.api.AutocorrectDecision |
| 4 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.CONTEXT_RECEIVER |
| 5 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.CONTEXT_RECEIVER_LIST |
| 6 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUN |
| 7 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.FUNCTION_TYPE |
| 8 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.GT |
| 9 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.RPAR |
| 10 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_ARGUMENT_LIST |
| 11 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PROJECTION |
| 12 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_REFERENCE |
| 13 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER |
| 14 | +import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST |
| 15 | +import com.pinterest.ktlint.rule.engine.core.api.IndentConfig |
| 16 | +import com.pinterest.ktlint.rule.engine.core.api.IndentConfig.Companion.DEFAULT_INDENT_CONFIG |
| 17 | +import com.pinterest.ktlint.rule.engine.core.api.RuleId |
| 18 | +import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint |
| 19 | +import com.pinterest.ktlint.rule.engine.core.api.SinceKtlint.Status.STABLE |
| 20 | +import com.pinterest.ktlint.rule.engine.core.api.children20 |
| 21 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.EditorConfig |
| 22 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_SIZE_PROPERTY |
| 23 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.INDENT_STYLE_PROPERTY |
| 24 | +import com.pinterest.ktlint.rule.engine.core.api.editorconfig.MAX_LINE_LENGTH_PROPERTY |
| 25 | +import com.pinterest.ktlint.rule.engine.core.api.firstChildLeafOrSelf20 |
| 26 | +import com.pinterest.ktlint.rule.engine.core.api.ifAutocorrectAllowed |
| 27 | +import com.pinterest.ktlint.rule.engine.core.api.indent20 |
| 28 | +import com.pinterest.ktlint.rule.engine.core.api.indentWithoutNewlinePrefix |
| 29 | +import com.pinterest.ktlint.rule.engine.core.api.isPartOf |
| 30 | +import com.pinterest.ktlint.rule.engine.core.api.isPartOfComment20 |
| 31 | +import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithNewline20 |
| 32 | +import com.pinterest.ktlint.rule.engine.core.api.isWhiteSpaceWithoutNewline20 |
| 33 | +import com.pinterest.ktlint.rule.engine.core.api.lastChildLeafOrSelf20 |
| 34 | +import com.pinterest.ktlint.rule.engine.core.api.nextLeaf |
| 35 | +import com.pinterest.ktlint.rule.engine.core.api.parent |
| 36 | +import com.pinterest.ktlint.rule.engine.core.api.prevLeaf |
| 37 | +import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceAfterMe |
| 38 | +import com.pinterest.ktlint.rule.engine.core.api.upsertWhitespaceBeforeMe |
| 39 | +import com.pinterest.ktlint.ruleset.standard.StandardRule |
| 40 | +import org.jetbrains.kotlin.com.intellij.lang.ASTNode |
| 41 | + |
| 42 | +/** |
| 43 | + * Wrapping of context receiver list to a separate line. |
| 44 | + * |
| 45 | + * IMPORTANT: This rule only affects a context receiver list that does not contain a context receiver. Context receivers are deprecated |
| 46 | + * since Kotlin 2.2.0, and are wrapped by the 'context-receiver-wrapping' rule. |
| 47 | + */ |
| 48 | +@SinceKtlint("1.7", STABLE) |
| 49 | +public class ContextReceiverListWrappingRule : |
| 50 | + StandardRule( |
| 51 | + id = "context-receiver-list-wrapping", |
| 52 | + usesEditorConfigProperties = |
| 53 | + setOf( |
| 54 | + INDENT_SIZE_PROPERTY, |
| 55 | + INDENT_STYLE_PROPERTY, |
| 56 | + MAX_LINE_LENGTH_PROPERTY, |
| 57 | + ), |
| 58 | + ) { |
| 59 | + private var indentConfig = DEFAULT_INDENT_CONFIG |
| 60 | + private var maxLineLength = MAX_LINE_LENGTH_PROPERTY.defaultValue |
| 61 | + |
| 62 | + override fun beforeFirstNode(editorConfig: EditorConfig) { |
| 63 | + indentConfig = |
| 64 | + IndentConfig( |
| 65 | + indentStyle = editorConfig[INDENT_STYLE_PROPERTY], |
| 66 | + tabWidth = editorConfig[INDENT_SIZE_PROPERTY], |
| 67 | + ) |
| 68 | + maxLineLength = editorConfig.maxLineLength() |
| 69 | + } |
| 70 | + |
| 71 | + override fun beforeVisitChildNodes( |
| 72 | + node: ASTNode, |
| 73 | + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision, |
| 74 | + ) { |
| 75 | + when { |
| 76 | + node.elementType == CONTEXT_RECEIVER_LIST && node.isContextParameter() -> { |
| 77 | + visitContextReceiverList(node, emit) |
| 78 | + } |
| 79 | + |
| 80 | + node.elementType == TYPE_ARGUMENT_LIST && node.isContextParameter() -> { |
| 81 | + visitContextReceiverTypeArgumentList(node, emit) |
| 82 | + } |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + private fun ASTNode.isContextParameter(): Boolean = isPartOf(CONTEXT_RECEIVER_LIST) && findChildByType(CONTEXT_RECEIVER) == null |
| 87 | + |
| 88 | + private fun visitContextReceiverList( |
| 89 | + node: ASTNode, |
| 90 | + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision, |
| 91 | + ) { |
| 92 | + // Context receiver must be followed by new line or comment unless it is a type reference of a parameter |
| 93 | + node |
| 94 | + .takeUnless { it.isTypeReferenceParameterInFunction() } |
| 95 | + ?.lastChildLeafOrSelf20 |
| 96 | + ?.nextLeaf { !it.isWhiteSpaceWithoutNewline20 && !it.isPartOfComment20 } |
| 97 | + ?.takeIf { !it.isWhiteSpaceWithNewline20 } |
| 98 | + ?.let { nodeAfterContextReceiver -> |
| 99 | + emit(nodeAfterContextReceiver.startOffset, "Expected a newline after the context parameter", true) |
| 100 | + .ifAutocorrectAllowed { |
| 101 | + nodeAfterContextReceiver |
| 102 | + .firstChildLeafOrSelf20 |
| 103 | + .upsertWhitespaceBeforeMe(indentConfig.parentIndentOf(node)) |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Check line length assuming that the context receiver is indented correctly. Wrapping rule must however run before indenting. |
| 108 | + if (!node.textContains('\n') && |
| 109 | + node.indentWithoutNewlinePrefix.length + node.textLength > maxLineLength |
| 110 | + ) { |
| 111 | + node |
| 112 | + .children20 |
| 113 | + .filter { it.elementType == VALUE_PARAMETER } |
| 114 | + .forEach { |
| 115 | + emit( |
| 116 | + it.startOffset, |
| 117 | + "Newline expected before context parameter as max line length is violated", |
| 118 | + true, |
| 119 | + ).ifAutocorrectAllowed { |
| 120 | + it |
| 121 | + .prevLeaf |
| 122 | + ?.upsertWhitespaceAfterMe(indentConfig.childIndentOf(node)) |
| 123 | + } |
| 124 | + } |
| 125 | + node |
| 126 | + .findChildByType(RPAR) |
| 127 | + ?.let { rpar -> |
| 128 | + emit( |
| 129 | + rpar.startOffset, |
| 130 | + "Newline expected before closing parenthesis as max line length is violated", |
| 131 | + true, |
| 132 | + ).ifAutocorrectAllowed { |
| 133 | + rpar.upsertWhitespaceBeforeMe(node.indent20) |
| 134 | + } |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private fun ASTNode.isTypeReferenceParameterInFunction() = |
| 140 | + takeIf { it.elementType == CONTEXT_RECEIVER_LIST } |
| 141 | + ?.parent |
| 142 | + ?.takeIf { it.elementType == FUNCTION_TYPE } |
| 143 | + ?.parent |
| 144 | + ?.takeIf { it.elementType == TYPE_REFERENCE } |
| 145 | + ?.parent |
| 146 | + ?.takeIf { it.elementType == VALUE_PARAMETER } |
| 147 | + ?.parent |
| 148 | + ?.takeIf { it.elementType == VALUE_PARAMETER_LIST } |
| 149 | + ?.parent |
| 150 | + ?.let { it.elementType == FUN } |
| 151 | + ?: false |
| 152 | + |
| 153 | + private fun visitContextReceiverTypeArgumentList( |
| 154 | + node: ASTNode, |
| 155 | + emit: (offset: Int, errorMessage: String, canBeAutoCorrected: Boolean) -> AutocorrectDecision, |
| 156 | + ) { |
| 157 | + val contextReceiverText = node.parent?.text.orEmpty() |
| 158 | + // Check line length assuming that the context receiver is indented correctly. Wrapping rule must however run |
| 159 | + // before indenting. |
| 160 | + if (!contextReceiverText.contains('\n') && |
| 161 | + node.indentWithoutNewlinePrefix.length + contextReceiverText.length > maxLineLength |
| 162 | + ) { |
| 163 | + node |
| 164 | + .children20 |
| 165 | + .filter { it.elementType == TYPE_PROJECTION } |
| 166 | + .forEach { |
| 167 | + emit( |
| 168 | + it.startOffset, |
| 169 | + "Newline expected before context parameter type projection as max line length is violated", |
| 170 | + true, |
| 171 | + ).ifAutocorrectAllowed { |
| 172 | + it.upsertWhitespaceBeforeMe(indentConfig.childIndentOf(node)) |
| 173 | + } |
| 174 | + } |
| 175 | + node |
| 176 | + .findChildByType(GT) |
| 177 | + ?.let { gt -> |
| 178 | + emit( |
| 179 | + gt.startOffset, |
| 180 | + "Newline expected before closing angle bracket as max line length is violated", |
| 181 | + true, |
| 182 | + ).ifAutocorrectAllowed { |
| 183 | + // Ideally, the closing angle bracket should be de-indented to make it consistent with |
| 184 | + // de-indentation of closing ")", "}" and "]". This however would be inconsistent with how the |
| 185 | + // type argument lists are formatted by IntelliJ IDEA default formatter. |
| 186 | + gt.upsertWhitespaceBeforeMe(indentConfig.childIndentOf(node)) |
| 187 | + } |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +public val CONTEXT_RECEIVER_LIST_WRAPPING_RULE_ID: RuleId = ContextReceiverListWrappingRule().ruleId |
0 commit comments