Skip to content

Commit ff2f35b

Browse files
Allow value arguments with a multiline expression to be indented on a separate line
Closes #1217
1 parent fcf24b9 commit ff2f35b

File tree

4 files changed

+54
-2
lines changed

4 files changed

+54
-2
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,9 @@ Previously the default value for `.editorconfig` property `max_line_length` was
258258
* Enforce spacing around rangeUntil operator `..<` similar to the range operator `..` in `range-spacing` ([#1858](https://github.com/pinterest/ktlint/issues/1858))
259259
* When `.editorconfig` property `ij_kotlin_imports_layout` contains a `|` but no import exists that match any pattern before the first `|` then do not report a violation nor insert a blank line `import-ordering` ([#1845](https://github.com/pinterest/ktlint/issues/1845))
260260
* When negate-patterns only are specified in Ktlint CLI then automatically add the default include patterns (`**/*.kt` and `**/*.kts`) so that all Kotlin files excluding the files matching the negate-patterns will be processed ([#1847](https://github.com/pinterest/ktlint/issues/1847))
261+
* Do not remove newlines from multiline type parameter lists `type-parameter-list-spacing` ([#1867](https://github.com/pinterest/ktlint/issues/1867))
262+
* Wrap each type parameter in a multiline type parameter list `wrapping` ([#1867](https://github.com/pinterest/ktlint/issues/1867))
263+
* Allow value arguments with a multiline expression to be indented on a separate line `indent` ([#1217](https://github.com/pinterest/ktlint/issues/1217))
261264

262265
### Changed
263266
* Wrap the parameters of a function literal containing a multiline parameter list (only in `ktlint_official` code style) `parameter-list-wrapping` ([#1681](https://github.com/pinterest/ktlint/issues/1681)).

ktlint-cli/src/main/kotlin/com/pinterest/ktlint/cli/internal/KtlintCommandLine.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ private lateinit var logger: KLogger
6363

6464
@Command(
6565
headerHeading =
66-
"""
66+
"""
6767
An anti-bikeshedding Kotlin linter with built-in formatter.
6868
(https://github.com/pinterest/ktlint).
6969

ktlint-ruleset-standard/src/main/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRule.kt

+20
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_CONSTRAINT_LIS
7171
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_PARAMETER_LIST
7272
import com.pinterest.ktlint.rule.engine.core.api.ElementType.TYPE_REFERENCE
7373
import com.pinterest.ktlint.rule.engine.core.api.ElementType.USER_TYPE
74+
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT
7475
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_ARGUMENT_LIST
7576
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER
7677
import com.pinterest.ktlint.rule.engine.core.api.ElementType.VALUE_PARAMETER_LIST
@@ -204,6 +205,9 @@ public class IndentationRule :
204205
lastChildIndent = "",
205206
)
206207

208+
node.elementType == VALUE_ARGUMENT ->
209+
visitValueArgument(node)
210+
207211
node.elementType == SECONDARY_CONSTRUCTOR ->
208212
visitSecondaryConstructor(node)
209213

@@ -327,6 +331,22 @@ public class IndentationRule :
327331
}
328332
}
329333

334+
private fun visitValueArgument(node: ASTNode) {
335+
if (codeStyle == ktlint_official) {
336+
// Deviate from standard IntelliJ IDEA formatting to allow formatting below:
337+
// val foo = foo(
338+
// parameterName =
339+
// "The quick brown fox "
340+
// .plus("jumps ")
341+
// .plus("over the lazy dog"),
342+
// )
343+
startIndentContext(
344+
fromAstNode = node,
345+
lastChildIndent = "",
346+
)
347+
}
348+
}
349+
330350
private fun visitSecondaryConstructor(node: ASTNode) {
331351
node
332352
.findChildByType(CONSTRUCTOR_DELEGATION_CALL)

ktlint-ruleset-standard/src/test/kotlin/com/pinterest/ktlint/ruleset/standard/rules/IndentationRuleTest.kt

+30-1
Original file line numberDiff line numberDiff line change
@@ -4850,7 +4850,7 @@ internal class IndentationRuleTest {
48504850
}
48514851

48524852
@Test
4853-
fun `Given ktlint_official code style then indent the type when it does not fit on the same line as the variable name`() {
4853+
fun `Given a type which does not fit on the same line as the variable name`() {
48544854
val code =
48554855
"""
48564856
// $MAX_LINE_LENGTH_MARKER $EOL_CHAR
@@ -4874,6 +4874,35 @@ internal class IndentationRuleTest {
48744874
.setMaxLineLength()
48754875
.isFormattedAs(formattedCode)
48764876
}
4877+
4878+
@Test
4879+
fun `Issue 1217 - Given a function parameter with a multiline expression starting on a new line`() {
4880+
val code =
4881+
"""
4882+
val foo = foo(
4883+
parameterName =
4884+
"The quick brown fox "
4885+
.plus("jumps ")
4886+
.plus("over the lazy dog"),
4887+
)
4888+
""".trimIndent()
4889+
val formattedCode =
4890+
"""
4891+
val foo = foo(
4892+
parameterName =
4893+
"The quick brown fox "
4894+
.plus("jumps ")
4895+
.plus("over the lazy dog"),
4896+
)
4897+
""".trimIndent()
4898+
indentationRuleAssertThat(code)
4899+
.withEditorConfigOverride(CODE_STYLE_PROPERTY to ktlint_official)
4900+
.hasLintViolations(
4901+
LintViolation(3, 1, "Unexpected indentation (4) (should be 8)"),
4902+
LintViolation(4, 1, "Unexpected indentation (8) (should be 12)"),
4903+
LintViolation(5, 1, "Unexpected indentation (8) (should be 12)"),
4904+
).isFormattedAs(formattedCode)
4905+
}
48774906
}
48784907

48794908
@Nested

0 commit comments

Comments
 (0)