Skip to content

Use Unicode words for movement #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Mar 8, 2021
Merged
Changes from all commits
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
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)
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