highlight

package
v0.3.2 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 5 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 用 chroma 把源码切成带类型的片段,供界面按行上色。

只做「哪一段是什么 token」,不决定颜色:同一份切分结果在浅色和深色 主题下都能用,颜色在渲染时按当前主题查。这样切换主题不需要重新切分。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NeedHighlight

func NeedHighlight(name string) bool

NeedHighlight 判断这个文件名值不值得高亮。

词法器认不出来的一律走纯文本。chroma 会把 .txt 之类也认成 plaintext 词法器,那种「高亮」只是把每行标成 Text,白跑一遍切分,所以按名字挡掉。

func Style

func Style(dark bool) *chroma.Style

Style 按明暗取配色方案。名字不存在时 chroma 会退回默认方案, 不会返回 nil。

Types

type Entry

type Entry struct {
	// Hex 是 "#rrggbb";空串表示这套配色没规定颜色,该用默认前景色。
	Hex    string
	Bold   bool
	Italic bool
}

Entry 是渲染一段文字需要的样式。

func Resolve

func Resolve(style *chroma.Style, t chroma.TokenType) Entry

Resolve 查 token 类型在给定配色下的样式。切分本身不看样式, 所以这个函数只给测试和 Palette 用。

type File

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

File 是一个文件切分好的结果,按行(从 1 开始)取用。

func NewLines

func NewLines(name string, lines []string) *File

NewLines 切分文件内容。lines 是按行切好的内容(不含换行符)。

必须整份文件一起切:多行字符串、块注释这类结构单看一行是判断不出来的, 而 diff 恰好经常出现在这些结构附近。

func (*File) Line

func (f *File) Line(n int) []Seg

Line 返回第 n 行(从 1 开始)的片段。超出范围或没有内容时返回 nil, 调用方按整行一个颜色处理即可。

func (*File) Plain

func (f *File) Plain() bool

Plain 报告文件是否按纯文本处理。

type Palette

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

Palette 是一套配色方案,缓存每个 token 类型解析好的样式。

切分结果和明暗无关,颜色却随主题变 —— 所以颜色在渲染时按当前主题 解析,解析一次就缓存下来,不必每帧对每一段重新做一遍样式继承。

func NewPalette

func NewPalette(dark bool) *Palette

NewPalette 按明暗取一套配色。

func (*Palette) Entry

func (p *Palette) Entry(s Seg) Entry

Entry 返回某个片段该用什么样式。不认识的片段类型返回零值, 调用方用默认前景色即可。

type Seg

type Seg struct {
	Start, End int
	Type       chroma.TokenType
}

Seg 是行内一段同类型的文本,区间是该行里的字节下标。

Jump to

Keyboard shortcuts

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