Skip to content

Commit

Permalink
Use refactored Registers types
Browse files Browse the repository at this point in the history
This change switches from Register/Registers from helix-core to the
new registers types introduced in the parent commits. This is an
unfortunately noisy change: we need to update virtually all callsites
that access the registers.

We need to move Registers out of the Editor type to make this work:
Registers taking '&mut self' and Editor parameters ('&' or '&mut')
causes a borrowing conflict if Registers is part of Editor. We move
Registers up to the Context types to resolve this.
  • Loading branch information
the-mikedavis committed May 13, 2023
1 parent 2b3b29d commit 8e5d159
Show file tree
Hide file tree
Showing 10 changed files with 90 additions and 162 deletions.
1 change: 0 additions & 1 deletion helix-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ pub mod movement;
pub mod object;
pub mod path;
mod position;
pub mod register;
pub mod search;
pub mod selection;
pub mod shellwords;
Expand Down
89 changes: 0 additions & 89 deletions helix-core/src/register.rs

This file was deleted.

6 changes: 6 additions & 0 deletions helix-term/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use helix_view::{
document::DocumentSavedEventResult,
editor::{ConfigEvent, EditorEvent},
graphics::Rect,
register::Registers,
theme,
tree::Layout,
Align, Editor,
Expand Down Expand Up @@ -64,6 +65,7 @@ pub struct Application {
compositor: Compositor,
terminal: Terminal,
pub editor: Editor,
registers: Registers,

config: Arc<ArcSwap<Config>>,

Expand Down Expand Up @@ -237,6 +239,7 @@ impl Application {
compositor,
terminal,
editor,
registers: Registers::default(),

config,

Expand All @@ -255,6 +258,7 @@ impl Application {
async fn render(&mut self) {
let mut cx = crate::compositor::Context {
editor: &mut self.editor,
registers: &mut self.registers,
jobs: &mut self.jobs,
scroll: None,
};
Expand Down Expand Up @@ -505,6 +509,7 @@ impl Application {
pub async fn handle_idle_timeout(&mut self) {
let mut cx = crate::compositor::Context {
editor: &mut self.editor,
registers: &mut self.registers,
jobs: &mut self.jobs,
scroll: None,
};
Expand Down Expand Up @@ -625,6 +630,7 @@ impl Application {
) {
let mut cx = crate::compositor::Context {
editor: &mut self.editor,
registers: &mut self.registers,
jobs: &mut self.jobs,
scroll: None,
};
Expand Down
92 changes: 58 additions & 34 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ use helix_view::{
info::Info,
input::KeyEvent,
keyboard::KeyCode,
register::Registers,
tree,
view::View,
Document, DocumentId, Editor, ViewId,
Expand Down Expand Up @@ -83,6 +84,7 @@ pub struct Context<'a> {
pub register: Option<char>,
pub count: Option<NonZeroUsize>,
pub editor: &'a mut Editor,
pub registers: &'a mut Registers,

pub callback: Option<crate::compositor::Callback>,
pub on_next_key_callback: Option<OnKeyCallback>,
Expand Down Expand Up @@ -189,6 +191,7 @@ impl MappableCommand {
if let Some(command) = typed::TYPABLE_COMMAND_MAP.get(name.as_str()) {
let mut cx = compositor::Context {
editor: cx.editor,
registers: cx.registers,
jobs: cx.jobs,
scroll: None,
};
Expand Down Expand Up @@ -1828,11 +1831,13 @@ fn search_impl(

fn search_completions(cx: &mut Context, reg: Option<char>) -> Vec<String> {
let mut items = reg
.and_then(|reg| cx.editor.registers.get(reg))
.map_or(Vec::new(), |reg| reg.read().iter().take(200).collect());
.and_then(|reg| cx.registers.read(reg, cx.editor))
.map_or(Vec::new(), |contents| {
contents.iter().take(200).cloned().collect()
});
items.sort_unstable();
items.dedup();
items.into_iter().cloned().collect()
items.into_iter().collect()
}

fn search(cx: &mut Context) {
Expand Down Expand Up @@ -1891,9 +1896,8 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
let count = cx.count();
let config = cx.editor.config();
let scrolloff = config.scrolloff;
let (_, doc) = current!(cx.editor);
let registers = &cx.editor.registers;
if let Some(query) = registers.read('/').and_then(|query| query.last()) {
if let Some(query) = cx.registers.last('/', cx.editor) {
let doc = doc!(cx.editor);
let contents = doc.text().slice(..).to_string();
let search_config = &config.search;
let case_insensitive = if search_config.smart_case {
Expand All @@ -1902,7 +1906,7 @@ fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Dir
false
};
let wrap_around = search_config.wrap_around;
if let Ok(regex) = RegexBuilder::new(query)
if let Ok(regex) = RegexBuilder::new(&query)
.case_insensitive(case_insensitive)
.multi_line(true)
.build()
Expand Down Expand Up @@ -1955,12 +1959,14 @@ fn search_selection(cx: &mut Context) {
.join("|");

let msg = format!("register '{}' set to '{}'", '/', &regex);
cx.editor.registers.push('/', regex);
cx.editor.set_status(msg);
match cx.registers.push('/', cx.editor, regex) {
Ok(_) => cx.editor.set_status(msg),
Err(err) => cx.editor.set_error(err.to_string()),
}
}

fn make_search_word_bounded(cx: &mut Context) {
let regex = match cx.editor.registers.last('/') {
let regex = match cx.registers.last('/', cx.editor) {
Some(regex) => regex,
None => return,
};
Expand All @@ -1978,14 +1984,16 @@ fn make_search_word_bounded(cx: &mut Context) {
if !start_anchored {
new_regex.push_str("\\b");
}
new_regex.push_str(regex);
new_regex.push_str(&regex);
if !end_anchored {
new_regex.push_str("\\b");
}

let msg = format!("register '{}' set to '{}'", '/', &new_regex);
cx.editor.registers.push('/', new_regex);
cx.editor.set_status(msg);
match cx.registers.push('/', cx.editor, regex) {
Ok(_) => cx.editor.set_status(msg),
Err(err) => cx.editor.set_error(err.to_string()),
}
}

fn global_search(cx: &mut Context) {
Expand Down Expand Up @@ -2295,20 +2303,24 @@ enum Operation {
}

fn delete_selection_impl(cx: &mut Context, op: Operation) {
let (view, doc) = current!(cx.editor);
let (view, doc) = current_ref!(cx.editor);

let selection = doc.selection(view.id);
let selection = doc.selection(view.id).clone();

if cx.register != Some('_') {
// first yank the selection
let text = doc.text().slice(..);
let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
let reg_name = cx.register.unwrap_or('"');
cx.editor.registers.write(reg_name, values);
if let Err(err) = cx.registers.write(reg_name, cx.editor, values) {
cx.editor.set_error(err.to_string());
}
};

let (view, doc) = current!(cx.editor);

// then delete
let transaction = Transaction::change_by_selection(doc.text(), selection, |range| {
let transaction = Transaction::change_by_selection(doc.text(), &selection, |range| {
(range.from(), range.to(), None)
});
doc.apply(&transaction, view.id);
Expand Down Expand Up @@ -2677,6 +2689,7 @@ pub fn command_palette(cx: &mut Context) {
register: None,
count: std::num::NonZeroUsize::new(1),
editor: cx.editor,
registers: cx.registers,
callback: None,
on_next_key_callback: None,
jobs: cx.jobs,
Expand Down Expand Up @@ -3616,11 +3629,14 @@ fn yank(cx: &mut Context) {
cx.register.unwrap_or('"')
);

cx.editor
match cx
.registers
.write(cx.register.unwrap_or('"'), values);
.write(cx.register.unwrap_or('"'), cx.editor, values)
{
Ok(_) => cx.editor.set_status(msg),
Err(err) => cx.editor.set_error(err.to_string()),
}

cx.editor.set_status(msg);
exit_select_mode(cx);
}

Expand Down Expand Up @@ -3859,10 +3875,10 @@ fn paste_primary_clipboard_before(cx: &mut Context) {
fn replace_with_yanked(cx: &mut Context) {
let count = cx.count();
let reg_name = cx.register.unwrap_or('"');
let (view, doc) = current!(cx.editor);
let registers = &mut cx.editor.registers;

if let Some(values) = registers.read(reg_name) {
if let Some(values) = cx.registers.read(reg_name, cx.editor) {
let (view, doc) = current!(cx.editor);

if !values.is_empty() {
let repeat = std::iter::repeat(
values
Expand Down Expand Up @@ -3928,11 +3944,11 @@ fn replace_selections_with_primary_clipboard(cx: &mut Context) {
fn paste(cx: &mut Context, pos: Paste) {
let count = cx.count();
let reg_name = cx.register.unwrap_or('"');
let (view, doc) = current!(cx.editor);
let registers = &mut cx.editor.registers;

if let Some(values) = registers.read(reg_name) {
paste_impl(values, doc, view, pos, count, cx.editor.mode);
if let Some(values) = cx.registers.read(reg_name, cx.editor) {
let (view, doc) = current!(cx.editor);

paste_impl(&values, doc, view, pos, count, cx.editor.mode);
}
}

Expand Down Expand Up @@ -4639,7 +4655,7 @@ fn wonly(cx: &mut Context) {
}

fn select_register(cx: &mut Context) {
cx.editor.autoinfo = Some(Info::from_registers(&cx.editor.registers));
cx.editor.autoinfo = Some(Info::from_registers(cx.registers));
cx.on_next_key(move |cx, event| {
if let Some(ch) = event.char() {
cx.editor.autoinfo = None;
Expand All @@ -4649,7 +4665,7 @@ fn select_register(cx: &mut Context) {
}

fn insert_register(cx: &mut Context) {
cx.editor.autoinfo = Some(Info::from_registers(&cx.editor.registers));
cx.editor.autoinfo = Some(Info::from_registers(cx.registers));
cx.on_next_key(move |cx, event| {
if let Some(ch) = event.char() {
cx.editor.autoinfo = None;
Expand Down Expand Up @@ -5362,9 +5378,12 @@ fn record_macro(cx: &mut Context) {
}
})
.collect::<String>();
cx.editor.registers.write(reg, vec![s]);
cx.editor
.set_status(format!("Recorded to register [{}]", reg));
match cx.registers.write(reg, cx.editor, vec![s]) {
Ok(_) => cx
.editor
.set_status(format!("Recorded to register [{}]", reg)),
Err(err) => cx.editor.set_error(err.to_string()),
}
} else {
let reg = cx.register.take().unwrap_or('@');
cx.editor.macro_recording = Some((reg, Vec::new()));
Expand All @@ -5384,8 +5403,13 @@ fn replay_macro(cx: &mut Context) {
return;
}

let keys: Vec<KeyEvent> = if let Some([keys_str]) = cx.editor.registers.read(reg) {
match helix_view::input::parse_macro(keys_str) {
let keys: Vec<KeyEvent> = if let Some(keys_str) = cx
.registers
.read(reg, cx.editor)
.filter(|values| values.len() == 1)
.map(|values| values[0].clone())
{
match helix_view::input::parse_macro(&keys_str) {
Ok(keys) => keys,
Err(err) => {
cx.editor.set_error(format!("Invalid macro: {}", err));
Expand Down
4 changes: 2 additions & 2 deletions helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2178,7 +2178,7 @@ fn clear_register(

ensure!(args.len() <= 1, ":clear-register takes at most 1 argument");
if args.is_empty() {
cx.editor.registers.clear();
cx.registers.clear();
cx.editor.set_status("All registers cleared");
return Ok(());
}
Expand All @@ -2188,7 +2188,7 @@ fn clear_register(
format!("Invalid register {}", args[0])
);
let register = args[0].chars().next().unwrap_or_default();
match cx.editor.registers.remove(register) {
match cx.registers.remove(register) {
Some(_) => cx
.editor
.set_status(format!("Register {} cleared", register)),
Expand Down
Loading

0 comments on commit 8e5d159

Please sign in to comment.