flare

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2022 License: MIT Imports: 12 Imported by: 0

README

Flare

Flare is a syntax highlighting library built using the incremental parser GPeg.

Features:

  • Supports fast incremental rehighlighting for use in an editor.
  • Can load and use highlighters at runtime.
  • Multiple languages are supported, and it is easy to add more (check the languages/ directory).
  • Supports limited context-sensitivity (back references) for handling HERE-doc style blocks.
  • Supports including languages within each other.
  • Fairly efficient.
  • Can format highlighted code as HTML output.

See the hcat program in ./cmd/hcat for an example that can highlight code files at the terminal or output highlighted HTML.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddLanguage

func AddLanguage(name string, loader func() ([]byte, error))

AddLanguage adds support for a new language. The 'loader' should return the highlighting grammar when called.

func SetLoader

func SetLoader(loader func(name string) ([]byte, error))

Types

type Highlighter

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

A Highlighter corresponds to a compiled GPeg program and a mapping from captures to highlight groups. It can be used to generate highlight results from a io.ReaderAt.

func LoadHighlighter

func LoadHighlighter(lang string, data []byte, memo bool) (*Highlighter, error)

LoadHighlighter loads a highlighter from some data.

func LoadHighlighterBuiltin

func LoadHighlighterBuiltin(name string, memo bool) (*Highlighter, error)

LoadHighlighterBuiltin compiles and loads the given highlighter. Use 'memo' if the highlighter will be used in an incremental setting (editor).

func (*Highlighter) HighlightFunc

func (h *Highlighter) HighlightFunc(r io.ReaderAt, tbl memo.Table, draw func(text []byte, group string), i *vm.Interval)

HighlightFunc is similar to HighlightMatches but calls a function for each match instead of storing it in a table.

func (*Highlighter) HighlightMatches

func (h *Highlighter) HighlightMatches(r io.ReaderAt, tbl memo.Table, i *vm.Interval) *Matches

HighlightMatches performs syntax highlighting on the given ReaderAt with a possibly pre-filled memoization table, over a specified interval. It generates a Matches structure that can be queried for the highlight results. If the specified interval is nil, the full file is used.

NOTE: if using an interval, make sure it is as small as possible. Using nil and the full file range are not equivalent. Parse time is proportional to the size of the window, unless nil is used. However, if nil is used memory usage may be high if the input is large because the full parse tree is returned.

type Matches

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

Matches is a structure that stores highlighting results corresponding to a certain range of the document. It supports querying the highlight group of any index within the range.

func (*Matches) Group

func (m *Matches) Group(idx int) string

Group returns the highlighting group at the specified index.

func (*Matches) InRange

func (m *Matches) InRange(p int) bool

InRange returns true if the given offset is in this Matches structure.

Directories

Path Synopsis
cmd

Jump to

Keyboard shortcuts

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