Skip to main content

CodeBlock

Struct CodeBlock 

Source
pub struct CodeBlock<'a> { /* private fields */ }
Expand description

A themed, syntax-highlighted code block — a language label, a left rail, a code background, and verbatim (never-reflowed) body lines.

code_block demo

Colors come entirely from Theme::code; token classes are resolved by whatever Highlighter you plug in (none → plain, theme-colored text).

§Options

BuilderDefaultEffect
new(lang, source)language tag + source (split on \n)
highlighter(&h)plainplug in syntax highlighting
label(bool)trueshow/hide the language-label row
line_numbers(bool)falseshow a line-number gutter
start_line(n)1first gutter line number
use tuika::prelude::*;
let theme = Theme::default();
// No highlighter → plain, theme-colored code; hide the label row.
let block = CodeBlock::new("rust", "fn main() {}").label(false);
// `block` is a `View`; render it through `tuika::paint` or embed it in a
// `Flex`. Supply a highlighter with `.highlighter(&my_highlighter)` — see
// the `tuika-codeformatters` crate for a tree-sitter one.

Implementations§

Source§

impl<'a> CodeBlock<'a>

Source

pub fn new(lang: impl Into<String>, source: impl AsRef<str>) -> Self

A code block for source in language lang (e.g. "rust", "py", or "" for no language). source is split on newlines into body lines.

Source

pub fn highlighter(self, highlighter: &'a dyn Highlighter) -> Self

Plug in a syntax highlighter; without one the body renders plain.

Source

pub fn label(self, show: bool) -> Self

Whether to show the language-label row (default true).

Source

pub fn line_numbers(self, show: bool) -> Self

Show (or hide) a right-aligned line-number gutter before the rail. The gutter counts from start_line (default 1).

Source

pub fn start_line(self, first: usize) -> Self

Set the first gutter line number, implying line_numbers(true). Useful when a block is a slice of a larger file.

Trait Implementations§

Source§

impl View for CodeBlock<'_>

Source§

fn measure(&self, available: Size) -> Size

Report the intrinsic content size wanted given available.
Source§

fn render(&self, area: Rect, surface: &mut Surface<'_>, ctx: &RenderCtx<'_>)

Paint the view into the assigned area through surface.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for CodeBlock<'a>

§

impl<'a> !Send for CodeBlock<'a>

§

impl<'a> !Sync for CodeBlock<'a>

§

impl<'a> !UnwindSafe for CodeBlock<'a>

§

impl<'a> Freeze for CodeBlock<'a>

§

impl<'a> Unpin for CodeBlock<'a>

§

impl<'a> UnsafeUnpin for CodeBlock<'a>

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> 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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. 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.