textedit

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 2026 License: MIT Imports: 2 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 textedit provides byte spans and edits for source-preserving transformations.

Spans refer to the original source buffer. Apply checks edit bounds and overlap before producing replacement bytes, and Position maps byte offsets to line and column diagnostics. Language-specific parsing and edit intent belong to callers.

Index

Constants

View Source
const MatchBudget = 4_000_000

MatchBudget is the most cells Match's table may take between two sequences' common ends: 4,000,000 int32s, 16 MB. Past it, the middle is all changed, which callers read as a change to hold for, never as sameness.

Variables

This section is empty.

Functions

func Apply

func Apply(src []byte, edits []Edit) ([]byte, error)

func Match added in v0.7.0

func Match(n, m int, same func(i, j int) bool) (from []int, within bool)

Match pairs two sequences' elements, a's n and b's m, by a longest common subsequence after their common start and end, same saying whether a[i] and b[j] are equal: from[j] is the index in a that b[j] keeps, or -1 for one b adds or alters. Where the middle's table would pass MatchBudget, every element of it is unmatched and within is false: the comparison's memory stays bounded, its answer "changed" (Codex's review of 8e314b96, finding 5, where a license comparison took 51 MB for a 25 KB pair). It's the one way the comparisons of lines and words match, each mapping the pairs to its own answer.

func Position

func Position(src []byte, offset int) (line, col int)

func Unmatched added in v0.7.0

func Unmatched(n int, from []int) []int

Unmatched are the indices of a that from, Match's answer for b, keeps none of.

Types

type Edit

type Edit struct {
	Span Span
	New  []byte
}

type EditError

type EditError struct {
	Type EditErrorType
	Edit Edit
}

func (EditError) Error

func (e EditError) Error() string

type EditErrorType

type EditErrorType int
const (
	ReversedSpan EditErrorType = iota

	OutOfBounds

	Overlap
)

func (EditErrorType) String

func (t EditErrorType) String() string

type Span

type Span struct {
	Start int
	End   int
}

func (Span) Len

func (s Span) Len() int

func (Span) Text

func (s Span) Text(src []byte) string

Jump to

Keyboard shortcuts

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