Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Vi o command to swap anchor and cursor #877

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
}
Comment on lines +116 to +119
Copy link
Member

@sholderbach sholderbach Feb 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think in theory this dispatch from the parser is insufficient as it would compete with the normal mode o (open insert mode in the line below) and we don't discriminate between the modes here.

But as we don't have that o implemented yet (as far as i can tell) this should be fine.

_ => 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