v1

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2020 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Invalid = Span{/* contains filtered or unexported fields */}

Invalid is a span that reports false from IsValid

Functions

func ApplyEdits

func ApplyEdits(before []byte, edits []TextEdit) []byte

ApplyEdits applies the set of edits to the before and returns the resulting content. It may panic or produce garbage if the edits are not valid for the provided before content.

func Compare

func Compare(a, b Span) int

func ComparePoint

func ComparePoint(a, b Point) int

func CompareURI

func CompareURI(a, b URI) int

func SortTextEdits

func SortTextEdits(d []TextEdit)

SortTextEdits attempts to order all edits by their starting points. The sort is stable so that edits with the same starting point will not be reordered.

Types

type ComputeEdits

type ComputeEdits func(uri URI, before, after []byte) ([]TextEdit, error)

ComputeEdits is the type for a function that produces a set of edits that convert from the before content to the after content.

type Converter

type Converter interface {
	//ToPosition converts from an offset to a line:column pair.
	ToPosition(offset int) (int, int, error)
	//ToOffset converts from a line:column pair to an offset.
	ToOffset(line, col int) (int, error)
}

Converter is the interface to an object that can convert between line:column and offset forms for a single file.

type FileConverter

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

func (*FileConverter) ToOffset

func (l *FileConverter) ToOffset(line, col int) (int, error)

func (*FileConverter) ToPosition

func (l *FileConverter) ToPosition(offset int) (int, int, error)

type Hunk

type Hunk struct {
	// The line in the original source where the hunk starts.
	FromLine int
	// The line in the original source where the hunk finishes.
	ToLine int
	// The set of line based edits to apply.
	Lines []Line
}

Hunk represents a contiguous set of line edits to apply.

type Line

type Line struct {
	// Kind is the type of line this represents, deletion, insertion or copy.
	Kind OpKind
	// Content is the content of this line.
	// For deletion it is the line being removed, for all others it is the line
	// to put in the output.
	Content []byte
}

Line represents a single line operation to apply as part of a Hunk.

type OpKind

type OpKind int

OpKind is used to denote the type of operation a line represents.

const (
	// Delete is the operation kind for a line that is present in the input
	// but not in the output.
	Delete OpKind = iota
	// Insert is the operation kind for a line that is new in the output.
	Insert
	// Equal is the operation kind for a line that is the same in the input and
	// output, often used to provide context around edited lines.
	Equal
)

func (OpKind) String

func (k OpKind) String() string

String returns a human readable representation of an OpKind. It is not intended for machine processing.

type Point

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

Point represents a single point within a file. In general this should only be used as part of a Span, as on its own it does not carry enough information.

func NewPoint

func NewPoint(line, col, offset int) Point

func (Point) Column

func (p Point) Column() int

func (Point) HasOffset

func (p Point) HasOffset() bool

func (Point) HasPosition

func (p Point) HasPosition() bool

func (Point) IsValid

func (p Point) IsValid() bool

func (Point) Line

func (p Point) Line() int

func (*Point) MarshalJSON

func (p *Point) MarshalJSON() ([]byte, error)

func (Point) Offset

func (p Point) Offset() int

func (*Point) UnmarshalJSON

func (p *Point) UnmarshalJSON(b []byte) error

type Range

type Range struct {
	FileSet   *token.FileSet
	Start     token.Pos
	End       token.Pos
	Converter Converter
}

Range represents a source code range in token.Pos form. It also carries the FileSet that produced the positions, so that it is self contained.

func NewRange

func NewRange(fset *token.FileSet, start, end token.Pos) Range

NewRange creates a new Range from a FileSet and two positions. To represent a point pass a 0 as the end pos.

func (Range) IsPoint

func (r Range) IsPoint() bool

IsPoint returns true if the range represents a single point.

func (Range) Span

func (r Range) Span() (Span, error)

Span converts a Range to a Span that represents the Range. It will fill in all the members of the Span, calculating the line and column information.

type Span

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

Span represents a source code range in standardized form.

func FileSpan

func FileSpan(tok *token.File, converter Converter, start, end token.Pos) (Span, error)

FileSpan returns a span within tok, using converter to translate between offsets and positions.

func New

func New(uri URI, start Point, end Point) Span

func (Span) End

func (s Span) End() Point

func (Span) Format

func (s Span) Format(f fmt.State, c rune)

Format implements fmt.Formatter to print the Location in a standard form. The format produced is one that can be read back in using Parse.

func (Span) HasOffset

func (s Span) HasOffset() bool

func (Span) HasPosition

func (s Span) HasPosition() bool

func (Span) IsPoint

func (s Span) IsPoint() bool

func (Span) IsValid

func (s Span) IsValid() bool

func (*Span) MarshalJSON

func (s *Span) MarshalJSON() ([]byte, error)

func (Span) Range

func (s Span) Range(converter *TokenConverter) (Range, error)

Range converts a Span to a Range that represents the Span for the supplied File.

func (Span) Start

func (s Span) Start() Point

func (Span) URI

func (s Span) URI() URI

func (*Span) UnmarshalJSON

func (s *Span) UnmarshalJSON(b []byte) error

func (Span) WithAll

func (s Span) WithAll(c Converter) (Span, error)

func (Span) WithOffset

func (s Span) WithOffset(c Converter) (Span, error)

func (Span) WithPosition

func (s Span) WithPosition(c Converter) (Span, error)

type TextEdit

type TextEdit struct {
	Span    Span
	NewText []byte
}

TextEdit represents a change to a section of a document. The text within the specified span should be replaced by the supplied new text.

func LineEdits

func LineEdits(before []byte, edits []TextEdit) []TextEdit

LineEdits takes a set of edits and expands and merges them as necessary to ensure that there are only full line edits left when it is done.

type TokenConverter

type TokenConverter struct {
	FileConverter
	// contains filtered or unexported fields
}

TokenConverter is a Converter backed by a token file set and file. It uses the file set methods to work out the conversions, which makes it fast and does not require the file contents.

func NewContentConverter

func NewContentConverter(filename string, content []byte) *TokenConverter

NewContentConverter returns an implementation of Converter for the given file content.

func NewTokenConverter

func NewTokenConverter(fset *token.FileSet, f *token.File) *TokenConverter

NewTokenConverter returns an implementation of Converter backed by a token.File.

type URI

type URI string

URI represents the full URI for a file.

func URIFromPath

func URIFromPath(path string) URI

URIFromPath returns a span URI for the supplied file path. It will always have the file scheme.

func URIFromURI

func URIFromURI(s string) URI

func (URI) Filename

func (uri URI) Filename() string

Filename returns the file path for the given URI. It is an error to call this on a URI that is not a valid filename.

func (URI) IsFile

func (uri URI) IsFile() bool

type Unified

type Unified struct {
	// From is the name of the original file.
	From []byte
	// To is the name of the modified file.
	To []byte
	// Hunks is the set of edit hunks needed to transform the file content.
	Hunks []*Hunk
}

Unified represents a set of edits as a unified diff.

func ToUnified

func ToUnified(from, to []byte, content []byte, edits []TextEdit) Unified

ToUnified takes a file contents and a sequence of edits, and calculates a unified diff that represents those edits.

func (Unified) Format

func (u Unified) Format(f fmt.State, r rune)

Format converts a unified diff to the standard textual form for that diff. The output of this function can be passed to tools like patch.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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