highlight: Simplify tokenizer event handling
e5e534c85d308e1954adb20dfd99c68a10bf0b16
Inline the one-use event context in the TextMate tokenizer and expose internal match fields directly inside the grammar module. This keeps the tokenizer behavior the same while removing wrapper code from the branch. Assisted-by: Codex:gpt-5.5
1 parent
40a52e23
crates/diff/src/highlight.rs
+3 -4
| 177 | 177 | pub fn highlight(&mut self, request: HighlightRequest) -> HighlightResult { |
|
| 178 | 178 | self.highlight_group(vec![request]).remove(0) |
|
| 179 | 179 | } |
|
| 180 | 180 | ||
| 181 | 181 | /// Highlights multiple requests. |
|
| 182 | - | pub fn highlight_requests<I>(&mut self, requests: I) -> impl Iterator<Item = HighlightResult> |
|
| 182 | + | pub fn highlight_requests<I>(&mut self, requests: I) -> Vec<HighlightResult> |
|
| 183 | 183 | where |
|
| 184 | 184 | I: IntoIterator<Item = HighlightRequest>, |
|
| 185 | 185 | { |
|
| 186 | 186 | let mut batches: BTreeMap<HighlightBatchKey, Vec<HighlightRequest>> = BTreeMap::new(); |
|
| 187 | 187 | for request in requests { |
| 192 | 192 | } |
|
| 193 | 193 | ||
| 194 | 194 | batches |
|
| 195 | 195 | .into_values() |
|
| 196 | 196 | .flat_map(|requests| self.highlight_group(requests)) |
|
| 197 | - | .collect::<Vec<_>>() |
|
| 198 | - | .into_iter() |
|
| 197 | + | .collect() |
|
| 199 | 198 | } |
|
| 200 | 199 | ||
| 201 | 200 | /// Highlights grouped requests with one range highlighter. |
|
| 202 | 201 | fn highlight_group(&mut self, requests: Vec<HighlightRequest>) -> Vec<HighlightResult> { |
|
| 203 | 202 | let Some(first) = requests.first() else { |
| 253 | 252 | } |
|
| 254 | 253 | } |
|
| 255 | 254 | ||
| 256 | 255 | impl DiffHighlighter for BuiltinHighlighter { |
|
| 257 | 256 | fn highlight_batch(&mut self, requests: Vec<HighlightRequest>) -> Vec<HighlightResult> { |
|
| 258 | - | self.highlight_requests(requests).collect() |
|
| 257 | + | self.highlight_requests(requests) |
|
| 259 | 258 | } |
|
| 260 | 259 | } |
|
| 261 | 260 | ||
| 262 | 261 | impl HighlightRequest { |
|
| 263 | 262 | /// Returns the unhighlighted source row for this request. |
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/src/grammar/pattern.rs
+7 -30
| 134 | 134 | /// scope spans it emitted, and any begin/end rule it pushed onto the stack. |
|
| 135 | 135 | pub(super) struct Match { |
|
| 136 | 136 | /// Start byte of the match in the line. |
|
| 137 | 137 | pub(super) start: usize, |
|
| 138 | 138 | /// End byte of the match (or of the same-line close, for begin/end rules). |
|
| 139 | - | end: usize, |
|
| 139 | + | pub(super) end: usize, |
|
| 140 | 140 | /// Scope spans emitted by the match and its captures. |
|
| 141 | 141 | pub(super) spans: Vec<ScopeSpan>, |
|
| 142 | 142 | /// New rule pushed onto the parser stack, when this match opened one. |
|
| 143 | - | open_rule: Option<OpenRule>, |
|
| 143 | + | pub(super) open_rule: Option<OpenRule>, |
|
| 144 | 144 | } |
|
| 145 | 145 | ||
| 146 | 146 | /// Shared inputs for finding the next pattern. |
|
| 147 | 147 | pub(super) struct PatternSearch<'line, 'sets, 'cache> { |
|
| 148 | 148 | /// Pattern sets in the compiled grammar. |
| 218 | 218 | /// Raw regex hit: byte range plus the positions of any retained capture groups. |
|
| 219 | 219 | pub(super) struct RegexMatch { |
|
| 220 | 220 | /// Start byte of the full regex match. |
|
| 221 | 221 | pub(super) start: usize, |
|
| 222 | 222 | /// End byte of the full regex match. |
|
| 223 | - | end: usize, |
|
| 223 | + | pub(super) end: usize, |
|
| 224 | 224 | /// Byte ranges for capture groups by capture index, when captures were retained. |
|
| 225 | 225 | captures: Vec<Option<(usize, usize)>>, |
|
| 226 | 226 | } |
|
| 227 | 227 | ||
| 228 | 228 | /// Compiled grammar pattern arena and indexes. |
| 354 | 354 | // land on the boundary; stop before emitting spans past the capture. |
|
| 355 | 355 | if matched.start >= end { |
|
| 356 | 356 | break; |
|
| 357 | 357 | } |
|
| 358 | 358 | let next_pos = matched.next_pos(context.line).min(end); |
|
| 359 | - | spans.extend( |
|
| 360 | - | matched |
|
| 361 | - | .spans |
|
| 362 | - | .into_iter() |
|
| 363 | - | .filter_map(|span| clamp_scope_span(span, end)), |
|
| 364 | - | ); |
|
| 359 | + | spans.extend(matched.spans.into_iter().filter_map(|mut span| { |
|
| 360 | + | span.end = span.end.min(end); |
|
| 361 | + | (span.start < span.end).then_some(span) |
|
| 362 | + | })); |
|
| 365 | 363 | pos = next_pos; |
|
| 366 | 364 | } |
|
| 367 | 365 | } |
|
| 368 | 366 | } |
|
| 369 | 367 | ||
| 370 | - | /// Clamps a span to a retokenized capture's end byte. |
|
| 371 | - | fn clamp_scope_span(mut span: ScopeSpan, end: usize) -> Option<ScopeSpan> { |
|
| 372 | - | span.end = span.end.min(end); |
|
| 373 | - | (span.start < span.end).then_some(span) |
|
| 374 | - | } |
|
| 375 | - | ||
| 376 | 368 | impl ScopeInterner { |
|
| 377 | 369 | /// Returns the id for a scope, inserting it when needed. |
|
| 378 | 370 | fn intern(&mut self, scope: &str) -> ScopeId { |
|
| 379 | 371 | if let Some(id) = self.ids.get(scope) { |
|
| 380 | 372 | return *id; |
| 982 | 974 | self.start |
|
| 983 | 975 | } else { |
|
| 984 | 976 | self.next_pos(line) |
|
| 985 | 977 | } |
|
| 986 | 978 | } |
|
| 987 | - | ||
| 988 | - | /// Returns the open rule pushed onto the parser stack by this match, if any. |
|
| 989 | - | pub(super) fn open_rule(&self) -> Option<OpenRule> { |
|
| 990 | - | self.open_rule.clone() |
|
| 991 | - | } |
|
| 992 | - | ||
| 993 | - | /// Returns the byte position where this match ended. |
|
| 994 | - | pub(super) fn end(&self) -> usize { |
|
| 995 | - | self.end |
|
| 996 | - | } |
|
| 997 | 979 | } |
|
| 998 | 980 | ||
| 999 | 981 | impl RegexMatch { |
|
| 1000 | 982 | /// Finds one regex match starting at `pos`. |
|
| 1001 | 983 | pub(super) fn find( |
| 1075 | 1057 | /// Returns the next byte position after this regex match. |
|
| 1076 | 1058 | pub(super) fn next_pos(&self, line: &str) -> usize { |
|
| 1077 | 1059 | next_pos_after(line, self.start, self.end) |
|
| 1078 | 1060 | } |
|
| 1079 | 1061 | ||
| 1080 | - | /// Returns the byte position where this match ended. |
|
| 1081 | - | pub(super) fn end(&self) -> usize { |
|
| 1082 | - | self.end |
|
| 1083 | - | } |
|
| 1084 | - | ||
| 1085 | 1062 | /// Returns the captured text for one group, or `None` when the group is unset. |
|
| 1086 | 1063 | pub(super) fn capture_text<'a>(&self, index: usize, line: &'a str) -> Option<&'a str> { |
|
| 1087 | 1064 | let (start, end) = self.captures.get(index)?.as_ref().copied()?; |
|
| 1088 | 1065 | line.get(start..end) |
|
| 1089 | 1066 | } |
crates/highlight/src/grammar/tokenize.rs
+36 -83
| 105 | 105 | ); |
|
| 106 | 106 | let Some(found) = self.grammar.patterns.find_next(&mut search, pos) else { |
|
| 107 | 107 | return line.len(); |
|
| 108 | 108 | }; |
|
| 109 | 109 | let next_pos = found.next_pos_after_open(line); |
|
| 110 | - | let opened = found.open_rule(); |
|
| 111 | - | let anchor = found.end(); |
|
| 110 | + | let opened = found.open_rule; |
|
| 111 | + | let anchor = found.end; |
|
| 112 | 112 | spans.extend(found.spans); |
|
| 113 | 113 | ||
| 114 | 114 | if let Some(opened) = opened { |
|
| 115 | 115 | state.stack.push(opened); |
|
| 116 | 116 | anchors.set_begin_position(anchor); |
| 136 | 136 | return self.advance_inside_while_rule(state, line, pos, spans, pattern, anchors); |
|
| 137 | 137 | }; |
|
| 138 | 138 | let event = self.next_event_inside(pattern, &end, line, pos, *anchors); |
|
| 139 | 139 | let next_pos = event.next_pos(line); |
|
| 140 | 140 | // Parent rules wrap the whole span consumed by this step; the current |
|
| 141 | - | // rule's own content scope is emitted per-event by `InsideEvent::apply`. |
|
| 141 | + | // rule's own content scope is emitted below for each event. |
|
| 142 | 142 | if let Some((_current, parents)) = state.stack.split_last() { |
|
| 143 | 143 | for open in parents { |
|
| 144 | 144 | if let Some(pattern) = self.grammar.pattern_by_rule(open.rule_id) { |
|
| 145 | 145 | pattern.push_content_visible(spans, pos, next_pos); |
|
| 146 | 146 | } |
|
| 147 | 147 | } |
|
| 148 | 148 | } |
|
| 149 | - | let mut context = EventContext { |
|
| 150 | - | state, |
|
| 151 | - | line, |
|
| 152 | - | spans, |
|
| 153 | - | pattern, |
|
| 154 | - | sets: &self.grammar.pattern_sets, |
|
| 155 | - | cache: &mut self.cache, |
|
| 156 | - | anchors, |
|
| 157 | - | }; |
|
| 158 | - | event.apply(pos, next_pos, &mut context) |
|
| 149 | + | ||
| 150 | + | match event { |
|
| 151 | + | InsideEvent::Close(close) => { |
|
| 152 | + | pattern.push_content_visible(spans, pos, close.start); |
|
| 153 | + | let mut span_context = |
|
| 154 | + | SpanContext::new(&self.grammar.pattern_sets, line, &mut self.cache, *anchors); |
|
| 155 | + | pattern.append_match_spans(&close, &pattern.captures.end, spans, &mut span_context); |
|
| 156 | + | if let Some(closed) = state.stack.pop() { |
|
| 157 | + | anchors.restore_begin_position(closed.anchor_position); |
|
| 158 | + | } |
|
| 159 | + | } |
|
| 160 | + | InsideEvent::Nested(nested) => { |
|
| 161 | + | let anchor = nested.end; |
|
| 162 | + | let opened = nested.open_rule; |
|
| 163 | + | ||
| 164 | + | pattern.push_content_visible(spans, pos, next_pos); |
|
| 165 | + | spans.extend(nested.spans); |
|
| 166 | + | ||
| 167 | + | if let Some(opened) = opened { |
|
| 168 | + | state.stack.push(opened); |
|
| 169 | + | anchors.set_begin_position(anchor); |
|
| 170 | + | } |
|
| 171 | + | } |
|
| 172 | + | InsideEvent::None => { |
|
| 173 | + | pattern.push_content_visible(spans, pos, next_pos); |
|
| 174 | + | } |
|
| 175 | + | } |
|
| 176 | + | next_pos |
|
| 159 | 177 | } |
|
| 160 | 178 | ||
| 161 | 179 | /// Checks active begin/while rules before scanning a continued line. |
|
| 162 | 180 | fn check_while_conditions( |
|
| 163 | 181 | &mut self, |
| 190 | 208 | &matched, |
|
| 191 | 209 | &pattern.captures.while_rule, |
|
| 192 | 210 | spans, |
|
| 193 | 211 | &mut context, |
|
| 194 | 212 | ); |
|
| 195 | - | anchors.set_begin_position(matched.end()); |
|
| 196 | - | pos = pos.max(matched.end()); |
|
| 213 | + | anchors.set_begin_position(matched.end); |
|
| 214 | + | pos = pos.max(matched.end); |
|
| 197 | 215 | index += 1; |
|
| 198 | 216 | } |
|
| 199 | 217 | spans.sort_by_key(|span| (span.start, span.end)); |
|
| 200 | 218 | pos |
|
| 201 | 219 | } |
| 221 | 239 | let Some(nested) = nested else { |
|
| 222 | 240 | pattern.push_content_visible(spans, pos, line.len()); |
|
| 223 | 241 | return line.len(); |
|
| 224 | 242 | }; |
|
| 225 | 243 | let next_pos = nested.next_pos_after_open(line); |
|
| 226 | - | let opened = nested.open_rule(); |
|
| 227 | - | let anchor = nested.end(); |
|
| 244 | + | let opened = nested.open_rule; |
|
| 245 | + | let anchor = nested.end; |
|
| 228 | 246 | ||
| 229 | 247 | pattern.push_content_visible(spans, pos, next_pos); |
|
| 230 | 248 | spans.extend(nested.spans); |
|
| 231 | 249 | ||
| 232 | 250 | if let Some(opened) = opened { |
| 312 | 330 | ||
| 313 | 331 | impl InsideEvent { |
|
| 314 | 332 | /// Returns the byte position this event advances to. |
|
| 315 | 333 | fn next_pos(&self, line: &str) -> usize { |
|
| 316 | 334 | match self { |
|
| 317 | - | Self::Close(close) => close.end(), |
|
| 335 | + | Self::Close(close) => close.end, |
|
| 318 | 336 | Self::Nested(nested) => nested.next_pos_after_open(line), |
|
| 319 | 337 | Self::None => line.len(), |
|
| 320 | 338 | } |
|
| 321 | 339 | } |
|
| 322 | - | ||
| 323 | - | /// Emits the event's spans and updates the parser stack. |
|
| 324 | - | /// |
|
| 325 | - | /// `next_pos` is the position the event advances to, as returned by |
|
| 326 | - | /// [`InsideEvent::next_pos`]; it is also this function's return value. |
|
| 327 | - | fn apply(self, pos: usize, next_pos: usize, context: &mut EventContext<'_>) -> usize { |
|
| 328 | - | match self { |
|
| 329 | - | Self::Close(close) => { |
|
| 330 | - | context |
|
| 331 | - | .pattern |
|
| 332 | - | .push_content_visible(context.spans, pos, close.start); |
|
| 333 | - | let mut span_context = |
|
| 334 | - | SpanContext::new(context.sets, context.line, context.cache, *context.anchors); |
|
| 335 | - | context.pattern.append_match_spans( |
|
| 336 | - | &close, |
|
| 337 | - | &context.pattern.captures.end, |
|
| 338 | - | context.spans, |
|
| 339 | - | &mut span_context, |
|
| 340 | - | ); |
|
| 341 | - | if let Some(closed) = context.state.stack.pop() { |
|
| 342 | - | context |
|
| 343 | - | .anchors |
|
| 344 | - | .restore_begin_position(closed.anchor_position); |
|
| 345 | - | } |
|
| 346 | - | } |
|
| 347 | - | Self::Nested(nested) => { |
|
| 348 | - | let anchor = nested.end(); |
|
| 349 | - | let opened = nested.open_rule(); |
|
| 350 | - | ||
| 351 | - | context |
|
| 352 | - | .pattern |
|
| 353 | - | .push_content_visible(context.spans, pos, next_pos); |
|
| 354 | - | context.spans.extend(nested.spans); |
|
| 355 | - | ||
| 356 | - | if let Some(opened) = opened { |
|
| 357 | - | context.state.stack.push(opened); |
|
| 358 | - | context.anchors.set_begin_position(anchor); |
|
| 359 | - | } |
|
| 360 | - | } |
|
| 361 | - | Self::None => { |
|
| 362 | - | context |
|
| 363 | - | .pattern |
|
| 364 | - | .push_content_visible(context.spans, pos, next_pos); |
|
| 365 | - | } |
|
| 366 | - | } |
|
| 367 | - | next_pos |
|
| 368 | - | } |
|
| 369 | - | } |
|
| 370 | - | ||
| 371 | - | /// Mutable context used while applying an inside-rule event. |
|
| 372 | - | struct EventContext<'a> { |
|
| 373 | - | /// Parser state being updated. |
|
| 374 | - | state: &'a mut LineState, |
|
| 375 | - | /// Line being tokenized. |
|
| 376 | - | line: &'a str, |
|
| 377 | - | /// Scope spans emitted for the current line. |
|
| 378 | - | spans: &'a mut Vec<ScopeSpan>, |
|
| 379 | - | /// Pattern whose body is currently active. |
|
| 380 | - | pattern: &'a Pattern, |
|
| 381 | - | /// Pattern sets in the compiled grammar. |
|
| 382 | - | sets: &'a [PatternSet], |
|
| 383 | - | /// Cache for dynamically resolved end regexes. |
|
| 384 | - | cache: &'a mut EndRegexCache, |
|
| 385 | - | /// Anchor availability for subsequent matches. |
|
| 386 | - | anchors: &'a mut AnchorState, |
|
| 387 | 340 | } |