Skip to content

Commit 88c5de3

Browse files
committed
improve loose list detection
1 parent d237d4a commit 88c5de3

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

crates/quarto-markdown-pandoc/src/pandoc/treesitter.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ use crate::traversals::bottomup_traverse_concrete_tree;
6767

6868
use treesitter_utils::pandocnativeintermediate::PandocNativeIntermediate;
6969

70+
fn get_block_source_info(block: &Block) -> &SourceInfo {
71+
match block {
72+
Block::Plain(b) => &b.source_info,
73+
Block::Paragraph(b) => &b.source_info,
74+
Block::LineBlock(b) => &b.source_info,
75+
Block::CodeBlock(b) => &b.source_info,
76+
Block::RawBlock(b) => &b.source_info,
77+
Block::BlockQuote(b) => &b.source_info,
78+
Block::OrderedList(b) => &b.source_info,
79+
Block::BulletList(b) => &b.source_info,
80+
Block::DefinitionList(b) => &b.source_info,
81+
Block::Header(b) => &b.source_info,
82+
Block::HorizontalRule(b) => &b.source_info,
83+
Block::Table(b) => &b.source_info,
84+
Block::Figure(b) => &b.source_info,
85+
Block::Div(b) => &b.source_info,
86+
Block::BlockMetadata(b) => &b.source_info,
87+
Block::NoteDefinitionPara(b) => &b.source_info,
88+
Block::NoteDefinitionFencedBlock(b) => &b.source_info,
89+
}
90+
}
91+
7092
fn process_list(
7193
node: &tree_sitter::Node,
7294
children: Vec<(String, PandocNativeIntermediate)>,
@@ -81,6 +103,7 @@ fn process_list(
81103

82104
let mut has_loose_item = false;
83105
let mut last_para_range: Option<Range> = None;
106+
let mut last_item_end_row: Option<usize> = None;
84107
let mut list_items: Vec<Blocks> = Vec::new();
85108
let mut is_ordered_list: Option<ListAttributes> = None;
86109

@@ -131,6 +154,14 @@ fn process_list(
131154
}
132155
}
133156

157+
// Check if there's a blank line between the last item and this item
158+
if let Some(last_end) = last_item_end_row {
159+
if child_range.start.row > last_end {
160+
// There's at least one blank line between items
161+
has_loose_item = true;
162+
}
163+
}
164+
134165
// is this item definitely loose?
135166
if blocks
136167
.iter()
@@ -150,6 +181,9 @@ fn process_list(
150181
// last paragraph range after setting has_loose_item,
151182
// but we do it in case we want to use it later
152183
last_para_range = None;
184+
last_item_end_row = blocks
185+
.last()
186+
.map(|b| get_block_source_info(b).range.end.row);
153187
list_items.push(blocks);
154188
continue;
155189
}
@@ -170,6 +204,9 @@ fn process_list(
170204
// last_para_range since this item can't participate in loose detection
171205
last_para_range = None;
172206
}
207+
last_item_end_row = blocks
208+
.last()
209+
.map(|b| get_block_source_info(b).range.end.row);
173210
list_items.push(blocks);
174211
}
175212

0 commit comments

Comments
 (0)