compmd 0.12.0

The CompMD processor: parses, validates and expands CompMD (Component Markdown) documents. In development.
Documentation
//! Positions in code points (spec §10.5) for text the fork of markdown-rs parsed, whose positions count bytes.

use crate::line_index::LineIndex;
use crate::tree::{Point, Position};

/// Text being parsed, and where it starts in its document: a fallback or a definition's body is parsed on its own
/// and placed where it is written.
pub(crate) struct Source<'a> {
    text: &'a str,
    origin: Point,
    /// Where each of its lines starts.
    lines: LineIndex,
}

impl<'a> Source<'a> {
    /// `text` placed at `origin`: its first line starts at `origin`, later lines at column 1.
    pub(crate) fn new(text: &'a str, origin: Point) -> Self {
        Self {
            text,
            origin,
            lines: LineIndex::of(text),
        }
    }

    /// The text.
    pub(crate) fn text(&self) -> &'a str {
        self.text
    }

    /// The point at byte `index` of the text.
    pub(crate) fn point(&self, index: usize) -> Point {
        let line = self.lines.line_of_byte(index);
        let start = self
            .lines
            .line_start(line)
            .unwrap_or_else(|| unreachable!("a byte is on a line"));
        let column = self.text[start.byte..index].chars().count();
        Point {
            line: self.origin.line + line - 1,
            column: if line == 1 {
                self.origin.column + column
            } else {
                1 + column
            },
            offset: self.origin.offset + start.offset + column,
        }
    }

    /// The position of a node the fork made.
    pub(crate) fn position(
        &self,
        position: Option<&markdown::unist::Position>,
    ) -> Option<Position> {
        position.map(|position| Position {
            start: self.point(position.start.offset),
            end: self.point(position.end.offset),
        })
    }

    /// Line `line` of the document, without its line ending, and the point where it starts; `None` if the text
    /// doesn't hold it.
    pub(crate) fn line(&self, line: usize) -> Option<(&'a str, Point)> {
        let index = line.checked_sub(self.origin.line)? + 1;
        let start = self.lines.line_start(index)?.byte;
        let end = self
            .lines
            .line_start(index + 1)
            .map_or(self.text.len(), |next| next.byte);
        let text = self.text[start..end].trim_end_matches(['\n', '\r']);
        Some((text, self.point(start)))
    }
}

/// The point `chars` code points further on the same line.
pub(crate) fn right(point: Point, chars: usize) -> Point {
    Point {
        column: point.column + chars,
        offset: point.offset + chars,
        ..point
    }
}

/// The start of a document.
pub(crate) const START: Point = Point {
    line: 1,
    column: 1,
    offset: 0,
};

#[cfg(test)]
mod tests {
    use super::*;

    fn at(line: usize, column: usize, offset: usize) -> Point {
        Point {
            line,
            column,
            offset,
        }
    }

    #[test]
    fn counts_code_points_on_every_kind_of_line_ending() {
        let source = Source::new("aé\r\nb\rc\nd", START);
        assert_eq!(source.point(3), at(1, 3, 2));
        assert_eq!(source.point(5), at(2, 1, 4));
        assert_eq!(source.point(7), at(3, 1, 6));
        assert_eq!(source.point(9), at(4, 1, 8));
    }

    #[test]
    fn places_text_at_its_origin() {
        let source = Source::new("ab\ncd", at(4, 7, 30));
        assert_eq!(source.point(1), at(4, 8, 31));
        assert_eq!(source.point(4), at(5, 2, 34));
    }

    #[test]
    fn gives_a_line_and_where_it_starts() {
        let source = Source::new("ab\r\ncd\n", at(3, 1, 10));
        assert_eq!(source.line(4), Some(("cd", at(4, 1, 14))));
        assert_eq!(source.line(2), None);
        assert_eq!(source.line(9), None);
    }

    #[test]
    fn moves_right_on_a_line() {
        assert_eq!(right(at(2, 3, 9), 4), at(2, 7, 13));
    }
}