Split code blocks into multiple rich_text lines

This improves layout diffing considerably!
This commit is contained in:
Héctor Ramón Jiménez 2025-02-01 01:07:35 +01:00
parent c2155b82b3
commit eb81679e60
No known key found for this signature in database
GPG key ID: 7CC46565708259A7

View file

@ -116,7 +116,7 @@ pub enum Item {
/// A code block. /// A code block.
/// ///
/// You can enable the `highlighter` feature for syntax highlighting. /// You can enable the `highlighter` feature for syntax highlighting.
CodeBlock(Text), CodeBlock(Vec<Text>),
/// A list. /// A list.
List { List {
/// The first number of the list, if it is ordered. /// The first number of the list, if it is ordered.
@ -377,6 +377,7 @@ fn parse_with<'a>(
} }
let mut spans = Vec::new(); let mut spans = Vec::new();
let mut code = Vec::new();
let mut strong = false; let mut strong = false;
let mut emphasis = false; let mut emphasis = false;
let mut strikethrough = false; let mut strikethrough = false;
@ -587,7 +588,7 @@ fn parse_with<'a>(
produce( produce(
state.borrow_mut(), state.borrow_mut(),
&mut lists, &mut lists,
Item::CodeBlock(Text::new(spans.drain(..).collect())), Item::CodeBlock(code.drain(..).collect()),
source, source,
) )
} }
@ -605,9 +606,9 @@ fn parse_with<'a>(
#[cfg(feature = "highlighter")] #[cfg(feature = "highlighter")]
if let Some(highlighter) = &mut highlighter { if let Some(highlighter) = &mut highlighter {
for line in text.lines() { for line in text.lines() {
spans.extend_from_slice( code.push(Text::new(
highlighter.highlight_line(&format!("{line}\n")), highlighter.highlight_line(line).to_vec(),
); ));
} }
return None; return None;
@ -871,13 +872,14 @@ where
})) }))
.spacing(spacing * 0.75) .spacing(spacing * 0.75)
.into(), .into(),
Item::CodeBlock(code) => container( Item::CodeBlock(lines) => container(
scrollable( scrollable(
container( container(column(lines.iter().map(|line| {
rich_text(code.spans(style)) rich_text(line.spans(style))
.font(Font::MONOSPACE) .font(Font::MONOSPACE)
.size(code_size), .size(code_size)
) .into()
})))
.padding(spacing.0 / 2.0), .padding(spacing.0 / 2.0),
) )
.direction(scrollable::Direction::Horizontal( .direction(scrollable::Direction::Horizontal(