|
| 1 | +use std::{path::PathBuf, time::Duration}; |
| 2 | + |
1 | 3 | use helix_event::{register_hook, send_blocking};
|
2 |
| -use helix_view::{ |
3 |
| - handlers::{BlameEvent, Handlers}, |
4 |
| - Editor, |
5 |
| -}; |
| 4 | +use helix_vcs::DiffProviderRegistry; |
| 5 | +use helix_view::handlers::{BlameEvent, Handlers}; |
| 6 | +use tokio::{task::JoinHandle, time::Instant}; |
6 | 7 |
|
7 | 8 | use crate::{events::PostCommand, job};
|
8 | 9 |
|
9 |
| -pub struct BlameHandler; |
| 10 | +#[derive(Default)] |
| 11 | +pub struct BlameHandler { |
| 12 | + worker: Option<JoinHandle<anyhow::Result<String>>>, |
| 13 | +} |
| 14 | + |
| 15 | +async fn compute_diff( |
| 16 | + file: PathBuf, |
| 17 | + line: u32, |
| 18 | + diff_providers: DiffProviderRegistry, |
| 19 | +) -> anyhow::Result<String> { |
| 20 | + // std::thread::sleep(Duration::from_secs(5)); |
| 21 | + // Ok("hhe".to_string()) |
| 22 | + diff_providers |
| 23 | + .blame_line(&file, line) |
| 24 | + .map(|s| s.to_string()) |
| 25 | +} |
10 | 26 |
|
11 | 27 | impl helix_event::AsyncHook for BlameHandler {
|
12 | 28 | type Event = BlameEvent;
|
13 | 29 |
|
14 | 30 | fn handle_event(
|
15 | 31 | &mut self,
|
16 |
| - _event: Self::Event, |
| 32 | + event: Self::Event, |
17 | 33 | _timeout: Option<tokio::time::Instant>,
|
18 | 34 | ) -> Option<tokio::time::Instant> {
|
19 |
| - self.finish_debounce(); |
20 |
| - None |
| 35 | + if let Some(worker) = &self.worker { |
| 36 | + if worker.is_finished() { |
| 37 | + self.finish_debounce(); |
| 38 | + return None; |
| 39 | + } |
| 40 | + return Some(Instant::now() + Duration::from_millis(50)); |
| 41 | + } |
| 42 | + |
| 43 | + let BlameEvent::PostCommand { |
| 44 | + file, |
| 45 | + cursor_line, |
| 46 | + diff_providers, |
| 47 | + } = event; |
| 48 | + |
| 49 | + let worker = tokio::spawn(compute_diff(file, cursor_line, diff_providers)); |
| 50 | + self.worker = Some(worker); |
| 51 | + Some(Instant::now() + Duration::from_millis(50)) |
21 | 52 | }
|
22 | 53 |
|
23 | 54 | fn finish_debounce(&mut self) {
|
24 |
| - // TODO: this blocks on the main thread. Figure out how not to do that |
25 |
| - // |
26 |
| - // Attempts so far: |
27 |
| - // - tokio::spawn |
28 |
| - // - std::thread::spawn |
29 |
| - // |
30 |
| - // For some reason none of the above fix the issue of blocking the UI. |
31 |
| - job::dispatch_blocking(move |editor, _| { |
32 |
| - request_git_blame(editor); |
33 |
| - }) |
| 55 | + if let Some(worker) = &self.worker { |
| 56 | + if worker.is_finished() { |
| 57 | + let worker = self.worker.take().unwrap(); |
| 58 | + tokio::spawn(handle_worker(worker)); |
| 59 | + } |
| 60 | + } |
34 | 61 | }
|
35 | 62 | }
|
36 | 63 |
|
| 64 | +async fn handle_worker(worker: JoinHandle<anyhow::Result<String>>) { |
| 65 | + let Ok(Ok(outcome)) = worker.await else { |
| 66 | + return; |
| 67 | + }; |
| 68 | + job::dispatch(move |editor, _| { |
| 69 | + let doc = doc_mut!(editor); |
| 70 | + doc.blame = Some(outcome); |
| 71 | + }) |
| 72 | + .await; |
| 73 | +} |
| 74 | + |
37 | 75 | pub(super) fn register_hooks(handlers: &Handlers) {
|
38 | 76 | let tx = handlers.blame.clone();
|
39 | 77 | register_hook!(move |event: &mut PostCommand<'_, '_>| {
|
40 | 78 | if event.cx.editor.config().vcs.blame {
|
41 |
| - send_blocking(&tx, BlameEvent::PostCommand); |
| 79 | + let blame_enabled = event.cx.editor.config().vcs.blame; |
| 80 | + let (view, doc) = current!(event.cx.editor); |
| 81 | + let text = doc.text(); |
| 82 | + let selection = doc.selection(view.id); |
| 83 | + let Some(file) = doc.path() else { |
| 84 | + panic!(); |
| 85 | + }; |
| 86 | + if !blame_enabled { |
| 87 | + panic!(); |
| 88 | + } |
| 89 | + |
| 90 | + let cursor_lin = text.char_to_line(selection.primary().cursor(doc.text().slice(..))); |
| 91 | + let Ok(cursor_line) = TryInto::<u32>::try_into(cursor_lin) else { |
| 92 | + panic!(); |
| 93 | + }; |
| 94 | + |
| 95 | + send_blocking( |
| 96 | + &tx, |
| 97 | + BlameEvent::PostCommand { |
| 98 | + file: file.to_path_buf(), |
| 99 | + cursor_line, |
| 100 | + diff_providers: event.cx.editor.diff_providers.clone(), |
| 101 | + }, |
| 102 | + ); |
42 | 103 | }
|
43 | 104 |
|
44 | 105 | Ok(())
|
45 | 106 | });
|
46 | 107 | }
|
47 |
| - |
48 |
| -fn request_git_blame(editor: &mut Editor) { |
49 |
| - let blame_enabled = editor.config().vcs.blame; |
50 |
| - let (view, doc) = current!(editor); |
51 |
| - let text = doc.text(); |
52 |
| - let selection = doc.selection(view.id); |
53 |
| - let Some(file) = doc.path() else { |
54 |
| - return; |
55 |
| - }; |
56 |
| - if !blame_enabled { |
57 |
| - return; |
58 |
| - } |
59 |
| - |
60 |
| - let cursor_lin = text.char_to_line(selection.primary().cursor(doc.text().slice(..))); |
61 |
| - let Ok(cursor_line) = TryInto::<u32>::try_into(cursor_lin) else { |
62 |
| - return; |
63 |
| - }; |
64 |
| - |
65 |
| - // 0-based into 1-based line number |
66 |
| - let Ok(output) = editor.diff_providers.blame_line(file, cursor_line + 1) else { |
67 |
| - return; |
68 |
| - }; |
69 |
| - |
70 |
| - doc.blame = Some(output.to_string()); |
71 |
| -} |
|
0 commit comments