refactor: remove some layers of abstraction
This commit is contained in:
parent
e6bf6bc2f4
commit
215fb29fe6
1 changed files with 18 additions and 26 deletions
|
@ -1,7 +1,6 @@
|
||||||
use std::{path::PathBuf, time::Duration};
|
use std::time::Duration;
|
||||||
|
|
||||||
use helix_event::{register_hook, send_blocking};
|
use helix_event::{register_hook, send_blocking};
|
||||||
use helix_vcs::DiffProviderRegistry;
|
|
||||||
use helix_view::handlers::{BlameEvent, Handlers};
|
use helix_view::handlers::{BlameEvent, Handlers};
|
||||||
use tokio::{task::JoinHandle, time::Instant};
|
use tokio::{task::JoinHandle, time::Instant};
|
||||||
|
|
||||||
|
@ -12,16 +11,6 @@ pub struct BlameHandler {
|
||||||
worker: Option<JoinHandle<anyhow::Result<String>>>,
|
worker: Option<JoinHandle<anyhow::Result<String>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn compute_diff(
|
|
||||||
file: PathBuf,
|
|
||||||
line: u32,
|
|
||||||
diff_providers: DiffProviderRegistry,
|
|
||||||
) -> anyhow::Result<String> {
|
|
||||||
diff_providers
|
|
||||||
.blame_line(&file, line)
|
|
||||||
.map(|s| s.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
impl helix_event::AsyncHook for BlameHandler {
|
impl helix_event::AsyncHook for BlameHandler {
|
||||||
type Event = BlameEvent;
|
type Event = BlameEvent;
|
||||||
|
|
||||||
|
@ -44,7 +33,11 @@ impl helix_event::AsyncHook for BlameHandler {
|
||||||
diff_providers,
|
diff_providers,
|
||||||
} = event;
|
} = event;
|
||||||
|
|
||||||
let worker = tokio::spawn(compute_diff(file, cursor_line, diff_providers));
|
let worker = tokio::spawn(async move {
|
||||||
|
diff_providers
|
||||||
|
.blame_line(&file, cursor_line)
|
||||||
|
.map(|s| s.to_string())
|
||||||
|
});
|
||||||
self.worker = Some(worker);
|
self.worker = Some(worker);
|
||||||
Some(Instant::now() + Duration::from_millis(50))
|
Some(Instant::now() + Duration::from_millis(50))
|
||||||
}
|
}
|
||||||
|
@ -53,23 +46,21 @@ impl helix_event::AsyncHook for BlameHandler {
|
||||||
if let Some(worker) = &self.worker {
|
if let Some(worker) = &self.worker {
|
||||||
if worker.is_finished() {
|
if worker.is_finished() {
|
||||||
let worker = self.worker.take().unwrap();
|
let worker = self.worker.take().unwrap();
|
||||||
tokio::spawn(handle_worker(worker));
|
tokio::spawn(async move {
|
||||||
|
let Ok(Ok(outcome)) = worker.await else {
|
||||||
|
return;
|
||||||
|
};
|
||||||
|
job::dispatch(move |editor, _| {
|
||||||
|
let doc = doc_mut!(editor);
|
||||||
|
doc.blame = Some(outcome);
|
||||||
|
})
|
||||||
|
.await;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn handle_worker(worker: JoinHandle<anyhow::Result<String>>) {
|
|
||||||
let Ok(Ok(outcome)) = worker.await else {
|
|
||||||
return;
|
|
||||||
};
|
|
||||||
job::dispatch(move |editor, _| {
|
|
||||||
let doc = doc_mut!(editor);
|
|
||||||
doc.blame = Some(outcome);
|
|
||||||
})
|
|
||||||
.await;
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(super) fn register_hooks(handlers: &Handlers) {
|
pub(super) fn register_hooks(handlers: &Handlers) {
|
||||||
let tx = handlers.blame.clone();
|
let tx = handlers.blame.clone();
|
||||||
register_hook!(move |event: &mut PostCommand<'_, '_>| {
|
register_hook!(move |event: &mut PostCommand<'_, '_>| {
|
||||||
|
@ -80,6 +71,7 @@ pub(super) fn register_hooks(handlers: &Handlers) {
|
||||||
let Some(file) = doc.path() else {
|
let Some(file) = doc.path() else {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
};
|
};
|
||||||
|
let file = file.to_path_buf();
|
||||||
|
|
||||||
let Ok(cursor_line) = TryInto::<u32>::try_into(
|
let Ok(cursor_line) = TryInto::<u32>::try_into(
|
||||||
text.char_to_line(selection.primary().cursor(doc.text().slice(..))),
|
text.char_to_line(selection.primary().cursor(doc.text().slice(..))),
|
||||||
|
@ -90,7 +82,7 @@ pub(super) fn register_hooks(handlers: &Handlers) {
|
||||||
send_blocking(
|
send_blocking(
|
||||||
&tx,
|
&tx,
|
||||||
BlameEvent::PostCommand {
|
BlameEvent::PostCommand {
|
||||||
file: file.to_path_buf(),
|
file,
|
||||||
cursor_line,
|
cursor_line,
|
||||||
diff_providers: event.cx.editor.diff_providers.clone(),
|
diff_providers: event.cx.editor.diff_providers.clone(),
|
||||||
},
|
},
|
||||||
|
|
Loading…
Add table
Reference in a new issue