diff --git a/src/formatter.rs b/src/formatter.rs index 6327623..af53c53 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -512,9 +512,9 @@ pub fn find_good_break(slice: &RopeSlice, lower_limit: usize, byte_idx: usize) - let mut prev = if i == slice_len { None } else { - Some(slice.char(byte_idx)) + Some(slice.char(i)) }; - let mut char_itr = slice.chars_at(byte_idx); + let mut char_itr = slice.chars_at(i); while i > lower_limit { let c = char_itr.prev(); if WS_CHARS.contains(&c.unwrap()) && prev.map(|pc| !WS_CHARS.contains(&pc)).unwrap_or(true) diff --git a/src/graphemes.rs b/src/graphemes.rs index e245a56..4ba62a1 100644 --- a/src/graphemes.rs +++ b/src/graphemes.rs @@ -41,6 +41,8 @@ pub fn prev_grapheme_boundary(slice: &RopeSlice, byte_idx: usize) -> usize { // Bounds check debug_assert!(byte_idx <= slice.len()); + let byte_idx = slice.ceil_char_boundary(byte_idx); + // Get the chunk with our byte index in it. let (mut chunk, mut chunk_byte_idx) = slice.chunk(byte_idx); @@ -82,6 +84,8 @@ pub fn next_grapheme_boundary(slice: &RopeSlice, byte_idx: usize) -> usize { // Bounds check debug_assert!(byte_idx <= slice.len()); + let byte_idx = slice.floor_char_boundary(byte_idx); + // Get the chunk with our byte index in it. let (mut chunk, mut chunk_byte_idx) = slice.chunk(byte_idx); @@ -115,6 +119,11 @@ pub fn is_grapheme_boundary(slice: &RopeSlice, byte_idx: usize) -> bool { // Get the chunk with our byte index in it. let (chunk, chunk_byte_idx) = slice.chunk(byte_idx); + // Can't be a grapheme boundary if it's not a char boundary. + if !chunk.is_char_boundary(chunk_byte_idx) { + return false; + } + // Set up the grapheme cursor. let mut gc = GraphemeCursor::new(byte_idx, slice.len(), true);