highlight: Bundle the built-in syntaxes

40a52e23679af4b2d4cb0f8062502843ccc1d0af
The vendored TextMate grammars lived in crates/diff, so any test that
needed a real grammar had to sit in the diff crate even when it only
exercised the highlight engine.

Move the grammars into crates/highlight behind a `syntaxes` feature
that exposes each as a source constant, and have rustik-diff enable
the feature. The two pure CSS tokenization tests move to a highlight
integration test; the theme-dependent ones stay with rustik-diff,
which owns the theme asset.

Assisted-by: Claude:claude-opus-4.7
Alexis Sellier committed ago 1 parent 11f1d466
crates/diff/Cargo.toml +1 -1
6 6
version.workspace = true
7 7
edition.workspace = true
8 8
license.workspace = true
9 9
10 10
[dependencies]
11 -
rustik-highlight.workspace = true
11 +
rustik-highlight = { workspace = true, features = ["syntaxes"] }
12 12
rustik-repo.workspace = true
13 13
rustik-tui = { workspace = true, features = ["widgets"] }
14 14
rustik-tui-termion.workspace = true
15 15
16 16
[[bench]]
crates/diff/src/bin/rustik-diff/builtins.rs +6 -6
10 10
/// Built-in syntax theme JSON.
11 11
const THEME: &str = include_str!("../../themes/rustik-catppuccin-mocha.json");
12 12
/// Common syntax grammars loaded by the interactive pager.
13 13
const SYNTAXES: [SyntaxAsset; 6] = [
14 14
    SyntaxAsset {
15 -
        source: include_str!("../../syntaxes/c.json"),
15 +
        source: syntax::syntaxes::C,
16 16
        file_types: &["c", "h"],
17 17
    },
18 18
    SyntaxAsset {
19 -
        source: include_str!("../../syntaxes/css.json"),
19 +
        source: syntax::syntaxes::CSS,
20 20
        file_types: &["css"],
21 21
    },
22 22
    SyntaxAsset {
23 -
        source: include_str!("../../syntaxes/html.json"),
23 +
        source: syntax::syntaxes::HTML,
24 24
        file_types: &["html", "htm"],
25 25
    },
26 26
    SyntaxAsset {
27 -
        source: include_str!("../../syntaxes/markdown.json"),
27 +
        source: syntax::syntaxes::MARKDOWN,
28 28
        file_types: &["md", "markdown", "mdown"],
29 29
    },
30 30
    SyntaxAsset {
31 -
        source: include_str!("../../syntaxes/rust.json"),
31 +
        source: syntax::syntaxes::RUST,
32 32
        file_types: &["rs"],
33 33
    },
34 34
    SyntaxAsset {
35 -
        source: include_str!("../../syntaxes/typescript.json"),
35 +
        source: syntax::syntaxes::TYPESCRIPT,
36 36
        file_types: &["ts", "cts", "mts"],
37 37
    },
38 38
];
39 39
40 40
/// Binary-local highlight assets loaded for one diff document.
crates/diff/src/highlight.rs +2 -45
381 381
        assert_eq!(source.line(1), None);
382 382
        assert_eq!(source.line_index(2), Some(0));
383 383
        assert_eq!(source.line(2), Some("two"));
384 384
    }
385 385
386 -
    #[test]
387 -
    fn css_root_comments_are_highlighted() {
388 -
        let grammar = syntax::Grammar::from_str(include_str!("syntaxes/css.json")).unwrap();
389 -
        let mut state = syntax::LineState::default();
390 -
        let line = "/**/";
391 -
        let spans = grammar.tokenize_line(&mut state, line);
392 -
        let scopes = spans
393 -
            .iter()
394 -
            .filter_map(|span| grammar.scopes.get(span.scope.index()))
395 -
            .map(String::as_str)
396 -
            .collect::<Vec<_>>();
397 -
398 -
        assert!(scopes.contains(&"comment.block.css"));
399 -
    }
400 -
401 -
    #[test]
402 -
    fn css_property_values_highlight_urls_and_colors_inside_rule() {
403 -
        let grammar = syntax::Grammar::from_str(include_str!("syntaxes/css.json")).unwrap();
404 -
        let scopes = css_rule_scopes(
405 -
            &grammar,
406 -
            "background: url(icon.svg); color: rgb(1 2 3); border-color: #aabbcc;",
407 -
        );
408 -
409 -
        assert!(scopes.contains(&"meta.property-list.css"));
410 -
        assert!(scopes.contains(&"meta.property-value.css"));
411 -
        assert!(scopes.contains(&"meta.function.url.css"));
412 -
        assert!(scopes.contains(&"meta.function.color.css"));
413 -
        assert!(scopes.contains(&"constant.other.color.rgb-value.hex.css"));
414 -
    }
415 -
416 386
    #[test]
417 387
    fn css_custom_properties_use_variable_theme_style() {
418 -
        let grammar = syntax::Grammar::from_str(include_str!("syntaxes/css.json")).unwrap();
388 +
        let grammar = syntax::Grammar::from_str(syntax::syntaxes::CSS).unwrap();
419 389
        let theme =
420 390
            syntax::Theme::parse(include_str!("themes/rustik-catppuccin-mocha.json")).unwrap();
421 391
        let line = "  --color-background-light: #dad8d8;";
422 392
        let mut state = syntax::LineState::default();
423 393
        let mut tokenizer = syntax::LineTokenizer::new(&grammar);
441 411
        );
442 412
    }
443 413
444 414
    #[test]
445 415
    fn css_var_references_use_variable_theme_style() {
446 -
        let grammar = syntax::Grammar::from_str(include_str!("syntaxes/css.json")).unwrap();
416 +
        let grammar = syntax::Grammar::from_str(syntax::syntaxes::CSS).unwrap();
447 417
        let theme =
448 418
            syntax::Theme::parse(include_str!("themes/rustik-catppuccin-mocha.json")).unwrap();
449 419
        let line = "  color: var(--color-dimmer);";
450 420
        let mut state = syntax::LineState::default();
451 421
        let mut tokenizer = syntax::LineTokenizer::new(&grammar);
467 437
                b: 172
468 438
            })
469 439
        );
470 440
    }
471 441
472 -
    fn css_rule_scopes<'a>(grammar: &'a syntax::Grammar, line: &str) -> Vec<&'a str> {
473 -
        let mut state = syntax::LineState::default();
474 -
        let mut tokenizer = syntax::LineTokenizer::new(grammar);
475 -
        let _ = tokenizer.tokenize_line(&mut state, ".x {");
476 -
        let spans = tokenizer.tokenize_line(&mut state, line);
477 -
478 -
        spans
479 -
            .iter()
480 -
            .filter_map(|span| grammar.scopes.get(span.scope.index()))
481 -
            .map(String::as_str)
482 -
            .collect()
483 -
    }
484 -
485 442
    fn scopes_for_byte<'a>(
486 443
        grammar: &'a syntax::Grammar,
487 444
        spans: &'a [syntax::ScopeSpan],
488 445
        byte: usize,
489 446
    ) -> Vec<&'a str> {
crates/diff/src/viewer.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
src/a.rs +0 -0
src/b.rs +0 -0
README.md +0 -0
a.css +0 -0
a.css +0 -0
assets/css/style.css +0 -0
assets/css/style.css +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
a.rs +0 -0
b.rs +0 -0
crates/highlight/Cargo.toml +4 -0
9 9
10 10
[dependencies]
11 11
onig.workspace = true
12 12
serde.workspace = true
13 13
14 +
[features]
15 +
# Bundle the built-in TextMate grammar sources (see the `syntaxes` module).
16 +
syntaxes = []
17 +
14 18
[[bench]]
15 19
name = "tokenize"
16 20
harness = false
17 21
18 22
[lints]
crates/highlight/src/lib.rs +3 -0
24 24
mod theme;
25 25
mod util;
26 26
27 27
pub mod json;
28 28
29 +
#[cfg(feature = "syntaxes")]
30 +
pub mod syntaxes;
31 +
29 32
pub use blob::{BlobHighlighter, LineBuffer, LineTokens, OwnedLineTokens, StyledLine};
30 33
pub use error::Error;
31 34
pub use grammar::{Grammar, GrammarKind, LineState, LineTokenizer, ScopeId, ScopeSpan};
32 35
pub use raw::{RawCapture, RawGrammar, RawPattern, RawStyle, RawTheme, RawThemeRule};
33 36
pub use registry::{GrammarQuery, Registry};
crates/highlight/src/syntaxes.rs added +23 -0
1 +
//! Built-in TextMate grammar sources.
2 +
//!
3 +
//! Available with the `syntaxes` feature. Each constant holds the raw grammar
4 +
//! JSON for one language, ready to compile with `Grammar::from_str` or parse
5 +
//! into a `RawGrammar`.
6 +
7 +
/// C grammar source.
8 +
pub const C: &str = include_str!("../syntaxes/c.json");
9 +
10 +
/// CSS grammar source.
11 +
pub const CSS: &str = include_str!("../syntaxes/css.json");
12 +
13 +
/// HTML grammar source.
14 +
pub const HTML: &str = include_str!("../syntaxes/html.json");
15 +
16 +
/// Markdown grammar source.
17 +
pub const MARKDOWN: &str = include_str!("../syntaxes/markdown.json");
18 +
19 +
/// Rust grammar source.
20 +
pub const RUST: &str = include_str!("../syntaxes/rust.json");
21 +
22 +
/// TypeScript grammar source.
23 +
pub const TYPESCRIPT: &str = include_str!("../syntaxes/typescript.json");
crates/diff/src/syntaxes/LICENSE → crates/highlight/syntaxes/LICENSE renamed +0 -0
crates/diff/src/syntaxes/c.json → crates/highlight/syntaxes/c.json renamed +0 -0
crates/diff/src/syntaxes/css.json → crates/highlight/syntaxes/css.json renamed +0 -0
crates/diff/src/syntaxes/html.json → crates/highlight/syntaxes/html.json renamed +0 -0
crates/diff/src/syntaxes/markdown.json → crates/highlight/syntaxes/markdown.json renamed +0 -0
crates/diff/src/syntaxes/rust.json → crates/highlight/syntaxes/rust.json renamed +0 -0
crates/diff/src/syntaxes/typescript.json → crates/highlight/syntaxes/typescript.json renamed +0 -0
crates/highlight/tests/syntaxes.rs added +50 -0
1 +
//! Tokenization tests for the built-in `syntaxes` grammars.
2 +
#![cfg(feature = "syntaxes")]
3 +
4 +
use std::str::FromStr;
5 +
6 +
use rustik_highlight::{Grammar, LineState, LineTokenizer, syntaxes};
7 +
8 +
#[test]
9 +
fn css_root_comments_are_highlighted() {
10 +
    let grammar = Grammar::from_str(syntaxes::CSS).unwrap();
11 +
    let mut state = LineState::default();
12 +
    let line = "/**/";
13 +
    let spans = grammar.tokenize_line(&mut state, line);
14 +
    let scopes = spans
15 +
        .iter()
16 +
        .filter_map(|span| grammar.scopes.get(span.scope.index()))
17 +
        .map(String::as_str)
18 +
        .collect::<Vec<_>>();
19 +
20 +
    assert!(scopes.contains(&"comment.block.css"));
21 +
}
22 +
23 +
#[test]
24 +
fn css_property_values_highlight_urls_and_colors_inside_rule() {
25 +
    let grammar = Grammar::from_str(syntaxes::CSS).unwrap();
26 +
    let scopes = css_rule_scopes(
27 +
        &grammar,
28 +
        "background: url(icon.svg); color: rgb(1 2 3); border-color: #aabbcc;",
29 +
    );
30 +
31 +
    assert!(scopes.contains(&"meta.property-list.css"));
32 +
    assert!(scopes.contains(&"meta.property-value.css"));
33 +
    assert!(scopes.contains(&"meta.function.url.css"));
34 +
    assert!(scopes.contains(&"meta.function.color.css"));
35 +
    assert!(scopes.contains(&"constant.other.color.rgb-value.hex.css"));
36 +
}
37 +
38 +
/// Tokenizes `line` inside a CSS rule body and returns its scope names.
39 +
fn css_rule_scopes<'a>(grammar: &'a Grammar, line: &str) -> Vec<&'a str> {
40 +
    let mut state = LineState::default();
41 +
    let mut tokenizer = LineTokenizer::new(grammar);
42 +
    let _ = tokenizer.tokenize_line(&mut state, ".x {");
43 +
    let spans = tokenizer.tokenize_line(&mut state, line);
44 +
45 +
    spans
46 +
        .iter()
47 +
        .filter_map(|span| grammar.scopes.get(span.scope.index()))
48 +
        .map(String::as_str)
49 +
        .collect()
50 +
}