Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/register.rs')
| -rw-r--r-- | helix-view/src/register.rs | 39 |
1 files changed, 21 insertions, 18 deletions
diff --git a/helix-view/src/register.rs b/helix-view/src/register.rs index 3a2e1b7c..3f7844cd 100644 --- a/helix-view/src/register.rs +++ b/helix-view/src/register.rs @@ -1,10 +1,11 @@ use std::{borrow::Cow, collections::HashMap, iter}; use anyhow::Result; +use arc_swap::access::DynAccess; use helix_core::NATIVE_LINE_ENDING; use crate::{ - clipboard::{get_clipboard_provider, ClipboardProvider, ClipboardType}, + clipboard::{ClipboardProvider, ClipboardType}, Editor, }; @@ -20,28 +21,25 @@ use crate::{ /// * Document path (`%`): filename of the current buffer /// * System clipboard (`*`) /// * Primary clipboard (`+`) -#[derive(Debug)] pub struct Registers { /// The mapping of register to values. /// Values are stored in reverse order when inserted with `Registers::write`. /// The order is reversed again in `Registers::read`. This allows us to /// efficiently prepend new values in `Registers::push`. inner: HashMap<char, Vec<String>>, - clipboard_provider: Box<dyn ClipboardProvider>, + clipboard_provider: Box<dyn DynAccess<ClipboardProvider>>, pub last_search_register: char, } -impl Default for Registers { - fn default() -> Self { +impl Registers { + pub fn new(clipboard_provider: Box<dyn DynAccess<ClipboardProvider>>) -> Self { Self { inner: Default::default(), - clipboard_provider: get_clipboard_provider(), + clipboard_provider, last_search_register: '/', } } -} -impl Registers { pub fn read<'a>(&'a self, name: char, editor: &'a Editor) -> Option<RegisterValues<'a>> { match name { '_' => Some(RegisterValues::new(iter::empty())), @@ -64,7 +62,7 @@ impl Registers { Some(RegisterValues::new(iter::once(path))) } '*' | '+' => Some(read_from_clipboard( - self.clipboard_provider.as_ref(), + &self.clipboard_provider.load(), self.inner.get(&name), match name { '+' => ClipboardType::Clipboard, @@ -84,8 +82,8 @@ impl Registers { '_' => Ok(()), '#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")), '*' | '+' => { - self.clipboard_provider.set_contents( - values.join(NATIVE_LINE_ENDING.as_str()), + self.clipboard_provider.load().set_contents( + &values.join(NATIVE_LINE_ENDING.as_str()), match name { '+' => ClipboardType::Clipboard, '*' => ClipboardType::Selection, @@ -114,7 +112,10 @@ impl Registers { '*' => ClipboardType::Selection, _ => unreachable!(), }; - let contents = self.clipboard_provider.get_contents(clipboard_type)?; + let contents = self + .clipboard_provider + .load() + .get_contents(&clipboard_type)?; let saved_values = self.inner.entry(name).or_default(); if !contents_are_saved(saved_values, &contents) { @@ -127,7 +128,8 @@ impl Registers { } value.push_str(&contents); self.clipboard_provider - .set_contents(value, clipboard_type)?; + .load() + .set_contents(&value, clipboard_type)?; Ok(()) } @@ -198,7 +200,8 @@ impl Registers { fn clear_clipboard(&mut self, clipboard_type: ClipboardType) { if let Err(err) = self .clipboard_provider - .set_contents("".into(), clipboard_type) + .load() + .set_contents("", clipboard_type) { log::error!( "Failed to clear {} clipboard: {err}", @@ -210,17 +213,17 @@ impl Registers { } } - pub fn clipboard_provider_name(&self) -> Cow<str> { - self.clipboard_provider.name() + pub fn clipboard_provider_name(&self) -> String { + self.clipboard_provider.load().name().into_owned() } } fn read_from_clipboard<'a>( - provider: &dyn ClipboardProvider, + provider: &ClipboardProvider, saved_values: Option<&'a Vec<String>>, clipboard_type: ClipboardType, ) -> RegisterValues<'a> { - match provider.get_contents(clipboard_type) { + match provider.get_contents(&clipboard_type) { Ok(contents) => { // If we're pasting the same values that we just yanked, re-use // the saved values. This allows pasting multiple selections |