Skip to content

Commit 74e25c1

Browse files
zfz7bennetbo
andauthored
Fix incorrect checkbox placement in Markdown preview (#19383)
- Closes #12515 Before fix: <img width="1506" alt="Screenshot 2024-10-17 at 09 50 19" src="https://github.com/user-attachments/assets/250f50cb-0119-4b96-bc9b-7258aa83247c"> After fix: <img width="1027" alt="Screenshot 2024-10-17 at 09 52 36" src="https://github.com/user-attachments/assets/c2eb7e4a-3c03-466c-b215-7fcc22eed024"> Testing: - Manual testing - Added unit test Test results, these tests fail on the main branch for my setup as well, I have docker running but still had some failures: ``` failures: tests::integration_tests::test_context_collaboration_with_reconnect tests::integration_tests::test_formatting_buffer tests::integration_tests::test_fs_operations tests::integration_tests::test_git_branch_name tests::integration_tests::test_git_diff_base_change tests::integration_tests::test_git_status_sync tests::integration_tests::test_join_after_restart tests::integration_tests::test_join_call_after_screen_was_shared tests::integration_tests::test_joining_channels_and_calling_multiple_users_simultaneously tests::integration_tests::test_leaving_project tests::integration_tests::test_leaving_worktree_while_opening_buffer tests::integration_tests::test_local_settings tests::integration_tests::test_lsp_hover tests::integration_tests::test_mute_deafen tests::integration_tests::test_open_buffer_while_getting_definition_pointing_to_it tests::integration_tests::test_pane_split_left tests::integration_tests::test_prettier_formatting_buffer tests::integration_tests::test_preview_tabs tests::integration_tests::test_project_reconnect tests::integration_tests::test_project_search tests::integration_tests::test_project_symbols tests::integration_tests::test_propagate_saves_and_fs_changes tests::integration_tests::test_references tests::integration_tests::test_reloading_buffer_manually tests::integration_tests::test_right_click_menu_behind_collab_panel tests::integration_tests::test_room_location tests::integration_tests::test_room_uniqueness tests::integration_tests::test_server_restarts tests::integration_tests::test_unshare_project tests::notification_tests::test_notifications tests::random_project_collaboration_tests::test_random_project_collaboration tests::remote_editing_collaboration_tests::test_sharing_an_ssh_remote_project test result: FAILED. 156 passed; 32 failed; 0 ignored; 0 measured; 0 filtered out; finished in 100.98s ``` Comments: I do not have a ton of rust knowledge, so very open to feedback. TYSM Release Notes: - Fix Incorrect checkbox placement in Markdown preview --------- Co-authored-by: Bennet Bo Fenner <[email protected]>
1 parent b355a6f commit 74e25c1

File tree

1 file changed

+61
-23
lines changed

1 file changed

+61
-23
lines changed

crates/markdown_preview/src/markdown_parser.rs

+61-23
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,20 @@ struct MarkdownParser<'a> {
3636
language_registry: Option<Arc<LanguageRegistry>>,
3737
}
3838

39+
struct MarkdownListItem {
40+
content: Vec<ParsedMarkdownElement>,
41+
item_type: ParsedMarkdownListItemType,
42+
}
43+
44+
impl Default for MarkdownListItem {
45+
fn default() -> Self {
46+
Self {
47+
content: Vec::new(),
48+
item_type: ParsedMarkdownListItemType::Unordered,
49+
}
50+
}
51+
}
52+
3953
impl<'a> MarkdownParser<'a> {
4054
fn new(
4155
tokens: Vec<(Event<'a>, Range<usize>)>,
@@ -475,9 +489,8 @@ impl<'a> MarkdownParser<'a> {
475489
let (_, list_source_range) = self.previous().unwrap();
476490

477491
let mut items = Vec::new();
478-
let mut items_stack = vec![Vec::new()];
492+
let mut items_stack = vec![MarkdownListItem::default()];
479493
let mut depth = 1;
480-
let mut task_item = None;
481494
let mut order = order;
482495
let mut order_stack = Vec::new();
483496

@@ -517,8 +530,9 @@ impl<'a> MarkdownParser<'a> {
517530
start_item_range = source_range.clone();
518531

519532
self.cursor += 1;
520-
items_stack.push(Vec::new());
533+
items_stack.push(MarkdownListItem::default());
521534

535+
let mut task_list = None;
522536
// Check for task list marker (`- [ ]` or `- [x]`)
523537
if let Some(event) = self.current_event() {
524538
// If there is a linebreak in between two list items the task list marker will actually be the first element of the paragraph
@@ -527,7 +541,7 @@ impl<'a> MarkdownParser<'a> {
527541
}
528542

529543
if let Some((Event::TaskListMarker(checked), range)) = self.current() {
530-
task_item = Some((*checked, range.clone()));
544+
task_list = Some((*checked, range.clone()));
531545
self.cursor += 1;
532546
}
533547
}
@@ -539,13 +553,21 @@ impl<'a> MarkdownParser<'a> {
539553
let text = self.parse_text(false, Some(range.clone()));
540554
let block = ParsedMarkdownElement::Paragraph(text);
541555
if let Some(content) = items_stack.last_mut() {
542-
content.push(block);
556+
let item_type = if let Some((checked, range)) = task_list {
557+
ParsedMarkdownListItemType::Task(checked, range)
558+
} else if let Some(order) = order {
559+
ParsedMarkdownListItemType::Ordered(order)
560+
} else {
561+
ParsedMarkdownListItemType::Unordered
562+
};
563+
content.item_type = item_type;
564+
content.content.push(block);
543565
}
544566
} else {
545567
let block = self.parse_block().await;
546568
if let Some(block) = block {
547-
if let Some(content) = items_stack.last_mut() {
548-
content.extend(block);
569+
if let Some(list_item) = items_stack.last_mut() {
570+
list_item.content.extend(block);
549571
}
550572
}
551573
}
@@ -559,19 +581,11 @@ impl<'a> MarkdownParser<'a> {
559581
Event::End(TagEnd::Item) => {
560582
self.cursor += 1;
561583

562-
let item_type = if let Some((checked, range)) = task_item {
563-
ParsedMarkdownListItemType::Task(checked, range)
564-
} else if let Some(order) = order {
565-
ParsedMarkdownListItemType::Ordered(order)
566-
} else {
567-
ParsedMarkdownListItemType::Unordered
568-
};
569-
570584
if let Some(current) = order {
571585
order = Some(current + 1);
572586
}
573587

574-
if let Some(content) = items_stack.pop() {
588+
if let Some(list_item) = items_stack.pop() {
575589
let source_range = source_ranges
576590
.remove(&depth)
577591
.unwrap_or(start_item_range.clone());
@@ -580,9 +594,9 @@ impl<'a> MarkdownParser<'a> {
580594
let source_range = source_range.start..source_range.end - 1;
581595
let item = ParsedMarkdownElement::ListItem(ParsedMarkdownListItem {
582596
source_range,
583-
content,
597+
content: list_item.content,
584598
depth,
585-
item_type,
599+
item_type: list_item.item_type,
586600
});
587601

588602
if let Some(index) = insertion_indices.get(&depth) {
@@ -592,8 +606,6 @@ impl<'a> MarkdownParser<'a> {
592606
items.push(item);
593607
}
594608
}
595-
596-
task_item = None;
597609
}
598610
_ => {
599611
if depth == 0 {
@@ -603,10 +615,10 @@ impl<'a> MarkdownParser<'a> {
603615
// or the list item contains blocks that should be rendered after the nested list items
604616
let block = self.parse_block().await;
605617
if let Some(block) = block {
606-
if let Some(items_stack) = items_stack.last_mut() {
618+
if let Some(list_item) = items_stack.last_mut() {
607619
// If we did not insert any nested items yet (in this case insertion index is set), we can append the block to the current list item
608620
if !insertion_indices.contains_key(&depth) {
609-
items_stack.extend(block);
621+
list_item.content.extend(block);
610622
continue;
611623
}
612624
}
@@ -722,7 +734,6 @@ mod tests {
722734
use gpui::BackgroundExecutor;
723735
use language::{tree_sitter_rust, HighlightId, Language, LanguageConfig, LanguageMatcher};
724736
use pretty_assertions::assert_eq;
725-
726737
use ParsedMarkdownListItemType::*;
727738

728739
async fn parse(input: &str) -> ParsedMarkdown {
@@ -956,6 +967,33 @@ Some other content
956967
);
957968
}
958969

970+
#[gpui::test]
971+
async fn test_list_with_indented_task() {
972+
let parsed = parse(
973+
"\
974+
- [ ] TODO
975+
- [x] Checked
976+
- Unordered
977+
1. Number 1
978+
1. Number 2
979+
1. Number A
980+
",
981+
)
982+
.await;
983+
984+
assert_eq!(
985+
parsed.children,
986+
vec![
987+
list_item(0..12, 1, Task(false, 2..5), vec![p("TODO", 6..10)]),
988+
list_item(13..26, 2, Task(true, 15..18), vec![p("Checked", 19..26)]),
989+
list_item(29..40, 2, Unordered, vec![p("Unordered", 31..40)]),
990+
list_item(43..54, 2, Ordered(1), vec![p("Number 1", 46..54)]),
991+
list_item(57..68, 2, Ordered(2), vec![p("Number 2", 60..68)]),
992+
list_item(69..80, 1, Ordered(1), vec![p("Number A", 72..80)]),
993+
],
994+
);
995+
}
996+
959997
#[gpui::test]
960998
async fn test_list_with_linebreak_is_handled_correctly() {
961999
let parsed = parse(

0 commit comments

Comments
 (0)