Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement register history and access using a picker. #10604

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 19 additions & 0 deletions Cargo.lock

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

119 changes: 103 additions & 16 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,8 @@ impl MappableCommand {
paste_clipboard_before, "Paste clipboard before selections",
paste_primary_clipboard_after, "Paste primary clipboard after selections",
paste_primary_clipboard_before, "Paste primary clipboard before selections",
paste_picker_after, "Paste after using picker",
paste_picker_before, "Paste before using picker",
indent, "Indent selection",
unindent, "Unindent selection",
format_selections, "Format selection",
Expand Down Expand Up @@ -517,6 +519,7 @@ impl MappableCommand {
decrement, "Decrement item under cursor",
record_macro, "Record macro",
replay_macro, "Replay macro",
replay_macro_picker, "Replay macro picker",
command_palette, "Open command palette",
goto_word, "Jump to a two-character label",
extend_to_word, "Extend to a two-character label",
Expand Down Expand Up @@ -2190,7 +2193,7 @@ fn search_selection(cx: &mut Context) {
.join("|");

let msg = format!("register '{}' set to '{}'", register, &regex);
match cx.editor.registers.push(register, regex) {
match cx.editor.registers.push(register, &regex) {
Ok(_) => {
cx.editor.registers.last_search_register = register;
cx.editor.set_status(msg)
Expand Down Expand Up @@ -2230,7 +2233,7 @@ fn make_search_word_bounded(cx: &mut Context) {
}

let msg = format!("register '{}' set to '{}'", register, &new_regex);
match cx.editor.registers.push(register, new_regex) {
match cx.editor.registers.push(register, &new_regex) {
Ok(_) => {
cx.editor.registers.last_search_register = register;
cx.editor.set_status(msg)
Expand Down Expand Up @@ -2670,7 +2673,7 @@ fn delete_selection_impl(cx: &mut Context, op: Operation, yank: YankAction) {
let text = doc.text().slice(..);
let values: Vec<String> = selection.fragments(text).map(Cow::into_owned).collect();
let reg_name = cx.register.unwrap_or('"');
if let Err(err) = cx.editor.registers.write(reg_name, values) {
if let Err(err) = cx.editor.registers.push_many(reg_name, &values) {
cx.editor.set_error(err.to_string());
return;
}
Expand Down Expand Up @@ -4124,7 +4127,7 @@ fn yank_impl(editor: &mut Editor, register: char) {
.collect();
let selections = values.len();

match editor.registers.write(register, values) {
match editor.registers.push_many(register, &values) {
Ok(_) => editor.set_status(format!(
"yanked {selections} selection{} to register {register}",
if selections == 1 { "" } else { "s" }
Expand All @@ -4149,7 +4152,7 @@ fn yank_joined_impl(editor: &mut Editor, separator: &str, register: char) {
acc
});

match editor.registers.write(register, vec![joined]) {
match editor.registers.push(register, &joined) {
Ok(_) => editor.set_status(format!(
"joined and yanked {selections} selection{} to register {register}",
if selections == 1 { "" } else { "s" }
Expand Down Expand Up @@ -4182,7 +4185,7 @@ fn yank_primary_selection_impl(editor: &mut Editor, register: char) {

let selection = doc.selection(view.id).primary().fragment(text).to_string();

match editor.registers.write(register, vec![selection]) {
match editor.registers.push(register, &selection) {
Ok(_) => editor.set_status(format!("yanked primary selection to register {register}",)),
Err(err) => editor.set_error(err.to_string()),
}
Expand Down Expand Up @@ -4324,7 +4327,6 @@ fn replace_with_yanked_impl(editor: &mut Editor, register: char, count: usize) {
let Some(values) = editor
.registers
.read(register, editor)
.filter(|values| values.len() > 0)
else {
return;
};
Expand Down Expand Up @@ -4396,6 +4398,45 @@ fn paste_before(cx: &mut Context) {
exit_select_mode(cx);
}

fn paste_picker(cx: &mut Context, pos: Paste) {
let register = cx.register.unwrap_or('"');
cx.editor.autoinfo = Some(Info::from_selected_register(&cx.editor.registers, register));
cx.on_next_key(move |cx, event| {
if let Some(mut ch) = event.char() {
cx.editor.autoinfo = None;
if ch == 'p' {
// Allow a user to press p and get the no picker behavior.
ch = '0';
}
let Ok(index) = usize::from_str_radix(&ch.to_string(), 16) else {
log::debug!("During paste, {ch:?} was not parsed as a number");
return;
};
paste_from_index(cx.editor, register, index, pos, cx.count());
}
})
}

fn paste_from_index(editor: &mut Editor, register: char, index: usize, pos: Paste, count: usize) {
let Ok(Some(values)) = editor.registers.read_nth(register, index) else {
return;
};
let values: Vec<_> = values.map(|value| value.to_string()).collect();

let (view, doc) = current!(editor);
paste_impl(&values, doc, view, pos, count, editor.mode);
}

fn paste_picker_after(cx: &mut Context) {
paste_picker(cx, Paste::After);
exit_select_mode(cx);
}

fn paste_picker_before(cx: &mut Context) {
paste_picker(cx, Paste::Before);
exit_select_mode(cx);
}

fn get_lines(doc: &Document, view_id: ViewId) -> Vec<usize> {
let mut lines = Vec::new();

Expand Down Expand Up @@ -5202,12 +5243,7 @@ fn insert_register(cx: &mut Context) {
if let Some(ch) = event.char() {
cx.editor.autoinfo = None;
cx.register = Some(ch);
paste(
cx.editor,
cx.register.unwrap_or('"'),
Paste::Cursor,
cx.count(),
);
paste(cx.editor, ch, Paste::Cursor, cx.count());
}
})
}
Expand Down Expand Up @@ -5947,7 +5983,7 @@ fn record_macro(cx: &mut Context) {
}
})
.collect::<String>();
match cx.editor.registers.write(reg, vec![s]) {
match cx.editor.registers.push(reg, &s) {
Ok(_) => cx
.editor
.set_status(format!("Recorded to register [{}]", reg)),
Expand Down Expand Up @@ -5976,7 +6012,6 @@ fn replay_macro(cx: &mut Context) {
.editor
.registers
.read(reg, cx.editor)
.filter(|values| values.len() == 1)
.map(|mut values| values.next().unwrap())
{
match helix_view::input::parse_macro(&keys) {
Expand All @@ -5991,9 +6026,13 @@ fn replay_macro(cx: &mut Context) {
return;
};

replay_macro_impl(cx, reg, keys);
}

fn replay_macro_impl(cx: &mut Context, register: char, keys: Vec<KeyEvent>) {
// Once the macro has been fully validated, it's marked as being under replay
// to ensure we don't fall into infinite recursion.
cx.editor.macro_replaying.push(reg);
cx.editor.macro_replaying.push(register);

let count = cx.count();
cx.callback.push(Box::new(move |compositor, cx| {
Expand All @@ -6009,6 +6048,54 @@ fn replay_macro(cx: &mut Context) {
}));
}

fn replay_macro_picker(cx: &mut Context) {
let register = cx.register.unwrap_or('@');

if cx.editor.macro_replaying.contains(&register) {
cx.editor.set_error(format!(
"Cannot replay from register [{}] because already replaying from same register",
register
));
return;
}
cx.editor.autoinfo = Some(Info::from_selected_register(&cx.editor.registers, register));
cx.on_next_key(move |cx, event| {
cx.editor.autoinfo = None;

let Some(ch) = event.char() else {
return;
};
let Ok(index) = usize::from_str_radix(&ch.to_string(), 16) else {
log::debug!("During replay macro picker, {ch:?} was not parsed as a number");
return;
};
match parse_keys_from_register_index(cx, register, index) {
Ok(keys) => {
replay_macro_impl(cx, register, keys);
}
Err(err) => {
cx.editor.set_error(format!(
"Register [{register}] @ index {index}: invalid macro: {err}"
));
}
}
})
}

fn parse_keys_from_register_index(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to break this out because of a conflict between mutable and immutable borrows of Context and Editor.

cx: &mut Context,
register: char,
index: usize,
) -> Result<Vec<KeyEvent>, anyhow::Error> {
let Some(mut it) = cx.editor.registers.read_nth(register, index)? else {
return Err(anyhow!("Register [{register}] @ index {index} failed to read"));
};
let Some(keys) = it.next() else {
return Err(anyhow!("Register [{register}] @ index {index} was empty"));
};
helix_view::input::parse_macro(&keys)
}

fn goto_word(cx: &mut Context) {
jump_to_word(cx, Movement::Move)
}
Expand Down
2 changes: 1 addition & 1 deletion helix-term/src/commands/typed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2447,7 +2447,7 @@ fn yank_diagnostic(
bail!("No diagnostics under primary selection");
}

cx.editor.registers.write(reg, diag)?;
cx.editor.registers.push_many(reg, &diag)?;
cx.editor.set_status(format!(
"Yanked {n} diagnostic{} to register {reg}",
if n == 1 { "" } else { "s" }
Expand Down
5 changes: 3 additions & 2 deletions helix-term/src/keymap/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,12 +149,13 @@ pub fn default() -> HashMap<Mode, KeyTrie> {

"y" => yank,
// yank_all
"p" => paste_after,
"p" => paste_picker_after,
// paste_all
"P" => paste_before,
"P" => paste_picker_before,

"Q" => record_macro,
"q" => replay_macro,
"A-q" => replay_macro_picker,

">" => indent,
"<" => unindent,
Expand Down
29 changes: 9 additions & 20 deletions helix-term/src/ui/prompt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,26 +311,17 @@ impl Prompt {
direction: CompletionDirection,
) {
(self.callback_fn)(cx, &self.line, PromptEvent::Abort);
let mut values = match cx.editor.registers.read(register, cx.editor) {
Some(values) if values.len() > 0 => values.rev(),
_ => return,
};

let end = values.len().saturating_sub(1);

let index = match direction {
CompletionDirection::Forward => self.history_pos.map_or(0, |i| i + 1),
CompletionDirection::Backward => self
.history_pos
.unwrap_or_else(|| values.len())
.saturating_sub(1),
}
.min(end);

self.line = values.nth(index).unwrap().to_string();
// Appease the borrow checker.
drop(values);
CompletionDirection::Backward => {
self.history_pos.unwrap_or(usize::MAX).saturating_sub(1)
}
};
let Ok(Some(value)) = cx.editor.registers.read_nth(register, index) else {
return;
};

self.line = value.collect();
self.history_pos = Some(index);

self.move_end();
Expand Down Expand Up @@ -586,9 +577,7 @@ impl Component for Prompt {
if last_item != self.line {
// store in history
if let Some(register) = self.history_register {
if let Err(err) =
cx.editor.registers.push(register, self.line.clone())
{
if let Err(err) = cx.editor.registers.push(register, &self.line) {
cx.editor.set_error(err.to_string());
}
};
Expand Down