use std::borrow::Cow;
use std::ops::Range;
use pulldown_cmark::{Event, Parser, Tag, TagEnd};
use crate::doc::parse_options;
#[derive(Debug)]
pub(super) enum Kind {
Quote,
Item {
marker: Range<usize>,
number: Option<u64>,
task: Option<(Range<usize>, bool)>,
depth: usize,
},
}
#[derive(Debug)]
pub(super) struct Container {
pub(super) range: Range<usize>,
pub(super) kind: Kind,
parent: Option<usize>,
}
impl Container {
pub(super) fn is_quote(&self) -> bool {
matches!(self.kind, Kind::Quote)
}
}
#[derive(Debug, PartialEq, Eq)]
pub(super) enum Part {
Marker(usize),
Quote,
}
#[derive(Debug, Default)]
pub(crate) struct Syntax {
pub(super) containers: Vec<Container>,
leaves: Vec<Range<usize>>,
}
impl Syntax {
pub(crate) fn parse(source: &str, base: usize) -> Self {
let mut syntax = Self::default();
let mut open: Vec<usize> = Vec::new();
let mut lists: Vec<Option<u64>> = Vec::new();
for (event, range) in Parser::new_ext(source, parse_options()).into_offset_iter() {
let local = range.start;
let range = range.start + base..range.end + base;
let kind = match event {
Event::Start(Tag::BlockQuote(_)) => Kind::Quote,
Event::Start(Tag::Item) => {
let number = lists.last_mut().and_then(Option::as_mut).map(|next| {
*next += 1;
*next - 1
});
let marker = marker_len(source, local, number.is_some());
Kind::Item {
marker: range.start..range.start + marker,
number,
task: None,
depth: lists.len(),
}
}
Event::Start(Tag::List(start)) => {
lists.push(start);
continue;
}
Event::End(TagEnd::List(_)) => {
lists.pop();
continue;
}
Event::End(TagEnd::BlockQuote(_) | TagEnd::Item) => {
open.pop();
continue;
}
Event::TaskListMarker(checked) => {
let item = open.last().and_then(|&i| syntax.containers.get_mut(i));
if let Some(Container { kind: Kind::Item { task, .. }, .. }) = item {
*task = Some((range, checked));
}
continue;
}
Event::Text(_)
| Event::Code(_)
| Event::Html(_)
| Event::InlineHtml(_)
| Event::InlineMath(_)
| Event::DisplayMath(_)
| Event::FootnoteReference(_) => {
syntax.leaves.push(range);
continue;
}
_ => continue,
};
open.push(syntax.containers.len());
let parent = open.iter().rev().nth(1).copied();
syntax.containers.push(Container { range, kind, parent });
}
syntax
}
pub(super) fn is_leaf(&self, offset: usize) -> bool {
let at = self.leaves.partition_point(|leaf| leaf.end <= offset);
self.leaves.get(at).is_some_and(|leaf| leaf.contains(&offset))
}
fn innermost(&self, offset: usize) -> Option<usize> {
let mut at = self.containers.partition_point(|c| c.range.start <= offset).checked_sub(1);
while let Some(index) = at {
let container = self.containers.get(index)?;
if container.range.contains(&offset) {
return Some(index);
}
at = container.parent;
}
None
}
pub(super) fn chain(&self, offset: usize) -> Vec<usize> {
let mut chain: Vec<usize> = self.ancestors(offset).collect();
chain.reverse();
chain
}
fn ancestors(&self, offset: usize) -> impl Iterator<Item = usize> + '_ {
std::iter::successors(self.innermost(offset), |&i| self.containers.get(i).and_then(|c| c.parent))
}
pub(super) fn part_at(&self, source: &str, base: usize, offset: usize) -> Option<Part> {
if self.is_leaf(offset) {
return None;
}
let inner = self.innermost(offset)?;
if let Some(Container { kind: Kind::Item { marker, task, .. }, .. }) = self.containers.get(inner)
&& (marker.contains(&offset) || task.as_ref().is_some_and(|(range, _)| range.contains(&offset)))
{
return Some(Part::Marker(inner));
}
let byte = offset.checked_sub(base).and_then(|i| source.as_bytes().get(i));
let quoted =
|| self.ancestors(offset).any(|i| self.containers.get(i).is_some_and(Container::is_quote));
(byte == Some(&b'>') && quoted()).then_some(Part::Quote)
}
pub(crate) fn aligned_source<'a>(&self, source: &'a str, base: usize) -> Cow<'a, str> {
let mut bytes = source.as_bytes().to_vec();
let mut set = |offset: usize, text: &[u8]| {
let at = offset.saturating_sub(base);
if let Some(target) = bytes.get_mut(at..at + text.len())
&& target.is_ascii()
{
target.copy_from_slice(text);
}
};
for container in &self.containers {
let Kind::Item { marker, number, task, .. } = &container.kind else { continue };
match number {
None => set(marker.start, b"-"),
Some(n) => {
let spelled = format!("{n}.");
if spelled.len() == marker.len() {
set(marker.start, spelled.as_bytes());
}
}
}
if let Some((range, checked)) = task {
set(range.start, if *checked { b"[x]" } else { b"[ ]" });
}
}
if bytes == source.as_bytes() {
return Cow::Borrowed(source);
}
String::from_utf8(bytes).map_or(Cow::Borrowed(source), Cow::Owned)
}
}
fn marker_len(source: &str, at: usize, ordered: bool) -> usize {
if !ordered {
return 1;
}
let rest = source.get(at..).unwrap_or_default();
rest.bytes().take_while(u8::is_ascii_digit).count() + 1
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn markers_and_task_boxes_are_found_where_the_parser_puts_them() {
let source = "> - a\n> 1) [X] b\n> 2) c";
let syntax = Syntax::parse(source, 100);
let byte = |text: &str| 100 + source.find(text).unwrap_or_default();
assert!(matches!(syntax.part_at(source, 100, byte("- a")), Some(Part::Marker(_))));
assert!(matches!(syntax.part_at(source, 100, byte(") [X]")), Some(Part::Marker(_))));
assert!(matches!(syntax.part_at(source, 100, byte("X]")), Some(Part::Marker(_))));
assert_eq!(syntax.part_at(source, 100, byte("> 2")), Some(Part::Quote));
assert_eq!(syntax.part_at(source, 100, byte("a")), None);
assert_eq!(syntax.aligned_source(source, 100), "> - a\n> 1. [x] b\n> 2. c");
}
#[test]
fn escaped_and_inline_syntax_is_content() {
let source = "- a \n \\- b > c\n\n `- d`";
let syntax = Syntax::parse(source, 0);
for text in ["- b", "> c", "- d"] {
assert_eq!(syntax.part_at(source, 0, source.find(text).unwrap_or_default()), None, "{text}");
}
}
}