Skip to main content

Syntax

Struct Syntax 

Source
pub struct Syntax {
Show 15 fields pub code: String, pub lexer_name: String, pub theme: String, pub line_numbers: bool, pub start_line: usize, pub line_range: Option<(usize, usize)>, pub word_wrap: bool, pub tab_size: usize, pub padding: (usize, usize, usize, usize), pub highlight_lines: HashSet<usize>, pub background_color: Option<String>, pub indent_guides: bool, pub code_width: Option<usize>, pub dedent: bool, pub style_ranges: Vec<(Style, Range<usize>)>,
}
Expand description

Syntax-highlighted code display.

Renders code with syntax highlighting, optional line numbers, word wrap, theme selection, and more.

Fields§

§code: String

The source code to highlight.

§lexer_name: String

Language name for syntax lookup (e.g., “rust”, “python”, “json”).

§theme: String

Theme name (e.g., “base16-ocean.dark”, “base16-mocha.dark”).

§line_numbers: bool

Whether to display line numbers.

§start_line: usize

Starting line number (default 1).

§line_range: Option<(usize, usize)>

Optional (start, end) line range to display (1-based, inclusive).

§word_wrap: bool

Whether to wrap long lines.

§tab_size: usize

Tab width for tab expansion (default 4).

§padding: (usize, usize, usize, usize)

(top, right, bottom, left) padding (top/bottom in blank lines, left/right in spaces inside the code area). Mirrors the CSS shorthand convention used by Padding and matches rich v14.1.0+ Syntax.padding (4-tuple).

Use Self::with_padding / unpack_padding to construct from any shorthand variant: n(n, n, n, n), (v, h)(v, h, v, h), (t, h, b)(t, h, b, h), (t, r, b, l) → as-is.

§highlight_lines: HashSet<usize>

Line numbers to highlight with a special background. Stored as a HashSet for O(1) lookup per rendered line.

§background_color: Option<String>

Optional override for background color (CSS hex like “#282c34”).

§indent_guides: bool

Whether to show indent guides.

§code_width: Option<usize>

Fixed width for code area (excluding line numbers), or None for auto.

§dedent: bool

Whether to auto-dedent code by stripping common leading whitespace.

§style_ranges: Vec<(Style, Range<usize>)>

Style ranges to apply on top of syntax highlighting. Each entry is a (style, character_range) pair applied during rendering.

Implementations§

Source§

impl Syntax

Source

pub fn new(code: &str, lexer_name: &str) -> Self

Create a new Syntax with defaults: base16-mocha.dark theme (Monokai-like), no line numbers.

Source

pub fn from_path(path: &str) -> Result<Self, SyntaxError>

Create a Syntax by reading a file and auto-detecting the language from its extension.

Source

pub fn with_theme(self, theme: &str) -> Self

Set the theme.

Source

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

Enable or disable line numbers.

Source

pub fn with_start_line(self, start: usize) -> Self

Set the starting line number.

Source

pub fn with_line_range(self, range: (usize, usize)) -> Self

Set the line range to display (1-based, inclusive).

Source

pub fn with_word_wrap(self, wrap: bool) -> Self

Enable or disable word wrap.

Source

pub fn with_tab_size(self, size: usize) -> Self

Set the tab size for tab expansion.

Source

pub fn with_highlight_lines( self, lines: impl IntoIterator<Item = usize>, ) -> Self

Set which line numbers to highlight.

Accepts any iterator or Vec; lines are stored in a HashSet for O(1) per-line lookup during rendering.

Source

pub fn with_indent_guides(self, guides: bool) -> Self

Enable or disable indent guides.

Source

pub fn with_code_width(self, width: usize) -> Self

Set a fixed code width.

Source

pub fn with_dedent(self, dedent: bool) -> Self

Enable or disable auto-dedent of common leading whitespace.

Source

pub fn with_padding(self, spec: PaddingSpec) -> Self

Set padding from a PaddingSpec shorthand, expanding to the full (top, right, bottom, left) tuple stored in self.padding.

let s = Syntax::new("fn main() {}", "rs")
    .with_padding(PaddingSpec::VertHoriz(1, 2));
assert_eq!(s.padding, (1, 2, 1, 2));
Source

pub fn stylize_range(&mut self, style: Style, range: Range<usize>)

Add a style to apply over a flat character range of the code.

The range is in terms of character offsets into the original code string (after dedent, if enabled). Multiple ranges may overlap; they are applied in order on top of the syntax highlighting.

Source

pub fn stylize_range_linecol( &mut self, style: Style, start: (usize, usize), end: (usize, usize), )

Add a style to apply over a (line, col) range of the code.

Both start and end are (line, col) pairs where line is 1-based and col is 0-based character offset within the line. The pair is converted to a flat character offset and delegated to stylize_range.

Matches the start=(line, col), end=(line, col) form used by Python rich.

Source

pub fn measure(&self) -> Measurement

Measure the width required to render this Syntax.

Respects line_range when set — only visible lines contribute to the maximum width, matching what render_syntax actually outputs.

Trait Implementations§

Source§

impl Clone for Syntax

Source§

fn clone(&self) -> Syntax

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 Syntax

Source§

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

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

impl Display for Syntax

Source§

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

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

impl Renderable for Syntax

Implement the Renderable trait so Syntax can be printed by Console.

Source§

fn gilt_console( &self, _console: &Console, options: &ConsoleOptions, ) -> Vec<Segment>

Produce segments for rendering on the given console with given options.

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> RenderableExt for T
where T: Renderable + 'static,

Source§

fn into_boxed_renderable(self) -> RenderableBox

Convert this renderable into a RenderableBox for type-erased storage. Read more
Source§

impl<T> ToCompactString for T
where T: Display,

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.