Add `move_buffer_{left,right,start,end}` commands

pull/11790/head
emilylime 2 months ago
parent 8e41e19890
commit 8373919e6d

@ -422,10 +422,14 @@ impl MappableCommand {
goto_last_change, "Goto last change", goto_last_change, "Goto last change",
goto_line_start, "Goto line start", goto_line_start, "Goto line start",
goto_line_end, "Goto line end", goto_line_end, "Goto line end",
goto_next_buffer, "Goto next buffer",
goto_previous_buffer, "Goto previous buffer",
goto_line_end_newline, "Goto newline at line end", goto_line_end_newline, "Goto newline at line end",
goto_first_nonwhitespace, "Goto first non-blank in line", goto_first_nonwhitespace, "Goto first non-blank in line",
goto_next_buffer, "Goto next buffer",
goto_previous_buffer, "Goto previous buffer",
move_buffer_left, "Move focused buffer to the left",
move_buffer_right, "Move focused buffer to the right",
move_buffer_start, "Move focused buffer to the start of the buffer list",
move_buffer_end, "Move focused buffer to the end of the buffer list",
trim_selections, "Trim whitespace from selections", trim_selections, "Trim whitespace from selections",
extend_to_line_start, "Extend to line start", extend_to_line_start, "Extend to line start",
extend_to_first_nonwhitespace, "Extend to first non-blank in line", extend_to_first_nonwhitespace, "Extend to first non-blank in line",
@ -888,6 +892,30 @@ fn goto_buffer(editor: &mut Editor, direction: Direction, count: usize) {
editor.switch(id, Action::Replace); editor.switch(id, Action::Replace);
} }
fn move_buffer_left(cx: &mut Context) {
let count = cx.count();
// Equivalent to `(i - count).rem_euclid(len)` but avoids overflow issues.
move_buffer(cx.editor, |i, len| (i + len - count % len) % len)
}
fn move_buffer_right(cx: &mut Context) {
let count = cx.count();
move_buffer(cx.editor, |i, len| (i + count) % len)
}
fn move_buffer_start(cx: &mut Context) {
move_buffer(cx.editor, |_, _| 0)
}
fn move_buffer_end(cx: &mut Context) {
move_buffer(cx.editor, |_, len| len - 1)
}
#[inline]
fn move_buffer(editor: &mut Editor, index_transform: impl FnOnce(usize, usize) -> usize) {
let current = view!(editor).doc;
let index = editor.documents.get_index_of(&current).unwrap();
let new_index = index_transform(index, editor.documents.len());
editor.documents.move_index(index, new_index);
}
fn extend_to_line_start(cx: &mut Context) { fn extend_to_line_start(cx: &mut Context) {
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
goto_line_start_impl(view, doc, Movement::Extend) goto_line_start_impl(view, doc, Movement::Extend)

@ -119,6 +119,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"e" => goto_prev_entry, "e" => goto_prev_entry,
"T" => goto_prev_test, "T" => goto_prev_test,
"p" => goto_prev_paragraph, "p" => goto_prev_paragraph,
"[" => move_buffer_left,
"{" => move_buffer_start,
"space" => add_newline_above, "space" => add_newline_above,
}, },
"]" => { "Right bracket" "]" => { "Right bracket"
@ -133,6 +135,8 @@ pub fn default() -> HashMap<Mode, KeyTrie> {
"e" => goto_next_entry, "e" => goto_next_entry,
"T" => goto_next_test, "T" => goto_next_test,
"p" => goto_next_paragraph, "p" => goto_next_paragraph,
"]" => move_buffer_right,
"}" => move_buffer_end,
"space" => add_newline_below, "space" => add_newline_below,
}, },

Loading…
Cancel
Save