highlight: Fix missing `contentScope` support
be4ef1047681b82264e8d067a6f9b24bdf2a7694
1 parent
dcf00514
crates/diff/src/highlight.rs
+18 -0
| 369 | 369 | } |
|
| 370 | 370 | ||
| 371 | 371 | #[cfg(test)] |
|
| 372 | 372 | mod tests { |
|
| 373 | 373 | use super::*; |
|
| 374 | + | use std::str::FromStr; |
|
| 374 | 375 | ||
| 375 | 376 | #[test] |
|
| 376 | 377 | fn source_lines_before_head_are_absent() { |
|
| 377 | 378 | let source = HighlightSource::new("two\nthree\n", 2); |
|
| 378 | 379 | ||
| 379 | 380 | assert_eq!(source.line_index(1), None); |
|
| 380 | 381 | assert_eq!(source.line(1), None); |
|
| 381 | 382 | assert_eq!(source.line_index(2), Some(0)); |
|
| 382 | 383 | assert_eq!(source.line(2), Some("two")); |
|
| 383 | 384 | } |
|
| 385 | + | ||
| 386 | + | #[test] |
|
| 387 | + | fn css_declaration_fragment_uses_property_value_scopes() { |
|
| 388 | + | let grammar = syntax::Grammar::from_str(include_str!("syntaxes/css.json")).unwrap(); |
|
| 389 | + | let mut state = syntax::LineState::default(); |
|
| 390 | + | let line = "background: url(data:image/svg+xml;utf8,icon.svg);"; |
|
| 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(&"meta.property-list.css")); |
|
| 399 | + | assert!(scopes.contains(&"meta.property-value.css")); |
|
| 400 | + | assert!(scopes.contains(&"meta.function.url.css")); |
|
| 401 | + | } |
|
| 384 | 402 | } |
crates/highlight/src/grammar/pattern.rs
+22 -2
| 87 | 87 | pub(super) struct Pattern { |
|
| 88 | 88 | /// Stable rule id used to reopen this pattern from line state. |
|
| 89 | 89 | id: usize, |
|
| 90 | 90 | /// Scope assigned by this pattern. |
|
| 91 | 91 | pub(super) scope: Scope, |
|
| 92 | + | /// Scope assigned to content inside a begin rule. |
|
| 93 | + | pub(super) content_scope: Scope, |
|
| 92 | 94 | /// Per-slot capture-scope mappings. |
|
| 93 | 95 | pub(super) captures: PatternCaptures, |
|
| 94 | 96 | /// Match form (single match or begin/end pair). |
|
| 95 | 97 | body: PatternBody, |
|
| 96 | 98 | /// Pattern set that may match inside this one. |
| 215 | 217 | && start < end |
|
| 216 | 218 | { |
|
| 217 | 219 | spans.push(ScopeSpan { start, end, scope }); |
|
| 218 | 220 | } |
|
| 219 | 221 | } |
|
| 222 | + | ||
| 223 | + | /// Pushes visible name and content scopes for an inner rule region. |
|
| 224 | + | pub(super) fn push_content_visible( |
|
| 225 | + | name: Self, |
|
| 226 | + | content: Self, |
|
| 227 | + | spans: &mut Vec<ScopeSpan>, |
|
| 228 | + | start: usize, |
|
| 229 | + | end: usize, |
|
| 230 | + | ) { |
|
| 231 | + | name.push_visible(spans, start, end); |
|
| 232 | + | content.push_visible(spans, start, end); |
|
| 233 | + | } |
|
| 220 | 234 | } |
|
| 221 | 235 | ||
| 222 | 236 | impl ScopeInterner { |
|
| 223 | 237 | /// Returns the id for a scope, inserting it when needed. |
|
| 224 | 238 | fn intern(&mut self, scope: &str) -> ScopeId { |
| 478 | 492 | self.next_rule += 1; |
|
| 479 | 493 | ||
| 480 | 494 | compiled.push(Pattern { |
|
| 481 | 495 | id, |
|
| 482 | 496 | scope, |
|
| 497 | + | content_scope: Scope::from_raw(pattern.content_name.as_deref(), &mut self.interner), |
|
| 483 | 498 | captures: PatternCaptures { |
|
| 484 | 499 | matched: self.interner.captures(&pattern.captures), |
|
| 485 | 500 | begin: self.interner.captures(&pattern.begin_captures), |
|
| 486 | 501 | end: self.interner.captures(&pattern.end_captures), |
|
| 487 | 502 | }, |
| 683 | 698 | ||
| 684 | 699 | let mut spans = Vec::new(); |
|
| 685 | 700 | self.append_match_spans(&begin, &self.captures.begin, &mut spans); |
|
| 686 | 701 | ||
| 687 | 702 | let (end_byte, open_rule) = if let Some(close) = &same_line_end { |
|
| 688 | - | self.scope |
|
| 689 | - | .push_visible(&mut spans, begin.end, close.matched.start); |
|
| 703 | + | Scope::push_content_visible( |
|
| 704 | + | self.scope, |
|
| 705 | + | self.content_scope, |
|
| 706 | + | &mut spans, |
|
| 707 | + | begin.end, |
|
| 708 | + | close.matched.start, |
|
| 709 | + | ); |
|
| 690 | 710 | spans.extend(close.spans.iter().copied()); |
|
| 691 | 711 | self.append_match_spans(&close.matched, &self.captures.end, &mut spans); |
|
| 692 | 712 | (close.matched.end, None) |
|
| 693 | 713 | } else { |
|
| 694 | 714 | // The begin region is already covered by `append_match_spans` above; |
crates/highlight/src/grammar/tokenize.rs
+31 -8
| 3 | 3 | //! Each step picks the next event in the line — a fresh root-pattern match, a |
|
| 4 | 4 | //! nested match inside the currently open begin/end rule, or that rule's end — |
|
| 5 | 5 | //! emits the spans it produces, and updates the parser state accordingly. |
|
| 6 | 6 | ||
| 7 | 7 | use super::end::{EndRegex, EndRegexCache}; |
|
| 8 | - | use super::pattern::{Match, OpenRule, Pattern, RegexMatch}; |
|
| 8 | + | use super::pattern::{Match, OpenRule, Pattern, RegexMatch, Scope}; |
|
| 9 | 9 | use super::*; |
|
| 10 | 10 | ||
| 11 | 11 | /// Reusable line tokenizer for one compiled grammar. |
|
| 12 | 12 | /// |
|
| 13 | 13 | /// The tokenizer keeps runtime scratch that is not part of immutable grammar |
| 140 | 140 | let nested = |
|
| 141 | 141 | pattern |
|
| 142 | 142 | .nested |
|
| 143 | 143 | .find_next(&self.grammar.pattern_sets, line, pos, &mut self.cache); |
|
| 144 | 144 | let Some(nested) = nested else { |
|
| 145 | - | pattern.scope.push_visible(spans, pos, line.len()); |
|
| 145 | + | Scope::push_content_visible( |
|
| 146 | + | pattern.scope, |
|
| 147 | + | pattern.content_scope, |
|
| 148 | + | spans, |
|
| 149 | + | pos, |
|
| 150 | + | line.len(), |
|
| 151 | + | ); |
|
| 146 | 152 | return line.len(); |
|
| 147 | 153 | }; |
|
| 148 | - | pattern.scope.push_visible(spans, pos, nested.start); |
|
| 149 | - | ||
| 150 | 154 | let next_pos = nested.next_pos_after_open(line); |
|
| 151 | 155 | let opened = nested.open_rule(); |
|
| 152 | 156 | ||
| 157 | + | Scope::push_content_visible(pattern.scope, pattern.content_scope, spans, pos, next_pos); |
|
| 153 | 158 | spans.extend(nested.spans); |
|
| 154 | 159 | ||
| 155 | 160 | if let Some(opened) = opened { |
|
| 156 | 161 | state.stack.push(opened); |
|
| 157 | 162 | } |
| 225 | 230 | spans: &mut Vec<ScopeSpan>, |
|
| 226 | 231 | pattern: &Pattern, |
|
| 227 | 232 | ) -> usize { |
|
| 228 | 233 | match self { |
|
| 229 | 234 | Self::Close(close) => { |
|
| 230 | - | pattern.scope.push_visible(spans, pos, close.start); |
|
| 235 | + | Scope::push_content_visible( |
|
| 236 | + | pattern.scope, |
|
| 237 | + | pattern.content_scope, |
|
| 238 | + | spans, |
|
| 239 | + | pos, |
|
| 240 | + | close.start, |
|
| 241 | + | ); |
|
| 231 | 242 | pattern.append_match_spans(&close, &pattern.captures.end, spans); |
|
| 232 | 243 | state.stack.pop(); |
|
| 233 | 244 | close.next_pos(line) |
|
| 234 | 245 | } |
|
| 235 | 246 | Self::Nested(nested) => { |
|
| 236 | - | pattern.scope.push_visible(spans, pos, nested.start); |
|
| 237 | - | ||
| 238 | 247 | let next_pos = nested.next_pos_after_open(line); |
|
| 248 | + | ||
| 249 | + | Scope::push_content_visible( |
|
| 250 | + | pattern.scope, |
|
| 251 | + | pattern.content_scope, |
|
| 252 | + | spans, |
|
| 253 | + | pos, |
|
| 254 | + | next_pos, |
|
| 255 | + | ); |
|
| 239 | 256 | let opened = nested.open_rule(); |
|
| 240 | 257 | ||
| 241 | 258 | spans.extend(nested.spans); |
|
| 242 | 259 | ||
| 243 | 260 | if let Some(opened) = opened { |
|
| 244 | 261 | state.stack.push(opened); |
|
| 245 | 262 | } |
|
| 246 | 263 | next_pos |
|
| 247 | 264 | } |
|
| 248 | 265 | Self::None => { |
|
| 249 | - | pattern.scope.push_visible(spans, pos, line.len()); |
|
| 266 | + | Scope::push_content_visible( |
|
| 267 | + | pattern.scope, |
|
| 268 | + | pattern.content_scope, |
|
| 269 | + | spans, |
|
| 270 | + | pos, |
|
| 271 | + | line.len(), |
|
| 272 | + | ); |
|
| 250 | 273 | line.len() |
|
| 251 | 274 | } |
|
| 252 | 275 | } |
|
| 253 | 276 | } |
|
| 254 | 277 | } |
crates/highlight/src/raw.rs
+3 -0
| 40 | 40 | /// A parsed, uncompiled TextMate pattern. |
|
| 41 | 41 | #[derive(Clone, Debug, Default, Deserialize)] |
|
| 42 | 42 | pub struct RawPattern { |
|
| 43 | 43 | /// Scope assigned to the match. |
|
| 44 | 44 | pub name: Option<String>, |
|
| 45 | + | /// Scope assigned to a begin rule's inner content. |
|
| 46 | + | #[serde(rename = "contentName")] |
|
| 47 | + | pub content_name: Option<String>, |
|
| 45 | 48 | /// Single-line match expression. |
|
| 46 | 49 | #[serde(rename = "match")] |
|
| 47 | 50 | pub match_rule: Option<String>, |
|
| 48 | 51 | /// Begin expression for a stateful rule. |
|
| 49 | 52 | pub begin: Option<String>, |
crates/highlight/src/tests.rs
+30 -0
| 243 | 243 | ||
| 244 | 244 | assert!(scopes.contains(&"meta.word.test")); |
|
| 245 | 245 | assert!(scopes.contains(&"entity.name.function.test")); |
|
| 246 | 246 | } |
|
| 247 | 247 | ||
| 248 | + | #[test] |
|
| 249 | + | fn content_name_scopes_begin_rule_content() { |
|
| 250 | + | let grammar = Grammar::from_str( |
|
| 251 | + | r#"{ |
|
| 252 | + | "name":"Test", |
|
| 253 | + | "scopeName":"source.test", |
|
| 254 | + | "patterns":[ |
|
| 255 | + | { |
|
| 256 | + | "begin":"\\{", |
|
| 257 | + | "end":"\\}", |
|
| 258 | + | "name":"meta.block.test", |
|
| 259 | + | "contentName":"meta.block.content.test", |
|
| 260 | + | "patterns":[{"match":"\\bx\\b","name":"variable.other.test"}] |
|
| 261 | + | } |
|
| 262 | + | ], |
|
| 263 | + | "repository":{} |
|
| 264 | + | }"#, |
|
| 265 | + | ) |
|
| 266 | + | .unwrap(); |
|
| 267 | + | let mut state = LineState::default(); |
|
| 268 | + | ||
| 269 | + | let _ = grammar.tokenize_line(&mut state, "{"); |
|
| 270 | + | let spans = grammar.tokenize_line(&mut state, "x"); |
|
| 271 | + | let scopes = scope_names(&grammar, &spans); |
|
| 272 | + | ||
| 273 | + | assert!(scopes.contains(&"meta.block.test")); |
|
| 274 | + | assert!(scopes.contains(&"meta.block.content.test")); |
|
| 275 | + | assert!(scopes.contains(&"variable.other.test")); |
|
| 276 | + | } |
|
| 277 | + | ||
| 248 | 278 | #[test] |
|
| 249 | 279 | fn line_buffer_reuses_storage_for_incremental_highlighting() { |
|
| 250 | 280 | let grammar = test_grammar(); |
|
| 251 | 281 | let theme = test_theme(); |
|
| 252 | 282 | let mut state = LineState::default(); |