CodeWriter

class CodeWriter(formatConfig: FormatConfig = FormatConfig())(source)

An indentation-aware string builder for generating formatted Kotlin code.

CodeWriter tracks the current indentation level and applies it consistently when writing lines. It uses the FormatConfig to determine indent characters and final newline behavior.

Constructors

Link copied to clipboard
constructor(formatConfig: FormatConfig = FormatConfig())

Functions

Link copied to clipboard
fun dedent()

Decreases the current indentation level by one.

Link copied to clipboard
fun indent()

Increases the current indentation level by one.

Link copied to clipboard
inline fun indented(block: CodeWriter.() -> Unit): CodeWriter

Increases the indentation level by one and executes block, then decreases it back.

Link copied to clipboard
fun reset()

Resets the writer, clearing all buffered content and resetting the indent level to zero.

Link copied to clipboard
open override fun toString(): String

Returns the generated code as a string, optionally appending a final newline per FormatConfig.insertFinalNewline.

Link copied to clipboard
fun write(text: String): CodeWriter

Writes text at the current indentation without appending a newline.

Link copied to clipboard
fun writeLine(text: String = ""): CodeWriter

Writes a line with the current indentation followed by a newline.

Link copied to clipboard

Appends raw text without any indentation.