use super::leaf::LeafState;
use super::{Interrupt, blocks_lazy_continuation};
use crate::parser::line::Line;
pub(super) struct Collector<'a> {
pub(super) lines: Vec<Line<'a>>,
pub(super) blanks: Vec<Line<'a>>,
state: LeafState<'a>,
}
impl<'a> Collector<'a> {
pub(super) fn new(depth: usize, interrupting: Interrupt) -> Self {
Self {
lines: Vec::new(),
blanks: Vec::new(),
state: LeafState::new(interrupting, depth),
}
}
pub(super) fn push(&mut self, line: Line<'a>) {
for blank in &self.blanks {
self.state.feed(blank);
}
self.lines.append(&mut self.blanks);
self.state.feed(&line);
self.lines.push(line);
}
pub(super) fn blank(&mut self, line: Line<'a>) {
self.blanks.push(line);
}
pub(super) fn lazy(&mut self, lines: &[Line<'a>], index: usize) -> bool {
let line = &lines[index];
if !self.blanks.is_empty() || !self.state.continues_with(line) || blocks_lazy_continuation(lines, index) {
return false;
}
self.push(Line { lazy: true, ..*line });
true
}
pub(super) fn keep_fence_blanks(&mut self) {
if self.blanks.first().is_some_and(|blank| self.state.fence_holds(blank)) {
self.lines.append(&mut self.blanks);
}
}
}