From 53ac833efbcd6b968fd7c88318525a8d491fea14 Mon Sep 17 00:00:00 2001 From: Michael Davis Date: Fri, 16 Feb 2024 11:26:00 -0500 Subject: [PATCH] Replace picker shutdown bool with version number This works nicely for dynamic pickers: we stop any running jobs like global search that are pushing to the injector by incrementing the version number when we start a new request. The boolean only allowed us to shut the picker down once, but with a usize a picker can have multiple "sessions" / "life-cycles" where it receives new options from an injector. --- helix-term/src/ui/picker.rs | 30 +++++++++++++++++------------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/helix-term/src/ui/picker.rs b/helix-term/src/ui/picker.rs index 5e7bda21b..4f94c6172 100644 --- a/helix-term/src/ui/picker.rs +++ b/helix-term/src/ui/picker.rs @@ -34,7 +34,7 @@ use std::{ io::Read, path::{Path, PathBuf}, sync::{ - atomic::{self, AtomicBool}, + atomic::{self, AtomicUsize}, Arc, }, }; @@ -154,7 +154,8 @@ pub struct Injector { dst: nucleo::Injector, columns: Arc<[Column]>, editor_data: Arc, - shutown: Arc, + version: usize, + picker_version: Arc, } impl Clone for Injector { @@ -163,7 +164,8 @@ impl Clone for Injector { dst: self.dst.clone(), columns: self.columns.clone(), editor_data: self.editor_data.clone(), - shutown: self.shutown.clone(), + version: self.version, + picker_version: self.picker_version.clone(), } } } @@ -172,7 +174,7 @@ pub struct InjectorShutdown; impl Injector { pub fn push(&self, item: T) -> Result<(), InjectorShutdown> { - if self.shutown.load(atomic::Ordering::Relaxed) { + if self.version != self.picker_version.load(atomic::Ordering::Relaxed) { return Err(InjectorShutdown); } @@ -220,7 +222,7 @@ pub struct Picker { columns: Arc<[Column]>, primary_column: usize, editor_data: Arc, - shutdown: Arc, + version: Arc, matcher: Nucleo, /// Current height of the completions box @@ -261,7 +263,8 @@ impl Picker { dst: matcher.injector(), columns: columns.into(), editor_data: Arc::new(editor_data), - shutown: Arc::new(AtomicBool::new(false)), + version: 0, + picker_version: Arc::new(AtomicUsize::new(0)), }; (matcher, streamer) } @@ -290,7 +293,7 @@ impl Picker { columns.into(), primary_column, Arc::new(editor_data), - Arc::new(AtomicBool::new(false)), + Arc::new(AtomicUsize::new(0)), callback_fn, ) } @@ -306,7 +309,7 @@ impl Picker { injector.columns, primary_column, injector.editor_data, - injector.shutown, + injector.picker_version, callback_fn, ) } @@ -316,7 +319,7 @@ impl Picker { columns: Arc<[Column]>, default_column: usize, editor_data: Arc, - shutdown: Arc, + version: Arc, callback_fn: impl Fn(&mut Context, &T, Action) + 'static, ) -> Self { assert!(!columns.is_empty()); @@ -340,7 +343,7 @@ impl Picker { primary_column: default_column, matcher, editor_data, - shutdown, + version, cursor: 0, prompt, query, @@ -361,7 +364,8 @@ impl Picker { dst: self.matcher.injector(), columns: self.columns.clone(), editor_data: self.editor_data.clone(), - shutown: self.shutdown.clone(), + version: self.version.load(atomic::Ordering::Relaxed), + picker_version: self.version.clone(), } } @@ -916,7 +920,7 @@ impl Component for Picker Component for Picker Drop for Picker { fn drop(&mut self) { // ensure we cancel any ongoing background threads streaming into the picker - self.shutdown.store(true, atomic::Ordering::Relaxed) + self.version.fetch_add(1, atomic::Ordering::Relaxed); } }