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

Fix a bug caused by too long return expressions #1256

Merged
merged 12 commits into from Jun 1, 2022
Expand Up @@ -270,7 +270,11 @@ public class CodeBlock private constructor(
if (format[p] != '%') {
var nextP = format.nextPotentialPlaceholderPosition(startIndex = p + 1)
if (nextP == -1) nextP = format.length
formatParts += format.substring(p, nextP)
var formatPart = format.substring(p, nextP)
zsqw123 marked this conversation as resolved.
Show resolved Hide resolved
if (formatPart.startsWith(RETURN_WITH_SPACE)) {
formatPart = formatPart.replaceRange(RETURN_WITH_SPACE.indices, RETURN_WITH_NBSP)
}
formatParts += formatPart
p = nextP
continue
}
Expand Down Expand Up @@ -458,6 +462,8 @@ public class CodeBlock private constructor(
private const val TYPE_NAME = 2
private val NO_ARG_PLACEHOLDERS = setOf("⇥", "⇤", "«", "»")
internal val EMPTY = CodeBlock(emptyList(), emptyList())
internal const val RETURN_WITH_SPACE = "return "
zsqw123 marked this conversation as resolved.
Show resolved Hide resolved
internal const val RETURN_WITH_NBSP = "return·"

@JvmStatic public fun of(format: String, vararg args: Any?): CodeBlock =
Builder().add(format, *args).build()
Expand Down
4 changes: 2 additions & 2 deletions kotlinpoet/src/main/java/com/squareup/kotlinpoet/FunSpec.kt
Expand Up @@ -558,8 +558,8 @@ public class FunSpec private constructor(
internal val String.isConstructor get() = this == CONSTRUCTOR
internal val String.isAccessor get() = this.isOneOf(GETTER, SETTER)

private val RETURN_EXPRESSION_BODY_PREFIX_SPACE = CodeBlock.of("return ")
private val RETURN_EXPRESSION_BODY_PREFIX_NBSP = CodeBlock.of("return·")
private val RETURN_EXPRESSION_BODY_PREFIX_SPACE = CodeBlock.of(CodeBlock.RETURN_WITH_SPACE)
private val RETURN_EXPRESSION_BODY_PREFIX_NBSP = CodeBlock.of(CodeBlock.RETURN_WITH_NBSP)
private val THROW_EXPRESSION_BODY_PREFIX_SPACE = CodeBlock.of("throw ")
private val THROW_EXPRESSION_BODY_PREFIX_NBSP = CodeBlock.of("throw·")

Expand Down
25 changes: 25 additions & 0 deletions kotlinpoet/src/test/java/com/squareup/kotlinpoet/FunSpecTest.kt
Expand Up @@ -206,6 +206,31 @@ class FunSpecTest {
)
}

@Test fun returnLongExpression() {
val funSpec = FunSpec.builder("foo")
.returns(String::class)
.addStatement("val placeholder = 1")
.addStatement("return \"Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong\"")
zsqw123 marked this conversation as resolved.
Show resolved Hide resolved
.build()
val sb = StringBuilder()
CodeWriter(sb).use {
funSpec.emit(
codeWriter = it,
enclosingName = null,
implicitModifiers = setOf(KModifier.PUBLIC),
includeKdocTags = false
)
}
assertThat(sb.toString()).isEqualTo(
"""
|public fun foo(): kotlin.String {
| val placeholder = 1
| return "Loooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooong"
|}
|""".trimMargin()
)
}

@Test fun functionParamWithKdoc() {
val funSpec = FunSpec.builder("foo")
.addParameter(
Expand Down