Skip to content

Commit

Permalink
Overlapping ranges (#106)
Browse files Browse the repository at this point in the history
* Fix for overlapping ranges

* Switch to commonmark spec for snapshot tests

* More chunk ranges in snapshots
  • Loading branch information
benbrandt authored Mar 2, 2024
1 parent cf0131a commit 9e427fa
Show file tree
Hide file tree
Showing 166 changed files with 181,098 additions and 201,233 deletions.
8 changes: 6 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,26 +239,30 @@ trait SemanticSplit {
offset: usize,
level: Self::Level,
) -> impl Iterator<Item = &(Self::Level, Range<usize>)> + '_ {
let first_item = self.ranges().find(|(l, _)| l == &level);
self.ranges()
.filter(move |(l, sep)| l >= &level && sep.start >= offset)
.skip_while(move |(l, r)| {
first_item.is_some_and(|(_, fir)| l > &level && r.contains(&fir.start))
})
}

/// Return a unique, sorted list of all line break levels present before the next max level, added
/// to all of the base semantic levels, in order from smallest to largest
fn levels_in_next_max_chunk(&self, offset: usize) -> impl Iterator<Item = Self::Level> + '_ {
let max_level = self.max_level();
let existing_levels = self
.ranges()
// Only start taking them from the offset
.filter(|(_, sep)| sep.start >= offset)
// Stop once we hit the first of the max level
.take_while(|(l, _)| l < &self.max_level())
.take_while_inclusive(|(l, _)| l < &max_level)
.map(|(l, _)| l)
.copied();

Self::PERSISTENT_LEVELS
.iter()
.copied()
.chain(once(self.max_level()))
.chain(existing_levels)
.sorted()
.dedup()
Expand Down
29 changes: 29 additions & 0 deletions src/unstable_markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -880,4 +880,33 @@ mod tests {
assert_eq!(SemanticLevel::Heading(level), markdown.max_level());
}
}

#[test]
fn test_ranges_after_offset_block() {
let markdown = Markdown::new("- [ ] incomplete task\n- [x] completed task");

assert_eq!(
vec![&(SemanticLevel::Block, 0..42),],
markdown
.ranges_after_offset(0, SemanticLevel::Block)
.collect::<Vec<_>>()
);
assert_eq!(SemanticLevel::Block, markdown.max_level());
}

#[test]
fn test_ranges_after_offset_item() {
let markdown = Markdown::new("- [ ] incomplete task\n- [x] completed task");

assert_eq!(
vec![
&(SemanticLevel::Item(SemanticSplitPosition::Own), 0..22),
&(SemanticLevel::Item(SemanticSplitPosition::Own), 22..42),
],
markdown
.ranges_after_offset(0, SemanticLevel::Item(SemanticSplitPosition::Own))
.collect::<Vec<_>>()
);
assert_eq!(SemanticLevel::Block, markdown.max_level());
}
}
52 changes: 0 additions & 52 deletions tests/inputs/markdown/blog_frontmatter.md

This file was deleted.

Loading

0 comments on commit 9e427fa

Please sign in to comment.