Skip to main content

Syntax

Struct Syntax 

Source
pub struct Syntax { /* private fields */ }
Expand description

A block of syntax-highlighted source code. Mirrors rich.syntax.Syntax.

Implementations§

Source§

impl Syntax

Source

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

Wrap lines wider than the render width instead of cropping them.

Off by default, matching upstream’s Syntax(word_wrap=False): a long line is cut at the width. Upstream’s CLI turns this on, which is why rich --syntax does too — cropping a source file silently loses code.

Source

pub fn new(code: impl Into<String>, language: impl Into<String>) -> Self

Highlight code as language (a name or file extension, e.g. "rust" or "rs"). Pass an empty/unknown language to render as plain text.

Source

pub fn tab_size(self, tab_size: usize) -> Self

How far a tab advances the column, in characters. Upstream’s Syntax(tab_size=…), default 4.

Tabs are expanded to spaces before highlighting (upstream’s code.expandtabs(self.tab_size)), so this is the only tab handling in play — the rendered code contains no U+0009 at all.

Source

pub fn padding(self, padding: usize) -> Self

Surround the code with padding cells of background on every side.

Upstream’s Markdown renders a fenced block as Syntax(..., padding=1), which is what gives a code block its blank inset row above and below and its one-column gutter. Without it the code sat flush against the surrounding text and every document containing a fence diverged.

Source

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

Choose the highlighting theme (a syntect theme name). Unknown names fall back to the default.

Trait Implementations§

Source§

impl Renderable for Syntax

Source§

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

Source§

fn measure(&self, _console: &Console, options: &ConsoleOptions) -> Measurement

The (minimum, maximum) cell width this renderable wants. The default assumes the renderable fills the available width (e.g. Panel, Table); Text overrides it with its content width so the top-level print path can shrink to fit. Port of __rich_measure__ / Measurement.get.

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