Skip to content

Commit

Permalink
Split code blocks into multiple rich_text lines
Browse files Browse the repository at this point in the history
This improves layout diffing considerably!
  • Loading branch information
hecrj committed Feb 1, 2025
1 parent c2155b8 commit 921cfee
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions widget/src/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ pub enum Item {
/// A code block.
///
/// You can enable the `highlighter` feature for syntax highlighting.
CodeBlock(Text),
CodeBlock(Vec<Text>),
/// A list.
List {
/// The first number of the list, if it is ordered.
Expand Down Expand Up @@ -377,6 +377,7 @@ fn parse_with<'a>(
}

let mut spans = Vec::new();
let mut code = Vec::new();
let mut strong = false;
let mut emphasis = false;
let mut strikethrough = false;
Expand Down Expand Up @@ -587,7 +588,7 @@ fn parse_with<'a>(
produce(
state.borrow_mut(),
&mut lists,
Item::CodeBlock(Text::new(spans.drain(..).collect())),
Item::CodeBlock(code.drain(..).collect()),
source,
)
}
Expand All @@ -605,9 +606,9 @@ fn parse_with<'a>(
#[cfg(feature = "highlighter")]
if let Some(highlighter) = &mut highlighter {
for line in text.lines() {
spans.extend_from_slice(
highlighter.highlight_line(&format!("{line}\n")),
);
code.push(Text::new(
highlighter.highlight_line(&format!("{line}")).to_vec(),

Check failure on line 610 in widget/src/markdown.rs

View workflow job for this annotation

GitHub Actions / all

useless use of `format!`
));
}

return None;
Expand Down Expand Up @@ -871,13 +872,14 @@ where
}))
.spacing(spacing * 0.75)
.into(),
Item::CodeBlock(code) => container(
Item::CodeBlock(lines) => container(
scrollable(
container(
rich_text(code.spans(style))
container(column(lines.iter().map(|line| {
rich_text(line.spans(style))
.font(Font::MONOSPACE)
.size(code_size),
)
.size(code_size)
.into()
})))
.padding(spacing.0 / 2.0),
)
.direction(scrollable::Direction::Horizontal(
Expand Down

0 comments on commit 921cfee

Please sign in to comment.