Documentation
¶
Overview ¶
Package highlighting provides syntax highlighting styles; it is based on github.com/alecthomas/chroma, which in turn was based on the python pygments package. Note that this package depends on core and parse and cannot be imported there; is imported in texteditor.
Index ¶
- Constants
- Variables
- func ChromaTagsLine(clex chroma.Lexer, txt string) (lexer.Line, error)
- func HTMLEscapeBytes(b []byte) []byte
- func HTMLEscapeRunes(r []rune) []byte
- func Init()
- func MarkupLineHTML(txt []rune, hitags, tags lexer.Line, escapeHTML bool) []byte
- func MarkupLineRich(hs *Style, sty *rich.Style, txt []rune, hitags, tags lexer.Line) rich.Text
- func MarkupPathsAsLinks(txt []rune, mu rich.Text, maxFields int) rich.Text
- func MergeAvailStyles()
- func TokenFromChroma(ct chroma.TokenType) token.Tokens
- func TokenToChroma(tok token.Tokens) chroma.TokenType
- func UpdateFromTheme()
- type Highlighter
- func (hi *Highlighter) Init(info *fileinfo.FileInfo, pist *parse.FileStates)
- func (hi *Highlighter) MarkupTagsAll(txt []byte) ([]lexer.Line, error)
- func (hi *Highlighter) MarkupTagsLine(ln int, txt []rune) (lexer.Line, error)
- func (hi *Highlighter) SetStyle(style HighlightingName)
- func (hi *Highlighter) UsingParse() bool
- type HighlightingName
- type Style
- func (hs *Style) CopyFrom(ss *Style)
- func (hs Style) OpenJSON(filename fsx.Filename) error
- func (hs Style) SaveJSON(filename fsx.Filename) error
- func (hs Style) Tag(tag token.Tokens) StyleEntry
- func (hs Style) TagRaw(tag token.Tokens) StyleEntry
- func (hs Style) ToCSS() map[token.Tokens]string
- func (hs Style) ToProperties() map[string]any
- type StyleEntry
- func (se StyleEntry) Inherit(ancestors ...StyleEntry) StyleEntry
- func (se StyleEntry) IsZero() bool
- func (se StyleEntry) String() string
- func (se StyleEntry) Sub(e StyleEntry) StyleEntry
- func (se StyleEntry) ToCSS() string
- func (se StyleEntry) ToProperties() map[string]any
- func (se StyleEntry) ToRichStyle(sty *rich.Style)
- func (se *StyleEntry) UpdateFromTheme()
- type Styles
- func (hs *Styles) Add() *Style
- func (hs *Styles) CopyFrom(os Styles)
- func (hs *Styles) Names() []string
- func (hs *Styles) OpenDefaults() error
- func (hs *Styles) OpenJSON(filename fsx.Filename) error
- func (hs *Styles) OpenSettings() error
- func (hs *Styles) SaveAll(dir fsx.Filename)
- func (hs *Styles) SaveJSON(filename fsx.Filename) error
- func (hs *Styles) SaveSettings() error
- type Trilean
- func (i Trilean) Desc() string
- func (i Trilean) Int64() int64
- func (i Trilean) MarshalText() ([]byte, error)
- func (t Trilean) Prefix(s string) string
- func (i *Trilean) SetInt64(in int64)
- func (i *Trilean) SetString(s string) error
- func (i Trilean) String() string
- func (i *Trilean) UnmarshalText(text []byte) error
- func (i Trilean) Values() []enums.Enum
Constants ¶
const ( EscapeHTML = true NoEscapeHTML = false )
maxLineLen prevents overflow in allocating line length
Variables ¶
var ( // StandardStyles are the styles from chroma package StandardStyles Styles // CustomStyles are user's special styles CustomStyles = Styles{} // AvailableStyles are all highlighting styles AvailableStyles Styles // StyleDefault is the default highlighting style name StyleDefault = HighlightingName("emacs") // StyleNames are all the names of all the available highlighting styles StyleNames []string // SettingsStylesFilename is the name of the preferences file in App data // directory for saving / loading the custom styles SettingsStylesFilename = "highlighting.json" // StylesChanged is used for gui updating while editing StylesChanged = false )
var DefaultStyle = HighlightingName("emacs")
DefaultStyle is the initial default style.
var Properties = map[token.Tokens]map[string]any{ token.TextSpellErr: { "text-decoration": 1 << uint32(rich.DottedUnderline), }, }
Properties are default properties for custom tags (tokens); if set in style then used there but otherwise we use these as a fallback; typically not overridden
var TokensToChromaMap = map[token.Tokens]chroma.TokenType{ token.EOF: chroma.EOFType, token.Background: chroma.Background, token.Error: chroma.Error, token.None: chroma.None, token.Keyword: chroma.Keyword, token.KeywordConstant: chroma.KeywordConstant, token.KeywordDeclaration: chroma.KeywordDeclaration, token.KeywordNamespace: chroma.KeywordNamespace, token.KeywordPseudo: chroma.KeywordPseudo, token.KeywordReserved: chroma.KeywordReserved, token.KeywordType: chroma.KeywordType, token.Name: chroma.Name, token.NameAttribute: chroma.NameAttribute, token.NameBuiltin: chroma.NameBuiltin, token.NameBuiltinPseudo: chroma.NameBuiltinPseudo, token.NameClass: chroma.NameClass, token.NameConstant: chroma.NameConstant, token.NameDecorator: chroma.NameDecorator, token.NameEntity: chroma.NameEntity, token.NameException: chroma.NameException, token.NameFunction: chroma.NameFunction, token.NameFunctionMagic: chroma.NameFunctionMagic, token.NameLabel: chroma.NameLabel, token.NameNamespace: chroma.NameNamespace, token.NameOperator: chroma.NameOperator, token.NameOther: chroma.NameOther, token.NamePseudo: chroma.NamePseudo, token.NameProperty: chroma.NameProperty, token.NameTag: chroma.NameTag, token.NameVar: chroma.NameVariable, token.NameVarAnonymous: chroma.NameVariableAnonymous, token.NameVarClass: chroma.NameVariableClass, token.NameVarGlobal: chroma.NameVariableGlobal, token.NameVarInstance: chroma.NameVariableInstance, token.NameVarMagic: chroma.NameVariableMagic, token.Literal: chroma.Literal, token.LiteralDate: chroma.LiteralDate, token.LiteralOther: chroma.LiteralOther, token.LitStr: chroma.LiteralString, token.LitStrAffix: chroma.LiteralStringAffix, token.LitStrAtom: chroma.LiteralStringAtom, token.LitStrBacktick: chroma.LiteralStringBacktick, token.LitStrBoolean: chroma.LiteralStringBoolean, token.LitStrChar: chroma.LiteralStringChar, token.LitStrDelimiter: chroma.LiteralStringDelimiter, token.LitStrDoc: chroma.LiteralStringDoc, token.LitStrDouble: chroma.LiteralStringDouble, token.LitStrEscape: chroma.LiteralStringEscape, token.LitStrHeredoc: chroma.LiteralStringHeredoc, token.LitStrInterpol: chroma.LiteralStringInterpol, token.LitStrName: chroma.LiteralStringName, token.LitStrOther: chroma.LiteralStringOther, token.LitStrRegex: chroma.LiteralStringRegex, token.LitStrSingle: chroma.LiteralStringSingle, token.LitStrSymbol: chroma.LiteralStringSymbol, token.LitNum: chroma.LiteralNumber, token.LitNumBin: chroma.LiteralNumberBin, token.LitNumFloat: chroma.LiteralNumberFloat, token.LitNumHex: chroma.LiteralNumberHex, token.LitNumInteger: chroma.LiteralNumberInteger, token.LitNumIntegerLong: chroma.LiteralNumberIntegerLong, token.LitNumOct: chroma.LiteralNumberOct, token.Operator: chroma.Operator, token.OperatorWord: chroma.OperatorWord, token.Punctuation: chroma.Punctuation, token.Comment: chroma.Comment, token.CommentHashbang: chroma.CommentHashbang, token.CommentMultiline: chroma.CommentMultiline, token.CommentSingle: chroma.CommentSingle, token.CommentSpecial: chroma.CommentSpecial, token.CommentPreproc: chroma.CommentPreproc, token.CommentPreprocFile: chroma.CommentPreprocFile, token.Text: chroma.Text, token.TextWhitespace: chroma.TextWhitespace, token.TextSymbol: chroma.TextSymbol, token.TextPunctuation: chroma.TextPunctuation, token.TextStyle: chroma.Generic, token.TextStyleDeleted: chroma.GenericDeleted, token.TextStyleEmph: chroma.GenericEmph, token.TextStyleError: chroma.GenericError, token.TextStyleHeading: chroma.GenericHeading, token.TextStyleInserted: chroma.GenericInserted, token.TextStyleOutput: chroma.GenericOutput, token.TextStylePrompt: chroma.GenericPrompt, token.TextStyleStrong: chroma.GenericStrong, token.TextStyleSubheading: chroma.GenericSubheading, token.TextStyleTraceback: chroma.GenericTraceback, token.TextStyleUnderline: chroma.GenericUnderline, }
TokensToChromaMap maps from Tokens to chroma.TokenType
Functions ¶
func ChromaTagsLine ¶
ChromaTagsLine returns tags for one line according to given chroma lexer
func HTMLEscapeBytes ¶
HTMLEscapeBytes escapes special characters like "<" to become "<". It escapes only five such characters: <, >, &, ' and ". It operates on a *copy* of the byte string and does not modify the input! otherwise it causes major problems..
func HTMLEscapeRunes ¶
HTMLEscapeRunes escapes special characters like "<" to become "<". It escapes only five such characters: <, >, &, ' and ". It operates on a *copy* of the byte string and does not modify the input! otherwise it causes major problems..
func Init ¶
func Init()
Init must be called to initialize the hi styles -- post startup so chroma stuff is all in place, and loads custom styles
func MarkupLineHTML ¶
MarkupLineHTML returns the line with html class tags added for each tag takes both the hi tags and extra tags. Only fully nested tags are supported, with any dangling ends truncated.
func MarkupLineRich ¶
MarkupLineRich returns the rich.Text styled line for each tag. Takes both the hi highlighting tags and extra tags. The style provides the starting default style properties.
func MarkupPathsAsLinks ¶
MarkupPathsAsLinks adds hyperlink span styles to given markup of given text, for any strings that look like file paths / urls. maxFields is the maximum number of fieldsto look for file paths in: 2 is a reasonable default, to avoid getting other false-alarm info later.
func MergeAvailStyles ¶
func MergeAvailStyles()
MergeAvailStyles updates AvailStyles as combination of std and custom styles
func TokenFromChroma ¶
FromChroma converts a chroma.TokenType to a parse token.Tokens
func TokenToChroma ¶
TokenToChroma converts to a chroma.TokenType
func UpdateFromTheme ¶
func UpdateFromTheme()
UpdateFromTheme normalizes the colors of all style entry such that they have consistent chromas and tones that guarantee sufficient text contrast in accordance with the color theme.
Types ¶
type Highlighter ¶
type Highlighter struct {
// syntax highlighting style to use
StyleName HighlightingName
// Has is whether there are highlighting parameters set
// (only valid after [Highlighter.init] has been called).
Has bool
// tab size, in chars
TabSize int
// Commpiled CSS properties for given highlighting style
CSSProperties map[string]any
// Style is the current highlighting style.
Style *Style
// contains filtered or unexported fields
}
Highlighter performs syntax highlighting, using parse if available, otherwise falls back on chroma.
func (*Highlighter) Init ¶
func (hi *Highlighter) Init(info *fileinfo.FileInfo, pist *parse.FileStates)
Init initializes the syntax highlighting for current params
func (*Highlighter) MarkupTagsAll ¶
func (hi *Highlighter) MarkupTagsAll(txt []byte) ([]lexer.Line, error)
MarkupTagsAll returns all the markup tags according to current syntax highlighting settings
func (*Highlighter) MarkupTagsLine ¶
MarkupTagsLine returns tags for one line according to current syntax highlighting settings
func (*Highlighter) SetStyle ¶
func (hi *Highlighter) SetStyle(style HighlightingName)
SetStyle sets the highlighting style and updates corresponding settings
func (*Highlighter) UsingParse ¶
func (hi *Highlighter) UsingParse() bool
UsingParse returns true if markup is using parse lexer / parser, which affects use of results
type HighlightingName ¶
type HighlightingName string
type Style ¶
type Style map[token.Tokens]*StyleEntry
Style is a full style map of styles for different token.Tokens tag values
func AvailableStyle ¶
func AvailableStyle(nm HighlightingName) *Style
AvailableStyle returns a style by name from the AvailStyles list -- if not found default is used as a fallback
func (Style) Tag ¶
func (hs Style) Tag(tag token.Tokens) StyleEntry
Tag returns a StyleEntry for given Tag. Will try sub-category or category if an exact match is not found. does NOT add the background properties -- those are always kept separate.
func (Style) TagRaw ¶
func (hs Style) TagRaw(tag token.Tokens) StyleEntry
TagRaw returns a StyleEntry for given tag without any inheritance of anything will be IsZero if not defined for this style
func (Style) ToProperties ¶
ToProperties generates a list of key-value properties for this style.
type StyleEntry ¶
type StyleEntry struct {
// Color is the text color.
Color color.RGBA `json:",omitempty"`
// Background color.
// In general it is not good to use this because it obscures highlighting.
Background color.RGBA `json:",omitempty"`
// Border color? not sure what this is -- not really used.
Border color.RGBA `display:"-" json:",omitempty"`
// Bold font.
Bold Trilean `json:",omitempty"`
// Italic font.
Italic Trilean `json:",omitempty"`
// Underline.
Underline Trilean `json:",omitempty"`
// DottedUnderline
DottedUnderline Trilean `json:",omitempty"`
// NoInherit indicates to not inherit these settings from sub-category or category levels.
// Otherwise everything with a Pass is inherited.
NoInherit bool `json:",omitempty"`
// contains filtered or unexported fields
}
StyleEntry is one value in the map of highlight style values
func (StyleEntry) Inherit ¶
func (se StyleEntry) Inherit(ancestors ...StyleEntry) StyleEntry
Inherit styles from ancestors.
Ancestors should be provided from oldest, furthest away to newest, closest.
func (StyleEntry) IsZero ¶
func (se StyleEntry) IsZero() bool
func (StyleEntry) String ¶
func (se StyleEntry) String() string
func (StyleEntry) Sub ¶
func (se StyleEntry) Sub(e StyleEntry) StyleEntry
Sub subtracts two style entries, returning an entry with only the differences set
func (StyleEntry) ToCSS ¶
func (se StyleEntry) ToCSS() string
ToCSS converts StyleEntry to CSS attributes.
func (StyleEntry) ToProperties ¶
func (se StyleEntry) ToProperties() map[string]any
ToProperties converts the StyleEntry to key-value properties.
func (StyleEntry) ToRichStyle ¶
func (se StyleEntry) ToRichStyle(sty *rich.Style)
ToRichStyle sets the StyleEntry to given rich.Style.
func (*StyleEntry) UpdateFromTheme ¶
func (se *StyleEntry) UpdateFromTheme()
UpdateFromTheme normalizes the colors of the style entry such that they have consistent chromas and tones that guarantee sufficient text contrast in accordance with the color theme.
type Styles ¶
Styles is a collection of styles
func (*Styles) OpenDefaults ¶
OpenDefaults opens the default highlighting styles (from chroma originally) These are encoded as an embed from defaults.highlighting
func (*Styles) OpenJSON ¶
Open hi styles from a JSON-formatted file. You can save and open styles to / from files to share, experiment, transfer, etc.
func (*Styles) OpenSettings ¶
OpenSettings opens Styles from Cogent Core standard prefs directory, using SettingsStylesFilename
func (*Styles) SaveJSON ¶
Save hi styles to a JSON-formatted file. You can save and open styles to / from files to share, experiment, transfer, etc.
func (*Styles) SaveSettings ¶
SaveSettings saves Styles to Cogent Core standard prefs directory, using SettingsStylesFilename
type Trilean ¶
type Trilean int32 //enums:enum
Trilean value for StyleEntry value inheritance.
const TrileanN Trilean = 3
TrileanN is the highest valid value for type Trilean, plus one.
func TrileanValues ¶
func TrileanValues() []Trilean
TrileanValues returns all possible values for the type Trilean.
func (Trilean) MarshalText ¶
MarshalText implements the encoding.TextMarshaler interface.
func (*Trilean) SetString ¶
SetString sets the Trilean value from its string representation, and returns an error if the string is invalid.
func (*Trilean) UnmarshalText ¶
UnmarshalText implements the encoding.TextUnmarshaler interface.