pub struct CodeBlock { /* private fields */ }Expand description
An immutable code fragment with embedded type references.
CodeBlock is the core composition primitive in sigil-stitch. It stores a tree
of CodeNode nodes — self-contained IR nodes produced from format strings
(%T, %N, %S, %L, etc.). CodeBlocks are produced by CodeBlockBuilder
and consumed by FileSpec during rendering.
Type references embedded via %T are automatically tracked for import resolution.
Use CodeBlock::builder() to construct a block incrementally, or
CodeBlock::of() for simple one-liners.
§Examples
use sigil_stitch::code_block::CodeBlock;
use sigil_stitch::lang::typescript::TypeScript;
use sigil_stitch::type_name::TypeName;
// One-liner with a type reference:
let user = TypeName::importable("./models", "User");
let block = CodeBlock::of("const u: %T = getUser()", (user,)).unwrap();
// Multi-statement block via builder:
let mut cb = CodeBlock::builder();
cb.add_statement("const x = 1", ());
cb.add_statement("const y = 2", ());
let block = cb.build().unwrap();Implementations§
Source§impl CodeBlock
impl CodeBlock
Sourcepub fn builder() -> CodeBlockBuilder
pub fn builder() -> CodeBlockBuilder
Create a new CodeBlockBuilder.
Sourcepub fn of(format: &str, args: impl IntoArgs) -> Result<Self, SigilStitchError>
pub fn of(format: &str, args: impl IntoArgs) -> Result<Self, SigilStitchError>
Create a CodeBlock from a single format string and arguments.
Sourcepub fn collect_imports(&self, out: &mut Vec<ImportRef>)
pub fn collect_imports(&self, out: &mut Vec<ImportRef>)
Collect all import references from this code block.
Sourcepub fn render_standalone(
&self,
lang: &dyn CodeLang,
width: usize,
) -> Result<String, SigilStitchError>
pub fn render_standalone( &self, lang: &dyn CodeLang, width: usize, ) -> Result<String, SigilStitchError>
Render this code block to a string without import resolution.
Creates a temporary empty import group and renders using the given language and target line width. Useful for quick one-off rendering in tests or when import management is not needed.