on master help sign in
Add ctrl-w in insert mode

It seemed to panic when I pressed too many times, but that is from lsp side.

commit40744ce8356cb9307f8cb9b2adf2c57b80b1ef9f authorIvan Tham <pickfire@riseup.net> dateSat 5 Jun 2021, 18:15 (5 years ago) parentaa8a8ba
5 files changed+56−7
helix-core/src/lib.rs+1 helix-core/src/movement.rs+4−4 helix-core/src/words.rs+33 helix-term/src/commands.rs+17−3 helix-term/src/keymap.rs+1
helix-core/src/lib.rs+1
@@ -16,6 +16,7 @@ pub mod selection;
1616mod state;
1717pub mod syntax;
1818mod transaction;
19+pub mod words;
1920
2021pub(crate) fn find_first_non_whitespace_char2(line: RopeSlice) -> Option<usize> {
2122 // find first non-whitespace char
helix-core/src/movement.rs+4−4
@@ -174,22 +174,22 @@ pub fn move_next_word_end(slice: RopeSlice, mut begin: usize, count: usize) -> O
174174
175175// used for by-word movement
176176
177−fn is_word(ch: char) -> bool {
177+pub(crate) fn is_word(ch: char) -> bool {
178178 ch.is_alphanumeric() || ch == '_'
179179}
180180
181−fn is_horiz_blank(ch: char) -> bool {
181+pub(crate) fn is_horiz_blank(ch: char) -> bool {
182182 matches!(ch, ' ' | '\t')
183183}
184184
185185#[derive(Debug, Eq, PartialEq)]
186−enum Category {
186+pub(crate) enum Category {
187187 Whitespace,
188188 Eol,
189189 Word,
190190 Punctuation,
191191}
192−fn categorize(ch: char) -> Category {
192+pub(crate) fn categorize(ch: char) -> Category {
193193 if ch == '\n' {
194194 Category::Eol
195195 } else if ch.is_ascii_whitespace() {
helix-core/src/words.rsnew file+33
@@ -0,0 +1,33 @@
1+use crate::movement::{categorize, is_horiz_blank, is_word, skip_over_prev};
2+use ropey::RopeSlice;
3+
4+#[must_use]
5+pub fn nth_prev_word_boundary(slice: RopeSlice, mut char_idx: usize, count: usize) -> usize {
6+ let mut with_end = false;
7+
8+ for _ in 0..count {
9+ if char_idx == 0 {
10+ break;
11+ }
12+
13+ // return if not skip while?
14+ skip_over_prev(slice, &mut char_idx, |ch| ch == '\n');
15+
16+ with_end = skip_over_prev(slice, &mut char_idx, is_horiz_blank);
17+
18+ // refetch
19+ let ch = slice.char(char_idx);
20+
21+ if is_word(ch) {
22+ with_end = skip_over_prev(slice, &mut char_idx, is_word);
23+ } else if ch.is_ascii_punctuation() {
24+ with_end = skip_over_prev(slice, &mut char_idx, |ch| ch.is_ascii_punctuation());
25+ }
26+ }
27+
28+ if with_end {
29+ char_idx
30+ } else {
31+ char_idx + 1
32+ }
33+}
helix-term/src/commands.rs+17−3
@@ -3,8 +3,8 @@ use helix_core::{
33 movement::{self, Direction},
44 object, pos_at_coords,
55 regex::{self, Regex},
6− register, search, selection, Change, ChangeSet, Position, Range, Rope, RopeSlice, Selection,
7− SmallVec, Tendril, Transaction,
6+ register, search, selection, words, Change, ChangeSet, Position, Range, Rope, RopeSlice,
7+ Selection, SmallVec, Tendril, Transaction,
88};
99
1010use helix_view::{
@@ -1782,7 +1782,6 @@ pub mod insert {
17821782
17831783 pub fn delete_char_forward(cx: &mut Context) {
17841784 let count = cx.count;
1785− let doc = cx.doc();
17861785 let (view, doc) = cx.current();
17871786 let text = doc.text().slice(..);
17881787 let transaction =
@@ -1795,6 +1794,21 @@ pub mod insert {
17951794 });
17961795 doc.apply(&transaction, view.id);
17971796 }
1797+
1798+ pub fn delete_word_backward(cx: &mut Context) {
1799+ let count = cx.count;
1800+ let (view, doc) = cx.current();
1801+ let text = doc.text().slice(..);
1802+ let transaction =
1803+ Transaction::change_by_selection(doc.text(), doc.selection(view.id), |range| {
1804+ (
1805+ words::nth_prev_word_boundary(text, range.head, count),
1806+ range.head,
1807+ None,
1808+ )
1809+ });
1810+ doc.apply(&transaction, view.id);
1811+ }
17981812}
17991813
18001814// Undo / Redo
helix-term/src/keymap.rs+1
@@ -360,6 +360,7 @@ pub fn default() -> Keymaps {
360360 } => commands::insert::insert_tab,
361361
362362 ctrl!('x') => commands::completion,
363+ ctrl!('w') => commands::insert::delete_word_backward,
363364 ),
364365 )
365366}