highlight

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 24, 2026 License: Apache-2.0 Imports: 8 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 highlight colours source code for the web UI's diffs. It tokenises a whole file with chroma, so strings and comments that span lines colour correctly, and hands back each line's tokens tagged with one of a small fixed set of CSS classes (tk-k, tk-s, ...). It never produces HTML: the templates render the tokens and escape their text.

Index

Constants

View Source
const Budget = 150 * time.Millisecond

Budget bounds the time spent tokenising one file. Lines the lexer has not reached by then are shown plain.

View Source
const MaxSize = 256 << 10

MaxSize is the largest file that is highlighted; bigger files are shown plain to keep pages fast.

Variables

This section is empty.

Functions

func Class

func Class(t chroma.TokenType) string

Class maps a chroma token type to one of the UI's syntax classes.

func File

func File(name string, src []byte, upto int) (lines [][]Token)

File tokenises src, the contents of the file at name, and returns its tokens line by line: index 0 is line 1, and no token holds a newline. Tokenising stops once the first upto lines are done (all lines when upto is 0 or less), since a diff rarely shows the end of a file; lexing from the top still gets multi-line strings and comments right. The result can be shorter than the file; missing lines are meant to be shown plain. It returns nil when the file should be shown plain: no lexer knows the name, the file is larger than MaxSize, or it is binary or not UTF-8.

Types

type Span

type Span struct {
	Text  string
	Class string
	Hi    bool
}

Span is a piece of a rendered diff line: syntax class plus whether it is part of the changed region of an edited line.

func Overlay

func Overlay(toks []Token, text string, segs []Span) []Span

Overlay combines a line's syntax tokens with its word-level change marks (segs; nil when the line was not edited in place) into spans cut at both kinds of boundary. text is the line as the diff shows it; when the tokens do not spell it exactly, Overlay returns nil and the caller shows the line without syntax colours.

func (Span) Classes

func (s Span) Classes() string

Classes is the value of the span's class attribute ("" when the span needs no element at all).

type Token

type Token struct {
	Text  string
	Class string
}

Token is a run of text on one line with its CSS class ("" for plain).

Jump to

Keyboard shortcuts

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