refactor: remove extra layer of sync

pull/13133/head
Nik Revenco 2025-03-25 11:46:36 +00:00
parent 8f0721f00a
commit f54fdef099
2 changed files with 10 additions and 28 deletions

View File

@ -1,4 +1,4 @@
use std::time::Duration; use std::{mem, time::Duration};
use helix_event::register_hook; use helix_event::register_hook;
use helix_vcs::FileBlame; use helix_vcs::FileBlame;
@ -8,13 +8,13 @@ use helix_view::{
handlers::{BlameEvent, Handlers}, handlers::{BlameEvent, Handlers},
DocumentId, DocumentId,
}; };
use tokio::{sync::oneshot, time::Instant}; use tokio::time::Instant;
use crate::job; use crate::job;
#[derive(Default)] #[derive(Default)]
pub struct BlameHandler { pub struct BlameHandler {
worker: Option<oneshot::Receiver<anyhow::Result<FileBlame>>>, file_blame: Option<anyhow::Result<FileBlame>>,
doc_id: DocumentId, doc_id: DocumentId,
show_blame_for_line_in_statusline: Option<u32>, show_blame_for_line_in_statusline: Option<u32>,
} }
@ -27,36 +27,18 @@ impl helix_event::AsyncHook for BlameHandler {
event: Self::Event, event: Self::Event,
_timeout: Option<tokio::time::Instant>, _timeout: Option<tokio::time::Instant>,
) -> Option<tokio::time::Instant> { ) -> Option<tokio::time::Instant> {
if let Some(worker) = &mut self.worker {
if worker.try_recv().is_ok() {
self.finish_debounce();
return None;
}
}
self.doc_id = event.doc_id; self.doc_id = event.doc_id;
self.show_blame_for_line_in_statusline = event.line; self.show_blame_for_line_in_statusline = event.line;
let (tx, rx) = oneshot::channel(); self.file_blame = Some(FileBlame::try_new(event.path));
tokio::spawn(async move {
let result = FileBlame::try_new(event.path);
let _ = tx.send(result);
});
self.worker = Some(rx);
Some(Instant::now() + Duration::from_millis(50)) Some(Instant::now() + Duration::from_millis(50))
} }
fn finish_debounce(&mut self) { fn finish_debounce(&mut self) {
let doc_id = self.doc_id; let doc_id = self.doc_id;
let line_blame = self.show_blame_for_line_in_statusline; let line_blame = self.show_blame_for_line_in_statusline;
if let Some(worker) = self.worker.take() { let result = mem::take(&mut self.file_blame);
if let Some(result) = result {
tokio::spawn(async move { tokio::spawn(async move {
let Ok(result) = worker.await else {
return;
};
job::dispatch(move |editor, _| { job::dispatch(move |editor, _| {
let Some(doc) = editor.document_mut(doc_id) else { let Some(doc) = editor.document_mut(doc_id) else {
return; return;

View File

@ -1,6 +1,6 @@
use anyhow::Context as _; use anyhow::Context as _;
use anyhow::Result; use anyhow::Result;
use std::cell::RefCell; use parking_lot::Mutex;
use std::collections::HashMap; use std::collections::HashMap;
use std::path::PathBuf; use std::path::PathBuf;
@ -24,7 +24,7 @@ enum LineBlameUnit {
#[derive(Debug)] #[derive(Debug)]
pub struct FileBlame { pub struct FileBlame {
/// A map from line numbers to blame for that line /// A map from line numbers to blame for that line
blame: RefCell<HashMap<u32, LineBlameUnit>>, blame: Mutex<HashMap<u32, LineBlameUnit>>,
/// The owning repository for this file's `ObjectId`s /// The owning repository for this file's `ObjectId`s
repo: gix::ThreadSafeRepository, repo: gix::ThreadSafeRepository,
} }
@ -47,7 +47,7 @@ impl FileBlame {
let blame_line = line.saturating_sub(inserted_lines) + removed_lines; let blame_line = line.saturating_sub(inserted_lines) + removed_lines;
let repo = self.repo.to_thread_local(); let repo = self.repo.to_thread_local();
let mut blame = self.blame.borrow_mut(); let mut blame = self.blame.lock();
let line_blame_unit = blame.get_mut(&blame_line); let line_blame_unit = blame.get_mut(&blame_line);
let commit = match line_blame_unit { let commit = match line_blame_unit {
@ -120,7 +120,7 @@ impl FileBlame {
.entries; .entries;
Ok(Self { Ok(Self {
blame: RefCell::new( blame: Mutex::new(
file_blame file_blame
.into_iter() .into_iter()
.flat_map(|blame| { .flat_map(|blame| {