Sign in

    #span

    Typed positions: Offset (a UTF-16 unit index), Length, Line and Column are never negative or zero where that makes no sense, because their constructors are the only way in. A Span is a start and a length. SourceText maps offsets to positions and lines.

    ///|
    test {
    let st = @span.SourceText::new("string Foo\nstring Bar\n", name="a.smithy")
    let at = @span.Offset::new(11).unwrap()
    debug_inspect(st.position(at), content="{ line: Line(2), column: Column(1) }")
    debug_inspect(
    st.line_text(@span.Line::first().next()),
    content="Some(\"string Bar\")",
    )
    }

    Column

    pub struct Column(Int) derive(Compare, Eq, Hash,
    Debug
    )

    A 1-based column, counting UTF-16 units as the Java reference does.

    Column::first

    fn Column::first() -> Column

    The first column of a line, column 1.

    Column::new

    fn Column::new(n : Int) -> Column?

    The column n, or None when n is less than 1.

    Column::next

    fn Column::next(self : Column) -> Column

    The column after self.

    Length

    pub struct Length(Int) derive(Compare, Eq, Hash,
    Debug
    )

    A count of UTF-16 units. Never negative.
    impl Add for Length

    Length::new

    fn Length::new(n : Int) -> Length?

    The length n, or None when n is negative.

    Length::of_string

    fn Length::of_string(s : String) -> Length

    The length of a string in UTF-16 units.

    Length::one

    fn Length::one() -> Length

    The length of a single unit.

    Length::zero

    fn Length::zero() -> Length

    The length of an empty range.

    Line

    pub struct Line(Int) derive(Compare, Eq, Hash,
    Debug
    )

    A 1-based line number.

    Line::first

    fn Line::first() -> Line

    The first line of a text, line 1.

    Line::new

    fn Line::new(n : Int) -> Line?

    The line n, or None when n is less than 1.

    Line::next

    fn Line::next(self : Line) -> Line

    The line after self.

    Offset

    pub struct Offset(Int) derive(Compare, Eq, Hash,
    Debug
    )

    A UTF-16 unit index into a source text. Never negative: Offset::new is the only way in from an Int.

    Offset::advance

    fn Offset::advance(self : Offset, by : Length) -> Offset

    Add is homogeneous in MoonBit, so stepping an offset by a length is a named method.

    Offset::distance

    fn Offset::distance(self : Offset, other : Offset) -> Length

    The number of units between two offsets, in either order.

    Offset::new

    fn Offset::new(n : Int) -> Offset?

    The offset n, or None when n is negative.

    Offset::next

    fn Offset::next(self : Offset) -> Offset

    The offset one unit after self.

    Offset::to

    fn Offset::to(self : Offset, end : Offset) -> Span

    The span from self to end, ordered either way.

    Offset::zero

    fn Offset::zero() -> Offset

    The offset of the first unit of a text.

    Position

    pub(all) struct Position {
    line : Line
    column : Column
    } derive(Eq, Hash,
    Debug
    )

    A 1-based line and column.

    Position::new

    fn Position::new(line : Line, column : Column) -> Position

    The position at line and column.

    SourceText

    pub struct SourceText {
    // private fields
    }

    A source text with its line table. The table is built once; every lookup is a pure function of it.

    SourceText::end

    fn SourceText::end(self : SourceText) -> Offset

    The offset just past the last unit.

    SourceText::line_count

    fn SourceText::line_count(self : SourceText) -> Int

    One more than the number of line breaks.

    SourceText::line_span

    fn SourceText::line_span(self : SourceText, line : Line) -> Span?

    The span of a line without its line break; None past the last line.

    SourceText::line_text

    fn SourceText::line_text(self : SourceText, line : Line) -> String?

    The text of a line, without its line break.

    SourceText::name

    fn SourceText::name(self : SourceText) -> String?

    The name given at construction (a file name), if any.

    SourceText::new

    fn SourceText::new(text : String, name? : String) -> SourceText

    LF and CRLF end a line; a lone CR does not.

    SourceText::position

    fn SourceText::position(self : SourceText, offset : Offset) -> Position

    The line and column of offset. An offset past the end is the position just after the last unit.

    SourceText::slice

    fn SourceText::slice(self : SourceText, span : Span) -> String

    The text of span, clamped to the text.

    SourceText::text

    fn SourceText::text(self : SourceText) -> String

    The whole source text.

    Span

    pub(all) struct Span {
    start : Offset
    length : Length
    } derive(Eq, Hash,
    Debug
    )

    A half-open range of a source text: start included, length units. Built from a length, so the end never precedes the start.

    Span::contains

    fn Span::contains(self : Span, offset : Offset) -> Bool

    Whether offset lies inside the span (the end is excluded).

    Span::end

    fn Span::end(self : Span) -> Offset

    The offset just past the last unit of the span.

    Span::is_empty

    fn Span::is_empty(self : Span) -> Bool

    Whether the span covers no units.

    Span::join

    fn Span::join(self : Span, other : Span) -> Span

    The smallest span that covers both.

    Span::new

    fn Span::new(start : Offset, length : Length) -> Span

    The span of length units starting at start.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io