highlight

package
v0.10.2 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2026 License: MIT Imports: 3 Imported by: 0

Documentation

Overview

Package highlight makes syntax colouring affordable on every keystroke.

syntax lexes one line given the state the line above left behind, so colouring line N means knowing the state at line N, which means lexing from line 0. At roughly a microsecond a line that is milliseconds of work per frame on a large file - paid on every keystroke, while editing near the bottom, for text that has not changed.

Cache removes that cost with one idea: keep the incoming state of every line. An edit at line N cannot affect any state above N, so re-lexing starts there. More importantly it also STOPS early - re-lexing runs downward only until a recomputed state matches the one already cached, because from that line on the lexer is in exactly the state it was in before and nothing below can have changed. Typing inside a function reaches that after one line; opening a block comment does not reach it until the comment closes, which is precisely when everything below really has changed colour. One rule covers both.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache holds per-line lexer states for one buffer.

A Cache belongs to a single buffer: it identifies edits through the buffer's own tracking, which is consumed on read. Pointing one Cache at two buffers gives correct but needlessly slow results, since each switch looks like a change it cannot account for.

func New

func New(lex syntax.Lexer) *Cache

New returns a cache that lexes with lex.

func (*Cache) Invalidate

func (c *Cache) Invalidate(fromLine int)

Invalidate discards what is known from fromLine downward.

Callers do not normally need this: edits are detected through the buffer. It is here for changes the buffer cannot report, such as a setting that alters how text is classified.

func (*Cache) Lexer

func (c *Cache) Lexer() syntax.Lexer

Lexer returns the lexer in use.

func (*Cache) SetLexer

func (c *Cache) SetLexer(lex syntax.Lexer)

SetLexer changes the language and discards everything, since states computed by one lexer mean nothing to another.

func (*Cache) Spans

func (c *Cache) Spans(b *text.Buffer, line int) []syntax.Span

Spans returns the classified runs of one line.

It returns nil for a line outside the buffer rather than panicking: a caller redrawing while a buffer shrinks underneath it should show nothing for a line that no longer exists, not crash.

Jump to

Keyboard shortcuts

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