Add movement shortcut for history (#1088)

alt-u and alt-U
pull/999/head
Ivan Tham 3 years ago committed by GitHub
parent 6fa76d9fe7
commit b7c3877e94
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -61,6 +61,8 @@
| `.` | Repeat last change | N/A | | `.` | Repeat last change | N/A |
| `u` | Undo change | `undo` | | `u` | Undo change | `undo` |
| `U` | Redo change | `redo` | | `U` | Redo change | `redo` |
| `Alt-u` | Move backward in history | `earlier` |
| `Alt-U` | Move forward in history | `later` |
| `y` | Yank selection | `yank` | | `y` | Yank selection | `yank` |
| `p` | Paste after selection | `paste_after` | | `p` | Paste after selection | `paste_after` |
| `P` | Paste before selection | `paste_before` | | `P` | Paste before selection | `paste_before` |

@ -282,7 +282,7 @@ impl History {
} }
/// Whether to undo by a number of edits or a duration of time. /// Whether to undo by a number of edits or a duration of time.
#[derive(Debug, PartialEq)] #[derive(Debug, PartialEq, Clone, Copy)]
pub enum UndoKind { pub enum UndoKind {
Steps(usize), Steps(usize),
TimePeriod(std::time::Duration), TimePeriod(std::time::Duration),

@ -1,5 +1,7 @@
use helix_core::{ use helix_core::{
comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes, indent, comment, coords_at_pos, find_first_non_whitespace_char, find_root, graphemes,
history::UndoKind,
indent,
indent::IndentStyle, indent::IndentStyle,
line_ending::{get_line_ending_of_str, line_end_char_index, str_is_line_ending}, line_ending::{get_line_ending_of_str, line_end_char_index, str_is_line_ending},
match_brackets, match_brackets,
@ -284,6 +286,8 @@ impl Command {
delete_word_backward, "Delete previous word", delete_word_backward, "Delete previous word",
undo, "Undo change", undo, "Undo change",
redo, "Redo change", redo, "Redo change",
earlier, "Move backward in history",
later, "Move forward in history",
yank, "Yank selection", yank, "Yank selection",
yank_joined_to_clipboard, "Join and yank selections to clipboard", yank_joined_to_clipboard, "Join and yank selections to clipboard",
yank_main_selection_to_clipboard, "Yank main selection to clipboard", yank_main_selection_to_clipboard, "Yank main selection to clipboard",
@ -1877,10 +1881,7 @@ mod cmd {
args: &[&str], args: &[&str],
_event: PromptEvent, _event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let uk = args let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?;
.join(" ")
.parse::<helix_core::history::UndoKind>()
.map_err(|s| anyhow!(s))?;
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let success = doc.earlier(view.id, uk); let success = doc.earlier(view.id, uk);
@ -1896,10 +1897,7 @@ mod cmd {
args: &[&str], args: &[&str],
_event: PromptEvent, _event: PromptEvent,
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
let uk = args let uk = args.join(" ").parse::<UndoKind>().map_err(|s| anyhow!(s))?;
.join(" ")
.parse::<helix_core::history::UndoKind>()
.map_err(|s| anyhow!(s))?;
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let success = doc.later(view.id, uk); let success = doc.later(view.id, uk);
if !success { if !success {
@ -3963,20 +3961,48 @@ pub mod insert {
// storing it? // storing it?
fn undo(cx: &mut Context) { fn undo(cx: &mut Context) {
let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let view_id = view.id; for _ in 0..count {
let success = doc.undo(view_id); if !doc.undo(view.id) {
if !success {
cx.editor.set_status("Already at oldest change".to_owned()); cx.editor.set_status("Already at oldest change".to_owned());
break;
}
} }
} }
fn redo(cx: &mut Context) { fn redo(cx: &mut Context) {
let count = cx.count();
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let view_id = view.id; for _ in 0..count {
let success = doc.redo(view_id); if !doc.redo(view.id) {
if !success {
cx.editor.set_status("Already at newest change".to_owned()); cx.editor.set_status("Already at newest change".to_owned());
break;
}
}
}
fn earlier(cx: &mut Context) {
let count = cx.count();
let (view, doc) = current!(cx.editor);
for _ in 0..count {
// rather than doing in batch we do this so get error halfway
if !doc.earlier(view.id, UndoKind::Steps(1)) {
cx.editor.set_status("Already at oldest change".to_owned());
break;
}
}
}
fn later(cx: &mut Context) {
let count = cx.count();
let (view, doc) = current!(cx.editor);
for _ in 0..count {
// rather than doing in batch we do this so get error halfway
if !doc.later(view.id, UndoKind::Steps(1)) {
cx.editor.set_status("Already at newest change".to_owned());
break;
}
} }
} }

@ -582,6 +582,8 @@ impl Default for Keymaps {
"u" => undo, "u" => undo,
"U" => redo, "U" => redo,
"A-u" => earlier,
"A-U" => later,
"y" => yank, "y" => yank,
// yank_all // yank_all

Loading…
Cancel
Save