source

package
v0.14.2-0...-12ef1ef Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2026 License: Apache-2.0 Imports: 18 Imported by: 0

Documentation

Overview

Package source provides a standard interfaces for working with source files and spans within them.

File is a source code file read from somewhere, which tracks its contents, its on-disk path, and book-keeping information for looking up offsets. Span is a region of some File which is used for diagnostics. Opener is a common interface for loading [File]s on disk.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type FS

type FS struct {
	fs.FS

	// If not nil, paths are passed to this function before being forwarded
	// to fs.
	PathMapper func(string) string
}

FS wraps an fs.FS to give it an Opener interface.

func (*FS) Open

func (fs *FS) Open(path string) (*File, error)

Open implements Opener.

type File

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

File is a source code file involved in a diagnostic.

It contains additional book-keeping information for resolving span locations. Files are immutable once created.

A nil *File behaves like an empty file with the path name "".

func NewFile

func NewFile(path, text string) *File

NewFile constructs a new source file.

func (*File) EOF

func (f *File) EOF() Span

EOF returns a Span pointing to the end-of-file.

func (*File) Indentation

func (f *File) Indentation(offset int) string

Indentation calculates the indentation some offset.

Indentation is defined as the substring between the last newline in before the offset and the first non-Pattern_White_Space after that newline.

func (*File) InverseLocation

func (f *File) InverseLocation(line, column int, units length.Unit) Location

InverseLocation inverts the operation in File.Location.

line and column should be 1-indexed, and units should be the units used to measure the column width. If units is [TermWidth], this function panics, because inverting a [TermWidth] location is not supported.

func (*File) Line

func (f *File) Line(line int) string

Line returns the given line, including its trailing newline.

line is expected to be 1-indexed.

func (*File) LineByOffset

func (f *File) LineByOffset(offset int) (number int)

LineByOffset searches this index to find the line number for the line containing this byte offset.

This operation is O(log n).

func (*File) LineOffsets

func (f *File) LineOffsets(line int) (start, end int)

LineOffsets returns the offsets for the given line, including its trailing newline.

line is expected to be 1-indexed.

func (*File) Location

func (f *File) Location(offset int, units length.Unit) Location

Location searches this index to build full Location information for the given byte offset.

This operation is O(log n).

func (*File) Path

func (f *File) Path() string

Path returns this file's filesystem path.

It doesn't need to be a real path, but it will be used to deduplicate spans according to their file.

func (*File) Span

func (f *File) Span(start, end int) Span

Span is a shorthand for creating a new Span.

func (*File) Text

func (f *File) Text() string

Text returns this file's textual contents.

type Location

type Location struct {
	// The byte offset for this location.
	Offset int

	// The line and column for this location, 1-indexed.
	//
	// The units of measurement for column depend on the [length.Unit] used when
	// constructing it.
	//
	// Because these are 1-indexed, a zero Line can be used as a sentinel.
	Line, Column int
}

Location is a user-displayable location within a source code file.

type Map

type Map cmpx.MapWrapper[string, *File]

Map implements Opener via lookup of a built-in map. This map is not directly accessible, to help avoid mistaken uses that cause different *Map pointer values (for the same built-in map value) to wind up in different queries, which breaks query caching.

Missing entries result in fs.ErrNotExist.

func NewMap

func NewMap(m map[string]*File) Map

NewMap creates a new Map wrapping the given map.

If passed nil, this will update the map to be an empty non-nil map.

func (Map) Add

func (m Map) Add(path, text string)

Add adds a new file to this map.

func (Map) Get

func (m Map) Get() map[string]*File

Get returns the map this Map wraps. This can be used to modify the map.

Never returns nil.

func (Map) Open

func (m Map) Open(path string) (*File, error)

Open implements Opener.

type Opener

type Opener interface {
	// Open opens a file, potentially returning an error.
	//
	// Note that the path of the returned file need not he path; this path should
	// *only* be used for diagnostics.
	//
	// A return value of [fs.ErrNotExist] is given special treatment by some
	// Opener adapters, such as the [Openers] type.
	Open(path string) (*File, error)
}

Opener is a mechanism for opening files.

Opener implementations are assumed by Protocompile to be comparable. It is sufficient to always ensure that the implementation uses a pointer receiver.

func WKTs

func WKTs() Opener

WKTs returns an Opener that yields in-memory Protobuf well-known type sources.

type Openers

type Openers []Opener

Openers wraps a sequence of [Opener]s.

When calling Open, it calls each Opener in sequence until one does not return fs.ErrNotExist.

func (*Openers) Open

func (o *Openers) Open(path string) (*File, error)

Open implements Opener.

type Span

type Span struct {
	// The file this span refers to. The file must be indexed, since we plan to
	// convert Start/End into editor coordinates.
	*File

	// The start and end byte offsets for this span.
	Start, End int
}

Span is a location within a File.

func Between

func Between(a, b Span) Span

Between is a helper function that returns a Span for the space between spans a and b, inclusive. If a and b do not have the same File or if the spans overlap, then this returns a zero span.

func GetSpan

func GetSpan(s Spanner) Span

GetSpan extracts a span from a Spanner, but returns the zero span when s is zero, which would otherwise panic.

func Join

func Join(spans ...Spanner) Span

Join joins a collection of spans, returning the smallest span that contains all of them.

IsZero spans among spans are ignored. If every span in spans is zero, returns the zero span.

If there are at least two distinct files among the non-zero spans, this function panics.

func JoinSeq

func JoinSeq[S Spanner](seq iter.Seq[S]) Span

JoinSeq is like Join, but takes a sequence of any spannable type.

func JoinSpanSeq

func JoinSpanSeq(seq iter.Seq[Span]) Span

See go.dev/issue/78336.

func JoinSpans

func JoinSpans(spans ...Span) Span

See go.dev/issue/78336.

func (Span) After

func (s Span) After() string

After returns all text after this span.

func (Span) Before

func (s Span) Before() string

Before returns all text before this span.

func (Span) EndLoc

func (s Span) EndLoc() Location

EndLoc returns the end location for this span.

func (Span) GrowLeft

func (s Span) GrowLeft(p func(r rune) bool) Span

GrowLeft returns a new span which contains the largest suffix of Span.Before which match p.

func (Span) GrowRight

func (s Span) GrowRight(p func(r rune) bool) Span

GrowRight returns a new span which contains the largest prefix of Span.After which match p.

func (Span) Indentation

func (s Span) Indentation() string

Indentation calculates the indentation at this span.

Indentation is defined as the substring between the last newline in Span.Before and the first non-Pattern_White_Space after that newline.

func (Span) IsZero

func (s Span) IsZero() bool

IsZero returns whether or not this is the zero span.

func (Span) Len

func (s Span) Len() int

Len returns the length of this span, in bytes.

func (Span) Range

func (s Span) Range(i, j int) Span

Range slices this span along the given byte indices.

Unlike slicing into a string, out-of-bounds indices are snapped to the boundaries of the string, and negative indices are taken from the back of the span. For example, s.RuneRange(-2, -1) is the final rune of the span (or an empty span, if s is empty).

func (Span) Rune

func (s Span) Rune(i int) Span

Rune is a shorthand for RuneRange(i, i+1) or RuneRange(i-1, i), depending on the sign of i.

func (Span) RuneRange

func (s Span) RuneRange(i, j int) Span

RuneRange slices this span along the given rune indices.

For example, s.RuneRange(0, 2) returns at most the first two runes of the span.

Unlike slicing into a string, out-of-bounds indices are snapped to the boundaries of the string, and negative indices are taken from the back of the span. For example, s.RuneRange(-2, -1) is the final rune of the span (or an empty span, if s is empty).

func (Span) Span

func (s Span) Span() Span

Span implements Spanner.

func (Span) StartLoc

func (s Span) StartLoc() Location

StartLoc returns the start location for this span.

func (Span) String

func (s Span) String() string

String implements [string.Stringer].

func (Span) Text

func (s Span) Text() string

Text returns the text corresponding to this span.

type Spanner

type Spanner interface {
	// Should return the zero [Span] to indicate that it does not contribute
	// span information.
	Span() Span
}

Spanner is any type with a Span.

type Workspace

type Workspace interface {
	// Paths returns an iterator for the paths of the Workspace.
	Paths() []string
}

Workspace is a set of Protobuf source paths.

Workspace implementations are assumed by Protocompile to be comparable. It is sufficient to always ensure that the implementation uses a pointer receiver.

func NewWorkspace

func NewWorkspace(paths ...string) Workspace

NewWorkspace returns a Workspace implementation for the given paths that is comparable.

No validations/transformations are performed on the given paths, it is the responsibility of the caller to enforce path order and validity.

Directories

Path Synopsis
Package length provides various length that can be used for configuring the behavior of line/column calculations.
Package length provides various length that can be used for configuring the behavior of line/column calculations.

Jump to

Keyboard shortcuts

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