pub struct KtCode { /* private fields */ }Expand description
A sequence of raw lines and nested blocks plus the imports its text references. Rendered with 4-space indentation per nesting level.
Implementations§
Source§impl KtCode
impl KtCode
pub fn new() -> Self
pub fn is_empty(&self) -> bool
Sourcepub fn lines(self, text: &str) -> Self
pub fn lines(self, text: &str) -> Self
Append a multi-line string: each line lands at the current level.
Sourcepub fn wline(self, s: impl Into<String>) -> Self
pub fn wline(self, s: impl Into<String>) -> Self
Append one line that is width-broken at render time (one call
argument / lambda parameter per line) if it exceeds the budget at its
actual nesting level. Use for composed statements of unbounded width
(call expressions, guards); plain Self::line for short fixed text.
Sourcepub fn try_finally(
self,
opener_prefix: impl Into<String>,
body: KtCode,
fin: KtCode,
) -> Self
pub fn try_finally( self, opener_prefix: impl Into<String>, body: KtCode, fin: KtCode, ) -> Self
Append try { body } finally { fin }. opener_prefix glues a binding
onto the try (e.g. "val __ret = ").
Sourcepub fn blk(
self,
opener: impl Into<String>,
f: impl FnOnce(KtCode) -> KtCode,
) -> Self
pub fn blk( self, opener: impl Into<String>, f: impl FnOnce(KtCode) -> KtCode, ) -> Self
Append a nested block: opener at the current level, children one
level deeper, then }.
Sourcepub fn blk_with(
self,
opener: impl Into<String>,
closer: impl Into<String>,
f: impl FnOnce(KtCode) -> KtCode,
) -> Self
pub fn blk_with( self, opener: impl Into<String>, closer: impl Into<String>, f: impl FnOnce(KtCode) -> KtCode, ) -> Self
Self::blk with an explicit closer (}), },, …).
Sourcepub fn push(self, other: KtCode) -> Self
pub fn push(self, other: KtCode) -> Self
Append another KtCode’s items (same level) and imports.
Sourcepub fn import(self, fqn: impl Into<String>) -> Self
pub fn import(self, fqn: impl Into<String>) -> Self
Register an FQN referenced only inside this raw text.
Sourcepub fn raw_reindent(text: &str) -> Self
pub fn raw_reindent(text: &str) -> Self
Build from a flat multi-line blob by recomputing nesting from brace
balance — for migrating bodies composed as unindented strings. Tracks
Kotlin string literals (incl. escapes) and // comments so braces
inside them don’t count. A line’s level drops first by its leading
closers (}, }) …), then the rest of its delta applies to the
following lines.
Sourcepub fn raw_reindent_wrapped(text: &str) -> Self
pub fn raw_reindent_wrapped(text: &str) -> Self
Like Self::raw_reindent, but additionally breaks any line that would
exceed the renderer’s maximum signature width across multiple lines,
one call argument (or lambda parameter) per line — the body-side analogue
of the width-aware signature layout in the renderer. The brace
delta that drives nesting is still computed from the original (un-broken)
line, so the wrapping never disturbs the surrounding block structure.