Files
oak-gpui/crates/vim/src/indent.rs
T
AidanVandConrad Irwin f702575255 Add support for resizing panes using vim motions (#21038)
Closes #8628

Release Notes:

- Added support for resizing the current pane using vim keybinds with
the intention to follow the functionality of vim
  - "ctrl-w +" to make a pane taller 
  - "ctrl-w -" to make the pane shorter
  - "ctrl-w >" to make a pane wider
  - "ctrl-w <" to make the pane narrower
- Changed vim pre_count and post_count to globals to allow for other
crates to use the vim count. In this case, it allows for resizing by
more than one unit. For example, "10 ctrl-w -" will decrease the height
of the pane 10 times more than "ctrl-w -"
- This pr does **not** add keybinds for making all panes in an axis
equal size and does **not** add support for resizing docks. This is
mentioned because these could be implied by the original issue

---------

Co-authored-by: Conrad Irwin <conrad.irwin@gmail.com>
2024-11-26 16:24:29 -08:00

139 lines
5.0 KiB
Rust

use crate::{motion::Motion, object::Object, state::Mode, Vim};
use collections::HashMap;
use editor::{display_map::ToDisplayPoint, Bias, Editor};
use gpui::actions;
use language::SelectionGoal;
use ui::ViewContext;
#[derive(PartialEq, Eq)]
pub(crate) enum IndentDirection {
In,
Out,
}
actions!(vim, [Indent, Outdent,]);
pub(crate) fn register(editor: &mut Editor, cx: &mut ViewContext<Vim>) {
Vim::action(editor, cx, |vim, _: &Indent, cx| {
vim.record_current_action(cx);
let count = Vim::take_count(cx).unwrap_or(1);
vim.store_visual_marks(cx);
vim.update_editor(cx, |vim, editor, cx| {
editor.transact(cx, |editor, cx| {
let original_positions = vim.save_selection_starts(editor, cx);
for _ in 0..count {
editor.indent(&Default::default(), cx);
}
vim.restore_selection_cursors(editor, cx, original_positions);
});
});
if vim.mode.is_visual() {
vim.switch_mode(Mode::Normal, true, cx)
}
});
Vim::action(editor, cx, |vim, _: &Outdent, cx| {
vim.record_current_action(cx);
let count = Vim::take_count(cx).unwrap_or(1);
vim.store_visual_marks(cx);
vim.update_editor(cx, |vim, editor, cx| {
editor.transact(cx, |editor, cx| {
let original_positions = vim.save_selection_starts(editor, cx);
for _ in 0..count {
editor.outdent(&Default::default(), cx);
}
vim.restore_selection_cursors(editor, cx, original_positions);
});
});
if vim.mode.is_visual() {
vim.switch_mode(Mode::Normal, true, cx)
}
});
}
impl Vim {
pub(crate) fn indent_motion(
&mut self,
motion: Motion,
times: Option<usize>,
dir: IndentDirection,
cx: &mut ViewContext<Self>,
) {
self.stop_recording(cx);
self.update_editor(cx, |_, editor, cx| {
let text_layout_details = editor.text_layout_details(cx);
editor.transact(cx, |editor, cx| {
let mut selection_starts: HashMap<_, _> = Default::default();
editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| {
let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
selection_starts.insert(selection.id, anchor);
motion.expand_selection(map, selection, times, false, &text_layout_details);
});
});
if dir == IndentDirection::In {
editor.indent(&Default::default(), cx);
} else {
editor.outdent(&Default::default(), cx);
}
editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| {
let anchor = selection_starts.remove(&selection.id).unwrap();
selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
});
});
});
});
}
pub(crate) fn indent_object(
&mut self,
object: Object,
around: bool,
dir: IndentDirection,
cx: &mut ViewContext<Self>,
) {
self.stop_recording(cx);
self.update_editor(cx, |_, editor, cx| {
editor.transact(cx, |editor, cx| {
let mut original_positions: HashMap<_, _> = Default::default();
editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| {
let anchor = map.display_point_to_anchor(selection.head(), Bias::Right);
original_positions.insert(selection.id, anchor);
object.expand_selection(map, selection, around);
});
});
if dir == IndentDirection::In {
editor.indent(&Default::default(), cx);
} else {
editor.outdent(&Default::default(), cx);
}
editor.change_selections(None, cx, |s| {
s.move_with(|map, selection| {
let anchor = original_positions.remove(&selection.id).unwrap();
selection.collapse_to(anchor.to_display_point(map), SelectionGoal::None);
});
});
});
});
}
}
#[cfg(test)]
mod test {
use crate::test::NeovimBackedTestContext;
#[gpui::test]
async fn test_indent_gv(cx: &mut gpui::TestAppContext) {
let mut cx = NeovimBackedTestContext::new(cx).await;
cx.set_neovim_option("shiftwidth=4").await;
cx.set_shared_state("ˇhello\nworld\n").await;
cx.simulate_shared_keystrokes("v j > g v").await;
cx.shared_state()
.await
.assert_eq("« hello\n ˇ» world\n");
}
}