Add movement shortcut for history (#1088)

alt-u and alt-U
This commit is contained in:
Ivan Tham 2021-11-14 23:16:47 +08:00 committed by GitHub
parent 6fa76d9fe7
commit b7c3877e94
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 48 additions and 18 deletions

View file

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

View file

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

View file

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

View file

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