use super::block::CODE_INDENT;
use super::flavor::Flavor;
use crate::node::Point;
pub(super) const BOM: char = '\u{feff}';
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(super) struct Indent {
pub(super) columns: usize,
pub(super) bytes: usize,
}
#[derive(Clone, Copy)]
pub(super) struct Line<'a> {
pub(super) number: u32,
pub(super) column: usize,
pub(super) text: &'a str,
pub(super) eol: &'a str,
pub(super) eof: bool,
pub(super) item_end: bool,
pub(super) lazy: bool,
pub(super) stops: &'a [TabStop],
pub(super) whole: &'a str,
pub(super) base: usize,
pub(super) pad: usize,
pub(super) ws: usize,
pub(super) flavor: Flavor,
pub(super) others: [usize; 3],
}
impl<'a> Line<'a> {
pub(super) fn code_indent(&self) -> usize {
if self.flavor.has_indented_code() {
CODE_INDENT
} else {
usize::MAX
}
}
fn start_column(&self) -> usize {
self.visual_column(self.column)
}
fn visual_column(&self, offset: usize) -> usize {
let at = self.base + offset;
let passed = self.stops.partition_point(|stop| stop.at < at);
match passed.checked_sub(1).map(|index| self.stops[index]) {
Some(stop) => stop.after + (at - stop.at - 1),
None => offset,
}
}
pub(super) fn indent(&self) -> Indent {
let start = self.start_column();
let end = self.visual_column(self.column + self.ws);
Indent {
columns: self.pad + end - start,
bytes: self.ws,
}
}
pub(super) fn is_blank(&self) -> bool {
self.ws == self.text.len()
}
pub(super) fn point(&self, byte: usize) -> Point {
let offset = self.column + byte;
Point {
line: self.number,
column: to_u32(offset - extra_bytes(&self.whole.as_bytes()[..offset.min(self.whole.len())]) + 1),
}
}
pub(super) fn skip(self, bytes: usize) -> Line<'a> {
let text = &self.text[bytes..];
Line {
column: self.column + bytes,
text,
pad: 0,
ws: self.ws.checked_sub(bytes).unwrap_or_else(|| leading_ws(text)),
..self
}
}
pub(super) fn skip_columns(self, columns: usize) -> Line<'a> {
let mut remaining = columns;
let consumed = remaining.min(self.pad);
let mut pad = self.pad - consumed;
remaining -= consumed;
let mut absolute = self.start_column();
let mut bytes = 0;
for byte in self.text.bytes() {
if remaining == 0 {
break;
}
match byte {
b' ' => {
remaining -= 1;
absolute += 1;
}
b'\t' => {
let width = (absolute / 4 + 1) * 4 - absolute;
absolute += width;
if width > remaining {
pad = width - remaining;
remaining = 0;
} else {
remaining -= width;
}
}
_ => break,
}
bytes += 1;
}
Line {
column: self.column + bytes,
text: &self.text[bytes..],
pad,
ws: self.ws - bytes,
..self
}
}
pub(super) fn end(&self) -> Point {
self.point(self.text.len())
}
}
#[derive(Clone, Copy)]
pub(super) struct TabStop {
at: usize,
after: usize,
}
pub(super) fn tab_stops(src: &str) -> Vec<TabStop> {
let mut stops = Vec::new();
let (mut anchor, mut anchor_column) = (if src.starts_with(BOM) { BOM.len_utf8() } else { 0 }, 0);
for (index, byte) in src.bytes().enumerate() {
match byte {
b'\n' | b'\r' => (anchor, anchor_column) = (index + 1, 0),
b'\t' => {
let column = anchor_column + (index - anchor);
let after = (column / 4 + 1) * 4;
stops.push(TabStop { at: index, after });
(anchor, anchor_column) = (index + 1, after);
}
_ => {}
}
}
stops
}
pub(super) fn to_u32(value: usize) -> u32 {
u32::try_from(value).unwrap_or(u32::MAX)
}
pub(super) fn extra_bytes(bytes: &[u8]) -> usize {
let (mut continuation, mut astral) = (0, 0);
for &byte in bytes {
continuation += usize::from(byte & 0xC0 == 0x80);
astral += usize::from(byte >= 0xF0);
}
continuation - astral
}
pub(super) fn leading_ws(text: &str) -> usize {
text.bytes().take_while(|byte| matches!(byte, b' ' | b'\t')).count()
}
fn last_others(text: &str) -> [usize; 3] {
let mut last = [0; 3];
for (index, byte) in text.bytes().enumerate() {
match byte {
b' ' | b'\t' => {}
b'*' => (last[1], last[2]) = (index + 1, index + 1),
b'-' => (last[0], last[2]) = (index + 1, index + 1),
b'_' => (last[0], last[1]) = (index + 1, index + 1),
_ => last = [index + 1; 3],
}
}
last
}
pub(super) fn split_lines<'a>(src: &'a str, flavor: Flavor, stops: &'a [TabStop]) -> Vec<Line<'a>> {
let bytes = src.as_bytes();
let mut lines = Vec::new();
let (mut start, mut index) = (0, 0);
let mut next_stop = 0;
let mut take_stops = |end: usize| {
let first = next_stop;
while stops.get(next_stop).is_some_and(|stop| stop.at < end) {
next_stop += 1;
}
&stops[first..next_stop]
};
while index < bytes.len() {
let end = index;
match bytes[index] {
b'\n' => index += 1,
b'\r' => index += if bytes.get(index + 1) == Some(&b'\n') { 2 } else { 1 },
_ => {
index += 1;
continue;
}
}
lines.push(Line {
number: to_u32(lines.len() + 1),
column: 0,
text: &src[start..end],
eol: &src[end..index],
eof: index == bytes.len(),
item_end: false,
lazy: false,
stops: take_stops(end),
whole: &src[start..end],
base: start,
pad: 0,
ws: leading_ws(&src[start..end]),
flavor,
others: last_others(&src[start..end]),
});
start = index;
}
if start < src.len() {
lines.push(Line {
number: to_u32(lines.len() + 1),
column: 0,
text: &src[start..],
eol: "",
eof: true,
item_end: false,
lazy: false,
stops: take_stops(src.len()),
whole: &src[start..],
base: start,
pad: 0,
ws: leading_ws(&src[start..]),
flavor,
others: last_others(&src[start..]),
});
}
lines
}