From 3b7760dfb0cdb547ff8c94f7685554f59d16bf0a Mon Sep 17 00:00:00 2001 From: ath3 Date: Tue, 15 Nov 2022 15:14:18 +0100 Subject: Refactor blackhole register (#4504) --- helix-core/src/register.rs | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) (limited to 'helix-core') diff --git a/helix-core/src/register.rs b/helix-core/src/register.rs index 1cff77ba..52eb6e3e 100644 --- a/helix-core/src/register.rs +++ b/helix-core/src/register.rs @@ -15,11 +15,7 @@ impl Register { } pub fn new_with_values(name: char, values: Vec) -> Self { - if name == '_' { - Self::new(name) - } else { - Self { name, values } - } + Self { name, values } } pub const fn name(&self) -> char { @@ -31,15 +27,11 @@ impl Register { } pub fn write(&mut self, values: Vec) { - if self.name != '_' { - self.values = values; - } + self.values = values; } pub fn push(&mut self, value: String) { - if self.name != '_' { - self.values.push(value); - } + self.values.push(value); } } @@ -54,19 +46,25 @@ impl Registers { self.inner.get(&name) } - pub fn get_mut(&mut self, name: char) -> &mut Register { - self.inner - .entry(name) - .or_insert_with(|| Register::new(name)) + pub fn read(&self, name: char) -> Option<&[String]> { + self.get(name).map(|reg| reg.read()) } pub fn write(&mut self, name: char, values: Vec) { - self.inner - .insert(name, Register::new_with_values(name, values)); + if name != '_' { + self.inner + .insert(name, Register::new_with_values(name, values)); + } } - pub fn read(&self, name: char) -> Option<&[String]> { - self.get(name).map(|reg| reg.read()) + pub fn push(&mut self, name: char, value: String) { + if name != '_' { + if let Some(r) = self.inner.get_mut(&name) { + r.push(value); + } else { + self.write(name, vec![value]); + } + } } pub fn first(&self, name: char) -> Option<&String> { -- cgit v1.2.3-70-g09d2