Use Cow<&str> for rope grapheme iterator.

This commit is contained in:
Nathan Vegdahl 2024-06-15 07:48:16 +02:00
parent 22d9f4f660
commit 076e988df0
2 changed files with 16 additions and 4 deletions

View File

@ -415,7 +415,7 @@ impl<'a> Iterator for BlockVisIter<'a> {
let mut word_width = 0; let mut word_width = 0;
self.word_buf.truncate(0); self.word_buf.truncate(0);
while let Some(g) = self.grapheme_itr.next().map(|g| Cow::<str>::from(g)) { while let Some(g) = self.grapheme_itr.next() {
let width = let width =
grapheme_vis_width_at_vis_pos(&g, self.pos.1 + word_width, self.tab_width); grapheme_vis_width_at_vis_pos(&g, self.pos.1 + word_width, self.tab_width);
self.word_buf.push((g.clone(), width)); self.word_buf.push((g.clone(), width));

View File

@ -156,9 +156,9 @@ impl<'a> RopeGraphemes<'a> {
} }
impl<'a> Iterator for RopeGraphemes<'a> { impl<'a> Iterator for RopeGraphemes<'a> {
type Item = RopeSlice<'a>; type Item = std::borrow::Cow<'a, str>;
fn next(&mut self) -> Option<RopeSlice<'a>> { fn next(&mut self) -> Option<std::borrow::Cow<'a, str>> {
let a = self.cursor.cur_cursor(); let a = self.cursor.cur_cursor();
let b; let b;
loop { loop {
@ -177,10 +177,22 @@ impl<'a> Iterator for RopeGraphemes<'a> {
self.cur_chunk_start += self.cur_chunk.len(); self.cur_chunk_start += self.cur_chunk.len();
self.cur_chunk = self.chunks.next().unwrap_or(""); 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!(), _ => 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())
}
} }
} }