crates/diff/src/highlighter.rs 12.4 KiB raw
1
//! Background syntax highlighting and context expansion for diff viewers.
2
//!
3
//! [`spawn_highlighter`] runs the blocking repo and syntax-highlighting work on
4
//! a dedicated thread so an interactive viewer stays responsive. A viewer feeds
5
//! it the [`HighlightRequest`]s collected by
6
//! [`DiffViewer::visible_highlight_requests`](crate::DiffViewer::visible_highlight_requests)
7
//! and the [`ContextRequest`]s prepared by
8
//! [`DiffViewer::context_request`](crate::DiffViewer::context_request), then
9
//! applies each [`HighlightOutput`] it produces.
10
11
use std::collections::{BTreeMap, BTreeSet};
12
use std::path::PathBuf;
13
use std::sync::mpsc::{self, SyncSender, TrySendError};
14
use std::thread;
15
16
use rustik_repo::{DiffRepository, git};
17
18
use crate::builtins::Builtins;
19
use crate::{
20
    BuiltinHighlighter, ContextRequest, ContextUpdate, Decode, DiffDocument, DiffHighlighter,
21
    DiffSide, FileId, HighlightRequest, HighlightResult, HighlightSource, Oid,
22
};
23
24
/// Source context lines fetched around highlighted rows.
25
const HIGHLIGHT_CONTEXT_LINES: usize = 64;
26
/// Bound on queued highlight and context commands.
27
const COMMAND_QUEUE: usize = 4;
28
29
/// A running background highlighter.
30
///
31
/// Submits highlight and context work to the worker thread spawned by
32
/// [`spawn_highlighter`]. Dropping the handle disconnects the command channel,
33
/// which stops the worker once it drains.
34
pub struct Highlighter {
35
    /// Command channel to the worker thread.
36
    commands: SyncSender<Command>,
37
}
38
39
impl Highlighter {
40
    /// Queues a batch of rows for highlighting.
41
    ///
42
    /// Returns the requests back when the worker queue is full or gone, so the
43
    /// caller can cancel them with
44
    /// [`DiffViewer::cancel_highlights`](crate::DiffViewer::cancel_highlights).
45
    /// Returns `None` once the batch is queued.
46
    pub fn highlight(&self, requests: Vec<HighlightRequest>) -> Option<Vec<HighlightRequest>> {
47
        // `try_send` errors return the command we sent, so the error always
48
        // holds `Command::Highlight`.
49
        match self.commands.try_send(Command::Highlight(requests)) {
50
            Ok(()) => None,
51
            Err(
52
                TrySendError::Full(Command::Highlight(requests))
53
                | TrySendError::Disconnected(Command::Highlight(requests)),
54
            ) => Some(requests),
55
            Err(_) => None,
56
        }
57
    }
58
59
    /// Queues a context refetch, returning whether it was accepted.
60
    pub fn context(&self, request: ContextRequest) -> bool {
61
        self.commands.try_send(Command::Context(request)).is_ok()
62
    }
63
}
64
65
/// Result produced by the background highlighter.
66
pub enum HighlightOutput {
67
    /// Highlighted source rows ready for
68
    /// [`DiffViewer::apply_highlights`](crate::DiffViewer::apply_highlights).
69
    Highlights(Vec<HighlightResult>),
70
    /// A decoded context update, or `None` when the refetch produced nothing,
71
    /// ready for
72
    /// [`DiffViewer::apply_context_update`](crate::DiffViewer::apply_context_update).
73
    Context(Option<ContextUpdate>),
74
}
75
76
/// Command sent to the worker thread.
77
enum Command {
78
    /// Highlight a batch of source rows.
79
    Highlight(Vec<HighlightRequest>),
80
    /// Refetch and decode a context update.
81
    Context(ContextRequest),
82
}
83
84
/// Spawns a background highlighter for `document`.
85
///
86
/// `output` is invoked on the worker thread for each produced
87
/// [`HighlightOutput`]; forward it to the UI thread (for example over a
88
/// channel) and apply it to the viewer.
89
pub fn spawn_highlighter(
90
    document: &DiffDocument,
91
    repo: Option<git::Repository>,
92
    output: impl Fn(HighlightOutput) + Send + 'static,
93
) -> Highlighter {
94
    let (commands, requests) = mpsc::sync_channel::<Command>(COMMAND_QUEUE);
95
    let seed = Seed::new(document);
96
97
    thread::spawn(move || {
98
        // Theme parsing and grammar compilation are blocking, so the worker is
99
        // built here rather than on the caller's thread.
100
        let mut worker = HighlightWorker::new(seed, repo);
101
        while let Ok(command) = requests.recv() {
102
            match command {
103
                Command::Highlight(requests) => {
104
                    if requests.is_empty() {
105
                        continue;
106
                    }
107
                    output(HighlightOutput::Highlights(worker.highlight(requests)));
108
                }
109
                Command::Context(request) => {
110
                    output(HighlightOutput::Context(worker.context(request)));
111
                }
112
            }
113
        }
114
    });
115
    Highlighter { commands }
116
}
117
118
/// Header-only document data the worker needs.
119
///
120
/// Extracted on the caller's thread so the document itself — which holds every
121
/// line of the patch — does not have to be cloned into the worker. Its size is
122
/// proportional to the number of changed files, not diff lines.
123
struct Seed {
124
    /// Changed file paths used to select built-in grammars.
125
    paths: Vec<PathBuf>,
126
    /// Source object ids keyed by diff file and side.
127
    head_oids: BTreeMap<(FileId, DiffSide), Oid>,
128
}
129
130
impl Seed {
131
    /// Extracts the worker's seed from `document`.
132
    fn new(document: &DiffDocument) -> Self {
133
        let mut head_oids = BTreeMap::new();
134
        for file in &document.files {
135
            if let Some(oid) = file.header.old_oid {
136
                head_oids.insert((file.id, DiffSide::Old), oid);
137
            }
138
            if let Some(oid) = file.header.new_oid {
139
                head_oids.insert((file.id, DiffSide::New), oid);
140
            }
141
        }
142
        Self {
143
            paths: Builtins::document_paths(document),
144
            head_oids,
145
        }
146
    }
147
}
148
149
/// Background worker for blocking repo and syntax-highlighting work.
150
struct HighlightWorker {
151
    /// Built-in syntax highlighter.
152
    highlighter: BuiltinHighlighter,
153
    /// Git-backed repo used for direct Git invocations.
154
    repo: Option<git::Repository>,
155
    /// Highlight source context cache keyed by diff file and side.
156
    context_sources: BTreeMap<(FileId, DiffSide), Vec<HighlightSource>>,
157
    /// Source object ids keyed by diff file and side.
158
    head_oids: BTreeMap<(FileId, DiffSide), Oid>,
159
    /// File-side heads already checked for first-line grammar metadata.
160
    loaded_heads: BTreeSet<(FileId, DiffSide)>,
161
}
162
163
impl HighlightWorker {
164
    /// Creates a background worker.
165
    fn new(seed: Seed, repo: Option<git::Repository>) -> Self {
166
        Self {
167
            highlighter: Builtins::highlighter_for_paths(&seed.paths)
168
                .unwrap_or_else(|_| BuiltinHighlighter::new(Default::default(), "")),
169
            repo,
170
            context_sources: BTreeMap::new(),
171
            head_oids: seed.head_oids,
172
            loaded_heads: BTreeSet::new(),
173
        }
174
    }
175
176
    /// Highlights one batch of source rows.
177
    fn highlight(&mut self, mut requests: Vec<HighlightRequest>) -> Vec<HighlightResult> {
178
        self.load_context_sources(&requests);
179
        for request in &mut requests {
180
            if let Some(source) = self.context_source_for(request) {
181
                request.source = source.clone();
182
            }
183
            self.load_head_grammar(request);
184
        }
185
        self.highlighter.highlight_batch(requests)
186
    }
187
188
    /// Loads first-line-selected grammars for a request's file side.
189
    fn load_head_grammar(&mut self, request: &HighlightRequest) {
190
        let key = (request.key.file, request.key.side);
191
        if !self.loaded_heads.insert(key) {
192
            return;
193
        }
194
        if request.source.head == 1
195
            && let Some(head) = request.source.text.lines().next()
196
        {
197
            let _ = Builtins::load_head_grammars(&mut self.highlighter, head);
198
            return;
199
        }
200
        let Some(repo) = self.repo.as_ref() else {
201
            return;
202
        };
203
        let Some(path) = request.path.as_deref() else {
204
            return;
205
        };
206
        if self.highlighter.has_path_grammar(path) {
207
            return;
208
        }
209
        let Some(oid) = self.head_oids.get(&key) else {
210
            return;
211
        };
212
        let head = repo.head_line(*oid);
213
        if let Ok(Some(head)) = head {
214
            let _ = Builtins::load_head_grammars(&mut self.highlighter, &head);
215
        }
216
    }
217
218
    /// Loads bounded source context for uncached request paths.
219
    fn load_context_sources(&mut self, requests: &[HighlightRequest]) {
220
        let Some(repo) = self.repo.as_ref() else {
221
            return;
222
        };
223
        let mut targets = BTreeMap::new();
224
        for request in requests {
225
            let key = (request.key.file, request.key.side);
226
            // Skip sides the viewer already supplied whole-file context for;
227
            // refetching them would cost a `git diff` whose result is unused.
228
            if self.context_sources.contains_key(&key) || request.source.head == 1 {
229
                continue;
230
            }
231
            if let Some(path) = request.path.as_deref() {
232
                targets.insert((path.to_path_buf(), request.key.side), request.key.file);
233
            }
234
        }
235
        if targets.is_empty() {
236
            return;
237
        }
238
239
        let paths = targets
240
            .keys()
241
            .map(|(path, _)| path.as_path())
242
            .collect::<BTreeSet<_>>()
243
            .into_iter()
244
            .collect::<Vec<_>>();
245
        let Ok(patch) = repo.diff(HIGHLIGHT_CONTEXT_LINES, &paths) else {
246
            return;
247
        };
248
        let Ok(document) = DiffDocument::decode_bytes(&patch) else {
249
            return;
250
        };
251
252
        for file in &document.files {
253
            for side in [DiffSide::Old, DiffSide::New] {
254
                let Some(path) = file.source_path(side) else {
255
                    continue;
256
                };
257
                let Some(file_id) = targets.get(&(path, side)).copied() else {
258
                    continue;
259
                };
260
                let sources = file
261
                    .hunks
262
                    .iter()
263
                    .map(|hunk| hunk.highlight_source(side))
264
                    .collect::<Vec<_>>();
265
                self.context_sources.insert((file_id, side), sources);
266
            }
267
        }
268
    }
269
270
    /// Returns cached context that contains the requested source row.
271
    fn context_source_for(&self, request: &HighlightRequest) -> Option<&HighlightSource> {
272
        // A source starting at line 1 already carries every line the tokenizer
273
        // state depends on. The bounded refetch would only narrow it, dropping
274
        // context for rows further than HIGHLIGHT_CONTEXT_LINES into a file.
275
        if request.source.head == 1 {
276
            return None;
277
        }
278
        let key = (request.key.file, request.key.side);
279
        let expected = request.source.line(request.key.source_line)?;
280
        self.context_sources
281
            .get(&key)?
282
            .iter()
283
            .find(|source| source.line(request.key.source_line) == Some(expected))
284
    }
285
286
    /// Refetches and decodes one context update.
287
    fn context(&mut self, request: ContextRequest) -> Option<ContextUpdate> {
288
        let repo = self.repo.as_ref()?;
289
        let paths = request.paths().collect::<Vec<_>>();
290
        let patch = repo.diff(request.context_lines(), &paths).ok()?;
291
292
        drop(paths);
293
        request.decode(&patch).ok().flatten()
294
    }
295
}
296
297
#[cfg(test)]
298
mod tests {
299
    use super::*;
300
    use crate::{HighlightKey, HighlightKeyInput, HunkId, RowId};
301
302
    /// Returns a request for row 5, carrying `source`.
303
    fn request(source: HighlightSource) -> HighlightRequest {
304
        HighlightRequest {
305
            key: HighlightKey::new(HighlightKeyInput {
306
                file: FileId::new(0),
307
                side: DiffSide::New,
308
                source_line: 5,
309
                text: "row",
310
                syntax: None,
311
                theme: "auto",
312
            }),
313
            hunk: HunkId::new(0),
314
            row: RowId::new(0),
315
            path: None,
316
            source,
317
        }
318
    }
319
320
    #[test]
321
    fn whole_file_sources_are_not_narrowed_to_bounded_context() {
322
        let mut worker = HighlightWorker::new(
323
            Seed {
324
                paths: Vec::new(),
325
                head_oids: BTreeMap::new(),
326
            },
327
            None,
328
        );
329
        worker.context_sources.insert(
330
            (FileId::new(0), DiffSide::New),
331
            vec![HighlightSource::new("row\n", 5)],
332
        );
333
334
        // A hunk-only source is still upgraded to the refetched context.
335
        let hunk_only = request(HighlightSource::new("row\n", 5));
336
        assert!(worker.context_source_for(&hunk_only).is_some());
337
338
        // A whole-file source is left alone: the refetch spans only
339
        // HIGHLIGHT_CONTEXT_LINES and would drop context before that.
340
        let whole_file = request(HighlightSource::new("a\nb\nc\nd\nrow\n", 1));
341
        assert!(worker.context_source_for(&whole_file).is_none());
342
    }
343
}