Skip to main content

comfit_core/
lib.rs

1//! Syntax highlighting and themes with optional HTML and ANSI rendering.
2//!
3//! Implement [`backend::Backend`] to connect a parser to the highlighting engine.
4//! All source ranges use UTF-8 byte offsets. Highlighted tokens borrow the source
5//! and capture names from the highlighter.
6
7pub mod backend;
8pub mod document;
9pub mod engine;
10pub mod highlight;
11mod language;
12mod output;
13pub mod query;
14#[cfg(any(feature = "render-html", feature = "render-ansi"))]
15mod render;
16mod syntax;
17pub mod theme;
18
19#[cfg(feature = "render-html")]
20pub use render::html;
21
22pub use document::Document;
23pub use engine::{Highlighter, Queries};
24pub use highlight::{
25    HighlightConfig, HighlightResult, Highlighted, Line, OwnedLine, OwnedToken, Token,
26};
27pub use rgb::RGB8;
28pub use theme::{Color, Modifier, Style, Theme, Underline, UnderlineStyle};
29
30#[cfg(feature = "render-html")]
31pub use html::{RenderHtmlConfig, RenderHtmlStyle};
32
33/// Errors from language registration, parsing, highlighting, or theme loading.
34#[derive(Debug, thiserror::Error)]
35pub enum Error {
36    /// HTML render options or named themes are invalid.
37    #[error("invalid HTML options: {0}")]
38    Html(String),
39    /// A parsing runtime failed.
40    #[error("Tree-sitter backend: {0}")]
41    Backend(String),
42    /// The requested root language is not registered.
43    #[error("unknown or unloaded language: {0}")]
44    UnknownLanguage(String),
45    /// A language ID or alias is empty, repeated, or already registered.
46    #[error("duplicate language ID or alias: {0}")]
47    DuplicateLanguage(String),
48    /// A query or predicate is invalid or unsupported.
49    #[error("invalid query: {0}")]
50    Query(String),
51    /// A capture or offset does not describe a valid UTF-8 source slice.
52    #[error("invalid UTF-8 range: {0:?}")]
53    InvalidRange(std::ops::Range<usize>),
54    /// Injection nesting exceeds 32 layers.
55    #[error("injection nesting exceeds 32 layers")]
56    InjectionDepth,
57    /// A theme contains invalid syntax or style values.
58    #[error("invalid theme: {0}")]
59    Theme(String),
60}