use crate::diagnostic::Location;
use crate::line_index::LineIndex;
use crate::parse::params::interpolations;
use crate::tree::{Point, Position};
pub(crate) struct Lines<'a> {
text: &'a str,
starts: &'a LineIndex,
}
impl<'a> Lines<'a> {
pub(crate) fn new(text: &'a str, starts: &'a LineIndex) -> Self {
Self {
text: text.strip_prefix('\u{feff}').unwrap_or(text),
starts,
}
}
pub(crate) fn line(&self, number: usize) -> Option<&'a str> {
let start = self.starts.line_start(number)?.byte;
let end = self
.starts
.line_start(number + 1)
.map_or(self.text.len(), |next| next.byte);
let line = &self.text[start..end];
let line = line.strip_suffix('\n').unwrap_or(line);
Some(line.strip_suffix('\r').unwrap_or(line))
}
pub(crate) fn from(&self, at: Point) -> Vec<char> {
self.line(at.line)
.map(|line| line.chars().skip(at.column.saturating_sub(1)).collect())
.unwrap_or_default()
}
pub(crate) fn span(&self, position: Position) -> Vec<(char, Location)> {
let (start, end) = (position.start, position.end);
(start.line..=end.line)
.flat_map(|line| {
let first = if line == start.line { start.column } else { 1 };
let text = self.line(line).unwrap_or("");
text.chars()
.enumerate()
.map(move |(index, c)| (c, Location::new(line, index + 1)))
.skip(first - 1)
.take_while(move |(_, at)| line < end.line || at.column < end.column)
})
.collect()
}
pub(crate) fn paths_from(&self, start: Point) -> Vec<Location> {
interpolations(&self.from(start))
.into_iter()
.map(|(index, _)| Location::new(start.line, start.column + index))
.collect()
}
pub(crate) fn path_at(&self, start: Point) -> Location {
self.paths_from(start)
.first()
.copied()
.unwrap_or_else(|| start.into())
}
pub(crate) fn name_at(&self, start: Point) -> Location {
let rest = self.from(start);
let colons = rest.iter().take_while(|c| **c == ':').count();
let spaces = rest[colons..]
.iter()
.take_while(|c| c.is_whitespace())
.count();
Location::new(start.line, start.column + colons + spaces)
}
pub(crate) fn language_at(&self, start: Point) -> Location {
let rest = self.from(start);
let fence = rest.iter().take_while(|c| matches!(c, '`' | '~')).count();
let spaces = rest[fence..]
.iter()
.take_while(|c| c.is_whitespace())
.count();
Location::new(start.line, start.column + fence + spaces)
}
}
#[cfg(test)]
mod tests {
use super::*;
fn point(line: usize, column: usize) -> Point {
Point {
line,
column,
offset: 0,
}
}
#[test]
fn names_follow_the_colons_and_white_space() {
let text = "\u{feff}::: grid\r\n :: chip[x]\nText :badge[b]";
let starts = LineIndex::new(text);
let lines = Lines::new(text, &starts);
assert_eq!(lines.name_at(point(1, 1)), Location::new(1, 5));
assert_eq!(lines.name_at(point(2, 3)), Location::new(2, 7));
assert_eq!(lines.name_at(point(3, 6)), Location::new(3, 7));
assert_eq!(lines.from(point(9, 1)), Vec::<char>::new());
assert_eq!(lines.from(point(0, 1)), Vec::<char>::new());
}
#[test]
fn a_code_blocks_language_follows_its_fence() {
let text = "```mermaid\n ~~~~ dot {a=1}";
let starts = LineIndex::new(text);
let lines = Lines::new(text, &starts);
assert_eq!(lines.language_at(point(1, 1)), Location::new(1, 4));
assert_eq!(lines.language_at(point(2, 3)), Location::new(2, 8));
}
#[test]
fn paths_are_past_their_braces() {
let text = "x {t=\"a {{ b }} {{c}}\"}\nnone";
let starts = LineIndex::new(text);
let lines = Lines::new(text, &starts);
assert_eq!(
lines.paths_from(point(1, 6)),
[Location::new(1, 12), Location::new(1, 19)]
);
assert_eq!(lines.path_at(point(1, 9)), Location::new(1, 12));
assert_eq!(lines.path_at(point(2, 1)), Location::new(2, 1));
}
#[test]
fn a_span_runs_across_lines() {
let text = "ab [x\ny](u) z";
let starts = LineIndex::new(text);
let lines = Lines::new(text, &starts);
let span = lines.span(Position {
start: point(1, 4),
end: point(2, 6),
});
let text: String = span.iter().map(|(c, _)| c).collect();
assert_eq!(text, "[xy](u)");
assert_eq!(span[2].1, Location::new(2, 1));
}
#[test]
fn a_line_is_without_its_line_ending_of_any_kind() {
let text = "a\r\nb\rc\nd";
let starts = LineIndex::new(text);
let lines = Lines::new(text, &starts);
let all: Vec<Option<&str>> = (1..=5).map(|line| lines.line(line)).collect();
assert_eq!(all, [Some("a"), Some("b"), Some("c"), Some("d"), None]);
assert_eq!(lines.name_at(point(3, 1)), Location::new(3, 1));
}
}