on master help sign in
Exclude cursor when doing ctrl-w (#2431)

Currently ctrl-w in insert mode deletes the cursor which results in unexpected behavior. The patch also reduces the selection to cursor before performing prev word to remove the behavior of removing unnecessary text when nothing should be removed. 1. `::#(|)#::` after `ctrl-w` should be `#(|)#::`, previously `#(|)#:` 2. `#(|::)#` after `ctrl-w` should be `#(|::)#`, previously `#(|)#` Fix #2390

commit0477d0289476c34e6b4d11a28165fee0c49063b3 authorIvan Tham <pickfire@riseup.net> dateWed 11 May 2022, 09:12 (4 years ago) parentc80ac84
1 file changed+17−4
helix-term/src/commands.rs+17−4
helix-term/src/commands.rs+17−4
@@ -2632,6 +2632,18 @@ pub mod insert {
26322632 pub type Hook = fn(&Rope, &Selection, char) -> Option<Transaction>;
26332633 pub type PostHook = fn(&mut Context, char);
26342634
2635+ /// Exclude the cursor in range.
2636+ fn exclude_cursor(text: RopeSlice, range: Range, cursor: Range) -> Range {
2637+ if range.to() == cursor.to() {
2638+ Range::new(
2639+ range.from(),
2640+ graphemes::prev_grapheme_boundary(text, cursor.to()),
2641+ )
2642+ } else {
2643+ range
2644+ }
2645+ }
2646+
26352647 // It trigger completion when idle timer reaches deadline
26362648 // Only trigger completion if the word under cursor is longer than n characters
26372649 pub fn idle_completion(cx: &mut Context) {
@@ -2948,10 +2960,11 @@ pub mod insert {
29482960 let (view, doc) = current!(cx.editor);
29492961 let text = doc.text().slice(..);
29502962
2951− let selection = doc
2952− .selection(view.id)
2953− .clone()
2954− .transform(|range| movement::move_prev_word_start(text, range, count));
2963+ let selection = doc.selection(view.id).clone().transform(|range| {
2964+ let cursor = Range::point(range.cursor(text));
2965+ let next = movement::move_prev_word_start(text, cursor, count);
2966+ exclude_cursor(text, next, range)
2967+ });
29552968 delete_selection_insert_mode(doc, view, &selection);
29562969 }
29572970