Track position correctly for peeked grapheme

pull/12959/head
Rose Hogenson 2025-04-04 10:33:03 -07:00
parent b6973c6a92
commit c67da4552c
2 changed files with 9 additions and 8 deletions

View File

@ -336,18 +336,15 @@ impl<'t> DocumentFormatter<'t> {
.change_position(visual_x, self.text_fmt.tab_width); .change_position(visual_x, self.text_fmt.tab_width);
word_width += grapheme.width(); word_width += grapheme.width();
} }
if let Some(grapheme) = &mut self.peeked_grapheme {
let visual_x = self.visual_pos.col + word_width;
grapheme
.grapheme
.change_position(visual_x, self.text_fmt.tab_width);
}
word_width word_width
} }
fn peek_grapheme(&mut self, col: usize, char_pos: usize) -> Option<&GraphemeWithSource<'t>> { fn peek_grapheme(&mut self, col: usize, char_pos: usize) -> Option<&GraphemeWithSource<'t>> {
if self.peeked_grapheme.is_none() { match &mut self.peeked_grapheme {
self.peeked_grapheme = self.advance_grapheme(col, char_pos); None => self.peeked_grapheme = self.advance_grapheme(col, char_pos),
Some(peeked_grapheme) => peeked_grapheme
.grapheme
.change_position(col, self.text_fmt.tab_width),
} }
self.peeked_grapheme.as_ref() self.peeked_grapheme.as_ref()
} }

View File

@ -80,6 +80,10 @@ fn softwrap_indentation() {
softwrap_text("\t\t\tfoo1 foo2 foo3 foo4 foo5 foo6\n"), softwrap_text("\t\t\tfoo1 foo2 foo3 foo4 foo5 foo6\n"),
" foo1 foo2 \n.foo3 foo4 foo5 \n.foo6 \n " " foo1 foo2 \n.foo3 foo4 foo5 \n.foo6 \n "
); );
assert_eq!(
softwrap_text("\t\tfoo1\n\t\tfoo2\n\t\tfoo3\n"),
" foo1 \n foo2 \n foo3 \n "
);
} }
#[test] #[test]