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 highlighter(self, highlighter: Arc<dyn CodeHighlighter>) -> Self

Highlight with highlighter instead of the default SyntectHighlighter. Theme names are the highlighter’s own.

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 padding_sides(self, padding: (usize, usize, usize, usize)) -> Self

Padding on each side, (top, right, bottom, left), in the code background (upstream padding as a tuple).

Source

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

Number the lines in a gutter (upstream line_numbers, default off).

Source

pub fn start_line(self, start_line: i64) -> Self

The number of the first line (upstream start_line, default 1). Any integer, as upstream’s: zero and negative numbers are drawn as they are.

Source

pub fn line_range(self, start: Option<i64>, end: Option<i64>) -> Self

Render only lines start..=end (1-based; None leaves that end open). Upstream line_range, with its Python semantics: a start of 0 or less is the first line, and a negative end counts back from the last line (lines[start - 1:end]) after the highlighter has stopped at the first line it reached.

Source

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

Mark these line numbers with a pointer in the gutter (upstream highlight_lines; shown with line_numbers).

Source

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

A fixed width for the code, excluding line numbers (upstream code_width; default all the available width).

Source

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

Override the theme’s background colour (upstream background_color).

Source

pub fn indent_guides(self, indent_guides: bool) -> Self

Draw indent guides (upstream indent_guides, default off; not on an ASCII-only console).

Source

pub fn stylize_range( &mut self, style: impl Into<StyleType>, start: SyntaxPosition, end: SyntaxPosition, style_before: bool, ) -> &mut Self

Style a range of the code, from start to end ((line, column), 1-based lines, 0-based columns), on top of the highlighting — or beneath it with style_before. Port of Syntax.stylize_range.

Source

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

Choose the highlighting theme, by the highlighter’s name for it. The default highlighter offers syntect’s themes plus upstream’s ansi_dark and ansi_light. Unknown names fall back to the highlighter’s default theme.

Source§

impl Syntax

Source

pub fn highlight(&self) -> Text

Highlight the code into a Text rather than a padded block. Port of Syntax.highlight: the theme background is the text’s base style and every token carries its own style. Tabs are expanded first, as _process_code does. Used by Markdown(inline_code_lexer=…).

Source

pub fn highlight_for(&self, console: &Console) -> Text

highlight with console’s default code highlighter (see ConsoleCodeHighlighting) when this Syntax has none of its own. Not in upstream.

Source

pub fn highlight_range( &self, line_range: Option<(Option<i64>, Option<i64>)>, console: Option<&Console>, ) -> Text

Port of Syntax.highlight(code, line_range) over this Syntax’s code: the highlighted Text in the base style (the theme’s background and background_color), with upstream’s justify, tab_size and no_wrap. With a line_range, lines before it are left unstyled and lines after it dropped. console supplies a default code highlighter, as in highlight_for.

Trait Implementations§

Source§

impl Renderable for Syntax

Source§

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

Port of Syntax.__rich_measure__. Like upstream it measures the raw source, where a tab counts as zero cells.

Source§

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

Port of Syntax.__rich_console__: the lines, inside Padding in the code’s base style when there is any padding.

Source§

fn fit_to_measurement(&self) -> bool

Whether a top-level Console::print shrinks this renderable to its measured width. Upstream renders every top-level renderable at the full console width, so the default is false; an extension may opt in.
Source§

fn vertical(&self) -> Option<VerticalAlign>

The vertical alignment a Table cell holding this renderable uses in place of its column’s. Upstream reads getattr(renderable, "vertical", None); Align sets it.

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 = !

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

fn try_from(value: U) -> Result<T, !>

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.