Skip to content

Commit 09a1c92

Browse files
author
Egor Andreevici
committed
Use default indent in parameter lists
1 parent 1f6b292 commit 09a1c92

3 files changed

Lines changed: 60 additions & 52 deletions

File tree

src/main/java/com/squareup/kotlinpoet/CodeWriter.kt

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ package com.squareup.kotlinpoet
1818
/** Sentinel value that indicates that no user-provided package has been set. */
1919
private val NO_PACKAGE = String()
2020

21+
private const val DEFAULT_INDENT_LEVEL = 1
22+
private const val CONTINUATION_INDENT_LEVEL = 2
23+
2124
private fun extractMemberName(part: String): String {
2225
require(Character.isJavaIdentifierStart(part[0])) { "not an identifier: $part" }
2326
for (i in 1..part.length) {
@@ -40,6 +43,7 @@ internal class CodeWriter constructor(
4043
) {
4144
private val out = LineWrapper(out, indent, 100)
4245
private var indentLevel = 0
46+
private var indentLevelIncrement = CONTINUATION_INDENT_LEVEL
4347

4448
private var kdoc = false
4549
private var comment = false
@@ -249,7 +253,7 @@ internal class CodeWriter constructor(
249253
statementLine = -1
250254
}
251255

252-
"%W" -> out.wrappingSpace(indentLevel + 2)
256+
"%W" -> out.wrappingSpace(indentLevel + indentLevelIncrement)
253257

254258
else -> {
255259
// Handle deferred type.
@@ -275,14 +279,18 @@ internal class CodeWriter constructor(
275279
}
276280
}
277281

278-
fun openWrappingGroup() = out.openWrappingGroup()
282+
fun openWrappingGroup() {
283+
out.openWrappingGroup()
284+
indentLevelIncrement = DEFAULT_INDENT_LEVEL
285+
}
279286

280287
fun closeWrappingGroup() {
281288
trailingNewline = out.closeWrappingGroup()
289+
indentLevelIncrement = CONTINUATION_INDENT_LEVEL
282290
}
283291

284292
fun emitWrappingSpace() = apply {
285-
out.wrappingSpace(indentLevel + 2)
293+
out.wrappingSpace(indentLevel + indentLevelIncrement)
286294
}
287295

288296
private fun emitStaticImportMember(canonical: String, part: String): Boolean {

src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -587,13 +587,13 @@ class KotlinPoetTest {
587587
.build()
588588
assertThat(source.toString()).isEqualTo("""
589589
|fun sum(
590-
| a: kotlin.Int,
591-
| b: kotlin.Int,
592-
| c: kotlin.Int,
593-
| d: kotlin.Int,
594-
| e: kotlin.Int,
595-
| f: kotlin.Int,
596-
| g: kotlin.Int
590+
| a: kotlin.Int,
591+
| b: kotlin.Int,
592+
| c: kotlin.Int,
593+
| d: kotlin.Int,
594+
| e: kotlin.Int,
595+
| f: kotlin.Int,
596+
| g: kotlin.Int
597597
|) = a + b + c
598598
|""".trimMargin())
599599
}
@@ -614,8 +614,8 @@ class KotlinPoetTest {
614614
.build()
615615
assertThat(source.toString()).isEqualTo("""
616616
|fun veryLongFunctionName(
617-
| veryLongParameterName: (java.io.Serializable, java.lang.Appendable, kotlin.Cloneable) -> kotlin.Unit,
618-
| i: kotlin.Int
617+
| veryLongParameterName: (java.io.Serializable, java.lang.Appendable, kotlin.Cloneable) -> kotlin.Unit,
618+
| i: kotlin.Int
619619
|) = kotlin.Unit
620620
|""".trimMargin())
621621
}

src/test/java/com/squareup/kotlinpoet/TypeSpecTest.kt

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -185,11 +185,11 @@ class TypeSpecTest {
185185
|
186186
|class Foo {
187187
| constructor(
188-
| id: Long,
189-
| @Ping one: String,
190-
| @Ping two: String,
191-
| @Pong("pong") three: String,
192-
| @Ping four: String
188+
| id: Long,
189+
| @Ping one: String,
190+
| @Ping two: String,
191+
| @Pong("pong") three: String,
192+
| @Ping four: String
193193
| ) {
194194
| /* code snippets */
195195
| }
@@ -272,9 +272,9 @@ class TypeSpecTest {
272272
| )
273273
| @POST("/foo/bar")
274274
| fun fooBar(
275-
| @Body things: Things<Thing>,
276-
| @QueryMap(encodeValues = false) query: Map<String, String>,
277-
| @Header("Authorization") authorization: String
275+
| @Body things: Things<Thing>,
276+
| @QueryMap(encodeValues = false) query: Map<String, String>,
277+
| @Header("Authorization") authorization: String
278278
| ): Observable<FooBar>
279279
|}
280280
|""".trimMargin())
@@ -2056,38 +2056,38 @@ class TypeSpecTest {
20562056
|
20572057
|class Taco {
20582058
| fun call(
2059-
| s0: String,
2060-
| s1: String,
2061-
| s2: String,
2062-
| s3: String,
2063-
| s4: String,
2064-
| s5: String,
2065-
| s6: String,
2066-
| s7: String,
2067-
| s8: String,
2068-
| s9: String,
2069-
| s10: String,
2070-
| s11: String,
2071-
| s12: String,
2072-
| s13: String,
2073-
| s14: String,
2074-
| s15: String,
2075-
| s16: String,
2076-
| s17: String,
2077-
| s18: String,
2078-
| s19: String,
2079-
| s20: String,
2080-
| s21: String,
2081-
| s22: String,
2082-
| s23: String,
2083-
| s24: String,
2084-
| s25: String,
2085-
| s26: String,
2086-
| s27: String,
2087-
| s28: String,
2088-
| s29: String,
2089-
| s30: String,
2090-
| s31: String
2059+
| s0: String,
2060+
| s1: String,
2061+
| s2: String,
2062+
| s3: String,
2063+
| s4: String,
2064+
| s5: String,
2065+
| s6: String,
2066+
| s7: String,
2067+
| s8: String,
2068+
| s9: String,
2069+
| s10: String,
2070+
| s11: String,
2071+
| s12: String,
2072+
| s13: String,
2073+
| s14: String,
2074+
| s15: String,
2075+
| s16: String,
2076+
| s17: String,
2077+
| s18: String,
2078+
| s19: String,
2079+
| s20: String,
2080+
| s21: String,
2081+
| s22: String,
2082+
| s23: String,
2083+
| s24: String,
2084+
| s25: String,
2085+
| s26: String,
2086+
| s27: String,
2087+
| s28: String,
2088+
| s29: String,
2089+
| s30: String,
2090+
| s31: String
20912091
| ) {
20922092
| call("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
20932093
| "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",

0 commit comments

Comments
 (0)