Skip to main content

Module syntax

Module syntax 

Source
Expand description

Syntax highlighting.

Port of rich/syntax.py’s renderable surface. A Syntax highlights a block of source code for a given language and theme, producing colored Segments (a solid block: each line is padded to the render width with the theme background).

Highlighting goes through a CodeHighlighter: the Syntax’s own (Syntax::highlighter), else the console’s default (ConsoleCodeHighlighting), else the built-in one. With the syntax feature (on by default) the built-in one is SyntectHighlighter, which uses the syntect crate. With only the code feature there is no built-in engine: a Syntax with no highlighter of its own and none on the console renders as plain, unhighlighted code, in the same layout (padding, line numbers, wrapping, background_color and stylized ranges still apply; there is no theme background).

Divergence: upstream uses Pygments; syntect ships different grammars and themes. So the coloring is functional, not byte-identical to Python rich — see docs/DIVERGENCES.md. Everything else (the renderable protocol, width handling) matches the port’s conventions.

Structs§

Syntax
A block of syntax-highlighted source code. Mirrors rich.syntax.Syntax.
SyntectHighlighter
Highlights with syntect: its default grammars, its default themes, and upstream’s ansi_dark and ansi_light, which use the terminal’s own palette. This is the highlighter Syntax and Markdown use unless given another one.

Type Aliases§

SyntaxPosition
A (line, column) position in the code: 1-based line, 0-based column. Upstream’s SyntaxPosition: a 1-based line and a 0-based column. Both are signed, as upstream’s are (see Syntax::stylize_range).