Skip to content

Commit

Permalink
feat(commands): avoid selections in the normal/insert mode
Browse files Browse the repository at this point in the history
  • Loading branch information
usagi-flow committed May 7, 2024
1 parent cf5cc0e commit 4a68266
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
16 changes: 16 additions & 0 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2799,6 +2799,11 @@ fn ensure_selections_forward(cx: &mut Context) {
}

pub fn enter_insert_mode(cx: &mut Context) {
if EvilCommands::is_enabled() {
// In evil mode, selections are possible in the selection/visual mode only.
EvilCommands::collapse_selections(cx, CollapseMode::Backward);
}

cx.editor.mode = Mode::Insert;
}

Expand Down Expand Up @@ -2851,6 +2856,12 @@ fn append_mode(cx: &mut Context) {
)
});
doc.set_selection(view.id, selection);

// We already collapsed selections in `enter_insert_mode()`, but this function creates selections again,
// and we want to leave the cursor(s) at the end of the range(s).
if EvilCommands::is_enabled() {
EvilCommands::collapse_selections(cx, CollapseMode::Forward);
}
}

fn file_picker(cx: &mut Context) {
Expand Down Expand Up @@ -3567,6 +3578,11 @@ pub fn select_mode(cx: &mut Context) {
}

pub fn exit_select_mode(cx: &mut Context) {
if EvilCommands::is_enabled() {
// In evil mode, selections are possible in the selection/visual mode only.
EvilCommands::collapse_selections(cx, CollapseMode::ToHead);
}

if cx.editor.mode == Mode::Select {
cx.editor.mode = Mode::Normal;
}
Expand Down
64 changes: 64 additions & 0 deletions helix-term/src/commands/evil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ enum Motion {
LineEnd,
}

#[derive(Debug)]
pub enum CollapseMode {
Forward,
Backward,
ToAnchor,
ToHead,
}

impl TryFrom<char> for Motion {
type Error = ();

Expand Down Expand Up @@ -104,6 +112,62 @@ impl EvilCommands {
true
}

/// Collapse selections such that the selections cover one character per cursor only.
pub fn collapse_selections(cx: &mut Context, collapse_mode: CollapseMode) {
let (view, doc) = current!(cx.editor);

doc.set_selection(
view.id,
doc.selection(view.id).clone().transform(|mut range| {
log::warn!(
"Adjusting range (mode: {:?}): {} -> {}",
collapse_mode,
range.anchor,
range.head
);

// TODO: when exiting insert mode after appending, we end up on the character _after_ the curson,
// while vim returns to the character _before_ the cursor.

match collapse_mode {
CollapseMode::Forward => {
let end = range.anchor.max(range.head);
range.anchor = 0.max(end - 1);
range.head = end;
}
CollapseMode::Backward => {
let start = range.anchor.min(range.head);
range.anchor = start;
range.head = start + 1;
}
CollapseMode::ToAnchor => {
if range.head > range.anchor {
range.head = range.anchor + 1;
} else {
range.head = 0.max(range.anchor - 1);
}
}
CollapseMode::ToHead => {
if range.head > range.anchor {
range.anchor = 0.max(range.head - 1);
} else {
range.anchor = range.head + 1;
}
}
}

log::warn!(
"- Adjusted range (mode: {:?}): {} -> {}",
collapse_mode,
range.anchor,
range.head
);

return range;
}),
);
}

fn context() -> RwLockReadGuard<'static, EvilContext> {
return CONTEXT.read().unwrap();
}
Expand Down

0 comments on commit 4a68266

Please sign in to comment.