textpos

package
v0.0.16 Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2021 License: Apache-2.0, BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Overview

Package textpos provides types and functions for working with line-based positions of text in a textual document.

WARNING: This package's API is in flux. It is based on the "go/token" package.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Column

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

Column is a number indicating a horrizontal offset within a line of text.

Column may be used to designate byte or character offsets. Byte offsets are advantageous because they are simple well-defined but match cursor positions in most text editors. Characters are not a universally well-defined and require more complex lookup but generally correspond to cursor positions in text editors.

func ColumnFromOffset

func ColumnFromOffset(o int) Column

ColumnFromOffset returns a Column object from an offset value (where 0 indicates the first line).

func ColumnFromOrdinal

func ColumnFromOrdinal(o int) Column

ColumnFromOrdinal returns a Column object from an ordinal value (where 1 indicates the first line).

func (Column) IsValid

func (n Column) IsValid() bool

IsValid reports if the column value is valid (ordinal >= 1).

func (Column) Offset

func (n Column) Offset() int

Offset returns the Column number where 0 indicates the first Column.

func (Column) Ordinal

func (n Column) Ordinal() int

Ordinal returns the Column number where 1 indicates the first Column.

func (Column) String

func (n Column) String() string

String returns the ordinal value encoded as a base 10 string.

type File

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

A File is a handle for a file belonging to a FileSet. A File has a name, size, and line offset table.

func (*File) AddLine

func (f *File) AddLine(offset int)

AddLine adds the line offset for a new line. The line offset must be larger than the offset for the previous line and smaller than the file size; otherwise the line offset is ignored.

func (*File) Base

func (f *File) Base() int

Base returns the base offset of file f as registered with AddFile.

func (*File) Line

func (f *File) Line(p Pos) Line

Line returns the line number for the given file position p; p must be a Pos value in that file or NoPos.

func (*File) LineCount

func (f *File) LineCount() int

LineCount returns the number of lines in file f.

func (*File) LineStart

func (f *File) LineStart(line Line) Pos

LineStart returns the Pos value of the start of the specified line. It ignores any alternative positions set using AddLineColumnInfo. LineStart panics if the 1-based line number is invalid.

func (*File) MergeLine

func (f *File) MergeLine(line int)

MergeLine merges a line with the following line. It is akin to replacing the newline character at the end of the line with a space (to not change the remaining offsets). To obtain the line number, consult e.g. Position.Line. MergeLine will panic if given an invalid line number.

func (*File) Name

func (f *File) Name() string

Name returns the file name of file f as registered with AddFile.

func (*File) Offset

func (f *File) Offset(p Pos) int

Offset returns the offset for the given file position p; p must be a valid Pos value in that file. f.Offset(f.Pos(offset)) == offset.

func (*File) Pos

func (f *File) Pos(offset int) Pos

Pos returns the Pos value for the given file offset; the offset must be <= f.Size(). f.Pos(f.Offset(p)) == p.

func (*File) PosForLineColumn

func (f *File) PosForLineColumn(lc LineColumn) Pos

PosForLineColumn returns the position of the given (line, column) pair in the file or NoPos if the (line, column) pair is out of bounds.

func (*File) Position

func (f *File) Position(p Pos) (pos Position)

Position returns the Position value for the given file position p. Calling f.Position(p) is equivalent to calling f.PositionFor(p, true).

func (*File) SetLines

func (f *File) SetLines(lines []int) bool

SetLines sets the line offsets for a file and reports whether it succeeded. The line offsets are the offsets of the first character of each line; for instance for the content "ab\nc\n" the line offsets are {0, 3}. An empty file has an empty line offset table. Each line offset must be larger than the offset for the previous line and smaller than the file size; otherwise SetLines fails and returns false. Callers must not mutate the provided slice after SetLines returns.

func (*File) SetLinesForContent

func (f *File) SetLinesForContent(content []byte)

SetLinesForContent sets the line offsets for the given file content. It ignores position-altering //line comments.

func (*File) Size

func (f *File) Size() int

Size returns the size of file f as registered with AddFile.

type FileSet

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

A FileSet represents a set of source files. Methods of file sets are synchronized; multiple goroutines may invoke them concurrently.

The byte offsets for each file in a file set are mapped into distinct (integer) intervals, one interval [base, base+size] per file. Base represents the first byte in the file, and size is the corresponding file size. A Pos value is a value in such an interval. By determining the interval a Pos value belongs to, the file, its file base, and thus the byte offset (position) the Pos value is representing can be computed.

When adding a new file, a file base must be provided. That can be any integer value that is past the end of any interval of any file already in the file set. For convenience, FileSet.Base provides such a value, which is simply the end of the Pos interval of the most recently added file, plus one. Unless there is a need to extend an interval later, using the FileSet.Base should be used as argument for FileSet.AddFile.

func NewFileSet

func NewFileSet() *FileSet

NewFileSet creates a new file set.

func (*FileSet) AddFile

func (s *FileSet) AddFile(filename string, base, size int) *File

AddFile adds a new file with a given filename, base offset, and file size to the file set s and returns the file. Multiple files may have the same name. The base offset must not be smaller than the FileSet's Base(), and size must not be negative. As a special case, if a negative base is provided, the current value of the FileSet's Base() is used instead.

Adding the file will set the file set's Base() value to base + size + 1 as the minimum base value for the next file. The following relationship exists between a Pos value p for a given file offset offs:

int(p) = base + offs

with offs in the range [0, size] and thus p in the range [base, base+size]. For convenience, File.Pos may be used to create file-specific position values from a file offset.

func (*FileSet) Base

func (s *FileSet) Base() int

Base returns the minimum base offset that must be provided to AddFile when adding the next file.

func (*FileSet) File

func (s *FileSet) File(p Pos) (f *File)

File returns the file that contains the position p. If no such file is found (for instance for p == NoPos), the result is nil.

func (*FileSet) Iterate

func (s *FileSet) Iterate(f func(*File) bool)

Iterate calls f for the files in the file set in the order they were added until f returns false.

func (*FileSet) Position

func (s *FileSet) Position(p Pos) (pos Position)

Position converts a Pos p in the fileset into a Position value. Calling s.Position(p) is equivalent to calling s.PositionFor(p, true).

type Line

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

Line is the line number of some text in a file.

func LineFromOffset

func LineFromOffset(o int) Line

LineFromOffset returns a Line object from an offset value.

func LineFromOrdinal

func LineFromOrdinal(o int) Line

LineFromOrdinal returns a Line object from a positive value.

func (Line) IsValid

func (n Line) IsValid() bool

IsValid reports if the line value is valid (ordinal >= 1).

func (Line) Offset

func (n Line) Offset() int

Offset returns the line number where 0 indicates the first line.

func (Line) Ordinal

func (n Line) Ordinal() int

Ordinal returns the line number where 1 indicates the first line.

func (Line) String

func (n Line) String() string

String returns the ordinal value encoded as a base 10 string.

type LineColumn

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

LineColumn is a two dimensional textual position (line, column).

func MakeLineColumn

func MakeLineColumn(line Line, col Column) LineColumn

MakeLineColumn returns a new LineColumn tuple.

func (LineColumn) Column

func (p LineColumn) Column() Column

Column returns the column for the tuple.

func (LineColumn) Line

func (p LineColumn) Line() Line

Line returns the line for the tuple.

func (LineColumn) String

func (p LineColumn) String() string

String returns a string representation of a LineColumn pair.

If column and line are valid, returns "lineOrdinal:columnOrdinal."

type Pos

type Pos int

Pos is a compact encoding of a source position within a file set. It can be converted into a Position for a more convenient, but much larger, representation.

The Pos value for a given file is a number in the range [base, base+size], where base and size are specified when a file is added to the file set. The difference between a Pos value and the corresponding file base corresponds to the byte offset of that position (represented by the Pos value) from the beginning of the file. Thus, the file base offset is the Pos value representing the first byte in the file.

To create the Pos value for a specific source offset (measured in bytes), first add the respective file to the current file set using FileSet.AddFile and then call File.Pos(offset) for that file. Given a Pos value p for a specific file set fset, the corresponding Position value is obtained by calling fset.Position(p).

Pos values can be compared directly with the usual comparison operators: If two Pos values p and q are in the same file, comparing p and q is equivalent to comparing the respective source file offsets. If p and q are in different files, p < q is true if the file implied by p was added to the respective file set before the file implied by q.

const NoPos Pos = 0

NoPos is the zero value for Pos; there is no file and line information associated with it, and NoPos.IsValid() is false. NoPos is always smaller than any other Pos value. The corresponding Position value for NoPos is the zero value for Position.

func (Pos) IsValid

func (p Pos) IsValid() bool

IsValid reports whether the position is valid.

type Position

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

Position describes an arbitrary source position including the file, line, and column location. A Position is valid if the line number is > 0.

func (Position) Column

func (p Position) Column() Column

Column returns the column of the position.

func (Position) FileName

func (p Position) FileName() string

FileName returns the fileName of the position.

func (Position) IsValid

func (p Position) IsValid() bool

IsValid reports whether the position is valid.

func (Position) Line

func (p Position) Line() Line

Line returns the line of the position.

func (Position) Offset

func (p Position) Offset() int

Offset returns the offset of the position.

func (Position) String

func (p Position) String() string

String returns a string in one of several forms:

file:line:column    valid position with file name
file:line           valid position with file name but no column (column == 0)
line:column         valid position without file name
line                valid position without file name and no column (column == 0)
file                invalid position with file name
-                   invalid position without file name

type Range

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

Range is a range within a single file.

The range specifies all of the characters in the interval [r.Start(), r.End()).

func (*Range) End

func (r *Range) End() Position

End returns the end position of the range.

func (*Range) EndPos

func (r *Range) EndPos() Pos

EndPos returns the end position of the range.

func (*Range) File

func (r *Range) File() *File

File returns the file within which the range is specified.

func (*Range) Start

func (r *Range) Start() Position

Start returns the start position of the range.

func (*Range) StartPos

func (r *Range) StartPos() Pos

StartPos returns the start position of the range.

Jump to

Keyboard shortcuts

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