@cruglobal/js-hcl2 - v0.1.1
    Preparing search index...

    Class SourceFile

    Wraps a source string with a precomputed line-offset index so that positionOf(offset) runs in O(log lines) amortized. Constructing a SourceFile is one linear pass over the text.

    Recognised line terminators: LF (\n), CRLF (\r\n), and bare CR (\r). HCL2's spec only names LF and CRLF, but bare CR is accepted for robustness against files that crossed a Classic-Mac-era checkout.

    Index

    Constructors

    Properties

    filename: string
    lineStarts: readonly number[]

    UTF-16 offsets of each line's first character. Always starts with 0, and contains one entry per line. lineStarts.length equals the number of lines. For a file ending in a newline, the final entry points just past the end of text.

    text: string

    Accessors

    • get lineCount(): number

      Number of lines in the file. Always at least 1.

      Returns number

    Methods

    • 0-based offset of the first character of line (1-based).

      Parameters

      • line: number

      Returns number

    • Returns the text of the given 1-based line, excluding the trailing line terminator. Works for the last line even if the file has no final newline.

      Parameters

      • line: number

      Returns string

    • Convert a UTF-16 offset to a full Position. Throws RangeError if the offset is outside [0, text.length]. An offset equal to text.length is valid and represents end-of-file.

      Parameters

      • offset: number

      Returns Position