highlight: Support Markdown frontmatter embeds
565b66faedd695c1b78e6417fe33eefe48f6e509
Resolve TextMate begin/while rules the same way as begin/end rules when their while pattern contains begin-capture backreferences. Keep begin/while containers even when their state scope comes from contentName, which lets Markdown frontmatter close before normal body text. Wire Markdown to the bundled YAML, HTML, CSS, and JavaScript grammars so frontmatter and embedded HTML includes can resolve without enabling the broad HTML derivative grammar. Assisted-by: Codex:gpt-5.5
1 parent
b541deb0
crates/diff/src/bin/rustik-diff/builtins.rs
+6 -1
| 52 | 52 | dependencies: &[], |
|
| 53 | 53 | }, |
|
| 54 | 54 | SyntaxAsset { |
|
| 55 | 55 | source: syntax::syntaxes::MARKDOWN, |
|
| 56 | 56 | file_types: &["md", "markdown", "mdown"], |
|
| 57 | - | dependencies: &[], |
|
| 57 | + | dependencies: &[ |
|
| 58 | + | syntax::syntaxes::YAML, |
|
| 59 | + | syntax::syntaxes::HTML, |
|
| 60 | + | syntax::syntaxes::CSS, |
|
| 61 | + | syntax::syntaxes::JAVASCRIPT, |
|
| 62 | + | ], |
|
| 58 | 63 | }, |
|
| 59 | 64 | SyntaxAsset { |
|
| 60 | 65 | source: syntax::syntaxes::PYTHON, |
|
| 61 | 66 | file_types: &["py", "pyw"], |
|
| 62 | 67 | dependencies: &[], |
crates/highlight/src/grammar/pattern.rs
+31 -25
| 71 | 71 | /// Stateful begin/while rule. |
|
| 72 | 72 | BeginWhile { |
|
| 73 | 73 | /// Begin regex source (also the entry inside the parent set's RegSet). |
|
| 74 | 74 | begin_source: String, |
|
| 75 | 75 | /// While regex tested at the start of each continued line. |
|
| 76 | - | while_regex: Regex, |
|
| 76 | + | while_rule: EndPattern, |
|
| 77 | 77 | }, |
|
| 78 | 78 | } |
|
| 79 | 79 | ||
| 80 | 80 | /// Stable arena id for a compiled pattern set. |
|
| 81 | 81 | #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd)] |
| 127 | 127 | ||
| 128 | 128 | /// Begin/end rule pushed onto the parser stack and held across line boundaries. |
|
| 129 | 129 | /// |
|
| 130 | 130 | /// The matching [`Pattern`] is recovered through `Grammar::pattern_by_rule`, so |
|
| 131 | 131 | /// the open rule itself only stores what cannot be derived from the grammar: |
|
| 132 | - | /// the rule's id and the resolved end-regex source for dynamic end patterns. |
|
| 132 | + | /// the rule's id and any resolved regex source for dynamic end or while |
|
| 133 | + | /// patterns. |
|
| 133 | 134 | #[derive(Clone, Debug, Eq, PartialEq)] |
|
| 134 | 135 | pub(super) struct OpenRule { |
|
| 135 | 136 | /// Compiled rule id. |
|
| 136 | 137 | pub(super) rule_id: usize, |
|
| 137 | - | /// Resolved end-regex source (populated only for dynamic end patterns). |
|
| 138 | - | pub(super) dynamic_end: Option<String>, |
|
| 138 | + | /// Resolved regex source for dynamic end or while patterns. |
|
| 139 | + | pub(super) dynamic_rule: Option<String>, |
|
| 139 | 140 | /// Parent `\G` anchor restored when this rule closes. |
|
| 140 | 141 | pub(super) anchor_position: Option<usize>, |
|
| 141 | 142 | } |
|
| 142 | 143 | ||
| 143 | 144 | /// Result of one pattern matching in a line: the byte range it covered, the |
| 340 | 341 | } else { |
|
| 341 | 342 | Self::Visible(path) |
|
| 342 | 343 | } |
|
| 343 | 344 | } |
|
| 344 | 345 | ||
| 345 | - | /// Returns true when this scope carries parser state. |
|
| 346 | - | pub(super) fn has_scope(&self) -> bool { |
|
| 347 | - | !matches!(self, Self::None) |
|
| 348 | - | } |
|
| 349 | - | ||
| 350 | 346 | /// Pushes a span for this scope's region when it is visible and non-empty. |
|
| 351 | 347 | pub(super) fn push_visible(&self, spans: &mut Vec<ScopeSpan>, start: usize, end: usize) { |
|
| 352 | 348 | if let Self::Visible(path) = self |
|
| 353 | 349 | && start < end |
|
| 354 | 350 | { |
| 676 | 672 | end, |
|
| 677 | 673 | apply_end_pattern_last: truthy_flag(pattern.apply_end_pattern_last.as_ref()), |
|
| 678 | 674 | } |
|
| 679 | 675 | } |
|
| 680 | 676 | (None, Some(begin), None, Some(while_rule)) => { |
|
| 681 | - | if !scope.has_scope() { |
|
| 682 | - | self.inline_patterns(current, pattern, repositories, compiled, includes)?; |
|
| 683 | - | return Ok(()); |
|
| 684 | - | } |
|
| 685 | 677 | if Regex::new(begin).is_err() { |
|
| 686 | 678 | return Ok(()); |
|
| 687 | 679 | } |
|
| 688 | - | let Ok(while_regex) = Regex::new(while_rule) else { |
|
| 680 | + | let Some(while_rule) = EndPattern::compile(while_rule) else { |
|
| 689 | 681 | return Ok(()); |
|
| 690 | 682 | }; |
|
| 691 | 683 | PatternBody::BeginWhile { |
|
| 692 | 684 | begin_source: begin.clone(), |
|
| 693 | - | while_regex, |
|
| 685 | + | while_rule, |
|
| 694 | 686 | } |
|
| 695 | 687 | } |
|
| 696 | 688 | _ => { |
|
| 697 | 689 | self.inline_patterns(current, pattern, repositories, compiled, includes)?; |
|
| 698 | 690 | return Ok(()); |
| 950 | 942 | } |
|
| 951 | 943 | ||
| 952 | 944 | /// Returns the active end regex when reopening this pattern from line state. |
|
| 953 | 945 | pub(super) fn resume_end<'pat>( |
|
| 954 | 946 | &'pat self, |
|
| 955 | - | dynamic_end: Option<&str>, |
|
| 947 | + | dynamic_rule: Option<&str>, |
|
| 956 | 948 | cache: &mut EndRegexCache, |
|
| 957 | 949 | ) -> Option<EndRegex<'pat>> { |
|
| 958 | 950 | let PatternBody::BeginEnd { end, .. } = &self.body else { |
|
| 959 | 951 | return None; |
|
| 960 | 952 | }; |
|
| 961 | - | end.resume(dynamic_end, cache) |
|
| 953 | + | end.resume(dynamic_rule, cache) |
|
| 962 | 954 | } |
|
| 963 | 955 | ||
| 964 | 956 | /// Finds this rule's while match at the start of a continued line. |
|
| 965 | - | pub(super) fn while_match(&self, line: &str, anchors: AnchorState) -> Option<RegexMatch> { |
|
| 966 | - | let PatternBody::BeginWhile { while_regex, .. } = &self.body else { |
|
| 957 | + | pub(super) fn while_match( |
|
| 958 | + | &self, |
|
| 959 | + | dynamic_while: Option<&str>, |
|
| 960 | + | line: &str, |
|
| 961 | + | cache: &mut EndRegexCache, |
|
| 962 | + | anchors: AnchorState, |
|
| 963 | + | ) -> Option<RegexMatch> { |
|
| 964 | + | let PatternBody::BeginWhile { while_rule, .. } = &self.body else { |
|
| 967 | 965 | return None; |
|
| 968 | 966 | }; |
|
| 967 | + | let while_regex = while_rule.resume(dynamic_while, cache)?; |
|
| 969 | 968 | let matched = RegexMatch::find( |
|
| 970 | - | while_regex, |
|
| 969 | + | while_regex.regex(), |
|
| 971 | 970 | line, |
|
| 972 | 971 | 0, |
|
| 973 | 972 | !self.captures.while_rule.is_empty(), |
|
| 974 | 973 | anchors, |
|
| 975 | 974 | )?; |
| 1048 | 1047 | match &self.body { |
|
| 1049 | 1048 | PatternBody::Match { .. } => !self.captures.matched.is_empty(), |
|
| 1050 | 1049 | PatternBody::BeginEnd { end, .. } => { |
|
| 1051 | 1050 | !self.captures.begin.is_empty() || end.is_dynamic() |
|
| 1052 | 1051 | } |
|
| 1053 | - | PatternBody::BeginWhile { .. } => { |
|
| 1054 | - | !self.captures.begin.is_empty() || !self.captures.while_rule.is_empty() |
|
| 1052 | + | PatternBody::BeginWhile { while_rule, .. } => { |
|
| 1053 | + | !self.captures.begin.is_empty() |
|
| 1054 | + | || !self.captures.while_rule.is_empty() |
|
| 1055 | + | || while_rule.is_dynamic() |
|
| 1055 | 1056 | } |
|
| 1056 | 1057 | } |
|
| 1057 | 1058 | } |
|
| 1058 | 1059 | ||
| 1059 | 1060 | /// The regex source used in the parent set's combined `RegSet`. |
| 1106 | 1107 | end_pattern: &EndPattern, |
|
| 1107 | 1108 | begin: RegexMatch, |
|
| 1108 | 1109 | search: &mut PatternSearch<'_, '_, '_>, |
|
| 1109 | 1110 | ) -> Option<Match> { |
|
| 1110 | 1111 | let end_regex = end_pattern.resolve_for_begin(&begin, search.line, search.cache)?; |
|
| 1111 | - | let dynamic_end = end_regex.dynamic_source(); |
|
| 1112 | + | let dynamic_rule = end_regex.dynamic_source(); |
|
| 1112 | 1113 | let mut spans = Vec::new(); |
|
| 1113 | 1114 | let mut context = search.span_context(); |
|
| 1114 | 1115 | self.append_match_spans(&begin, &self.captures.begin, &mut spans, &mut context); |
|
| 1115 | 1116 | ||
| 1116 | 1117 | Some(Match { |
|
| 1117 | 1118 | start: begin.start, |
|
| 1118 | 1119 | end: begin.end, |
|
| 1119 | 1120 | spans, |
|
| 1120 | 1121 | open_rule: Some(OpenRule { |
|
| 1121 | 1122 | rule_id: self.id, |
|
| 1122 | - | dynamic_end, |
|
| 1123 | + | dynamic_rule, |
|
| 1123 | 1124 | anchor_position: search.anchors.begin_position(), |
|
| 1124 | 1125 | }), |
|
| 1125 | 1126 | }) |
|
| 1126 | 1127 | } |
|
| 1127 | 1128 |
| 1129 | 1130 | fn begin_while_match( |
|
| 1130 | 1131 | &self, |
|
| 1131 | 1132 | begin: RegexMatch, |
|
| 1132 | 1133 | search: &mut PatternSearch<'_, '_, '_>, |
|
| 1133 | 1134 | ) -> Match { |
|
| 1135 | + | let PatternBody::BeginWhile { while_rule, .. } = &self.body else { |
|
| 1136 | + | return self.simple_match(begin, search); |
|
| 1137 | + | }; |
|
| 1138 | + | let while_regex = while_rule.resolve_for_begin(&begin, search.line, search.cache); |
|
| 1139 | + | let dynamic_rule = while_regex.and_then(|while_regex| while_regex.dynamic_source()); |
|
| 1134 | 1140 | let mut spans = Vec::new(); |
|
| 1135 | 1141 | let end = begin.next_pos(search.line); |
|
| 1136 | 1142 | ||
| 1137 | 1143 | let mut context = search.span_context(); |
|
| 1138 | 1144 | self.append_match_spans(&begin, &self.captures.begin, &mut spans, &mut context); |
| 1142 | 1148 | start: begin.start, |
|
| 1143 | 1149 | end, |
|
| 1144 | 1150 | spans, |
|
| 1145 | 1151 | open_rule: Some(OpenRule { |
|
| 1146 | 1152 | rule_id: self.id, |
|
| 1147 | - | dynamic_end: None, |
|
| 1153 | + | dynamic_rule, |
|
| 1148 | 1154 | anchor_position: search.anchors.begin_position(), |
|
| 1149 | 1155 | }), |
|
| 1150 | 1156 | } |
|
| 1151 | 1157 | } |
|
| 1152 | 1158 | } |
crates/highlight/src/grammar/tokenize.rs
+8 -3
| 126 | 126 | ) -> usize { |
|
| 127 | 127 | let Some(pattern) = self.grammar.pattern_by_rule(open.rule_id) else { |
|
| 128 | 128 | state.stack.pop(); |
|
| 129 | 129 | return pos; |
|
| 130 | 130 | }; |
|
| 131 | - | let Some(end) = pattern.resume_end(open.dynamic_end.as_deref(), &mut self.cache) else { |
|
| 131 | + | let Some(end) = pattern.resume_end(open.dynamic_rule.as_deref(), &mut self.cache) else { |
|
| 132 | 132 | return self.advance_inside_while_rule(state, line, pos, spans, pattern, anchors); |
|
| 133 | 133 | }; |
|
| 134 | 134 | let event = self.next_event_inside(pattern, &end, line, pos, *anchors); |
|
| 135 | 135 | let next_pos = event.next_pos(line); |
|
| 136 | 136 | // Parent rules wrap the whole span consumed by this step; the current |
| 185 | 185 | while index < state.stack.len() { |
|
| 186 | 186 | let Some(pattern) = self.grammar.pattern_by_rule(state.stack[index].rule_id) else { |
|
| 187 | 187 | state.stack.truncate(index); |
|
| 188 | 188 | break; |
|
| 189 | 189 | }; |
|
| 190 | - | let Some(matched) = pattern.while_match(line, *anchors) else { |
|
| 190 | + | let Some(matched) = pattern.while_match( |
|
| 191 | + | state.stack[index].dynamic_rule.as_deref(), |
|
| 192 | + | line, |
|
| 193 | + | &mut self.cache, |
|
| 194 | + | *anchors, |
|
| 195 | + | ) else { |
|
| 191 | 196 | if pattern |
|
| 192 | - | .resume_end(state.stack[index].dynamic_end.as_deref(), &mut self.cache) |
|
| 197 | + | .resume_end(state.stack[index].dynamic_rule.as_deref(), &mut self.cache) |
|
| 193 | 198 | .is_none() |
|
| 194 | 199 | { |
|
| 195 | 200 | state.stack.truncate(index); |
|
| 196 | 201 | break; |
|
| 197 | 202 | } |