Documentation
¶
Overview ¶
Package source provides utilities for referring to source code.
Index ¶
- Constants
- func Format(w io.Writer, snippet Snippet) error
- type Chunk
- type File
- func (f *File) Bytes(location Location) []byte
- func (f *File) Content() []byte
- func (f *File) Context(location Location) []byte
- func (f *File) EndColumn(location Location) uint32
- func (f *File) EndLine(location Location) uint32
- func (f *File) Len() uint32
- func (f *File) Path() string
- func (f *File) Postamble(location Location) []byte
- func (f *File) Preamble(location Location) []byte
- func (f *File) StartColumn(location Location) uint32
- func (f *File) StartLine(location Location) uint32
- type Indicator
- type Line
- type Location
- type Snippet
- type SnippetOption
Constants ¶
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 )
const UnknownLocation = Location(0)
UnknownLocation is the default unset location.
Variables ¶
This section is empty.
Functions ¶
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 (*File) Bytes ¶
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) Context ¶
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 ¶
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 ¶
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) Postamble ¶
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 ¶
Preamble returns the content before a location on the same line as the start of the location.
func (*File) StartColumn ¶
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.
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 ¶
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 (Location) Compare ¶
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 ¶
End returns the offset into the file of the first byte after the end of the location.
func (Location) Snippet ¶
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.
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.