Skip to main content

Syntax

Struct Syntax 

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

Implementations§

Source§

impl Syntax

Source

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

Create a new Syntax object for the given code and language.

§Arguments
  • code - The source code to highlight.
  • lexer - The language name (e.g., “rust”, “python”, “javascript”).
§Example
use rich_rs::Syntax;

let syntax = Syntax::new("print('hello')", "python");
Source

pub fn from_path(path: impl AsRef<Path>) -> Result<Self>

Create a Syntax object from a file path.

The language is auto-detected from the file extension.

§Arguments
  • path - Path to the source file.
§Returns

Ok(Syntax) if the file was read successfully, Err otherwise.

§Example
let syntax = Syntax::from_path("src/main.rs")?;
Source

pub fn guess_lexer(path: impl AsRef<Path>, code: Option<&str>) -> String

Guess the language/lexer for a file path.

§Arguments
  • path - Path to examine.
  • code - Optional code content for better detection.
§Returns

The best-guess language name.

Source

pub fn get_theme(name: &str) -> Box<dyn SyntaxTheme>

Get a theme by name.

§Arguments
  • name - Theme name (e.g., “monokai”, “dracula”, “github-dark”).
§Returns

A boxed SyntaxTheme. Falls back to Monokai if the theme is not found.

Source

pub fn available_themes() -> Vec<&'static str>

List available theme names.

Source

pub fn available_languages() -> Vec<String>

List available language/lexer names.

Source

pub fn with_theme(self, theme: impl AsRef<str>) -> Self

Set the theme by name.

This overrides the Console’s theme for this renderable.

Source

pub fn with_custom_theme(self, theme: Box<dyn SyntaxTheme>) -> Self

Set a custom theme.

This overrides the Console’s theme for this renderable.

Source

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

Enable or disable code dedenting.

Source

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

Enable or disable line numbers.

Source

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

Set the starting line number.

Source

pub fn with_line_range(self, start: Option<usize>, end: Option<usize>) -> Self

Set the line range to display.

§Arguments
  • start - Optional start line (1-based, inclusive).
  • end - Optional end line (1-based, inclusive).
Source

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

Set lines to highlight.

Source

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

Set a fixed code width.

Source

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

Set the tab size.

Source

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

Enable or disable word wrapping.

NOTE: Not yet implemented - option is stored for future use.

Source

pub fn with_background_color(self, color: Color) -> Self

Set a background color override.

Source

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

Enable or disable indent guides.

NOTE: Not yet implemented - option is stored for future use.

Source

pub fn with_padding(self, padding: impl Into<PaddingDimensions>) -> Self

Set padding around the syntax block.

Source

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

Add a custom style range to apply on top of syntax highlighting.

Positions are (line, column) where line is 1-based and column is 0-based.

§Arguments
  • style - Style to apply to the range.
  • start - Start position as (line, column).
  • end - End position as (line, column).
Source

pub fn with_highlight_range( self, style: Style, start: (usize, usize), end: (usize, usize), ) -> Self

Builder method to add a highlight range.

Positions are (line, column) where line is 1-based and column is 0-based.

Source

pub fn code(&self) -> &str

Get the source code.

Source

pub fn lexer(&self) -> &str

Get the lexer/language name.

Source

pub fn line_numbers(&self) -> bool

Check if line numbers are enabled.

Source

pub fn tab_size(&self) -> usize

Get the tab size.

Source

pub fn highlight(&self) -> Text

Highlight the code and return a Text object.

This converts syntect-highlighted code into a rich-rs Text object with styled spans.

Trait Implementations§

Source§

impl Debug for Syntax

Source§

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

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

impl Renderable for Syntax

Source§

fn render( &self, console: &Console<Stdout>, options: &ConsoleOptions, ) -> Segments

Render this object to a sequence of segments.
Source§

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

Measure the minimum and maximum width requirements. Read more

Auto Trait Implementations§

§

impl Freeze for Syntax

§

impl !RefUnwindSafe for Syntax

§

impl Send for Syntax

§

impl Sync for Syntax

§

impl Unpin for Syntax

§

impl !UnwindSafe for Syntax

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.