buffer

package
v1.9.1 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2026 License: MIT Imports: 6 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package buffer holds the piece tree text buffer, immutable snapshots, positions and the line index (SPEC §6.1, §6.2).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AreIntersecting

func AreIntersecting(a, b Range) bool

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

func AreIntersectingOrTouching(a, b Range) bool

AreIntersectingOrTouching reports whether two ranges overlap or share an end.

func CompareUsingEnds

func CompareUsingEnds(a, b Range) int

CompareUsingEnds orders ranges by end, then by start.

func CompareUsingStarts

func CompareUsingStarts(a, b Range) int

CompareUsingStarts orders ranges by start, then by end.

func SelectionsEqual

func SelectionsEqual(a, b []Selection) bool

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 New

func New(text string) *Buffer

New makes a Buffer that holds text.

func NewFromReader

func NewFromReader(r io.Reader) (*Buffer, error)

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

func (b *Buffer) Delete(start, end int) error

Delete removes the bytes [start, end). It fails with the tag OffsetRangeInvalid unless 0 <= start <= end <= Len().

func (*Buffer) Insert

func (b *Buffer) Insert(offset int, text string) error

Insert puts text at the byte offset. It fails with the tag OffsetOutOfRange unless 0 <= offset <= Len().

func (*Buffer) Replace

func (b *Buffer) Replace(start, end int, text string) error

Replace puts text in place of the bytes [start, end), in one step. It fails with the tag OffsetRangeInvalid unless 0 <= start <= end <= Len().

func (*Buffer) Snapshot

func (b *Buffer) Snapshot() Snapshot

Snapshot returns the text as it is now, in O(1).

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

func (b *Builder) Buffer() *Buffer

Buffer returns the Buffer of everything written. The Builder is empty after it.

func (*Builder) Write

func (b *Builder) Write(p []byte) (int, error)

Write adds text to the end of what is built. It always succeeds.

func (*Builder) WriteString

func (b *Builder) WriteString(s string)

WriteString adds text to the end of what is built.

type Direction

type Direction int

Direction tells which end of a selection is the active one.

const (
	// LTR is a selection whose anchor is at its start and whose active end is at
	// its end: made by moving the cursor forward.
	LTR Direction = iota
	// RTL is a selection whose anchor is at its end and whose active end is at its
	// start: made by moving the cursor backward.
	RTL
)

type Position

type Position struct {
	Line int
	Col  int
}

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

func MaxPosition(a, b Position) Position

MaxPosition is the later of two positions.

func MinPosition

func MinPosition(a, b Position) Position

MinPosition is the earlier of two positions.

func (Position) Compare

func (p Position) Compare(q Position) int

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) Delta

func (p Position) Delta(lines, cols int) Position

Delta moves p by a number of lines and byte columns.

func (Position) Equal

func (p Position) Equal(q Position) bool

Equal reports whether p and q are the same place.

func (Position) IsBefore

func (p Position) IsBefore(q Position) bool

IsBefore reports whether p is strictly before q.

func (Position) IsBeforeOrEqual

func (p Position) IsBeforeOrEqual(q Position) bool

IsBeforeOrEqual reports whether p is before q or the same place.

func (Position) With

func (p Position) With(line, col int) Position

With replaces the line and the column.

type Range

type Range struct {
	Start Position
	End   Position
}

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

func NewRange(start, end Position) Range

NewRange makes a range from two positions and swaps them when end is before start, as VS Code's Range constructor does.

func RangeFromPositions

func RangeFromPositions(a, b Position) Range

RangeFromPositions makes the range between two positions, in either order.

func (Range) CollapseToEnd

func (r Range) CollapseToEnd() Range

CollapseToEnd is the empty range at r's end.

func (Range) CollapseToStart

func (r Range) CollapseToStart() Range

CollapseToStart is the empty range at r's start.

func (Range) ContainsPosition

func (r Range) ContainsPosition(p Position) bool

ContainsPosition reports whether p is inside the range; both ends count as inside.

func (Range) ContainsRange

func (r Range) ContainsRange(o Range) bool

ContainsRange reports whether o lies inside r; o may share r's ends.

func (Range) Intersect

func (r Range) Intersect(o Range) (Range, bool)

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) IsEmpty

func (r Range) IsEmpty() bool

IsEmpty reports whether the range covers no text.

func (Range) SpansMultipleLines

func (r Range) SpansMultipleLines() bool

SpansMultipleLines reports whether the range starts and ends on different lines.

func (Range) StrictContainsPosition

func (r Range) StrictContainsPosition(p Position) bool

StrictContainsPosition reports whether p is inside the range and is neither of its ends.

func (Range) StrictContainsRange

func (r Range) StrictContainsRange(o Range) bool

StrictContainsRange reports whether o lies inside r without sharing either of r's ends.

func (Range) Union

func (r Range) Union(o Range) Range

Union is the smallest range that contains both r and o (VS Code's plusRange).

func (Range) WithEnd

func (r Range) WithEnd(p Position) Range

WithEnd replaces the end. The result is swapped when the new end is before the start.

func (Range) WithStart

func (r Range) WithStart(p Position) Range

WithStart replaces the start. The result is swapped when the new start is after the end, as in VS Code's setStartPosition.

type Selection

type Selection struct {
	Anchor Position
	Active Position
}

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 NewCursor

func NewCursor(p Position) Selection

NewCursor is the empty selection at p.

func SelectionFromRange

func SelectionFromRange(r Range, d Direction) Selection

SelectionFromRange makes the selection that covers r and has the direction d.

func (Selection) Direction

func (s Selection) Direction() 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) End

func (s Selection) End() Position

End is the later end.

func (Selection) Equal

func (s Selection) Equal(o Selection) bool

Equal reports whether two selections have the same anchor and active end (VS Code's selectionsEqual).

func (Selection) IsEmpty

func (s Selection) IsEmpty() bool

IsEmpty reports whether the selection is a cursor.

func (Selection) Range

func (s Selection) Range() Range

Range is the text the selection covers, start before end.

func (Selection) Start

func (s Selection) Start() Position

Start is the earlier end.

func (Selection) WithEnd

func (s Selection) WithEnd(p Position) Selection

WithEnd replaces the end and keeps the direction, as VS Code's setEndPosition does.

func (Selection) WithStart

func (s Selection) WithStart(p Position) Selection

WithStart replaces the start and keeps the direction, as VS Code's setStartPosition does: the anchor is the start of an LTR selection, the active end of an RTL one.

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

func (s Snapshot) Chunks() iter.Seq[string]

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) Len

func (s Snapshot) Len() int

Len is the length of the text in bytes.

func (Snapshot) LineContent

func (s Snapshot) LineContent(line int) (string, error)

LineContent is the text of a line without its line feed. It fails with the tag LineOutOfRange unless 0 <= line < LineCount().

func (Snapshot) LineCount

func (s Snapshot) LineCount() int

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) LineFeeds

func (s Snapshot) LineFeeds() int

LineFeeds is the number of line feeds in the text.

func (Snapshot) LineLength

func (s Snapshot) LineLength(line int) (int, error)

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

func (s Snapshot) LineStart(line int) (int, error)

LineStart is the offset where a line starts. It fails with the tag LineOutOfRange unless 0 <= line < LineCount().

func (Snapshot) Lines

func (s Snapshot) Lines(from int) iter.Seq2[int, string]

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

func (s Snapshot) OffsetAt(p Position) int

OffsetAt is the offset of a position, after ValidatePosition has moved it into the text.

func (Snapshot) PositionAt

func (s Snapshot) PositionAt(offset int) Position

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

func (s Snapshot) Slice(start, end int) (string, error)

Slice returns the bytes [start, end) as a string. It fails with the tag OffsetRangeInvalid unless 0 <= start <= end <= Len().

func (Snapshot) String

func (s Snapshot) String() string

String is the whole text.

func (Snapshot) Text

func (s Snapshot) Text(r Range) string

Text is the text of a range, after ValidateRange has moved it into the text.

func (Snapshot) ValidatePosition

func (s Snapshot) ValidatePosition(p Position) Position

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

func (s Snapshot) ValidateRange(r Range) Range

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.

Jump to

Keyboard shortcuts

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