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.rs60
1 files changed, 30 insertions, 30 deletions
diff --git a/helix-view/src/register.rs b/helix-view/src/register.rs
index b2bb53ac..166d2027 100644
--- a/helix-view/src/register.rs
+++ b/helix-view/src/register.rs
@@ -1,11 +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::{ClipboardError, ClipboardProvider, ClipboardType},
+ clipboard::{get_clipboard_provider, ClipboardProvider, ClipboardType},
+ document::SCRATCH_BUFFER_NAME,
Editor,
};
@@ -21,25 +21,28 @@ 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 DynAccess<ClipboardProvider>>,
+ clipboard_provider: Box<dyn ClipboardProvider>,
pub last_search_register: char,
}
-impl Registers {
- pub fn new(clipboard_provider: Box<dyn DynAccess<ClipboardProvider>>) -> Self {
+impl Default for Registers {
+ fn default() -> Self {
Self {
inner: Default::default(),
- clipboard_provider,
+ clipboard_provider: get_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())),
@@ -58,11 +61,18 @@ impl Registers {
Some(RegisterValues::new(doc.selection(view.id).fragments(text)))
}
'%' => {
- let path = doc!(editor).display_name();
+ let doc = doc!(editor);
+
+ let path = doc
+ .path()
+ .as_ref()
+ .map(|p| p.to_string_lossy())
+ .unwrap_or_else(|| SCRATCH_BUFFER_NAME.into());
+
Some(RegisterValues::new(iter::once(path)))
}
'*' | '+' => Some(read_from_clipboard(
- &self.clipboard_provider.load(),
+ self.clipboard_provider.as_ref(),
self.inner.get(&name),
match name {
'+' => ClipboardType::Clipboard,
@@ -82,8 +92,8 @@ impl Registers {
'_' => Ok(()),
'#' | '.' | '%' => Err(anyhow::anyhow!("Register {name} does not support writing")),
'*' | '+' => {
- self.clipboard_provider.load().set_contents(
- &values.join(NATIVE_LINE_ENDING.as_str()),
+ self.clipboard_provider.set_contents(
+ values.join(NATIVE_LINE_ENDING.as_str()),
match name {
'+' => ClipboardType::Clipboard,
'*' => ClipboardType::Selection,
@@ -112,10 +122,7 @@ impl Registers {
'*' => ClipboardType::Selection,
_ => unreachable!(),
};
- let contents = self
- .clipboard_provider
- .load()
- .get_contents(&clipboard_type)?;
+ let contents = self.clipboard_provider.get_contents(clipboard_type)?;
let saved_values = self.inner.entry(name).or_default();
if !contents_are_saved(saved_values, &contents) {
@@ -128,8 +135,7 @@ impl Registers {
}
value.push_str(&contents);
self.clipboard_provider
- .load()
- .set_contents(&value, clipboard_type)?;
+ .set_contents(value, clipboard_type)?;
Ok(())
}
@@ -145,8 +151,7 @@ impl Registers {
}
pub fn last<'a>(&'a self, name: char, editor: &'a Editor) -> Option<Cow<'a, str>> {
- self.read(name, editor)
- .and_then(|mut values| values.next_back())
+ self.read(name, editor).and_then(|values| values.last())
}
pub fn iter_preview(&self) -> impl Iterator<Item = (char, &str)> {
@@ -201,8 +206,7 @@ impl Registers {
fn clear_clipboard(&mut self, clipboard_type: ClipboardType) {
if let Err(err) = self
.clipboard_provider
- .load()
- .set_contents("", clipboard_type)
+ .set_contents("".into(), clipboard_type)
{
log::error!(
"Failed to clear {} clipboard: {err}",
@@ -214,17 +218,17 @@ impl Registers {
}
}
- pub fn clipboard_provider_name(&self) -> String {
- self.clipboard_provider.load().name().into_owned()
+ pub fn clipboard_provider_name(&self) -> Cow<str> {
+ self.clipboard_provider.name()
}
}
fn read_from_clipboard<'a>(
- provider: &ClipboardProvider,
+ provider: &dyn 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
@@ -239,10 +243,6 @@ fn read_from_clipboard<'a>(
RegisterValues::new(iter::once(contents.into()))
}
}
- Err(ClipboardError::ReadingNotSupported) => match saved_values {
- Some(values) => RegisterValues::new(values.iter().map(Cow::from).rev()),
- None => RegisterValues::new(iter::empty()),
- },
Err(err) => {
log::error!(
"Failed to read {} clipboard: {err}",
@@ -312,13 +312,13 @@ impl<'a> Iterator for RegisterValues<'a> {
}
}
-impl DoubleEndedIterator for RegisterValues<'_> {
+impl<'a> DoubleEndedIterator for RegisterValues<'a> {
fn next_back(&mut self) -> Option<Self::Item> {
self.iter.next_back()
}
}
-impl ExactSizeIterator for RegisterValues<'_> {
+impl<'a> ExactSizeIterator for RegisterValues<'a> {
fn len(&self) -> usize {
self.iter.len()
}