use super::error::MdxError;
use super::line::{extra_bytes, to_u32};
use super::mdx::TagKind;
use super::scan::eol_len;
use crate::node::{HeadingDepth, ListMarker, MdxAttributeContent, Node, Point, Position, TableAlignKind};
#[derive(Clone)]
pub(super) struct LineStart {
pub(super) offset: usize,
pub(super) point: Point,
}
#[derive(Clone)]
pub(super) struct InlineSource {
pub(super) text: String,
pub(super) lines: Vec<LineStart>,
pub(super) table: bool,
non_ascii: Option<Box<NonAscii>>,
}
#[derive(Clone)]
struct NonAscii {
line_extra: Vec<usize>,
cursor: std::cell::Cell<(usize, usize)>,
}
impl InlineSource {
pub(super) fn new<'a>(lines: impl ExactSizeIterator<Item = (&'a str, &'a str, Point)>) -> Self {
let count = lines.len();
let mut text = String::new();
let mut starts = Vec::with_capacity(count);
for (index, (line, eol, start)) in lines.enumerate() {
starts.push(LineStart {
offset: text.len(),
point: start,
});
text.push_str(line);
if index + 1 < count {
text.push_str(eol);
}
}
let mut source = Self {
text,
lines: starts,
table: false,
non_ascii: None,
};
source.index_lines();
source
}
fn index_lines(&mut self) {
if self.text.is_ascii() {
self.non_ascii = None;
return;
}
let mut line_extra = Vec::with_capacity(self.lines.len());
let mut extra = 0;
let mut from = 0;
for line in &self.lines {
extra += extra_bytes(&self.text.as_bytes()[from..line.offset]);
from = line.offset;
line_extra.push(extra);
}
self.non_ascii = Some(Box::new(NonAscii {
line_extra,
cursor: std::cell::Cell::new((0, 0)),
}));
}
fn extra_before(&self, offset: usize) -> usize {
let Some(non_ascii) = &self.non_ascii else {
return 0;
};
let bytes = self.text.as_bytes();
let (at, extra) = non_ascii.cursor.get();
let extra = if offset >= at {
extra + extra_bytes(&bytes[at..offset])
} else {
extra - extra_bytes(&bytes[offset..at])
};
non_ascii.cursor.set((offset, extra));
extra
}
pub(super) fn remove_prefix_and_one(&mut self, marker: usize) {
let bytes = self.text.as_bytes();
let one = if matches!(bytes.get(marker), Some(b' ' | b'\t')) {
1 + eol_len(bytes, marker + 1)
} else {
eol_len(bytes, marker)
};
self.drain_to(marker + one);
}
#[cfg(feature = "callout")]
pub(super) fn take_first_line(&mut self) -> String {
let Some(eol) = self.text.find(['\n', '\r']) else {
self.lines.clear();
return std::mem::take(&mut self.text);
};
let line = self.text[..eol].to_string();
let end = eol + eol_len(self.text.as_bytes(), eol);
self.drain_to(end);
line
}
fn drain_to(&mut self, end: usize) {
let new_point = self.point(end);
self.text.replace_range(..end, "");
let index = self.line_index(end);
let first = LineStart {
offset: 0,
point: Point {
line: self.lines[index].point.line,
column: new_point.column,
},
};
let rest = self.lines[index + 1..].iter().map(|line| LineStart {
offset: line.offset - end,
point: line.point.clone(),
});
self.lines = std::iter::once(first).chain(rest).collect();
self.index_lines();
}
pub(super) fn line_index(&self, offset: usize) -> usize {
self.lines
.partition_point(|line| line.offset <= offset)
.saturating_sub(1)
}
pub(super) fn end_point(&self, offset: usize) -> Point {
let line = &self.lines[self.line_index(offset)];
if line.offset == offset && offset > 0 {
return Point {
line: line.point.line,
column: 1,
};
}
self.point(offset)
}
pub(super) fn point(&self, offset: usize) -> Point {
let index = self.line_index(offset);
let LineStart { offset: start, point } = &self.lines[index];
let extra = match self
.non_ascii
.as_ref()
.and_then(|non_ascii| non_ascii.line_extra.get(index))
{
Some(before) => self.extra_before(offset) - before,
None => 0,
};
Point {
line: point.line,
column: point.column + to_u32(offset - start - extra),
}
}
}
pub(super) enum Block {
Node(Node),
Fenced(FencedBlock),
Inline(InlineBlock),
Quote(QuoteBlock),
List(ListBlock),
Footnote(FootnoteBlock),
Table(Vec<TableItem>),
Jsx(JsxTag),
Error(MdxError),
}
pub(super) struct FencedBlock {
pub(super) node: Node,
pub(super) closed: bool,
pub(super) lines: usize,
}
#[derive(Clone)]
pub(super) struct InlineBlock {
pub(super) source: InlineSource,
pub(super) kind: InlineKind,
}
#[derive(Clone)]
pub(super) enum InlineKind {
Paragraph,
Heading { depth: HeadingDepth, position: Position },
}
pub(super) struct QuoteBlock {
pub(super) children: Vec<Block>,
pub(super) position: Position,
}
pub(super) struct FootnoteBlock {
pub(super) ident: String,
pub(super) children: Vec<Block>,
pub(super) position: Position,
}
pub(super) struct ListBlock {
pub(super) ordered: bool,
pub(super) start: Option<u32>,
pub(super) marker: ListMarker,
pub(super) spread: bool,
pub(super) items: Vec<Item>,
}
pub(super) struct Item {
pub(super) checked: Option<bool>,
pub(super) children: Vec<Block>,
pub(super) position: Position,
pub(super) content_end: Point,
}
pub(super) enum TableItem {
Cell {
row: usize,
column: usize,
position: Position,
source: Option<InlineSource>,
},
Align {
align: Vec<TableAlignKind>,
position: Position,
},
}
pub(super) struct JsxTag {
pub(super) name: Option<String>,
pub(super) attributes: Vec<MdxAttributeContent>,
pub(super) kind: TagKind,
pub(super) position: Position,
}