1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
//! Syntect-backed syntax highlighting for code fences and diff bodies.
//!
//! Parses scopes only; no syntect theme is involved. Each scope region maps
//! onto a [`SyntaxRole`] (or plain). Callers resolve roles against the active
//! palette so highlighting follows fixed, custom, and terminal-sampled themes
//! and so diff add/remove colors can supply their own plain style.
//!
//! Language grammars come from [`two_face`]'s bat-derived dump (defaults plus
//! extras such as TypeScript and TOML), not syntect's smaller default set.
use std::{cell::Cell, path::Path, sync::LazyLock};
use ratatui::{style::Style, text::Span};
use regex::RegexBuilder;
use syntect::parsing::{ParseState, Scope, ScopeStack, SyntaxReference, SyntaxSet};
use super::theme::{SyntaxRole, Theme};
static SYNTAX_SET: LazyLock<SyntaxSet> = LazyLock::new(two_face::syntax::extra_newlines);
/// Soft cap on language-aware lines painted in one tool-card body pass. Beyond
/// this, remaining rows keep solid row colors so huge write/edit cards stay
/// interactive.
pub(in crate::tui) const MAX_TOOL_SYNTAX_LINES: usize = 2_500;
/// Soft cap on bytes per line for language-aware tool-card paint. Longer rows
/// keep solid add/remove/context colors.
///
/// Syntect's Markdown grammar is pathological on dense inline-code spans: a
/// single ~800-byte docs prose line with many `` `backticks` `` can take tens
/// of milliseconds. Expand paints both diff sides on the UI thread, so one
/// long `.md` edit felt like a stall. Line-count caps alone do not catch this.
pub(in crate::tui) const MAX_TOOL_SYNTAX_LINE_BYTES: usize = 256;
// Per-thread counter of syntect line parses (highlight + advance). Thread-local
// so parallel unit tests do not race the measurement.
thread_local! {
static HIGHLIGHT_LINE_CALLS: Cell<usize> = const { Cell::new(0) };
}
/// Scope prefixes in match order: specific selectors before general ones.
static ROLE_SELECTORS: LazyLock<Vec<(Scope, SyntaxRole)>> = LazyLock::new(|| {
[
("entity.name.function", SyntaxRole::Function),
("support.function", SyntaxRole::Function),
("entity.name.type", SyntaxRole::Type),
("support.type", SyntaxRole::Type),
("comment", SyntaxRole::Comment),
("string", SyntaxRole::String),
("constant", SyntaxRole::Constant),
("keyword", SyntaxRole::Keyword),
("storage", SyntaxRole::Keyword),
]
.into_iter()
.map(|(selector, role)| (Scope::new(selector).expect("valid scope selector"), role))
.collect()
});
/// One highlighted span: plain (no role) or a syntax role. Callers map plain to
/// their base style (body text, add/remove color, …).
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::tui) struct HighlightSegment {
pub(in crate::tui) text: String,
pub(in crate::tui) role: Option<SyntaxRole>,
}
impl HighlightSegment {
pub(in crate::tui) fn style(&self, plain: Style) -> Style {
match self.role {
Some(role) => Theme::syntax(role),
None => plain,
}
}
}
/// Stateful highlighter for one source stream. Feed lines in order.
#[derive(Clone)]
pub(in crate::tui) struct BlockHighlighter {
parse: ParseState,
stack: ScopeStack,
}
impl BlockHighlighter {
/// Highlighter for a fence info token such as `rust`, `ts`, or `py`, or
/// `None` when no bundled syntax matches (callers fall back to plain
/// styling).
pub(in crate::tui) fn for_language(token: &str) -> Option<Self> {
Some(Self::from_syntax(
SYNTAX_SET.find_syntax_by_token(canonical_language_token(token))?,
))
}
/// Highlighter from a file path (`src/lib.rs`, `Makefile`, …), or `None`
/// when the path has no bundled syntax.
///
/// Does not read the file; only the path/file name/extension are used so
/// display paths work offline. Callers should strip diff chrome (`a/`,
/// `b/`, rename arrows) before calling.
pub(in crate::tui) fn for_path(path: &str) -> Option<Self> {
Some(Self::from_syntax(syntax_for_path(path)?))
}
fn from_syntax(syntax: &SyntaxReference) -> Self {
Self {
parse: ParseState::new(syntax),
stack: ScopeStack::new(),
}
}
/// Role segments for one source line, without a trailing newline.
pub(in crate::tui) fn highlight_line(&mut self, line: &str) -> Vec<HighlightSegment> {
record_highlight_call();
// Syntect grammars expect the newline to drive state transitions.
let mut text = String::with_capacity(line.len() + 1);
text.push_str(line);
text.push('\n');
let Ok(ops) = self.parse.parse_line(&text, &SYNTAX_SET) else {
return vec![HighlightSegment {
text: line.to_string(),
role: None,
}];
};
let mut segments: Vec<HighlightSegment> = Vec::new();
let mut cursor = 0usize;
for (offset, op) in ops {
let offset = offset.min(line.len());
if offset > cursor {
push_merged(&mut segments, &line[cursor..offset], self.scope_role());
cursor = offset;
}
let _ = self.stack.apply(&op);
}
if cursor < line.len() {
push_merged(&mut segments, &line[cursor..], self.scope_role());
}
if segments.is_empty() {
segments.push(HighlightSegment {
text: String::new(),
role: None,
});
}
segments
}
/// Advance grammar state without allocating role segments.
///
/// Used for the unused side of a unified diff (old stream on context) so
/// multi-line tokens stay aligned without paying segment cost twice.
pub(in crate::tui) fn advance_line(&mut self, line: &str) {
record_highlight_call();
let mut text = String::with_capacity(line.len() + 1);
text.push_str(line);
text.push('\n');
let Ok(ops) = self.parse.parse_line(&text, &SYNTAX_SET) else {
return;
};
for (_, op) in ops {
let _ = self.stack.apply(&op);
}
}
/// Innermost scope that maps onto a role, if any.
fn scope_role(&self) -> Option<SyntaxRole> {
for scope in self.stack.as_slice().iter().rev() {
for (selector, role) in ROLE_SELECTORS.iter() {
if selector.is_prefix_of(*scope) {
return Some(*role);
}
}
}
None
}
}
fn push_merged(segments: &mut Vec<HighlightSegment>, text: &str, role: Option<SyntaxRole>) {
if let Some(last) = segments.last_mut() {
if last.role == role {
last.text.push_str(text);
return;
}
}
segments.push(HighlightSegment {
text: text.to_string(),
role,
});
}
/// Map common fence tags onto tokens the syntax dump actually registers.
///
/// Keep this short: prefer dump-native tokens (`ts`, `tsx`, `bash`) when the
/// author already used them. Only rewrite tags people type that still miss.
fn canonical_language_token(token: &str) -> &str {
match token {
"jsx" => "javascript",
"shell" | "console" => "bash",
other => other,
}
}
/// Resolve a bundled syntax from a display path without opening the file.
///
/// Mirrors syntect's path/extension probe: try the full file name first so
/// names like `Makefile` and `CMakeLists.txt` win, then the extension.
fn syntax_for_path(path: &str) -> Option<&'static SyntaxReference> {
let path = path.trim();
if path.is_empty() || path == "/dev/null" {
return None;
}
let path = Path::new(path);
let file_name = path.file_name()?.to_str()?;
SYNTAX_SET.find_syntax_by_extension(file_name).or_else(|| {
path.extension()
.and_then(|ext| ext.to_str())
.and_then(|ext| SYNTAX_SET.find_syntax_by_extension(ext))
})
}
/// Match pattern and the same semantics the grep tool used when searching.
#[derive(Clone, Debug, PartialEq, Eq)]
pub(in crate::tui) struct MatchQuery {
pub(in crate::tui) pattern: String,
pub(in crate::tui) literal: bool,
pub(in crate::tui) case_sensitive: bool,
}
impl MatchQuery {
pub(in crate::tui) fn new(
pattern: impl Into<String>,
literal: bool,
case_sensitive: bool,
) -> Self {
Self {
pattern: pattern.into(),
literal,
case_sensitive,
}
}
}
/// Byte ranges of pattern matches inside `text` for search-hit overlay.
///
/// Semantics mirror the grep tool: `literal` escapes the pattern before
/// compiling, and `case_sensitive` maps onto regex case-insensitivity so
/// Unicode case pairs (for example `Ä`/`ä`) match the engine that produced the
/// hits.
pub(in crate::tui) fn match_byte_ranges(text: &str, query: &MatchQuery) -> Vec<(usize, usize)> {
let pattern = query.pattern.as_str();
if pattern.is_empty() || text.is_empty() {
return Vec::new();
}
// Same construction as GrepRequest: escape literals, then compile once.
let source = if query.literal {
regex::escape(pattern)
} else {
pattern.to_string()
};
let Ok(re) = RegexBuilder::new(&source)
.case_insensitive(!query.case_sensitive)
.size_limit(1 << 20)
.dfa_size_limit(1 << 20)
.build()
else {
// Invalid regex would not have produced grep hits; do not invent matches.
return Vec::new();
};
re.find_iter(text).map(|m| (m.start(), m.end())).collect()
}
/// Build spans from role segments, overlaying match ranges with
/// [`Theme::search_match`]. Match ranges are byte offsets into the joined
/// segment text (the original source line).
pub(in crate::tui) fn spans_from_segments_with_matches(
segments: &[HighlightSegment],
plain: Style,
match_ranges: &[(usize, usize)],
) -> Vec<Span<'static>> {
if match_ranges.is_empty() {
return segments
.iter()
.map(|segment| Span::styled(segment.text.clone(), segment.style(plain)))
.collect();
}
let mut spans = Vec::new();
let mut offset = 0usize;
for segment in segments {
let base = segment.style(plain);
let text = segment.text.as_str();
let seg_start = offset;
let seg_end = offset + text.len();
let mut cursor = 0usize;
for &(m_start, m_end) in match_ranges {
if m_end <= seg_start || m_start >= seg_end {
continue;
}
let local_start = m_start.saturating_sub(seg_start).max(cursor);
let local_end = (m_end - seg_start).min(text.len());
if local_end <= local_start {
continue;
}
if local_start > cursor {
spans.push(Span::styled(text[cursor..local_start].to_string(), base));
}
spans.push(Span::styled(
text[local_start..local_end].to_string(),
Theme::search_match(base),
));
cursor = local_end;
}
if cursor < text.len() {
spans.push(Span::styled(text[cursor..].to_string(), base));
}
offset = seg_end;
}
if spans.is_empty() {
spans.push(Span::styled(String::new(), plain));
}
spans
}
/// Plain single-segment span list with optional match overlay.
pub(in crate::tui) fn spans_plain_with_matches(
text: &str,
plain: Style,
match_ranges: &[(usize, usize)],
) -> Vec<Span<'static>> {
let segments = [HighlightSegment {
text: text.to_string(),
role: None,
}];
spans_from_segments_with_matches(&segments, plain, match_ranges)
}
fn record_highlight_call() {
HIGHLIGHT_LINE_CALLS.with(|cell| cell.set(cell.get().saturating_add(1)));
}
/// Reset and read the highlight-line counter (tests / benches only).
#[cfg(test)]
pub(in crate::tui) fn take_highlight_line_calls() -> usize {
HIGHLIGHT_LINE_CALLS.with(|cell| cell.replace(0))
}
/// Reset the highlight-line counter (benches / tests).
#[cfg(test)]
pub(in crate::tui) fn reset_highlight_line_calls() {
HIGHLIGHT_LINE_CALLS.with(|cell| cell.set(0));
}
/// Warm the lazy syntax dump so first-paint benches exclude load cost.
#[cfg(test)]
pub(in crate::tui) fn warm_syntax_set() {
LazyLock::force(&SYNTAX_SET);
LazyLock::force(&ROLE_SELECTORS);
}
#[cfg(test)]
#[path = "syntax_tests.rs"]
mod tests;