Expand description
Headless syntax highlighting for ashlar: small, forward-only, linear-time lexers for common languages, and a line-state API so a view can re-highlight only the lines that changed.
No dependencies, no UI; builds for wasm32-unknown-unknown. Colours live in the UI layer
(ashlar_ui::code_view::SyntaxTheme); this crate only says which byte range is which Kind.
use ashlar_syntax::{highlight, lang_for_path, Kind};
let lang = lang_for_path("src/main.rs");
assert_eq!(lang, Some("rust"));
let spans = highlight(lang, "fn main() { let x = 42; }");
assert_eq!(spans.first(), Some(&(0..2, Kind::Keyword)));
// ordered, non-overlapping and covering the whole input
assert_eq!(spans.last().map(|(r, _)| r.end), Some(25));Incremental use: keep the State each line starts in. After an edit, re-highlight from the
edited line and stop as soon as a line ends in the same state it ended in before.
use ashlar_syntax::{highlight_line, Lang, State};
let mut spans = Vec::new();
let after_first = highlight_line(Lang::Rust, "let s = /* open", State::START, &mut spans);
assert_ne!(after_first, State::START); // inside a block comment
spans.clear();
let after_second = highlight_line(Lang::Rust, "still a comment */ x", after_first, &mut spans);
assert_eq!(after_second, State::START);Structs§
- Builtin
- The built-in lexers as a
Highlighter. - State
- The lexer state at the start of a line: whether it begins inside a block comment, a
multi-line string, a heredoc… Opaque,
Copyand cheap to compare;State::STARTis the state at the top of a file.
Enums§
Traits§
- Highlighter
- A syntax highlighting engine.
Builtinis the lexers of this crate; a bigger engine (a grammar-based one, say) can implement this and be used by the same views.
Functions§
- highlight
- Highlights
textaslang(a name, alias or extension; seeLang::from_name). - highlight_
into highlightwith a known language, appending toout(reuse it to save allocations). Offsets are relative totext; a span is merged into the last one ofoutwhen they touch and share a kind, so clearoutfirst unless you mean to continue it.- highlight_
line - Highlights one line (with or without its line ending), starting in
state, appending spans relative to the line’s start toout. Returns the state the next line starts in. - lang_
for_ path - The canonical language name for a file path (
"rust"for"src/main.rs"), orNone.
Type Aliases§
- Span
- A highlighted byte range. Ranges from this crate are ordered, non-overlapping, non-empty, on
charboundaries, cover the input, and neighbours never share a kind.