summaryrefslogtreecommitdiffstats
path: root/widget
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-02-01 01:07:35 +0100
committerLibravatar Héctor Ramón Jiménez <hector@hecrj.dev>2025-02-01 01:08:20 +0100
commiteb81679e604e2d1d45590c236fb5b2644c38f3d5 (patch)
tree1408755dccba1303251b38e2a8ec7f086058d707 /widget
parentc2155b82b35200585991a09945fb93903a61fccf (diff)
downloadiced-eb81679e604e2d1d45590c236fb5b2644c38f3d5.tar.gz
iced-eb81679e604e2d1d45590c236fb5b2644c38f3d5.tar.bz2
iced-eb81679e604e2d1d45590c236fb5b2644c38f3d5.zip
Split code blocks into multiple `rich_text` lines
This improves layout diffing considerably!
Diffstat (limited to 'widget')
-rw-r--r--widget/src/markdown.rs22
1 files changed, 12 insertions, 10 deletions
diff --git a/widget/src/markdown.rs b/widget/src/markdown.rs
index 658166ec..bb818d19 100644
--- a/widget/src/markdown.rs
+++ b/widget/src/markdown.rs
@@ -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.
@@ -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;
@@ -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,
)
}
@@ -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(line).to_vec(),
+ ));
}
return None;
@@ -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(