Skip to content

Commit

Permalink
Add Vi o command to swap anchor and cursor (#877)
Browse files Browse the repository at this point in the history
* Add command to swap anchor and cursor

* Add test for swapping anchor and cursor
  • Loading branch information
thomasschafer authored Feb 13, 2025
1 parent 446fb1d commit 6133ec0
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
34 changes: 34 additions & 0 deletions src/core_editor/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ impl Editor {
);
}
}
EditCommand::SwapCursorAndAnchor => self.swap_cursor_and_anchor(),
#[cfg(feature = "system_clipboard")]
EditCommand::CutSelectionSystem => self.cut_selection_to_system(),
#[cfg(feature = "system_clipboard")]
Expand Down Expand Up @@ -194,6 +195,14 @@ impl Editor {

self.update_undo_state(new_undo_behavior);
}

fn swap_cursor_and_anchor(&mut self) {
if let Some(anchor) = self.selection_anchor {
self.selection_anchor = Some(self.insertion_point());
self.line_buffer.set_insertion_point(anchor);
}
}

fn update_selection_anchor(&mut self, select: bool) {
self.selection_anchor = if select {
self.selection_anchor
Expand Down Expand Up @@ -1118,6 +1127,31 @@ mod test {
editor.run_edit_command(&EditCommand::Undo);
assert_eq!(editor.get_buffer(), "This \r\n is a test");
}

#[test]
fn test_swap_cursor_and_anchor() {
let mut editor = editor_with("This is some test content");
editor.line_buffer.set_insertion_point(0);
editor.update_selection_anchor(true);

for _ in 0..3 {
editor.run_edit_command(&EditCommand::MoveRight { select: true });
}
assert_eq!(editor.selection_anchor, Some(0));
assert_eq!(editor.insertion_point(), 3);
assert_eq!(editor.get_selection(), Some((0, 4)));

editor.run_edit_command(&EditCommand::SwapCursorAndAnchor);
assert_eq!(editor.selection_anchor, Some(3));
assert_eq!(editor.insertion_point(), 0);
assert_eq!(editor.get_selection(), Some((0, 4)));

editor.run_edit_command(&EditCommand::SwapCursorAndAnchor);
assert_eq!(editor.selection_anchor, Some(0));
assert_eq!(editor.insertion_point(), 3);
assert_eq!(editor.get_selection(), Some((0, 4)));
}

#[cfg(feature = "system_clipboard")]
mod without_system_clipboard {
use super::*;
Expand Down
8 changes: 8 additions & 0 deletions src/edit_mode/vi/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ where
let _ = input.next();
Some(Command::RepeatLastAction)
}
Some('o') => {
let _ = input.next();
Some(Command::SwapCursorAndAnchor)
}
_ => None,
}
}
Expand Down Expand Up @@ -143,6 +147,7 @@ pub enum Command {
ChangeInsidePair { left: char, right: char },
DeleteInsidePair { left: char, right: char },
YankInsidePair { left: char, right: char },
SwapCursorAndAnchor,
}

impl Command {
Expand Down Expand Up @@ -222,6 +227,9 @@ impl Command {
right: *right,
})]
}
Self::SwapCursorAndAnchor => {
vec![ReedlineOption::Edit(EditCommand::SwapCursorAndAnchor)]
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ pub enum EditCommand {

/// Copy left before char
CopyLeftBefore(char),
/// Swap the positions of the cursor and anchor
SwapCursorAndAnchor,

/// Cut selection to system clipboard
#[cfg(feature = "system_clipboard")]
Expand Down Expand Up @@ -448,6 +450,7 @@ impl Display for EditCommand {
EditCommand::CopyRightBefore(_) => write!(f, "CopyRightBefore Value: <char>"),
EditCommand::CopyLeftUntil(_) => write!(f, "CopyLeftUntil Value: <char>"),
EditCommand::CopyLeftBefore(_) => write!(f, "CopyLeftBefore Value: <char>"),
EditCommand::SwapCursorAndAnchor => write!(f, "SwapCursorAndAnchor"),
#[cfg(feature = "system_clipboard")]
EditCommand::CutSelectionSystem => write!(f, "CutSelectionSystem"),
#[cfg(feature = "system_clipboard")]
Expand Down Expand Up @@ -486,6 +489,7 @@ impl EditCommand {
| EditCommand::MoveLeftBefore { select, .. } => {
EditType::MoveCursor { select: *select }
}
EditCommand::SwapCursorAndAnchor => EditType::MoveCursor { select: true },

EditCommand::SelectAll => EditType::MoveCursor { select: true },
// Text edits
Expand Down

0 comments on commit 6133ec0

Please sign in to comment.