Skip to main content

Crate ashlar_syntax

Crate ashlar_syntax 

Source
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, Copy and cheap to compare; State::START is the state at the top of a file.

Enums§

Kind
What a span of source text is, for colouring.
Lang
A language the built-in lexers know.

Traits§

Highlighter
A syntax highlighting engine. Builtin is 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 text as lang (a name, alias or extension; see Lang::from_name).
highlight_into
highlight with a known language, appending to out (reuse it to save allocations). Offsets are relative to text; a span is merged into the last one of out when they touch and share a kind, so clear out first 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 to out. Returns the state the next line starts in.
lang_for_path
The canonical language name for a file path ("rust" for "src/main.rs"), or None.

Type Aliases§

Span
A highlighted byte range. Ranges from this crate are ordered, non-overlapping, non-empty, on char boundaries, cover the input, and neighbours never share a kind.