highlight: Add ERB grammar

8879398e2ae9089613f231684889e779e512fec4
Bundle the upstream ERB TextMate grammar and expose it through the
built-in syntax list with HTML, Ruby, CSS, and JavaScript dependencies.

Add syntax coverage for ERB tags, embedded Ruby, embedded HTML, and
JavaScript inside ERB script blocks.

Assisted-by: Codex:gpt-5.5
Alexis Sellier committed ago 1 parent 49add220
crates/diff/src/bin/rustik-diff/builtins.rs +12 -1
8 8
/// Theme name used for built-in diff highlighting.
9 9
const THEME_NAME: &str = "rustik-catppuccin-mocha";
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 -
const SYNTAXES: [SyntaxAsset; 12] = [
13 +
const SYNTAXES: [SyntaxAsset; 13] = [
14 14
    SyntaxAsset {
15 15
        source: syntax::syntaxes::C,
16 16
        file_types: &["c", "h"],
17 17
        dependencies: &[],
18 18
    },
19 19
    SyntaxAsset {
20 20
        source: syntax::syntaxes::CSS,
21 21
        file_types: &["css"],
22 22
        dependencies: &[],
23 23
    },
24 +
    SyntaxAsset {
25 +
        source: syntax::syntaxes::ERB,
26 +
        file_types: &["erb", "rhtml", "html.erb"],
27 +
        dependencies: &[
28 +
            syntax::syntaxes::HTML,
29 +
            syntax::syntaxes::RUBY,
30 +
            syntax::syntaxes::CSS,
31 +
            syntax::syntaxes::JAVASCRIPT,
32 +
        ],
33 +
    },
24 34
    SyntaxAsset {
25 35
        source: syntax::syntaxes::GO,
26 36
        file_types: &["go"],
27 37
        dependencies: &[],
28 38
    },
171 181
            "main.go",
172 182
            "app.js",
173 183
            "server.mjs",
174 184
            "Makefile",
175 185
            "rules.mk",
186 +
            "index.html.erb",
176 187
            "script.py",
177 188
            "task.rb",
178 189
            "build.sh",
179 190
        ] {
180 191
            assert!(
crates/highlight/src/syntaxes.rs +3 -0
8 8
pub const C: &str = include_str!("../syntaxes/c.json");
9 9
10 10
/// CSS grammar source.
11 11
pub const CSS: &str = include_str!("../syntaxes/css.json");
12 12
13 +
/// ERB grammar source.
14 +
pub const ERB: &str = include_str!("../syntaxes/erb.json");
15 +
13 16
/// Go grammar source.
14 17
pub const GO: &str = include_str!("../syntaxes/go.json");
15 18
16 19
/// HTML grammar source.
17 20
pub const HTML: &str = include_str!("../syntaxes/html.json");
crates/highlight/syntaxes/erb.json added +156 -0
1 +
{
2 +
  "displayName": "ERB",
3 +
  "fileTypes": [
4 +
    "erb",
5 +
    "rhtml",
6 +
    "html.erb"
7 +
  ],
8 +
  "injections": {
9 +
    "text.html.erb - (meta.embedded.block.erb | meta.embedded.line.erb | comment)": {
10 +
      "patterns": [
11 +
        {
12 +
          "begin": "^(\\s*)(?=<%+#(?![^%]*%>))",
13 +
          "beginCaptures": {
14 +
            "0": {
15 +
              "name": "punctuation.whitespace.comment.leading.erb"
16 +
            }
17 +
          },
18 +
          "end": "(?!\\G)(\\s*$\\n)?",
19 +
          "endCaptures": {
20 +
            "0": {
21 +
              "name": "punctuation.whitespace.comment.trailing.erb"
22 +
            }
23 +
          },
24 +
          "patterns": [
25 +
            {
26 +
              "include": "#comment"
27 +
            }
28 +
          ]
29 +
        },
30 +
        {
31 +
          "begin": "^(\\s*)(?=<%(?![^%]*%>))",
32 +
          "beginCaptures": {
33 +
            "0": {
34 +
              "name": "punctuation.whitespace.embedded.leading.erb"
35 +
            }
36 +
          },
37 +
          "end": "(?!\\G)(\\s*$\\n)?",
38 +
          "endCaptures": {
39 +
            "0": {
40 +
              "name": "punctuation.whitespace.embedded.trailing.erb"
41 +
            }
42 +
          },
43 +
          "patterns": [
44 +
            {
45 +
              "include": "#tags"
46 +
            }
47 +
          ]
48 +
        },
49 +
        {
50 +
          "include": "#comment"
51 +
        },
52 +
        {
53 +
          "include": "#tags"
54 +
        }
55 +
      ]
56 +
    }
57 +
  },
58 +
  "name": "erb",
59 +
  "patterns": [
60 +
    {
61 +
      "include": "text.html.basic"
62 +
    }
63 +
  ],
64 +
  "repository": {
65 +
    "comment": {
66 +
      "patterns": [
67 +
        {
68 +
          "begin": "<%+#",
69 +
          "beginCaptures": {
70 +
            "0": {
71 +
              "name": "punctuation.definition.comment.begin.erb"
72 +
            }
73 +
          },
74 +
          "end": "%>",
75 +
          "endCaptures": {
76 +
            "0": {
77 +
              "name": "punctuation.definition.comment.end.erb"
78 +
            }
79 +
          },
80 +
          "name": "comment.block.erb"
81 +
        }
82 +
      ]
83 +
    },
84 +
    "tags": {
85 +
      "patterns": [
86 +
        {
87 +
          "begin": "<%+(?!>)[-=]?(?![^%]*%>)",
88 +
          "beginCaptures": {
89 +
            "0": {
90 +
              "name": "punctuation.section.embedded.begin.erb"
91 +
            }
92 +
          },
93 +
          "contentName": "source.ruby",
94 +
          "end": "(-?%)>",
95 +
          "endCaptures": {
96 +
            "0": {
97 +
              "name": "punctuation.section.embedded.end.erb"
98 +
            },
99 +
            "1": {
100 +
              "name": "source.ruby"
101 +
            }
102 +
          },
103 +
          "name": "meta.embedded.block.erb",
104 +
          "patterns": [
105 +
            {
106 +
              "captures": {
107 +
                "1": {
108 +
                  "name": "punctuation.definition.comment.erb"
109 +
                }
110 +
              },
111 +
              "match": "(#).*?(?=-?%>)",
112 +
              "name": "comment.line.number-sign.erb"
113 +
            },
114 +
            {
115 +
              "include": "source.ruby"
116 +
            }
117 +
          ]
118 +
        },
119 +
        {
120 +
          "begin": "<%+(?!>)[-=]?",
121 +
          "beginCaptures": {
122 +
            "0": {
123 +
              "name": "punctuation.section.embedded.begin.erb"
124 +
            }
125 +
          },
126 +
          "contentName": "source.ruby",
127 +
          "end": "(-?%)>",
128 +
          "endCaptures": {
129 +
            "0": {
130 +
              "name": "punctuation.section.embedded.end.erb"
131 +
            },
132 +
            "1": {
133 +
              "name": "source.ruby"
134 +
            }
135 +
          },
136 +
          "name": "meta.embedded.line.erb",
137 +
          "patterns": [
138 +
            {
139 +
              "captures": {
140 +
                "1": {
141 +
                  "name": "punctuation.definition.comment.erb"
142 +
                }
143 +
              },
144 +
              "match": "(#).*?(?=-?%>)",
145 +
              "name": "comment.line.number-sign.erb"
146 +
            },
147 +
            {
148 +
              "include": "source.ruby"
149 +
            }
150 +
          ]
151 +
        }
152 +
      ]
153 +
    }
154 +
  },
155 +
  "scopeName": "text.html.erb"
156 +
}
crates/highlight/tests/syntaxes.rs +47 -0
8 8
#[test]
9 9
fn built_in_syntaxes_compile() {
10 10
    for source in [
11 11
        syntaxes::C,
12 12
        syntaxes::CSS,
13 +
        syntaxes::ERB,
13 14
        syntaxes::GO,
14 15
        syntaxes::HTML,
15 16
        syntaxes::JAVASCRIPT,
16 17
        syntaxes::MAKE,
17 18
        syntaxes::MARKDOWN,
23 24
    ] {
24 25
        Grammar::from_str(source).unwrap();
25 26
    }
26 27
}
27 28
29 +
#[test]
30 +
fn erb_embeds_html_and_ruby_scopes() {
31 +
    let erb: rustik_highlight::RawGrammar = syntaxes::ERB.parse().unwrap();
32 +
    let html: rustik_highlight::RawGrammar = syntaxes::HTML.parse().unwrap();
33 +
    let ruby: rustik_highlight::RawGrammar = syntaxes::RUBY.parse().unwrap();
34 +
    let grammar = Grammar::compile_with(&erb, &[html, ruby]).unwrap();
35 +
    let mut state = LineState::default();
36 +
    let mut tokenizer = LineTokenizer::new(&grammar);
37 +
    let spans = tokenizer.tokenize_line(&mut state, "<h1><% if ok %></h1>");
38 +
    let scopes = spans
39 +
        .iter()
40 +
        .filter_map(|span| grammar.scopes.get(span.scope.index()))
41 +
        .map(String::as_str)
42 +
        .collect::<Vec<_>>();
43 +
44 +
    assert!(scopes.contains(&"entity.name.tag.html"));
45 +
    assert!(scopes.contains(&"punctuation.section.embedded.begin.erb"));
46 +
    assert!(scopes.contains(&"source.ruby"));
47 +
    assert!(scopes.contains(&"keyword.control.ruby"));
48 +
}
49 +
50 +
#[test]
51 +
fn erb_embeds_multiline_javascript_scopes() {
52 +
    let erb: rustik_highlight::RawGrammar = syntaxes::ERB.parse().unwrap();
53 +
    let html: rustik_highlight::RawGrammar = syntaxes::HTML.parse().unwrap();
54 +
    let ruby: rustik_highlight::RawGrammar = syntaxes::RUBY.parse().unwrap();
55 +
    let css: rustik_highlight::RawGrammar = syntaxes::CSS.parse().unwrap();
56 +
    let javascript: rustik_highlight::RawGrammar = syntaxes::JAVASCRIPT.parse().unwrap();
57 +
    let grammar = Grammar::compile_with(&erb, &[html, ruby, css, javascript]).unwrap();
58 +
    let mut state = LineState::default();
59 +
    let mut tokenizer = LineTokenizer::new(&grammar);
60 +
    let _ = tokenizer.tokenize_line(&mut state, "<script>");
61 +
    let spans = tokenizer.tokenize_line(
62 +
        &mut state,
63 +
        "document.addEventListener('DOMContentLoaded', function () {",
64 +
    );
65 +
    let scopes = spans
66 +
        .iter()
67 +
        .filter_map(|span| grammar.scopes.get(span.scope.index()))
68 +
        .map(String::as_str)
69 +
        .collect::<Vec<_>>();
70 +
71 +
    assert!(scopes.contains(&"string.quoted.single.js"));
72 +
    assert!(scopes.contains(&"storage.type.function.js"));
73 +
}
74 +
28 75
#[test]
29 76
fn html_embeds_javascript_scopes() {
30 77
    let html: rustik_highlight::RawGrammar = syntaxes::HTML.parse().unwrap();
31 78
    let css: rustik_highlight::RawGrammar = syntaxes::CSS.parse().unwrap();
32 79
    let javascript: rustik_highlight::RawGrammar = syntaxes::JAVASCRIPT.parse().unwrap();