Documentation
¶
Overview ¶
Package buffer holds the piece tree text buffer, immutable snapshots, positions and the line index (SPEC §6.1, §6.2).
Index ¶
- func AreIntersecting(a, b Range) bool
- func AreIntersectingOrTouching(a, b Range) bool
- func CompareUsingEnds(a, b Range) int
- func CompareUsingStarts(a, b Range) int
- func SelectionsEqual(a, b []Selection) bool
- type Buffer
- type Builder
- type Direction
- type Position
- type Range
- func (r Range) CollapseToEnd() Range
- func (r Range) CollapseToStart() Range
- func (r Range) ContainsPosition(p Position) bool
- func (r Range) ContainsRange(o Range) bool
- func (r Range) Intersect(o Range) (Range, bool)
- func (r Range) IsEmpty() bool
- func (r Range) SpansMultipleLines() bool
- func (r Range) StrictContainsPosition(p Position) bool
- func (r Range) StrictContainsRange(o Range) bool
- func (r Range) Union(o Range) Range
- func (r Range) WithEnd(p Position) Range
- func (r Range) WithStart(p Position) Range
- type Selection
- func (s Selection) Direction() Direction
- func (s Selection) End() Position
- func (s Selection) Equal(o Selection) bool
- func (s Selection) IsEmpty() bool
- func (s Selection) Range() Range
- func (s Selection) Start() Position
- func (s Selection) WithEnd(p Position) Selection
- func (s Selection) WithStart(p Position) Selection
- type Snapshot
- func (s Snapshot) Chunks() iter.Seq[string]
- func (s Snapshot) Len() int
- func (s Snapshot) LineContent(line int) (string, error)
- func (s Snapshot) LineCount() int
- func (s Snapshot) LineFeeds() int
- func (s Snapshot) LineLength(line int) (int, error)
- func (s Snapshot) LineStart(line int) (int, error)
- func (s Snapshot) Lines(from int) iter.Seq2[int, string]
- func (s Snapshot) OffsetAt(p Position) int
- func (s Snapshot) PositionAt(offset int) Position
- func (s Snapshot) Slice(start, end int) (string, error)
- func (s Snapshot) String() string
- func (s Snapshot) Text(r Range) string
- func (s Snapshot) ValidatePosition(p Position) Position
- func (s Snapshot) ValidateRange(r Range) Range
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func AreIntersecting ¶
AreIntersecting reports whether two ranges share text. Ranges that only touch do not: the doc comment of VS Code's Range.areIntersecting says they do, but its code and its tests say they don't.
func AreIntersectingOrTouching ¶
AreIntersectingOrTouching reports whether two ranges overlap or share an end.
func CompareUsingEnds ¶
CompareUsingEnds orders ranges by end, then by start.
func CompareUsingStarts ¶
CompareUsingStarts orders ranges by start, then by end.
func SelectionsEqual ¶
SelectionsEqual reports whether two lists hold equal selections in the same order.
Types ¶
type Buffer ¶
type Buffer struct {
// contains filtered or unexported fields
}
Buffer is the text of a document: a piece tree over read-only chunks and an append-only add buffer (SPEC §6.1). Its offsets are byte offsets. A Buffer is used by one goroutine at a time; Snapshot gives other goroutines a view that edits never change.
func NewFromReader ¶
NewFromReader reads r to its end and returns a Buffer of what it gave. It fails with the tag BufferReadFailed when r does, and gives no Buffer.
func (*Buffer) Delete ¶
Delete removes the bytes [start, end). It fails with the tag OffsetRangeInvalid unless 0 <= start <= end <= Len().
func (*Buffer) Insert ¶
Insert puts text at the byte offset. It fails with the tag OffsetOutOfRange unless 0 <= offset <= Len().
type Builder ¶
type Builder struct {
// contains filtered or unexported fields
}
Builder makes a Buffer from text that arrives in parts, such as a file read in blocks. Text is stored in chunks of up to 64 KiB, and a chunk never ends in the middle of a UTF-8 sequence, even when the parts do. The text must already use "\n" alone for line endings (SPEC §6.1); the builder does not change it.
func (*Builder) Buffer ¶
Buffer returns the Buffer of everything written. The Builder is empty after it.
func (*Builder) WriteString ¶
WriteString adds text to the end of what is built.
type Position ¶
Position is a place in a text: a zero-based line and a zero-based byte column (SPEC §6.2, D6). It is the counterpart of VS Code's IPosition, which counts lines and UTF-16 columns from 1.
func MaxPosition ¶
MaxPosition is the later of two positions.
func MinPosition ¶
MinPosition is the earlier of two positions.
func (Position) Compare ¶
Compare orders two positions: negative when p is before q, zero when they are the same place, positive when p is after q.
func (Position) IsBeforeOrEqual ¶
IsBeforeOrEqual reports whether p is before q or the same place.
type Range ¶
Range is a span of text from Start up to End (SPEC §6.2). A Range made by NewRange never has End before Start, as in VS Code's Range. A Range with Start equal to End is empty.
func NewRange ¶
NewRange makes a range from two positions and swaps them when end is before start, as VS Code's Range constructor does.
func RangeFromPositions ¶
RangeFromPositions makes the range between two positions, in either order.
func (Range) CollapseToEnd ¶
CollapseToEnd is the empty range at r's end.
func (Range) CollapseToStart ¶
CollapseToStart is the empty range at r's start.
func (Range) ContainsPosition ¶
ContainsPosition reports whether p is inside the range; both ends count as inside.
func (Range) ContainsRange ¶
ContainsRange reports whether o lies inside r; o may share r's ends.
func (Range) Intersect ¶
Intersect is the part r and o have in common. The second result is false when they have none; ranges that only touch give an empty range. This is VS Code's intersectRanges.
func (Range) SpansMultipleLines ¶
SpansMultipleLines reports whether the range starts and ends on different lines.
func (Range) StrictContainsPosition ¶
StrictContainsPosition reports whether p is inside the range and is neither of its ends.
func (Range) StrictContainsRange ¶
StrictContainsRange reports whether o lies inside r without sharing either of r's ends.
type Selection ¶
Selection is a range with a direction (SPEC §6.2): the anchor stays where the selection began and the active end is where the cursor is. An empty selection is a cursor.
func SelectionFromRange ¶
SelectionFromRange makes the selection that covers r and has the direction d.
func (Selection) Direction ¶
Direction is LTR when the active end is at the end of the selection. An empty selection is LTR, as in VS Code.
func (Selection) Equal ¶
Equal reports whether two selections have the same anchor and active end (VS Code's selectionsEqual).
type Snapshot ¶
type Snapshot struct {
// contains filtered or unexported fields
}
Snapshot is an immutable view of the text at one moment. It is a value that can be copied freely and read from any goroutine while the Buffer it came from goes on changing.
func (Snapshot) Chunks ¶
Chunks goes through the text piece by piece, in order. The pieces are the buffer's own division of the text, so they have no meaning for the caller beyond being parts that put together give the text.
func (Snapshot) LineContent ¶
LineContent is the text of a line without its line feed. It fails with the tag LineOutOfRange unless 0 <= line < LineCount().
func (Snapshot) LineCount ¶
LineCount is the number of lines: one more than the line feeds, so an empty text has one empty line and a text that ends with a line feed ends with an empty line.
func (Snapshot) LineLength ¶
LineLength is the length of a line in bytes, without its line feed. It fails with the tag LineOutOfRange unless 0 <= line < LineCount().
func (Snapshot) LineStart ¶
LineStart is the offset where a line starts. It fails with the tag LineOutOfRange unless 0 <= line < LineCount().
func (Snapshot) Lines ¶
Lines goes through the lines from line from to the last, giving each one's index and its text without the line feed. A from before the first line starts at the first; one after the last gives nothing. The last line is given even when it is empty.
func (Snapshot) OffsetAt ¶
OffsetAt is the offset of a position, after ValidatePosition has moved it into the text.
func (Snapshot) PositionAt ¶
PositionAt is the position of an offset. An offset before the start or after the end of the text is moved to it. An offset in the middle of a UTF-8 sequence gives the column in the middle of it.
func (Snapshot) Slice ¶
Slice returns the bytes [start, end) as a string. It fails with the tag OffsetRangeInvalid unless 0 <= start <= end <= Len().
func (Snapshot) ValidatePosition ¶
ValidatePosition moves p into the text, as VS Code's validatePosition does: a line before the first becomes the start of the text, a line after the last the end of the text, and a column outside its line the nearest end of it. A column in the middle of a UTF-8 sequence moves back to the start of the character, as VS Code moves out of the middle of a surrogate pair.
func (Snapshot) ValidateRange ¶
ValidateRange moves both ends of r into the text, as VS Code's validateRange does. An end that is inside a UTF-8 sequence moves out of it: a range that covers text grows to take the whole character (the start moves back and the end moves forward), and an empty range moves back, so it does not grow.