Skip to content

Commit

Permalink
Addressing feedback:
Browse files Browse the repository at this point in the history
- Cleaned up clippy lints
- Removed `nonempty` dependency in favor of `helix-stdx-nonempty.rs`
- Changed Register initialisation signature to take clipboard provider
  rather than configuration struct.
  • Loading branch information
AlfGalf committed Jun 12, 2024
1 parent 642be71 commit d26be6f
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 42 deletions.
11 changes: 1 addition & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions helix-stdx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ homepage.workspace = true
dunce = "1.0"
etcetera = "0.8"
ropey = { version = "1.6.1", default-features = false }
serde = { version = "1.0" }
which = "6.0"
regex-cursor = "0.1.4"
bitflags = "2.4"
Expand Down
1 change: 1 addition & 0 deletions helix-stdx/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pub mod env;
pub mod faccess;
pub mod nonempty;
pub mod path;
pub mod rope;
59 changes: 59 additions & 0 deletions helix-stdx/src/nonempty.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
use serde::{de::Error, ser::SerializeSeq, Deserialize, Deserializer, Serialize, Serializer};

#[derive(Copy, Clone, Debug, Default, Eq, PartialEq, Hash)]
pub struct EmptyError;

#[derive(Clone, PartialEq, Eq, Debug)]
pub struct NonEmptyVec<T>
where
T: Clone,
{
head: T,
tail: Vec<T>,
}

impl<T: Clone> NonEmptyVec<T> {
pub fn head(&self) -> &T {
&self.head
}

pub fn tail(&self) -> &Vec<T> {
&self.tail
}

pub fn from_vec(mut vec: Vec<T>) -> Option<Self> {
if vec.is_empty() {
None
} else {
let head = vec.remove(0);
Some(Self { head, tail: vec })
}
}
}

impl<T: Serialize + Clone> Serialize for NonEmptyVec<T> {
fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
let mut seq = serializer.serialize_seq(Some(self.tail.len() + 1))?;
seq.serialize_element(&self.head)?;
for e in &self.tail {
seq.serialize_element(&e)?;
}
seq.end()
}
}

impl<'de, T: Deserialize<'de> + Clone> Deserialize<'de> for NonEmptyVec<T> {
fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
let v = <Vec<T>>::deserialize(deserializer)?;
let mut v = v.into_iter();

if let Some(first) = v.next() {
Ok(NonEmptyVec {
head: first,
tail: v.collect(),
})
} else {
Err(D::Error::custom("vector must be non-empty"))
}
}
}
2 changes: 1 addition & 1 deletion helix-term/src/health.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub fn clipboard() -> std::io::Result<()> {
}
Err(err) => {
writeln!(stdout, "{}", "Configuration file malformed".red())?;
writeln!(stdout, "{}", err.to_string())?;
writeln!(stdout, "{}", err)?;
return Ok(());
}
};
Expand Down
41 changes: 16 additions & 25 deletions helix-view/src/clipboard.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Implementation reference: https://github.com/neovim/neovim/blob/f2906a4669a2eef6d7bf86a29648793d63c98949/runtime/autoload/provider/clipboard.vim#L68-L152

use anyhow::Result;
use nonempty::NonEmpty;
use helix_stdx::nonempty::NonEmptyVec;
use serde::{Deserialize, Serialize};
use std::borrow::Cow;

Expand All @@ -20,7 +20,7 @@ pub trait ClipboardProvider: std::fmt::Debug {
impl dyn ClipboardProvider {
pub fn from_string(string: &str) -> Result<Box<dyn ClipboardProvider>> {
let config: ClipboardConfig = serde_json::from_str(string)?;
return Ok(config.get_provider());
Ok(config.get_provider())
}
}

Expand Down Expand Up @@ -222,51 +222,42 @@ impl ClipboardConfig {
ClipboardConfig::Custom(cust) => Box::new(command_provider::command::Provider {
name: "Custom configuration".to_string(),
get_cmd: command_provider::command::CommandConfig {
prg: cust.paste.head.clone(),
args: cust.paste.tail.clone(),
prg: cust.paste.head().clone(),
args: cust.paste.tail().clone(),
},
set_cmd: command_provider::command::CommandConfig {
prg: cust.copy.head.clone(),
args: cust.copy.tail.clone(),
prg: cust.copy.head().clone(),
args: cust.copy.tail().clone(),
},
get_primary_cmd: Some(command_provider::command::CommandConfig {
prg: cust.copy.head.clone(),
args: cust.copy.tail.clone(),
prg: cust.copy.head().clone(),
args: cust.copy.tail().clone(),
}),
set_primary_cmd: Some(command_provider::command::CommandConfig {
prg: cust.copy.head.clone(),
args: cust.copy.tail.clone(),
prg: cust.copy.head().clone(),
args: cust.copy.tail().clone(),
}),
}),
ClipboardConfig::None => Box::new(NoneProvider::new()),
ClipboardConfig::None => Box::<NoneProvider>::default(),
}
}
}

#[derive(Debug, PartialEq, Eq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct CustomClipboardConfig {
copy: NonEmpty<String>,
paste: NonEmpty<String>,
primary_copy: Option<NonEmpty<String>>,
primary_paste: Option<NonEmpty<String>>,
copy: NonEmptyVec<String>,
paste: NonEmptyVec<String>,
primary_copy: Option<NonEmptyVec<String>>,
primary_paste: Option<NonEmptyVec<String>>,
}

#[derive(Debug)]
#[derive(Debug, Default)]
pub struct NoneProvider {
buf: String,
primary_buf: String,
}

impl NoneProvider {
pub fn new() -> Self {
Self {
buf: String::new(),
primary_buf: String::new(),
}
}
}

impl ClipboardProvider for NoneProvider {
fn name(&self) -> std::borrow::Cow<'_, str> {
std::borrow::Cow::Borrowed("None (internal to helix)")
Expand Down
2 changes: 1 addition & 1 deletion helix-view/src/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1178,7 +1178,7 @@ impl Editor {
theme_loader,
last_theme: None,
last_selection: None,
registers: Registers::new(config.clone()),
registers: Registers::new(conf.clipboard_provider.get_provider()),
status_msg: None,
autoinfo: None,
idle_timer: Box::pin(sleep(conf.idle_timeout)),
Expand Down
8 changes: 3 additions & 5 deletions helix-view/src/register.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use std::{borrow::Cow, collections::HashMap, iter, sync::Arc};
use std::{borrow::Cow, collections::HashMap, iter};

use anyhow::Result;
use arc_swap::access::DynAccess;
use helix_core::NATIVE_LINE_ENDING;

use crate::{
clipboard::{ClipboardProvider, ClipboardType},
document::SCRATCH_BUFFER_NAME,
editor::Config,
Editor,
};

Expand Down Expand Up @@ -35,10 +33,10 @@ pub struct Registers {
}

impl Registers {
pub fn new(config: Arc<dyn DynAccess<Config>>) -> Self {
pub fn new(clipboard_provider: Box<dyn ClipboardProvider>) -> Self {
Self {
inner: Default::default(),
clipboard_provider: config.load().clipboard_provider.get_provider(),
clipboard_provider,
last_search_register: '/',
}
}
Expand Down

0 comments on commit d26be6f

Please sign in to comment.