From 215fb29fe6dff15f73e7b7c34adc92d9c9d225a3 Mon Sep 17 00:00:00 2001 From: Nik Revenco <154856872+NikitaRevenco@users.noreply.github.com> Date: Wed, 19 Mar 2025 12:35:35 +0000 Subject: [PATCH] refactor: remove some layers of abstraction --- helix-term/src/handlers/blame.rs | 44 +++++++++++++------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/helix-term/src/handlers/blame.rs b/helix-term/src/handlers/blame.rs index 0f157a95..8d7bf5b2 100644 --- a/helix-term/src/handlers/blame.rs +++ b/helix-term/src/handlers/blame.rs @@ -1,7 +1,6 @@ -use std::{path::PathBuf, time::Duration}; +use std::time::Duration; use helix_event::{register_hook, send_blocking}; -use helix_vcs::DiffProviderRegistry; use helix_view::handlers::{BlameEvent, Handlers}; use tokio::{task::JoinHandle, time::Instant}; @@ -12,16 +11,6 @@ pub struct BlameHandler { worker: Option>>, } -async fn compute_diff( - file: PathBuf, - line: u32, - diff_providers: DiffProviderRegistry, -) -> anyhow::Result { - diff_providers - .blame_line(&file, line) - .map(|s| s.to_string()) -} - impl helix_event::AsyncHook for BlameHandler { type Event = BlameEvent; @@ -44,7 +33,11 @@ impl helix_event::AsyncHook for BlameHandler { diff_providers, } = 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); Some(Instant::now() + Duration::from_millis(50)) } @@ -53,23 +46,21 @@ impl helix_event::AsyncHook for BlameHandler { if let Some(worker) = &self.worker { if worker.is_finished() { 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>) { - 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) { let tx = handlers.blame.clone(); register_hook!(move |event: &mut PostCommand<'_, '_>| { @@ -80,6 +71,7 @@ pub(super) fn register_hooks(handlers: &Handlers) { let Some(file) = doc.path() else { return Ok(()); }; + let file = file.to_path_buf(); let Ok(cursor_line) = TryInto::::try_into( text.char_to_line(selection.primary().cursor(doc.text().slice(..))), @@ -90,7 +82,7 @@ pub(super) fn register_hooks(handlers: &Handlers) { send_blocking( &tx, BlameEvent::PostCommand { - file: file.to_path_buf(), + file, cursor_line, diff_providers: event.cx.editor.diff_providers.clone(), },