Skip to main content

KtCode

Struct KtCode 

Source
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

Source

pub fn new() -> Self

Source

pub fn is_empty(&self) -> bool

Source

pub fn line(self, s: impl Into<String>) -> Self

Append one raw line.

Source

pub fn lines(self, text: &str) -> Self

Append a multi-line string: each line lands at the current level.

Source

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.

Source

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 = ").

Source

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 }.

Source

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 (}), },, …).

Source

pub fn push(self, other: KtCode) -> Self

Append another KtCode’s items (same level) and imports.

Source

pub fn import(self, fqn: impl Into<String>) -> Self

Register an FQN referenced only inside this raw text.

Source

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.

Source

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.

Source

pub fn render(&self, level: usize, out: &mut String)

Render with level leading 4-space indents per line.

Trait Implementations§

Source§

impl Clone for KtCode

Source§

fn clone(&self) -> KtCode

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for KtCode

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for KtCode

Source§

fn default() -> KtCode

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.