Documentation Language: Swift

Class

MTLWriter

Thread-safe actor for accumulating generated text with automatic indentation.

actor MTLWriter

Overview

MTL writers manage the output buffer for text generation, providing thread-safe text accumulation with automatic indentation handling. Writers track whether they are at the start of a line to apply indentation only when needed.

Overview

MTL writers support:

Indentation Behavior

Indentation is automatically applied:

let writer = MTLWriter(indentation: MTLIndentation(level: 1))

await writer.write("Hello")         // "    Hello" (indented)
await writer.write(" World")        // "    Hello World" (same line)
await writer.newLine()              // "    Hello World\n"
await writer.write("Next line")     // "    Hello World\n    Next line"

Example Usage

let writer = MTLWriter()

// Write text with automatic indentation
await writer.writeLine("class Example {")
writer.indentation = writer.indentation.increment()
await writer.writeLine("var x: Int")
writer.indentation = writer.indentation.decrement()
await writer.writeLine("}")

let output = await writer.getContent()
// class Example {
//     var x: Int
// }

Topics

Initializers

?
init(indentation:)

Creates a new writer with the specified initial indentation.

Instance Properties

V
indentation

The current indentation level.

Instance Methods

F
clear()

Clears the buffer and resets the writer state.

F
getContent()

Returns the accumulated buffer contents.

F
newLine(indent:)

Writes a newline character to the buffer.

F
setIndentation(_:)

Updates the current indentation level.

F
write(_:indent:)

Writes text to the buffer with optional automatic indentation.

F
writeLine(_:indent:)

Writes a line of text followed by a newline.

Default Implementations

Relationships

Conforms To