source

package
v0.0.0-...-1b8cbf5 Latest Latest
Warning

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

Go to latest
Published: Oct 3, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package source provides utilities for referring to source code.

Index

Constants

View Source
const (
	// MinimumSnippetWidth is the absolute minimum width of Snippet's content.
	MinimumSnippetWidth = 20
	// MinimumSnippetHeight is the absolute minimum height of a Snippet's content.
	MinimumSnippetHeight = 3
	// DefaultTabWidth is the number of spaces substituted for tabs.
	DefaultTabWidth = 2
)
View Source
const UnknownLocation = Location(0)

UnknownLocation is the default unset location.

Variables

This section is empty.

Functions

func Format

func Format(w io.Writer, snippet Snippet) error

Format emits the snippet to in text format.

Types

type Chunk

type Chunk struct {
	// Text is the literal text of the chunk.
	Text string
	// IsSource is true if the text of this chunk is source code.
	IsSource bool
}

Chunk is a single segment of text that never contains newlines, carriage returns, or tabs.

type File

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

File contains information for a source code file.

func NewFile

func NewFile(path string, content []byte) (*File, error)

NewFile returns a new file or an error if the content is larger than ~4 GiB.

func (*File) Bytes

func (f *File) Bytes(location Location) []byte

Bytes returns the bytes of content at the given location in this file or nil if the location is outside the range of this file.

func (*File) Content

func (f *File) Content() []byte

Content returns the content of the file.

func (*File) Context

func (f *File) Context(location Location) []byte

Context returns the content from the start of the line that contains the start of the location to the end of the line that contains the end of the location up to, but not including the trailing newline (and carriage return if present).

func (*File) EndColumn

func (f *File) EndColumn(location Location) uint32

EndColumn returns the 1-indexed column of the inclusive end of the location or zero if the location is outside the range of this file.

func (*File) EndLine

func (f *File) EndLine(location Location) uint32

EndLine returns the 1-indexed line of the inclusive end of the location or zero if the location is outside the range of this file.

func (*File) Len

func (f *File) Len() uint32

Len returns the number of bytes in the file.

func (*File) Path

func (f *File) Path() string

Path returns the path of the file.

func (*File) Postamble

func (f *File) Postamble(location Location) []byte

Postamble returns the content after a location on the same line as the end of the location up to, but not including the trailing newline (and carriage return if present).

func (*File) Preamble

func (f *File) Preamble(location Location) []byte

Preamble returns the content before a location on the same line as the start of the location.

func (*File) StartColumn

func (f *File) StartColumn(location Location) uint32

StartColumn returns the 1-indexed column of the inclusive start of the location or zero if the location is outside the range of this file.

func (*File) StartLine

func (f *File) StartLine(location Location) uint32

StartLine returns the 1-indexed line of the inclusive start of the location or zero if the location is outside the range of this file.

type Indicator

type Indicator struct {
	// Column is the 1-indexed column being indicated.
	Column int
}

Indicator describes a indicator of some piece of content.

type Line

type Line struct {
	// Number is the 1-indexed line number.
	Number int
	// Chunks are the chunks of text of the line in order.
	Chunks []Chunk
}

Line is a single numbered line of source code.

type Location

type Location uint64

Location points to a range of bytes in a source code file.

func NewLocation

func NewLocation(offset, length uint32) Location

NewLocation returns a new location with a given offset and length.

If the offset and length together (i.e. the end of the location) exceed the 4 GiB limit, length with be clamped to fit this limit.

func Span

func Span(start, end Location) Location

Span returns a Range that spans two given Ranges.

func (Location) Compare

func (l Location) Compare(o Location) int

Compare returns 0 if this location has the same byte offset as the given location, a negative number if this location has a smaller byte offset, or a positive number of this location has a larger byte offset.

func (Location) End

func (l Location) End() uint32

End returns the offset into the file of the first byte after the end of the location.

func (Location) Len

func (l Location) Len() uint32

Len returns the number of bytes in the location.

func (Location) Snippet

func (r Location) Snippet(file *File, width, height int, opts ...SnippetOption) (Snippet, error)

Snippet returns the range formatted to fit in the given `width` and `height`.

An error is returns if `width` is less than MinimumSnippetWidth or `height` is less than MinimumSnippetHeight.

func (Location) Start

func (l Location) Start() uint32

Start returns the offset into the file of the first byte in the location.

func (Location) String

func (l Location) String() string

String implements fmt.Stringer.

type Snippet

type Snippet struct {
	// Start and End indicate a section of the snippet that is of special
	// importance (as snippets typically include a bit of extra context).
	//
	// If there is only one line, these apply to that line, otherwise, Start
	// applies to the first line and End applies to the last.
	Start, End Indicator
	// Lines are the lines of the source code clipped
	Lines []Line
	// Width and Height are the upper bounds on the number of runes wide and lines
	// respectively in the Snippet.
	Width, Height int
}

Snippet is a section of source code, formatted to fit in a specified number of columns and lines with a range start and end indicated.

type SnippetOption

type SnippetOption func(*snippetOptions)

SnippetOption is an option to configure how snippets are created.

func WithTabWidth

func WithTabWidth(w int) SnippetOption

WithTabWidth returns a SnippetOption that overrides the DefaultTabWidth.

Jump to

Keyboard shortcuts

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