goxmap

package
v0.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2026 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

Package goxmap provides source positions and bidirectional mappings between generated GOX artifacts and their original source files.

Offsets and columns are byte based. Protocol-specific encodings, such as the UTF-16 positions used by LSP, are converted only at the package boundary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MarshalJSONDocument

func MarshalJSONDocument(sourceMap *Map, generated string, sources map[string]string) ([]byte, error)

MarshalJSONDocument serializes a source map as readable line, column and byte ranges. Lines and columns are zero based, matching LSP conventions.

Types

type BytePosition

type BytePosition struct {
	Line   int
	Column int
}

BytePosition identifies a zero-based line and byte column.

type JSONDocument

type JSONDocument struct {
	Version      int           `json:"version"`
	GeneratedURI string        `json:"generatedUri"`
	Sources      []string      `json:"sources"`
	Mappings     []JSONMapping `json:"mappings"`
}

type JSONMapping

type JSONMapping struct {
	Generated JSONRange `json:"generated"`
	Source    string    `json:"source"`
	Original  JSONRange `json:"original"`
	Kind      string    `json:"kind"`
}

type JSONPosition

type JSONPosition struct {
	Offset int `json:"offset"`
	Line   int `json:"line"`
	Column int `json:"column"`
}

type JSONRange

type JSONRange struct {
	Start JSONPosition `json:"start"`
	End   JSONPosition `json:"end"`
}

type LineIndex

type LineIndex struct {
	// contains filtered or unexported fields
}

LineIndex converts positions in one immutable source string. Newline bytes are represented by the position immediately after the line content. CRLF is treated as one newline and neither byte is part of the line content.

func NewLineIndex

func NewLineIndex(text string) *LineIndex

NewLineIndex indexes text without copying it. The string remains immutable by construction and may be shared safely by readers.

func (*LineIndex) ByteOffset

func (i *LineIndex) ByteOffset(position BytePosition) (int, error)

ByteOffset converts a byte-based position to an offset.

func (*LineIndex) BytePosition

func (i *LineIndex) BytePosition(offset int) (BytePosition, error)

BytePosition converts an offset to a byte-based position.

func (*LineIndex) LineCount

func (i *LineIndex) LineCount() int

LineCount returns the number of logical lines, including the empty final line when text ends in a newline.

func (*LineIndex) Text

func (i *LineIndex) Text() string

Text returns the indexed source text.

func (*LineIndex) UTF16Offset

func (i *LineIndex) UTF16Offset(position UTF16Position) (int, error)

UTF16Offset converts an LSP-compatible UTF-16 position to a byte offset. A position may not split a surrogate pair.

func (*LineIndex) UTF16Position

func (i *LineIndex) UTF16Position(offset int) (UTF16Position, error)

UTF16Position converts an offset to an LSP-compatible UTF-16 position.

type Map

type Map struct {
	// contains filtered or unexported fields
}

Map is an immutable, bidirectional source map. Generated ranges may not overlap, which keeps forward lookups deterministic. Multiple generated ranges may refer to the same original range.

func Compose

func Compose(generatedURI string, lower, upper *Map) (*Map, error)

Compose combines lower (generated -> intermediate) with upper (intermediate -> original). Only lower ranges that refer to upper's generated URI participate. Exact intersections stay exact; generated scaffolding or an upstream anchor remains an anchor.

func New

func New(generatedURI string, mappings []Mapping) (*Map, error)

New validates and constructs a source map. Input mappings are copied and sorted by their generated range.

func UnmarshalJSONDocument

func UnmarshalJSONDocument(data []byte) (*Map, error)

UnmarshalJSONDocument restores the immutable offset map used by the compiler and language tooling from its persisted, readable representation.

func (*Map) GeneratedPositions

func (m *Map) GeneratedPositions(uri string, offset int) []Position

GeneratedPositions maps an original byte offset to every generated location derived from it. Results follow generated-document order.

func (*Map) GeneratedURI

func (m *Map) GeneratedURI() string

GeneratedURI returns the identity of the generated document.

func (*Map) Mappings

func (m *Map) Mappings() []Mapping

Mappings returns a copy of the map's ordered segments.

func (*Map) OriginalPosition

func (m *Map) OriginalPosition(offset int) (Position, bool)

OriginalPosition maps a generated byte offset back to its GOX source.

func (*Map) OriginalRange

func (m *Map) OriginalRange(generated Range) (SourceRange, MappingKind, bool)

OriginalRange maps a non-empty generated range when it is fully contained in one mapping segment. Ranges crossing generated and source boundaries are intentionally rejected instead of being reported with false precision.

type Mapping

type Mapping struct {
	Generated Range
	Original  SourceRange
	Kind      MappingKind
}

Mapping relates one range in a generated document to an original source.

type MappingKind

type MappingKind uint8

MappingKind describes how precisely generated bytes correspond to source.

const (
	// Exact maps byte-for-byte preserved text. Generated and original ranges
	// must have the same length.
	Exact MappingKind = iota
	// Anchor associates generated scaffolding with an original range. Positions
	// inside it resolve to the start of the original range and are not exact.
	Anchor
)

type Position

type Position struct {
	URI    string
	Offset int
	Exact  bool
}

Position is the result of a source-map lookup.

type Range

type Range struct {
	Start int
	End   int
}

Range is a zero-based, half-open byte range [Start, End).

func (Range) Contains

func (r Range) Contains(offset int) bool

Contains reports whether offset is inside the half-open range.

func (Range) Empty

func (r Range) Empty() bool

Empty reports whether the range contains no bytes.

func (Range) Len

func (r Range) Len() int

Len returns the number of bytes in the range.

func (Range) Valid

func (r Range) Valid() bool

Valid reports whether the range contains non-negative, ordered offsets.

type SourceRange

type SourceRange struct {
	URI   string
	Range Range
}

SourceRange identifies a range in one source document.

type UTF16Position

type UTF16Position struct {
	Line      int
	Character int
}

UTF16Position identifies a zero-based line and UTF-16 code-unit column, as required by the Language Server Protocol.

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer builds transformed text and the map from that text to one immediate input document. It deliberately distinguishes copied bytes from generated scaffolding so callers cannot accidentally mark generated code as exact.

func NewWriter

func NewWriter(generatedURI, sourceURI, source string) (*Writer, error)

NewWriter creates a writer for one transformation stage.

func (*Writer) Build

func (w *Writer) Build() (string, *Map, error)

Build returns the generated text and its validated immutable map.

func (*Writer) Len

func (w *Writer) Len() int

Len returns the current generated byte length.

func (*Writer) String

func (w *Writer) String() string

String returns the generated text written so far.

func (*Writer) WriteGenerated

func (w *Writer) WriteGenerated(text string, anchor Range) error

WriteGenerated writes compiler-generated text anchored to a source range. Empty generated text is ignored; the anchor itself may be a point range.

func (*Writer) WriteSource

func (w *Writer) WriteSource(sourceRange Range) error

WriteSource copies an exact source range into the generated document.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL