diff --git a/src/formatter.rs b/src/formatter.rs index 7f21511..0450bbe 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -415,7 +415,7 @@ impl<'a> Iterator for BlockVisIter<'a> { let mut word_width = 0; self.word_buf.truncate(0); - while let Some(g) = self.grapheme_itr.next().map(|g| Cow::::from(g)) { + while let Some(g) = self.grapheme_itr.next() { let width = grapheme_vis_width_at_vis_pos(&g, self.pos.1 + word_width, self.tab_width); self.word_buf.push((g.clone(), width)); diff --git a/src/graphemes.rs b/src/graphemes.rs index dd729b4..e245a56 100644 --- a/src/graphemes.rs +++ b/src/graphemes.rs @@ -156,9 +156,9 @@ impl<'a> RopeGraphemes<'a> { } impl<'a> Iterator for RopeGraphemes<'a> { - type Item = RopeSlice<'a>; + type Item = std::borrow::Cow<'a, str>; - fn next(&mut self) -> Option> { + fn next(&mut self) -> Option> { let a = self.cursor.cur_cursor(); let b; loop { @@ -177,10 +177,22 @@ impl<'a> Iterator for RopeGraphemes<'a> { self.cur_chunk_start += self.cur_chunk.len(); self.cur_chunk = self.chunks.next().unwrap_or(""); } + Err(GraphemeIncomplete::PreContext(idx)) => { + let (chunk, byte_idx) = self.text.chunk(idx.saturating_sub(1)); + self.cursor.provide_context(chunk, byte_idx); + } _ => unreachable!(), } } - Some(self.text.slice(a..b)) + if a >= self.cur_chunk_start && b <= (self.cur_chunk_start + self.cur_chunk.len()) { + let a_internal = a - self.cur_chunk_start; + let b_internal = b - self.cur_chunk_start; + Some(std::borrow::Cow::Borrowed( + &self.cur_chunk[a_internal..b_internal], + )) + } else { + Some(self.text.slice(a..b).into()) + } } }