Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 47 additions & 22 deletions src/line_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,38 +100,63 @@ impl LineBuffer {
}

pub fn move_word_left(&mut self) -> usize {
match self
.buffer
.rmatch_indices(&[' ', '\t'][..])
.find(|(index, _)| index < &(self.insertion_point - 1))
{
Some((index, _)) => {
self.insertion_point = index + 1;
}
None => {
self.insertion_point = 0;
let mut words = self.buffer[..self.insertion_point - 1] // valid UTF-8 slice when insertion_point at grapheme boundary
.split_word_bound_indices()
.rev();

loop {
match words.next() {
Some((_, word)) if is_word_boundary(word) => {
// This is a word boundary, go to the next one
continue;
}
Some((index, _)) => {
self.insertion_point = index;
}
None => {
self.insertion_point = 0;
}
}

return self.insertion_point;
}
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;
}
None => {
self.insertion_point = self.get_buffer_len();
let mut words = self.buffer[self.insertion_point..]
.split_word_bound_indices();

let mut word_found = false;

loop {
match words.next() {
Some((offset, word)) => {
if word_found {
self.insertion_point += offset;
} else {
// If the current word isn't a word boundary we have found the word to move
// past
word_found = !is_word_boundary(word);

// Go to the next word
continue;
}
}
None => {
self.insertion_point = self.buffer.len();
}
}

return self.insertion_point;
}
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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This logic to identify a str as being a word boundary or not is copied from unicode-segmentation as it doesn't seem to be exposed as part of the public API.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems okay. We might want to put an issue on unicode-segmentation and ask if they'd be willing to make it public (if you haven't already)

}

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