Refactor blackhole register (#4504)

pull/4747/head
ath3 2 years ago committed by GitHub
parent 652497bdd6
commit 3b7760dfb0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -15,11 +15,7 @@ impl Register {
} }
pub fn new_with_values(name: char, values: Vec<String>) -> Self { pub fn new_with_values(name: char, values: Vec<String>) -> Self {
if name == '_' { Self { name, values }
Self::new(name)
} else {
Self { name, values }
}
} }
pub const fn name(&self) -> char { pub const fn name(&self) -> char {
@ -31,15 +27,11 @@ impl Register {
} }
pub fn write(&mut self, values: Vec<String>) { pub fn write(&mut self, values: Vec<String>) {
if self.name != '_' { self.values = values;
self.values = values;
}
} }
pub fn push(&mut self, value: String) { 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) self.inner.get(&name)
} }
pub fn get_mut(&mut self, name: char) -> &mut Register { pub fn read(&self, name: char) -> Option<&[String]> {
self.inner self.get(name).map(|reg| reg.read())
.entry(name)
.or_insert_with(|| Register::new(name))
} }
pub fn write(&mut self, name: char, values: Vec<String>) { pub fn write(&mut self, name: char, values: Vec<String>) {
self.inner if name != '_' {
.insert(name, Register::new_with_values(name, values)); self.inner
.insert(name, Register::new_with_values(name, values));
}
} }
pub fn read(&self, name: char) -> Option<&[String]> { pub fn push(&mut self, name: char, value: String) {
self.get(name).map(|reg| reg.read()) 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> { pub fn first(&self, name: char) -> Option<&String> {

@ -1805,7 +1805,7 @@ fn search_selection(cx: &mut Context) {
.join("|"); .join("|");
let msg = format!("register '{}' set to '{}'", '/', &regex); let msg = format!("register '{}' set to '{}'", '/', &regex);
cx.editor.registers.get_mut('/').push(regex); cx.editor.registers.push('/', regex);
cx.editor.set_status(msg); cx.editor.set_status(msg);
} }
@ -2121,16 +2121,14 @@ enum Operation {
fn delete_selection_impl(cx: &mut Context, op: Operation) { fn delete_selection_impl(cx: &mut Context, op: Operation) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);
let selection = doc.selection(view.id); let selection = doc.selection(view.id);
if cx.register != Some('_') { if cx.register != Some('_') {
// first yank the selection // first yank the selection
let text = doc.text().slice(..);
let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect(); let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
let reg_name = cx.register.unwrap_or('"'); let reg_name = cx.register.unwrap_or('"');
let registers = &mut cx.editor.registers; cx.editor.registers.write(reg_name, values);
let reg = registers.get_mut(reg_name);
reg.write(values);
}; };
// then delete // then delete
@ -5005,7 +5003,7 @@ fn record_macro(cx: &mut Context) {
} }
}) })
.collect::<String>(); .collect::<String>();
cx.editor.registers.get_mut(reg).write(vec![s]); cx.editor.registers.write(reg, vec![s]);
cx.editor cx.editor
.set_status(format!("Recorded to register [{}]", reg)); .set_status(format!("Recorded to register [{}]", reg));
} else { } else {

@ -294,23 +294,22 @@ impl Prompt {
direction: CompletionDirection, direction: CompletionDirection,
) { ) {
(self.callback_fn)(cx, &self.line, PromptEvent::Abort); (self.callback_fn)(cx, &self.line, PromptEvent::Abort);
let register = cx.editor.registers.get_mut(register).read(); let values = match cx.editor.registers.read(register) {
Some(values) if !values.is_empty() => values,
if register.is_empty() { _ => return,
return; };
}
let end = register.len().saturating_sub(1); let end = values.len().saturating_sub(1);
let index = match direction { let index = match direction {
CompletionDirection::Forward => self.history_pos.map_or(0, |i| i + 1), CompletionDirection::Forward => self.history_pos.map_or(0, |i| i + 1),
CompletionDirection::Backward => { CompletionDirection::Backward => {
self.history_pos.unwrap_or(register.len()).saturating_sub(1) self.history_pos.unwrap_or(values.len()).saturating_sub(1)
} }
} }
.min(end); .min(end);
self.line = register[index].clone(); self.line = values[index].clone();
self.history_pos = Some(index); self.history_pos = Some(index);
@ -548,10 +547,7 @@ impl Component for Prompt {
if last_item != self.line { if last_item != self.line {
// store in history // store in history
if let Some(register) = self.history_register { if let Some(register) = self.history_register {
cx.editor cx.editor.registers.push(register, self.line.clone());
.registers
.get_mut(register)
.push(self.line.clone());
}; };
} }

Loading…
Cancel
Save