Tweak rendering of multi-paragraph list items in markdown

This commit is contained in:
Max Brunsfeld 2023-04-27 11:39:34 -07:00
parent c6abb0db3a
commit 66d4cb8c14

View file

@ -382,7 +382,7 @@ fn render_blocks(
*number += 1; *number += 1;
*has_content = false; *has_content = false;
} else { } else {
text.push_str(" "); text.push_str("- ");
} }
} }
} }
@ -424,6 +424,10 @@ fn render_blocks(
} }
} }
if !text.is_empty() && !text.ends_with('\n') {
text.push('\n');
}
RenderedInfo { RenderedInfo {
theme_id, theme_id,
text, text,
@ -450,8 +454,11 @@ fn render_code(
} }
fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) { fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
let mut is_subsequent_paragraph_of_list = false;
if let Some((_, has_content)) = list_stack.last_mut() { if let Some((_, has_content)) = list_stack.last_mut() {
if !*has_content { if *has_content {
is_subsequent_paragraph_of_list = true;
} else {
*has_content = true; *has_content = true;
return; return;
} }
@ -466,6 +473,9 @@ fn new_paragraph(text: &mut String, list_stack: &mut Vec<(Option<u64>, bool)>) {
for _ in 0..list_stack.len().saturating_sub(1) { for _ in 0..list_stack.len().saturating_sub(1) {
text.push_str(" "); text.push_str(" ");
} }
if is_subsequent_paragraph_of_list {
text.push_str(" ");
}
} }
#[derive(Default)] #[derive(Default)]
@ -664,6 +674,7 @@ mod tests {
use lsp::LanguageServerId; use lsp::LanguageServerId;
use project::{HoverBlock, HoverBlockKind}; use project::{HoverBlock, HoverBlockKind};
use smol::stream::StreamExt; use smol::stream::StreamExt;
use unindent::Unindent;
use util::test::marked_text_ranges; use util::test::marked_text_ranges;
#[gpui::test] #[gpui::test]
@ -867,28 +878,99 @@ mod tests {
struct Row { struct Row {
blocks: Vec<HoverBlock>, blocks: Vec<HoverBlock>,
expected_marked_text: &'static str, expected_marked_text: String,
expected_styles: Vec<HighlightStyle>, expected_styles: Vec<HighlightStyle>,
} }
let rows = &[ let rows = &[
// Strong emphasis
Row { Row {
blocks: vec![HoverBlock { blocks: vec![HoverBlock {
text: "one **two** three".to_string(), text: "one **two** three".to_string(),
kind: HoverBlockKind::Markdown, kind: HoverBlockKind::Markdown,
}], }],
expected_marked_text: "one «two» three", expected_marked_text: "one «two» three\n".to_string(),
expected_styles: vec![HighlightStyle { expected_styles: vec![HighlightStyle {
weight: Some(Weight::BOLD), weight: Some(Weight::BOLD),
..Default::default() ..Default::default()
}], }],
}, },
// Links
Row { Row {
blocks: vec![HoverBlock { blocks: vec![HoverBlock {
text: "one [two](the-url) three".to_string(), text: "one [two](the-url) three".to_string(),
kind: HoverBlockKind::Markdown, kind: HoverBlockKind::Markdown,
}], }],
expected_marked_text: "one «two» three", expected_marked_text: "one «two» three\n".to_string(),
expected_styles: vec![HighlightStyle {
underline: Some(Underline {
thickness: 1.0.into(),
..Default::default()
}),
..Default::default()
}],
},
// Lists
Row {
blocks: vec![HoverBlock {
text: "
lists:
* one
- a
- b
* two
- [c](the-url)
- d
"
.unindent(),
kind: HoverBlockKind::Markdown,
}],
expected_marked_text: "
lists:
- one
- a
- b
- two
- «c»
- d
"
.unindent(),
expected_styles: vec![HighlightStyle {
underline: Some(Underline {
thickness: 1.0.into(),
..Default::default()
}),
..Default::default()
}],
},
// Multi-paragraph list items
Row {
blocks: vec![HoverBlock {
text: "
* one two
three
* four five
* six seven
eight
nine
* ten
* six
"
.unindent(),
kind: HoverBlockKind::Markdown,
}],
expected_marked_text: "
- one two three
- four five
- six seven eight
nine
- ten
- six
"
.unindent(),
expected_styles: vec![HighlightStyle { expected_styles: vec![HighlightStyle {
underline: Some(Underline { underline: Some(Underline {
thickness: 1.0.into(), thickness: 1.0.into(),
@ -903,7 +985,7 @@ mod tests {
blocks, blocks,
expected_marked_text, expected_marked_text,
expected_styles, expected_styles,
} in rows } in &rows[0..]
{ {
let rendered = render_blocks(0, &blocks, &Default::default(), &style); let rendered = render_blocks(0, &blocks, &Default::default(), &style);
@ -913,7 +995,8 @@ mod tests {
.zip(expected_styles.iter().cloned()) .zip(expected_styles.iter().cloned())
.collect::<Vec<_>>(); .collect::<Vec<_>>();
assert_eq!( assert_eq!(
rendered.text, expected_text, rendered.text,
dbg!(expected_text),
"wrong text for input {blocks:?}" "wrong text for input {blocks:?}"
); );
assert_eq!( assert_eq!(