From 68affa3c598723a8b9451ef3dcceda83ae161e39 Mon Sep 17 00:00:00 2001 From: BenoƮt CORTIER Date: Fri, 4 Jun 2021 22:21:31 -0400 Subject: Implement register selection User can select register to yank into with the " command. A new state is added to `Editor` and `commands::Context` structs. This state is managed by leveraging a new struct `RegisterSelection`. --- helix-view/src/editor.rs | 4 ++- helix-view/src/lib.rs | 2 ++ helix-view/src/register_selection.rs | 47 ++++++++++++++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 helix-view/src/register_selection.rs (limited to 'helix-view') diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index fa8dea2f..b69ae22f 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -1,4 +1,4 @@ -use crate::{theme::Theme, tree::Tree, Document, DocumentId, View, ViewId}; +use crate::{theme::Theme, tree::Tree, Document, DocumentId, RegisterSelection, View, ViewId}; use tui::layout::Rect; use std::path::PathBuf; @@ -13,6 +13,7 @@ pub struct Editor { pub tree: Tree, pub documents: SlotMap, pub count: Option, + pub register: RegisterSelection, pub theme: Theme, pub language_servers: helix_lsp::Registry, @@ -57,6 +58,7 @@ impl Editor { tree: Tree::new(area), documents: SlotMap::with_key(), count: None, + register: RegisterSelection::default(), theme, language_servers, status_msg: None, diff --git a/helix-view/src/lib.rs b/helix-view/src/lib.rs index 00bddad6..7e253320 100644 --- a/helix-view/src/lib.rs +++ b/helix-view/src/lib.rs @@ -1,5 +1,6 @@ pub mod document; pub mod editor; +pub mod register_selection; pub mod theme; pub mod tree; pub mod view; @@ -10,5 +11,6 @@ new_key_type! { pub struct ViewId; } pub use document::Document; pub use editor::Editor; +pub use register_selection::RegisterSelection; pub use theme::Theme; pub use view::View; diff --git a/helix-view/src/register_selection.rs b/helix-view/src/register_selection.rs new file mode 100644 index 00000000..d7f073b6 --- /dev/null +++ b/helix-view/src/register_selection.rs @@ -0,0 +1,47 @@ +/// Register selection and configuration +/// +/// This is a kind a of specialized `Option` for register selection. +/// Point is to keep whether the register selection has been explicitely +/// set or not while being convenient by knowing the default register name. +pub struct RegisterSelection { + selected: char, + default_name: char, +} + +impl RegisterSelection { + pub fn new(default_name: char) -> Self { + Self { + selected: default_name, + default_name, + } + } + + pub fn select(&mut self, name: char) { + self.selected = name; + } + + pub fn take(&mut self) -> Self { + Self { + selected: std::mem::replace(&mut self.selected, self.default_name), + default_name: self.default_name, + } + } + + pub fn is_default(&self) -> bool { + self.selected == self.default_name + } + + pub fn name(&self) -> char { + self.selected + } +} + +impl Default for RegisterSelection { + fn default() -> Self { + let default_name = '"'; + Self { + selected: default_name, + default_name, + } + } +} -- cgit v1.2.3-70-g09d2