Skip to content

Commit

Permalink
Use default indent in parameter lists
Browse files Browse the repository at this point in the history
  • Loading branch information
Egor Andreevici committed Dec 23, 2017
1 parent 1f6b292 commit 09a1c92
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 52 deletions.
14 changes: 11 additions & 3 deletions src/main/java/com/squareup/kotlinpoet/CodeWriter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ package com.squareup.kotlinpoet
/** Sentinel value that indicates that no user-provided package has been set. */
private val NO_PACKAGE = String()

private const val DEFAULT_INDENT_LEVEL = 1
private const val CONTINUATION_INDENT_LEVEL = 2

private fun extractMemberName(part: String): String {
require(Character.isJavaIdentifierStart(part[0])) { "not an identifier: $part" }
for (i in 1..part.length) {
Expand All @@ -40,6 +43,7 @@ internal class CodeWriter constructor(
) {
private val out = LineWrapper(out, indent, 100)
private var indentLevel = 0
private var indentLevelIncrement = CONTINUATION_INDENT_LEVEL

private var kdoc = false
private var comment = false
Expand Down Expand Up @@ -249,7 +253,7 @@ internal class CodeWriter constructor(
statementLine = -1
}

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

else -> {
// Handle deferred type.
Expand All @@ -275,14 +279,18 @@ internal class CodeWriter constructor(
}
}

fun openWrappingGroup() = out.openWrappingGroup()
fun openWrappingGroup() {
out.openWrappingGroup()
indentLevelIncrement = DEFAULT_INDENT_LEVEL
}

fun closeWrappingGroup() {
trailingNewline = out.closeWrappingGroup()
indentLevelIncrement = CONTINUATION_INDENT_LEVEL
}

fun emitWrappingSpace() = apply {
out.wrappingSpace(indentLevel + 2)
out.wrappingSpace(indentLevel + indentLevelIncrement)
}

private fun emitStaticImportMember(canonical: String, part: String): Boolean {
Expand Down
18 changes: 9 additions & 9 deletions src/test/java/com/squareup/kotlinpoet/KotlinPoetTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -587,13 +587,13 @@ class KotlinPoetTest {
.build()
assertThat(source.toString()).isEqualTo("""
|fun sum(
| a: kotlin.Int,
| b: kotlin.Int,
| c: kotlin.Int,
| d: kotlin.Int,
| e: kotlin.Int,
| f: kotlin.Int,
| g: kotlin.Int
| a: kotlin.Int,
| b: kotlin.Int,
| c: kotlin.Int,
| d: kotlin.Int,
| e: kotlin.Int,
| f: kotlin.Int,
| g: kotlin.Int
|) = a + b + c
|""".trimMargin())
}
Expand All @@ -614,8 +614,8 @@ class KotlinPoetTest {
.build()
assertThat(source.toString()).isEqualTo("""
|fun veryLongFunctionName(
| veryLongParameterName: (java.io.Serializable, java.lang.Appendable, kotlin.Cloneable) -> kotlin.Unit,
| i: kotlin.Int
| veryLongParameterName: (java.io.Serializable, java.lang.Appendable, kotlin.Cloneable) -> kotlin.Unit,
| i: kotlin.Int
|) = kotlin.Unit
|""".trimMargin())
}
Expand Down
80 changes: 40 additions & 40 deletions src/test/java/com/squareup/kotlinpoet/TypeSpecTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -185,11 +185,11 @@ class TypeSpecTest {
|
|class Foo {
| constructor(
| id: Long,
| @Ping one: String,
| @Ping two: String,
| @Pong("pong") three: String,
| @Ping four: String
| id: Long,
| @Ping one: String,
| @Ping two: String,
| @Pong("pong") three: String,
| @Ping four: String
| ) {
| /* code snippets */
| }
Expand Down Expand Up @@ -272,9 +272,9 @@ class TypeSpecTest {
| )
| @POST("/foo/bar")
| fun fooBar(
| @Body things: Things<Thing>,
| @QueryMap(encodeValues = false) query: Map<String, String>,
| @Header("Authorization") authorization: String
| @Body things: Things<Thing>,
| @QueryMap(encodeValues = false) query: Map<String, String>,
| @Header("Authorization") authorization: String
| ): Observable<FooBar>
|}
|""".trimMargin())
Expand Down Expand Up @@ -2056,38 +2056,38 @@ class TypeSpecTest {
|
|class Taco {
| fun call(
| s0: String,
| s1: String,
| s2: String,
| s3: String,
| s4: String,
| s5: String,
| s6: String,
| s7: String,
| s8: String,
| s9: String,
| s10: String,
| s11: String,
| s12: String,
| s13: String,
| s14: String,
| s15: String,
| s16: String,
| s17: String,
| s18: String,
| s19: String,
| s20: String,
| s21: String,
| s22: String,
| s23: String,
| s24: String,
| s25: String,
| s26: String,
| s27: String,
| s28: String,
| s29: String,
| s30: String,
| s31: String
| s0: String,
| s1: String,
| s2: String,
| s3: String,
| s4: String,
| s5: String,
| s6: String,
| s7: String,
| s8: String,
| s9: String,
| s10: String,
| s11: String,
| s12: String,
| s13: String,
| s14: String,
| s15: String,
| s16: String,
| s17: String,
| s18: String,
| s19: String,
| s20: String,
| s21: String,
| s22: String,
| s23: String,
| s24: String,
| s25: String,
| s26: String,
| s27: String,
| s28: String,
| s29: String,
| s30: String,
| s31: String
| ) {
| call("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15",
| "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29",
Expand Down

0 comments on commit 09a1c92

Please sign in to comment.