highlight

package
v2.0.14 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Groups map[string]Group

Groups contains all of the groups that are defined You can access them in the map via their string name

Functions

func CharacterCount

func CharacterCount(b []byte) int

CharacterCount returns the number of characters in a byte array Similar to utf8.RuneCount but for unicode characters

func CharacterCountInString

func CharacterCountInString(str string) int

CharacterCount returns the number of characters in a string Similar to utf8.RuneCountInString but for unicode characters

func DecodeCharacter

func DecodeCharacter(b []byte) (rune, []rune, int)

DecodeCharacter returns the next character from an array of bytes A character is a rune along with any accompanying combining runes

func DecodeCharacterInString

func DecodeCharacterInString(str string) (rune, []rune, int)

DecodeCharacterInString returns the next character from a string A character is a rune along with any accompanying combining runes

func GetIncludes

func GetIncludes(d *Def) []string

GetIncludes returns a list of filetypes that are included by this syntax def

func HasIncludes

func HasIncludes(d *Def) bool

HasIncludes returns whether this syntax def has any include statements

func ResolveIncludes

func ResolveIncludes(def *Def, files []*File)

ResolveIncludes will sort out the rules for including other filetypes You should call this after parsing all the Defs

Types

type Def

type Def struct {
	*Header
	// contains filtered or unexported fields
}

A Def is a full syntax definition for a language It has a filetype, information about how to detect the filetype based on filename or header (the first line of the file) Then it has the rules which define how to highlight the file

func ParseDef

func ParseDef(f *File, header *Header) (s *Def, err error)

ParseDef parses an input syntax file into a highlight Def

type File

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

func ParseFile

func ParseFile(input []byte) (f *File, err error)

type Group

type Group uint8

A Group represents a syntax group

func (Group) String

func (g Group) String() string

String returns the group name attached to the specific group

type Header struct {
	FileType       string
	FileNameRegex  *regexp.Regexp
	HeaderRegex    *regexp.Regexp
	SignatureRegex *regexp.Regexp
}

func MakeHeader

func MakeHeader(data []byte) (*Header, error)

MakeHeader takes a header (.hdr file) file and parses the header Header files make parsing more efficient when you only want to compute on the headers of syntax files A yaml file might take ~400us to parse while a header file only takes ~20us

func MakeHeaderYaml

func MakeHeaderYaml(data []byte) (*Header, error)

MakeHeaderYaml takes a yaml spec for a syntax file and parses the header

func (*Header) HasFileSignature added in v2.0.14

func (header *Header) HasFileSignature() bool

HasFileSignature checks the presence of a stored signature

func (*Header) MatchFileHeader added in v2.0.14

func (header *Header) MatchFileHeader(firstLine []byte) bool

func (*Header) MatchFileName added in v2.0.14

func (header *Header) MatchFileName(filename string) bool

MatchFileName will check the given file name with the stored regex

func (*Header) MatchFileSignature added in v2.0.14

func (header *Header) MatchFileSignature(line []byte) bool

MatchFileSignature will check the given line with the stored regex

type HeaderYaml

type HeaderYaml struct {
	FileType string `yaml:"filetype"`
	Detect   struct {
		FNameRegexStr     string `yaml:"filename"`
		HeaderRegexStr    string `yaml:"header"`
		SignatureRegexStr string `yaml:"signature"`
	} `yaml:"detect"`
}

type Highlighter

type Highlighter struct {
	Def *Def
	// contains filtered or unexported fields
}

A Highlighter contains the information needed to highlight a string

func NewHighlighter

func NewHighlighter(def *Def) *Highlighter

NewHighlighter returns a new highlighter from the given syntax definition

func (*Highlighter) HighlightMatches

func (h *Highlighter) HighlightMatches(input LineStates, startline, endline int)

HighlightMatches sets the matches for each line from startline to endline It sets all other matches in the buffer to nil to conserve memory This assumes that all the states are set correctly

func (*Highlighter) HighlightStates

func (h *Highlighter) HighlightStates(input LineStates)

HighlightStates correctly sets all states for the buffer

func (*Highlighter) HighlightString

func (h *Highlighter) HighlightString(input string) []LineMatch

HighlightString syntax highlights a string Use this function for simple syntax highlighting and use the other functions for more advanced syntax highlighting. They are optimized for quick rehighlighting of the same text with minor changes made

func (*Highlighter) ReHighlightLine

func (h *Highlighter) ReHighlightLine(input LineStates, lineN int)

ReHighlightLine will rehighlight the state and match for a single line

func (*Highlighter) ReHighlightStates

func (h *Highlighter) ReHighlightStates(input LineStates, startline int) int

ReHighlightStates will scan down from `startline` and set the appropriate end of line state for each line until it comes across a line whose state does not change returns the number of the final line

type LineMatch

type LineMatch map[int]Group

LineMatch represents the syntax highlighting matches for one line. Each index where the coloring is changed is marked with that color's group (represented as one byte)

type LineStates

type LineStates interface {
	LineBytes(n int) []byte
	LinesNum() int
	State(lineN int) State
	SetState(lineN int, s State)
	SetMatch(lineN int, m LineMatch)
	Lock()
	Unlock()
}

LineStates is an interface for a buffer-like object which can also store the states and matches for every line

type State

type State *region

A State represents the region at the end of a line

Jump to

Keyboard shortcuts

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