highlight: Simplify registry API
177209ae88d66e026b71cd789335ca36be289c98
Removes redundant registry helpers and keeps grammar and theme lookup on the registry itself.
1 parent
dbd571bf
crates/diff/src/highlight.rs
+4 -4
| 201 | 201 | return Vec::new(); |
|
| 202 | 202 | }; |
|
| 203 | 203 | let Ok(grammar) = self.registry.query(syntax::GrammarQuery { |
|
| 204 | 204 | syntax: (first.key.syntax != "auto").then_some(first.key.syntax.as_str()), |
|
| 205 | 205 | path: first.path.as_deref(), |
|
| 206 | - | first_line: first.source.text.lines().next(), |
|
| 206 | + | head: first.source.text.lines().next(), |
|
| 207 | 207 | }) else { |
|
| 208 | 208 | return requests.into_iter().map(HighlightResult::plain).collect(); |
|
| 209 | 209 | }; |
|
| 210 | 210 | let theme_name = if first.key.theme == "auto" { |
|
| 211 | 211 | self.default_theme.as_str() |
| 253 | 253 | } |
|
| 254 | 254 | } |
|
| 255 | 255 | ||
| 256 | 256 | impl Default for BuiltinHighlighter { |
|
| 257 | 257 | fn default() -> Self { |
|
| 258 | - | let mut registry = syntax::Registry::with_builtin_syntaxes(); |
|
| 259 | - | if registry.add_theme_json(BUILTIN_THEME).is_err() { |
|
| 260 | - | registry = syntax::Registry::with_builtin_syntaxes(); |
|
| 258 | + | let mut registry = syntax::Registry::default(); |
|
| 259 | + | if let Ok(theme) = BUILTIN_THEME.parse::<syntax::Theme>() { |
|
| 260 | + | registry.add_theme(theme); |
|
| 261 | 261 | } |
|
| 262 | 262 | Self::new(registry, BUILTIN_THEME_NAME) |
|
| 263 | 263 | } |
|
| 264 | 264 | } |
|
| 265 | 265 |
crates/highlight/src/grammar.rs
+10 -10
| 10 | 10 | use std::str::FromStr; |
|
| 11 | 11 | ||
| 12 | 12 | use onig::{RegSet, RegSetLead, Regex, Region, SearchOptions}; |
|
| 13 | 13 | ||
| 14 | 14 | use crate::json; |
|
| 15 | - | use crate::raw::{RawCapture, RawGrammar, RawPattern, first_line_patterns}; |
|
| 15 | + | use crate::raw::{RawCapture, RawGrammar, RawPattern, head_patterns}; |
|
| 16 | 16 | use crate::util::{key, next_char_boundary, path_keys, trim_line_end}; |
|
| 17 | 17 | use crate::{Error, MAX_INCLUDE_DEPTH}; |
|
| 18 | 18 | ||
| 19 | 19 | mod end; |
|
| 20 | 20 | mod pattern; |
| 87 | 87 | pub scopes: Vec<String>, |
|
| 88 | 88 | /// Tokenization strategy used by this grammar. |
|
| 89 | 89 | pub kind: GrammarKind, |
|
| 90 | 90 | /// Lowercase file extensions, file names, and aliases matched by this grammar. |
|
| 91 | 91 | file_types: HashSet<String>, |
|
| 92 | - | /// Regexes used to match a source file's first line. |
|
| 93 | - | first_line_match: Vec<Regex>, |
|
| 92 | + | /// Regexes used to match a source file's head line. |
|
| 93 | + | head_match: Vec<Regex>, |
|
| 94 | 94 | /// Root pattern set for TextMate tokenization. |
|
| 95 | 95 | patterns: PatternSet, |
|
| 96 | 96 | /// Path of pattern indices into `patterns`, indexed by rule id. |
|
| 97 | 97 | rule_paths: Vec<Box<[usize]>>, |
|
| 98 | 98 | } |
| 110 | 110 | /// Compiles a raw grammar. |
|
| 111 | 111 | pub fn compile(raw: &RawGrammar) -> Result<Self, Error> { |
|
| 112 | 112 | let mut next_rule = 0; |
|
| 113 | 113 | let mut interner = ScopeInterner::default(); |
|
| 114 | 114 | let patterns = PatternSet::compile(raw, &raw.patterns, &mut next_rule, 0, &mut interner)?; |
|
| 115 | - | let first_line_match = first_line_patterns(raw.first_line_match.as_ref()) |
|
| 115 | + | let head_match = head_patterns(raw.first_line_match.as_ref()) |
|
| 116 | 116 | .iter() |
|
| 117 | 117 | .map(|pattern| Regex::new(pattern).map_err(|_| Error::InvalidRegex(pattern.clone()))) |
|
| 118 | 118 | .collect::<Result<Vec<_>, _>>()?; |
|
| 119 | 119 | let rule_paths = patterns.collect_rule_paths(next_rule); |
|
| 120 | 120 |
| 127 | 127 | .unwrap_or_default() |
|
| 128 | 128 | .iter() |
|
| 129 | 129 | .chain(std::iter::once(&raw.name)) |
|
| 130 | 130 | .map(|item| key(item)) |
|
| 131 | 131 | .collect(), |
|
| 132 | - | first_line_match, |
|
| 132 | + | head_match, |
|
| 133 | 133 | patterns, |
|
| 134 | 134 | rule_paths, |
|
| 135 | 135 | scopes: interner.scopes, |
|
| 136 | 136 | kind: GrammarKind::TextMate, |
|
| 137 | 137 | }) |
| 141 | 141 | pub fn plain_text() -> Self { |
|
| 142 | 142 | Self { |
|
| 143 | 143 | name: PLAIN_TEXT_NAME.to_owned(), |
|
| 144 | 144 | scope_name: "text.plain".to_owned(), |
|
| 145 | 145 | file_types: ["txt".to_owned(), "text".to_owned()].into_iter().collect(), |
|
| 146 | - | first_line_match: Vec::new(), |
|
| 146 | + | head_match: Vec::new(), |
|
| 147 | 147 | patterns: PatternSet::empty(), |
|
| 148 | 148 | rule_paths: Vec::new(), |
|
| 149 | 149 | scopes: Vec::new(), |
|
| 150 | 150 | kind: GrammarKind::TextMate, |
|
| 151 | 151 | } |
| 159 | 159 | pub fn json() -> Self { |
|
| 160 | 160 | Self { |
|
| 161 | 161 | name: "JSON".to_owned(), |
|
| 162 | 162 | scope_name: "source.json".to_owned(), |
|
| 163 | 163 | file_types: ["json".to_owned()].into_iter().collect(), |
|
| 164 | - | first_line_match: Vec::new(), |
|
| 164 | + | head_match: Vec::new(), |
|
| 165 | 165 | patterns: PatternSet::empty(), |
|
| 166 | 166 | rule_paths: Vec::new(), |
|
| 167 | 167 | scopes: json::SCOPES |
|
| 168 | 168 | .iter() |
|
| 169 | 169 | .map(|scope| (*scope).to_owned()) |
| 182 | 182 | /// Returns true when this grammar matches a file path. |
|
| 183 | 183 | pub fn matches_path(&self, path: &Path) -> bool { |
|
| 184 | 184 | path_keys(path).any(|key| self.file_types.contains(&key)) |
|
| 185 | 185 | } |
|
| 186 | 186 | ||
| 187 | - | /// Returns true when this grammar matches the first source line. |
|
| 188 | - | pub fn matches_first_line(&self, line: &str) -> bool { |
|
| 189 | - | self.first_line_match |
|
| 187 | + | /// Returns true when this grammar matches the head source line. |
|
| 188 | + | pub fn matches_head(&self, line: &str) -> bool { |
|
| 189 | + | self.head_match |
|
| 190 | 190 | .iter() |
|
| 191 | 191 | .any(|regex| regex.find(line).is_some()) |
|
| 192 | 192 | } |
|
| 193 | 193 | ||
| 194 | 194 | /// Tokenizes one line, mutating the caller-owned line state. |
crates/highlight/src/raw.rs
+1 -1
| 150 | 150 | selectors |
|
| 151 | 151 | } |
|
| 152 | 152 | } |
|
| 153 | 153 | ||
| 154 | 154 | /// Normalizes `firstLineMatch` from TextMate's string-or-array shape. |
|
| 155 | - | pub(crate) fn first_line_patterns(value: Option<&serde_json::Value>) -> Vec<String> { |
|
| 155 | + | pub(crate) fn head_patterns(value: Option<&serde_json::Value>) -> Vec<String> { |
|
| 156 | 156 | match value { |
|
| 157 | 157 | Some(serde_json::Value::String(pattern)) => vec![pattern.clone()], |
|
| 158 | 158 | Some(serde_json::Value::Array(values)) => values |
|
| 159 | 159 | .iter() |
|
| 160 | 160 | .filter_map(|value| { |
crates/highlight/src/registry.rs
+20 -42
| 1 | 1 | //! Grammar and theme registry. |
|
| 2 | 2 | //! |
|
| 3 | 3 | //! A registry is the lookup layer between user-facing names, file paths, first |
|
| 4 | - | //! lines, and the compiled grammars/themes used by the highlighter. It keeps |
|
| 4 | + | //! head lines, and the compiled grammars/themes used by the highlighter. It keeps |
|
| 5 | 5 | //! inference policy out of tokenization so callers can load syntaxes once and |
|
| 6 | 6 | //! cheaply select the right one for each buffer. |
|
| 7 | 7 | ||
| 8 | 8 | use std::path::Path; |
|
| 9 | - | use std::str::FromStr; |
|
| 10 | 9 | ||
| 11 | 10 | use crate::Error; |
|
| 12 | 11 | use crate::grammar::{Grammar, PLAIN_TEXT_NAME}; |
|
| 13 | 12 | use crate::theme::Theme; |
|
| 14 | 13 | ||
| 15 | 14 | /// A reusable registry of compiled grammars and themes. |
|
| 16 | - | #[derive(Debug, Default)] |
|
| 15 | + | #[derive(Debug)] |
|
| 17 | 16 | pub struct Registry { |
|
| 18 | 17 | /// Registered grammars. |
|
| 19 | 18 | pub grammars: Vec<Grammar>, |
|
| 20 | 19 | /// Registered themes. |
|
| 21 | 20 | pub themes: Vec<Theme>, |
| 26 | 25 | pub struct GrammarQuery<'a> { |
|
| 27 | 26 | /// Explicit syntax name, root scope, extension, or file-type alias. |
|
| 28 | 27 | pub syntax: Option<&'a str>, |
|
| 29 | 28 | /// Source path used for file-name and extension inference. |
|
| 30 | 29 | pub path: Option<&'a Path>, |
|
| 31 | - | /// First source line used for shebang and modeline inference. |
|
| 32 | - | pub first_line: Option<&'a str>, |
|
| 30 | + | /// Head source line used for shebang and modeline inference. |
|
| 31 | + | pub head: Option<&'a str>, |
|
| 33 | 32 | } |
|
| 34 | 33 | ||
| 35 | 34 | impl Registry { |
|
| 36 | 35 | /// Creates an empty registry. |
|
| 37 | 36 | pub fn new() -> Self { |
|
| 38 | - | Self::default() |
|
| 39 | - | } |
|
| 40 | - | ||
| 41 | - | /// Creates an empty registry with a plain-text fallback grammar. |
|
| 42 | - | pub fn with_plain_text() -> Self { |
|
| 43 | - | let mut registry = Self::new(); |
|
| 44 | - | registry.add_grammar(Grammar::plain_text()); |
|
| 45 | - | registry |
|
| 46 | - | } |
|
| 47 | - | ||
| 48 | - | /// Creates an empty registry with the built-in fast grammars. |
|
| 49 | - | pub fn with_builtin_syntaxes() -> Self { |
|
| 50 | - | let mut registry = Self::with_plain_text(); |
|
| 51 | - | registry.add_grammar(Grammar::json()); |
|
| 52 | - | registry |
|
| 37 | + | Self { |
|
| 38 | + | grammars: Vec::new(), |
|
| 39 | + | themes: Vec::new(), |
|
| 40 | + | } |
|
| 53 | 41 | } |
|
| 54 | 42 | ||
| 55 | 43 | /// Adds a compiled grammar. |
|
| 56 | 44 | pub fn add_grammar(&mut self, grammar: Grammar) { |
|
| 57 | 45 | self.grammars.push(grammar); |
|
| 58 | 46 | } |
|
| 59 | 47 | ||
| 60 | - | /// Adds the dedicated fast JSON grammar. |
|
| 61 | - | pub fn add_json(&mut self) { |
|
| 62 | - | self.add_grammar(Grammar::json()); |
|
| 63 | - | } |
|
| 64 | - | ||
| 65 | - | /// Parses, compiles, and adds a grammar from JSON. |
|
| 66 | - | pub fn add_grammar_json(&mut self, input: &str) -> Result<(), Error> { |
|
| 67 | - | let grammar = Grammar::from_str(input)?; |
|
| 68 | - | self.add_grammar(grammar); |
|
| 69 | - | Ok(()) |
|
| 70 | - | } |
|
| 71 | - | ||
| 72 | 48 | /// Adds a compiled theme. |
|
| 73 | 49 | pub fn add_theme(&mut self, theme: Theme) { |
|
| 74 | 50 | self.themes.push(theme); |
|
| 75 | 51 | } |
|
| 76 | 52 | ||
| 77 | - | /// Parses, compiles, and adds a theme from JSON. |
|
| 78 | - | pub fn add_theme_json(&mut self, input: &str) -> Result<(), Error> { |
|
| 79 | - | let theme = Theme::from_str(input)?; |
|
| 80 | - | self.add_theme(theme); |
|
| 81 | - | Ok(()) |
|
| 82 | - | } |
|
| 83 | - | ||
| 84 | 53 | /// Finds a grammar by name, root scope, extension, or file-type alias. |
|
| 85 | 54 | pub fn grammar_by_name(&self, name: &str) -> Result<&Grammar, Error> { |
|
| 86 | 55 | self.grammars |
|
| 87 | 56 | .iter() |
|
| 88 | 57 | .find(|grammar| grammar.matches_name(name)) |
| 95 | 64 | .iter() |
|
| 96 | 65 | .find(|theme| theme.name.eq_ignore_ascii_case(name)) |
|
| 97 | 66 | .ok_or_else(|| Error::UnknownTheme(name.to_owned())) |
|
| 98 | 67 | } |
|
| 99 | 68 | ||
| 100 | - | /// Infers a grammar from explicit syntax, path, extension, and first line. |
|
| 69 | + | /// Infers a grammar from explicit syntax, path, extension, and head line. |
|
| 101 | 70 | pub fn query(&self, query: GrammarQuery<'_>) -> Result<&Grammar, Error> { |
|
| 102 | 71 | if let Some(syntax) = query.syntax { |
|
| 103 | 72 | return self.grammar_by_name(syntax); |
|
| 104 | 73 | } |
|
| 105 | 74 | if let Some(path) = query.path |
| 108 | 77 | .iter() |
|
| 109 | 78 | .find(|grammar| grammar.matches_path(path)) |
|
| 110 | 79 | { |
|
| 111 | 80 | return Ok(grammar); |
|
| 112 | 81 | } |
|
| 113 | - | if let Some(first_line) = query.first_line |
|
| 82 | + | if let Some(head) = query.head |
|
| 114 | 83 | && let Some(grammar) = self |
|
| 115 | 84 | .grammars |
|
| 116 | 85 | .iter() |
|
| 117 | - | .find(|grammar| grammar.matches_first_line(first_line)) |
|
| 86 | + | .find(|grammar| grammar.matches_head(head)) |
|
| 118 | 87 | { |
|
| 119 | 88 | return Ok(grammar); |
|
| 120 | 89 | } |
|
| 121 | 90 | self.grammar_by_name(PLAIN_TEXT_NAME) |
|
| 122 | 91 | } |
|
| 123 | 92 | } |
|
| 93 | + | ||
| 94 | + | impl Default for Registry { |
|
| 95 | + | fn default() -> Self { |
|
| 96 | + | let mut registry = Self::new(); |
|
| 97 | + | registry.add_grammar(Grammar::plain_text()); |
|
| 98 | + | registry.add_grammar(Grammar::json()); |
|
| 99 | + | registry |
|
| 100 | + | } |
|
| 101 | + | } |
crates/highlight/src/tests.rs
+4 -4
| 40 | 40 | .filter_map(|span| grammar.scopes.get(span.scope.index()).map(String::as_str)) |
|
| 41 | 41 | .collect() |
|
| 42 | 42 | } |
|
| 43 | 43 | ||
| 44 | 44 | #[test] |
|
| 45 | - | fn grammar_parses_textmate_keys_and_first_line_match() { |
|
| 45 | + | fn grammar_parses_textmate_keys_and_head_match() { |
|
| 46 | 46 | let grammar = test_grammar(); |
|
| 47 | 47 | ||
| 48 | 48 | assert_eq!(grammar.name, "Test"); |
|
| 49 | 49 | assert!(grammar.matches_name("test")); |
|
| 50 | 50 | assert!(grammar.matches_path(Path::new("main.test"))); |
|
| 51 | - | assert!(grammar.matches_first_line("#!/usr/bin/env test")); |
|
| 51 | + | assert!(grammar.matches_head("#!/usr/bin/env test")); |
|
| 52 | 52 | } |
|
| 53 | 53 | ||
| 54 | 54 | #[test] |
|
| 55 | 55 | fn tokenizer_preserves_multiline_state_incrementally() { |
|
| 56 | 56 | let grammar = test_grammar(); |
| 497 | 497 | ); |
|
| 498 | 498 | } |
|
| 499 | 499 | ||
| 500 | 500 | #[test] |
|
| 501 | 501 | fn registry_infers_by_path_and_falls_back_to_plain_text() { |
|
| 502 | - | let mut registry = Registry::with_plain_text(); |
|
| 502 | + | let mut registry = Registry::default(); |
|
| 503 | 503 | registry.add_grammar(test_grammar()); |
|
| 504 | 504 | registry.add_theme(test_theme()); |
|
| 505 | 505 | ||
| 506 | 506 | assert_eq!( |
|
| 507 | 507 | registry |
| 579 | 579 | assert!(saw_number); |
|
| 580 | 580 | } |
|
| 581 | 581 | ||
| 582 | 582 | #[test] |
|
| 583 | 583 | fn registry_can_infer_builtin_json_by_path() { |
|
| 584 | - | let registry = Registry::with_builtin_syntaxes(); |
|
| 584 | + | let registry = Registry::default(); |
|
| 585 | 585 | ||
| 586 | 586 | let grammar = registry |
|
| 587 | 587 | .query(GrammarQuery { |
|
| 588 | 588 | path: Some(Path::new("data.json")), |
|
| 589 | 589 | ..GrammarQuery::default() |