Skip to content

Commit

Permalink
Use Unicode words for movement (#5)
Browse files Browse the repository at this point in the history
* Use Unicode words for movement

This also skips multiple consecutive non-word characters (word boundaries) as part of the movement in a similar way to how bash/readline do it.

* Simplify logic

* Fix off by one slicing issue and simplify logic further
  • Loading branch information
basile-henry committed Mar 8, 2021
1 parent 4c63c7a commit b337d3a
Showing 1 changed file with 22 additions and 14 deletions.
36 changes: 22 additions & 14 deletions src/line_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,46 @@ impl LineBuffer {
}

pub fn move_word_left(&mut self) -> usize {
match self
.buffer
.rmatch_indices(&[' ', '\t'][..])
.find(|(index, _)| index < &(self.insertion_point - 1))
{
let mut words = self.buffer[..self.insertion_point]
.split_word_bound_indices()
.filter(|(_, word)| !is_word_boundary(word));

match words.next_back() {
Some((index, _)) => {
self.insertion_point = index + 1;
self.insertion_point = index;
}
None => {
self.insertion_point = 0;
}
}

self.insertion_point
}

pub fn move_word_right(&mut self) -> usize {
match self
.buffer
.match_indices(&[' ', '\t'][..])
.find(|(index, _)| index > &(self.insertion_point))
{
Some((index, _)) => {
self.insertion_point = index + 1;
let mut words = self.buffer[self.insertion_point..]
.split_word_bound_indices()
.filter(|(_, word)| !is_word_boundary(word));

match words.next() {
Some((offset, word)) => {
// Move the insertion point just past the end of the next word
self.insertion_point += offset + word.len();
}
None => {
self.insertion_point = self.get_buffer_len();
self.insertion_point = self.buffer.len();
}
}

self.insertion_point
}
}

/// Match any sequence of characters that are considered a word boundary
fn is_word_boundary(s: &str) -> bool {
!s.chars().any(char::is_alphanumeric)
}

#[test]
fn emoji_test() {
//TODO
Expand Down

0 comments on commit b337d3a

Please sign in to comment.