Skip to content

Add joinToCode with transform lambda #1874

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

Merged
merged 1 commit into from
Mar 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Change Log

* New: Supertype list wraps to one-per-line if the primary constructor spans multiple lines (#1866).
* New: Extract `MemberSpecHolder` interface for constructs that can hold `PropertySpec`s and `FunSpec`s and their builders (#1877).
* New: `joinToCode` variant which operates on any type, but requires a transform lambda to convert each element into a `CodeBlock`.
* Fix: Prevent name clashes between a function in class and a function call in current scope (#1850).
* Fix: Fix extension function imports (#1814).
* Fix: Omit implicit modifiers on FileSpec.scriptBuilder (#1813).
Expand Down
2 changes: 2 additions & 0 deletions kotlinpoet/api/kotlinpoet.api
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ public final class com/squareup/kotlinpoet/CodeBlocks {
public static final fun joinToCode (Ljava/util/Collection;Ljava/lang/CharSequence;)Lcom/squareup/kotlinpoet/CodeBlock;
public static final fun joinToCode (Ljava/util/Collection;Ljava/lang/CharSequence;Ljava/lang/CharSequence;)Lcom/squareup/kotlinpoet/CodeBlock;
public static final fun joinToCode (Ljava/util/Collection;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;)Lcom/squareup/kotlinpoet/CodeBlock;
public static final fun joinToCode (Ljava/util/Collection;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;)Lcom/squareup/kotlinpoet/CodeBlock;
public static synthetic fun joinToCode$default (Ljava/util/Collection;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;ILjava/lang/Object;)Lcom/squareup/kotlinpoet/CodeBlock;
public static synthetic fun joinToCode$default (Ljava/util/Collection;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Ljava/lang/CharSequence;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lcom/squareup/kotlinpoet/CodeBlock;
public static final fun withIndent (Lcom/squareup/kotlinpoet/CodeBlock$Builder;Lkotlin/jvm/functions/Function1;)Lcom/squareup/kotlinpoet/CodeBlock$Builder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -494,6 +494,10 @@ public class CodeBlock private constructor(
}
}

/**
* Join each [CodeBlock] in [this] into a single [CodeBlock] using [separator] with an optional
* [prefix] and [suffix].
*/
@JvmOverloads
public fun Collection<CodeBlock>.joinToCode(
separator: CharSequence = ", ",
Expand All @@ -505,6 +509,19 @@ public fun Collection<CodeBlock>.joinToCode(
return CodeBlock.of(placeholders.joinToString(separator, prefix, suffix), *blocks)
}

/**
* Apply [transform] to each element in [this], then join into a single [CodeBlock] using
* [separator] with an optional [prefix] and [suffix].
*/
public fun <T> Collection<T>.joinToCode(
separator: CharSequence = ", ",
prefix: CharSequence = "",
suffix: CharSequence = "",
transform: (T) -> CodeBlock,
): CodeBlock {
return map(transform).joinToCode(separator, prefix, suffix)
}

/**
* Builds new [CodeBlock] by populating newly created [CodeBlock.Builder] using provided
* [builderAction] and then converting it to [CodeBlock].
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,12 @@ class CodeBlockTest {
.isEqualTo(CodeBlock.of("(%L, %L, %L)", "taco1", "taco2", "taco3"))
}

@Test fun joinToCodeTransform() {
val blocks = listOf("taco1", "taco2", "taco3")
assertThat(blocks.joinToCode { CodeBlock.of("%S", it) })
.isEqualTo(CodeBlock.of("%S, %S, %S", "taco1", "taco2", "taco3"))
}

@Test fun beginControlFlowWithParams() {
val controlFlow = CodeBlock.builder()
.beginControlFlow("list.forEach { element ->")
Expand Down