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 Jul 23, 2023
1 parent 943b265 commit 7c752df
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 16 deletions.
45 changes: 29 additions & 16 deletions helix-term/src/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pub(crate) mod lsp;
pub(crate) mod typed;

pub use dap::*;
use helix_vcs::Hunk;
pub use evil::*;
use helix_vcs::Hunk;
pub use lsp::*;
use tokio::sync::oneshot;
use tui::widgets::Row;
Expand Down Expand Up @@ -2449,6 +2449,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 @@ -2501,6 +2506,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 @@ -3110,6 +3121,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 Expand Up @@ -5605,22 +5621,19 @@ where
let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..);

let selection = doc
.selection(view.id)
.clone()
.transform(|range| {
let old_anchor = range.anchor;
let mut new_range = move_fn(text, range, count);

new_range.anchor = match cx.editor.mode {
// In select mode, use a sticky anchor and move the head only
Mode::Select => old_anchor,
// When not in select mode, just move the cursor and do not select
_ => new_range.head
};
let selection = doc.selection(view.id).clone().transform(|range| {
let old_anchor = range.anchor;
let mut new_range = move_fn(text, range, count);

new_range.anchor = match cx.editor.mode {
// In select mode, use a sticky anchor and move the head only
Mode::Select => old_anchor,
// When not in select mode, just move the cursor and do not select
_ => new_range.head,
};

return new_range;
});
return new_range;
});

doc.set_selection(view.id, selection);
}
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 7c752df

Please sign in to comment.