on master help sign in
Add WORD textobject (#991)

* Add WORD textobject * Document WORD textobject

commita252ecd8c85af5cc16638a4752011e2e920fa652 authorOmnikar <omnikar5@gmail.com> dateSun 7 Nov 2021, 19:54 (5 years ago) parente0e227d
4 files changed+10−6
book/src/usage.md+1 helix-core/src/textobject.rs+6−5 helix-term/src/commands.rs+2−1 helix-term/src/ui/prompt.rs+1
book/src/usage.md+1
@@ -62,6 +62,7 @@ Currently supported: `word`, `surround`, `function`, `class`, `parameter`.
6262| Key after `mi` or `ma` | Textobject selected |
6363| --- | --- |
6464| `w` | Word |
65+| `W` | WORD |
6566| `(`, `[`, `'`, etc | Specified surround pairs |
6667| `f` | Function |
6768| `c` | Class |
helix-core/src/textobject.rs+6−5
@@ -10,7 +10,7 @@ use crate::surround;
1010use crate::syntax::LanguageConfiguration;
1111use crate::Range;
1212
13−fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction) -> usize {
13+fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction, long: bool) -> usize {
1414 use CharCategory::{Eol, Whitespace};
1515
1616 let iter = match direction {
@@ -33,7 +33,7 @@ fn find_word_boundary(slice: RopeSlice, mut pos: usize, direction: Direction) ->
3333 match categorize_char(ch) {
3434 Eol | Whitespace => return pos,
3535 category => {
36− if category != prev_category && pos != 0 && pos != slice.len_chars() {
36+ if !long && category != prev_category && pos != 0 && pos != slice.len_chars() {
3737 return pos;
3838 } else {
3939 match direction {
@@ -70,13 +70,14 @@ pub fn textobject_word(
7070 range: Range,
7171 textobject: TextObject,
7272 _count: usize,
73+ long: bool,
7374) -> Range {
7475 let pos = range.cursor(slice);
7576
76− let word_start = find_word_boundary(slice, pos, Direction::Backward);
77+ let word_start = find_word_boundary(slice, pos, Direction::Backward, long);
7778 let word_end = match slice.get_char(pos).map(categorize_char) {
7879 None | Some(CharCategory::Whitespace | CharCategory::Eol) => pos,
79− _ => find_word_boundary(slice, pos + 1, Direction::Forward),
80+ _ => find_word_boundary(slice, pos + 1, Direction::Forward, long),
8081 };
8182
8283 // Special case.
@@ -268,7 +269,7 @@ mod test {
268269 let slice = doc.slice(..);
269270 for &case in scenario {
270271 let (pos, objtype, expected_range) = case;
271− let result = textobject_word(slice, Range::point(pos), objtype, 1);
272+ let result = textobject_word(slice, Range::point(pos), objtype, 1, false);
272273 assert_eq!(
273274 result,
274275 expected_range.into(),
helix-term/src/commands.rs+2−1
@@ -4672,7 +4672,8 @@ fn select_textobject(cx: &mut Context, objtype: textobject::TextObject) {
46724672
46734673 let selection = doc.selection(view.id).clone().transform(|range| {
46744674 match ch {
4675− 'w' => textobject::textobject_word(text, range, objtype, count),
4675+ 'w' => textobject::textobject_word(text, range, objtype, count, false),
4676+ 'W' => textobject::textobject_word(text, range, objtype, count, true),
46764677 'c' => textobject_treesitter("class", range),
46774678 'f' => textobject_treesitter("function", range),
46784679 'p' => textobject_treesitter("parameter", range),
helix-term/src/ui/prompt.rs+1
@@ -491,6 +491,7 @@ impl Component for Prompt {
491491 doc.selection(view.id).primary(),
492492 textobject::TextObject::Inside,
493493 1,
494+ false,
494495 );
495496 let line = text.slice(range.from()..range.to()).to_string();
496497 if !line.is_empty() {