rope

package
v0.1.11 Latest Latest
Warning

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

Go to latest
Published: Oct 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 rope stores text as a persistent tree of chunks of UTF-8: an AVL tree whose leaves own up to 4 KB of the text each, and whose nodes sum the bytes, runes, UTF-16 units and newlines below them, as Zed's rope sums its chunks. An edit copies the chunks it touches and the path to them and shares the rest of the tree with the text before it, so a version of the text is a pointer, kept as long as wanted and read from any goroutine while others are made. ui.TextBuffer and ui/editor's buffers keep their text in it.

Index

Constants

View Source
const ChunkBytes = 4096

ChunkBytes is the most bytes a leaf holds.

Variables

This section is empty.

Functions

func ByteOfRune

func ByteOfRune(n *Node, index int) int

ByteOfRune returns the byte rune index starts at.

func ByteOfUnit

func ByteOfUnit(n *Node, index int) int

ByteOfUnit returns the byte of UTF-16 unit index, rounded down to a rune.

func Check

func Check(n *Node) error

Check reports what is wrong with the tree, for tests: an oversized or miscounted leaf, or an unbalanced or miscounted branch.

func Chunks

func Chunks(n *Node, a, b int, fn func(s string) bool)

Chunks calls fn with the text from a to b, a piece at a time, until fn returns false.

func CountRunes

func CountRunes(s string) int

CountRunes counts the runes of s, ASCII eight bytes at a time, as most code and logs are.

func LineStart

func LineStart(n *Node, line int) int

LineStart returns the byte line starts at, the end of the text past the last line.

func Read

func Read(n *Node, start, end int, out *strings.Builder)

Read writes the bytes start to end to out.

func RuneOfUnit

func RuneOfUnit(n *Node, index int) int

RuneOfUnit returns the rune holding UTF-16 unit index, or after it.

func Split

func Split(n *Node, at int) (*Node, *Node)

Split returns the text before byte at and the text from it.

func WriteTo

func WriteTo(n *Node, w io.Writer) (int64, error)

WriteTo writes the text to w, a chunk at a time.

Types

type Node

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

Node is a node of the tree: a leaf holding text, or a branch of two. A nil node is the empty text.

func Build

func Build(s string) *Node

Build returns a tree of s, owning its bytes, so that a small surviving fragment cannot pin a large input string.

func Join

func Join(a, b *Node) *Node

Join returns the text of a followed by that of b.

func Replace

func Replace(n *Node, start, end int, s string) *Node

Replace returns the text with runes start to end replaced by s.

func ReplaceBytes

func ReplaceBytes(n *Node, a, b int, s string) *Node

ReplaceBytes returns the text with bytes a to b replaced by s.

type Summary

type Summary struct{ Bytes, Runes, Units, Lines int }

Summary counts a text: its bytes, runes, UTF-16 units and newlines.

func Prefix

func Prefix(n *Node, at int) Summary

Prefix returns the summary of the text before byte at.

func Sum

func Sum(n *Node) Summary

Sum returns the summary of the text of n.

func Summarize

func Summarize(s string) Summary

Summarize counts s.

type Text

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

Text is a version of a text, read by bytes and lines, as code editing counts. Its zero value is the empty text.

func New

func New(s string) Text

New returns the text s.

func TextOf

func TextOf(n *Node) Text

TextOf returns the text of a tree.

func (Text) ByteAt

func (t Text) ByteAt(off int) byte

ByteAt returns the byte at off, or 0 past the end.

func (Text) Chunks

func (t Text) Chunks(a, b int, fn func(s string) bool)

Chunks calls fn with bytes a to b, a piece at a time, until fn returns false.

func (Text) EachLine

func (t Text) EachLine(from, to int, fn func(i, start int, s string) bool)

EachLine calls fn with lines from to to, exclusive, and the byte each starts at, reading them a batch at a time rather than finding each in the tree, until fn returns false. Batches of long lines are short, so that fn stopping early leaves little copied for nothing.

func (Text) EachLineBack

func (t Text) EachLineBack(from, to int, fn func(i, start int, s string) bool)

EachLineBack calls fn with lines from down to to, inclusive, as EachLine does going up.

func (Text) FromUTF16

func (t Text) FromUTF16(u int) int

FromUTF16 returns the byte of UTF-16 unit u, rounded down to a rune.

func (Text) Len

func (t Text) Len() int

func (Text) Line

func (t Text) Line(i int) string

Line returns line i, without its newline.

func (Text) LineCount

func (t Text) LineCount() int

func (Text) LineEnd

func (t Text) LineEnd(line int) int

LineEnd returns the byte line ends at, before its newline.

func (Text) LineOf

func (t Text) LineOf(off int) int

LineOf returns the line holding byte off.

func (Text) LineStart

func (t Text) LineStart(line int) int

LineStart returns the byte line starts at, clamped.

func (Text) Replace

func (t Text) Replace(a, b int, s string) Text

Replace returns the text with bytes a to b replaced by s.

func (Text) Root

func (t Text) Root() *Node

Root returns the tree of the text.

func (Text) RuneAt

func (t Text) RuneAt(off int) (rune, int)

RuneAt returns the rune starting at off and its size, 0 and 0 at the end.

func (Text) RuneBefore

func (t Text) RuneBefore(off int) (rune, int)

RuneBefore returns the rune ending at off and its size.

func (Text) Same

func (t Text) Same(u Text) bool

Same reports whether t and u are the same version, without comparing their bytes.

func (Text) Slice

func (t Text) Slice(a, b int) string

Slice returns bytes a to b.

func (Text) String

func (t Text) String() string

func (Text) UTF16

func (t Text) UTF16(off int) int

UTF16 returns how many UTF-16 units come before byte off.

Jump to

Keyboard shortcuts

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