Documentation
¶
Overview ¶
package histyle provides syntax highlighting styles -- it interoperates with github.com/alecthomas/chroma which in turn interoperates with the python pygments package. Note that this package depends on goki/gi and cannot be imported there -- is imported into goki/gi/giv
Index ¶
- Constants
- Variables
- func Init()
- func InitHiTagNames()
- func MergeAvailStyles()
- type HiTags
- func (ht HiTags) Category() HiTags
- func (ht HiTags) ClassName() string
- func (ht *HiTags) FromChroma(ct chroma.TokenType)
- func (i *HiTags) FromString(s string) error
- func (ht HiTags) InCategory(other HiTags) bool
- func (ht HiTags) InSubCategory(other HiTags) bool
- func (ev HiTags) MarshalJSON() ([]byte, error)
- func (ev HiTags) MarshalText() ([]byte, error)
- func (ht HiTags) Parent() HiTags
- func (i HiTags) String() string
- func (ht HiTags) StyleName() string
- func (ht HiTags) SubCategory() HiTags
- func (ht HiTags) ToChroma() chroma.TokenType
- func (ev *HiTags) UnmarshalJSON(b []byte) error
- func (ev *HiTags) UnmarshalText(b []byte) error
- type Style
- func (hs *Style) CopyFrom(ss Style)
- func (hs *Style) FromChroma(cs *chroma.Style)
- func (hs Style) OpenJSON(filename gi.FileName) error
- func (hs Style) SaveJSON(filename gi.FileName) error
- func (hs Style) Tag(tag HiTags) StyleEntry
- func (hs Style) TagRaw(tag HiTags) StyleEntry
- func (hs Style) ToCSS() map[HiTags]string
- func (hs Style) ToProps() ki.Props
- type StyleEntry
- func (he *StyleEntry) FromChroma(ce chroma.StyleEntry)
- func (s StyleEntry) Inherit(ancestors ...StyleEntry) StyleEntry
- func (s StyleEntry) IsZero() bool
- func (se StyleEntry) String() string
- func (s StyleEntry) Sub(e StyleEntry) StyleEntry
- func (se StyleEntry) ToCSS() string
- func (se StyleEntry) ToProps() ki.Props
- type StyleName
- type Styles
- func (hs *Styles) Add() Style
- func (hs *Styles) CopyFrom(os Styles)
- func (hs *Styles) FromChroma(cs map[string]*chroma.Style)
- func (hs *Styles) Names() []string
- func (hs *Styles) OpenJSON(filename gi.FileName) error
- func (hs *Styles) OpenPrefs() error
- func (hs *Styles) SaveJSON(filename gi.FileName) error
- func (hs *Styles) SavePrefs() error
- func (hs *Styles) ViewStd()
- type Trilean
Constants ¶
const ( // ChromaTag is our starting tag -- anything above this is custom.. ChromaTag chroma.TokenType = 10000 + iota // ChromaSpellErr tags a spelling error ChromaSpellErr )
Chroma tag token types -- our extensions to the chroma token type
const ( Whitespace = TextWhitespace Date = LiteralDate String = LiteralString StringAffix = LiteralStringAffix StringBacktick = LiteralStringBacktick StringChar = LiteralStringChar StringDelimiter = LiteralStringDelimiter StringDoc = LiteralStringDoc StringDouble = LiteralStringDouble StringEscape = LiteralStringEscape StringHeredoc = LiteralStringHeredoc StringInterpol = LiteralStringInterpol StringOther = LiteralStringOther StringRegex = LiteralStringRegex StringSingle = LiteralStringSingle StringSymbol = LiteralStringSymbol Number = LiteralNumber NumberBin = LiteralNumberBin NumberFloat = LiteralNumberFloat NumberHex = LiteralNumberHex NumberInteger = LiteralNumberInteger NumberIntegerLong = LiteralNumberIntegerLong NumberOct = LiteralNumberOct )
Aliases.
Variables ¶
var ChromaTagNames = map[chroma.TokenType]string{ ChromaSpellErr: "cse", }
ChromaTagNames are our style names for Chroma tags -- need to ensure CSS exists for these
var ChromaToHiTagsMap map[chroma.TokenType]HiTags
ChromaToHiTagsMap maps from chroma.TokenType to HiTags -- built from opposite map
var CustomStyles = Styles{}
CustomStyles are user's special styles
var HiTagNames map[HiTags]string
HiTagNames is the complete mapping from a HiTags tag to the css style name most are 2 letters -- populated from chroma.StandardTypes plus custom
var HiTagsProps = map[HiTags]ki.Props{ SpellErr: ki.Props{ "text-decoration": 1 << uint32(gi.DecoDottedUnderline), }, }
HiTagsProps are default properties for custom tags -- if set in style then used there but otherwise we use these as a fallback -- typically not overridden
var HiTagsToChromaMap = map[HiTags]chroma.TokenType{ EOFType: chroma.EOFType, Background: chroma.Background, LineNumbers: chroma.LineNumbers, LineNumbersTable: chroma.LineNumbersTable, LineHighlight: chroma.LineHighlight, LineTable: chroma.LineTable, LineTableTD: chroma.LineTableTD, Error: chroma.Error, Other: chroma.Other, None: chroma.None, Keyword: chroma.Keyword, KeywordConstant: chroma.KeywordConstant, KeywordDeclaration: chroma.KeywordDeclaration, KeywordNamespace: chroma.KeywordNamespace, KeywordPseudo: chroma.KeywordPseudo, KeywordReserved: chroma.KeywordReserved, KeywordType: chroma.KeywordType, Name: chroma.Name, NameAttribute: chroma.NameAttribute, NameBuiltin: chroma.NameBuiltin, NameBuiltinPseudo: chroma.NameBuiltinPseudo, NameClass: chroma.NameClass, NameConstant: chroma.NameConstant, NameDecorator: chroma.NameDecorator, NameEntity: chroma.NameEntity, NameException: chroma.NameException, NameFunction: chroma.NameFunction, NameFunctionMagic: chroma.NameFunctionMagic, NameKeyword: chroma.NameKeyword, NameLabel: chroma.NameLabel, NameNamespace: chroma.NameNamespace, NameOperator: chroma.NameOperator, NameOther: chroma.NameOther, NamePseudo: chroma.NamePseudo, NameProperty: chroma.NameProperty, NameTag: chroma.NameTag, NameVariable: chroma.NameVariable, NameVariableAnonymous: chroma.NameVariableAnonymous, NameVariableClass: chroma.NameVariableClass, NameVariableGlobal: chroma.NameVariableGlobal, NameVariableInstance: chroma.NameVariableInstance, NameVariableMagic: chroma.NameVariableMagic, Literal: chroma.Literal, LiteralDate: chroma.LiteralDate, LiteralOther: chroma.LiteralOther, LiteralString: chroma.LiteralString, LiteralStringAffix: chroma.LiteralStringAffix, LiteralStringAtom: chroma.LiteralStringAtom, LiteralStringBacktick: chroma.LiteralStringBacktick, LiteralStringBoolean: chroma.LiteralStringBoolean, LiteralStringChar: chroma.LiteralStringChar, LiteralStringDelimiter: chroma.LiteralStringDelimiter, LiteralStringDoc: chroma.LiteralStringDoc, LiteralStringDouble: chroma.LiteralStringDouble, LiteralStringEscape: chroma.LiteralStringEscape, LiteralStringHeredoc: chroma.LiteralStringHeredoc, LiteralStringInterpol: chroma.LiteralStringInterpol, LiteralStringName: chroma.LiteralStringName, LiteralStringOther: chroma.LiteralStringOther, LiteralStringRegex: chroma.LiteralStringRegex, LiteralStringSingle: chroma.LiteralStringSingle, LiteralStringSymbol: chroma.LiteralStringSymbol, LiteralNumber: chroma.LiteralNumber, LiteralNumberBin: chroma.LiteralNumberBin, LiteralNumberFloat: chroma.LiteralNumberFloat, LiteralNumberHex: chroma.LiteralNumberHex, LiteralNumberInteger: chroma.LiteralNumberInteger, LiteralNumberIntegerLong: chroma.LiteralNumberIntegerLong, LiteralNumberOct: chroma.LiteralNumberOct, Operator: chroma.Operator, OperatorWord: chroma.OperatorWord, Punctuation: chroma.Punctuation, Comment: chroma.Comment, CommentHashbang: chroma.CommentHashbang, CommentMultiline: chroma.CommentMultiline, CommentSingle: chroma.CommentSingle, CommentSpecial: chroma.CommentSpecial, CommentPreproc: chroma.CommentPreproc, CommentPreprocFile: chroma.CommentPreprocFile, Generic: chroma.Generic, GenericDeleted: chroma.GenericDeleted, GenericEmph: chroma.GenericEmph, GenericError: chroma.GenericError, GenericHeading: chroma.GenericHeading, GenericInserted: chroma.GenericInserted, GenericOutput: chroma.GenericOutput, GenericPrompt: chroma.GenericPrompt, GenericStrong: chroma.GenericStrong, GenericSubheading: chroma.GenericSubheading, GenericTraceback: chroma.GenericTraceback, GenericUnderline: chroma.GenericUnderline, Text: chroma.Text, TextWhitespace: chroma.TextWhitespace, TextSymbol: chroma.TextSymbol, TextPunctuation: chroma.TextPunctuation, SpellErr: ChromaSpellErr, }
HiTagsToChromaMap maps from HiTags to chroma.TokenType
var KiT_Style = kit.Types.AddType(&Style{}, StyleProps)
var KiT_StyleEntry = kit.Types.AddType(&StyleEntry{}, StyleEntryProps)
var KiT_Styles = kit.Types.AddType(&Styles{}, StylesProps)
var PrefsStylesFileName = "hi_styles.json"
PrefsStylesFileName is the name of the preferences file in App prefs directory for saving / loading the custom styles
var StyleDefault = StyleName("emacs")
StyleDefault is the default highlighting style name -- can set this to whatever you want
var StyleEntryProps = ki.Props{ "inline": true, }
var StyleNames []string
StyleNames are all the names of all the available highlighting styles
var StyleProps = ki.Props{ "MainMenu": ki.PropSlice{ {"AppMenu", ki.BlankProp{}}, {"File", ki.PropSlice{ {"OpenJSON", ki.Props{ "label": "Open from file", "desc": "You can save and open styles to / from files to share, experiment, transfer, etc", "shortcut": gi.KeyFunMenuOpen, "Args": ki.PropSlice{ {"File Name", ki.Props{ "ext": ".json", }}, }, }}, {"SaveJSON", ki.Props{ "label": "Save to file", "desc": "You can save and open styles to / from files to share, experiment, transfer, etc", "shortcut": gi.KeyFunMenuSaveAs, "Args": ki.PropSlice{ {"File Name", ki.Props{ "ext": ".json", }}, }, }}, }}, {"Edit", "Copy Cut Paste Dupe"}, {"Window", "Windows"}, }, "ToolBar": ki.PropSlice{ {"OpenJSON", ki.Props{ "label": "Open from file", "icon": "file-open", "desc": "You can save and open styles to / from files to share, experiment, transfer, etc -- save from standard ones and load into custom ones for example", "Args": ki.PropSlice{ {"File Name", ki.Props{ "ext": ".json", }}, }, }}, {"SaveJSON", ki.Props{ "label": "Save to file", "icon": "file-save", "desc": "You can save and open styles to / from files to share, experiment, transfer, etc -- save from standard ones and load into custom ones for example", "Args": ki.PropSlice{ {"File Name", ki.Props{ "ext": ".json", }}, }, }}, }, }
StyleProps define the ToolBar and MenuBar for view
var StylesChanged = false
StylesChanged is used for gui updating while editing
var StylesProps = ki.Props{ "MainMenu": ki.PropSlice{ {"AppMenu", ki.BlankProp{}}, {"File", ki.PropSlice{ {"OpenPrefs", ki.Props{}}, {"SavePrefs", ki.Props{ "shortcut": gi.KeyFunMenuSave, "updtfunc": func(sti interface{}, act *gi.Action) { act.SetActiveStateUpdt(StylesChanged && sti.(*Styles) == &CustomStyles) }, }}, {"sep-file", ki.BlankProp{}}, {"OpenJSON", ki.Props{ "label": "Open from file", "desc": "You can save and open styles to / from files to share, experiment, transfer, etc", "shortcut": gi.KeyFunMenuOpen, "Args": ki.PropSlice{ {"File Name", ki.Props{ "ext": ".json", }}, }, }}, {"SaveJSON", ki.Props{ "label": "Save to file", "desc": "You can save and open styles to / from files to share, experiment, transfer, etc", "shortcut": gi.KeyFunMenuSaveAs, "Args": ki.PropSlice{ {"File Name", ki.Props{ "ext": ".json", }}, }, }}, }}, {"Edit", "Copy Cut Paste Dupe"}, {"Window", "Windows"}, }, "ToolBar": ki.PropSlice{ {"Add", ki.Props{ "desc": "Add a new style to the list.", "icon": "plus", "updtfunc": func(sti interface{}, act *gi.Action) { act.SetActiveStateUpdt(sti.(*Styles) == &CustomStyles) }, }}, {"SavePrefs", ki.Props{ "desc": "saves styles to app prefs directory, in file hi_styles.json, which will be loaded automatically at startup into your CustomStyles.", "icon": "file-save", "updtfunc": func(sti interface{}, act *gi.Action) { act.SetActiveStateUpdt(StylesChanged && sti.(*Styles) == &CustomStyles) }, }}, {"sep-file", ki.BlankProp{}}, {"OpenJSON", ki.Props{ "label": "Open from file", "icon": "file-open", "desc": "You can save and open styles to / from files to share, experiment, transfer, etc", "Args": ki.PropSlice{ {"File Name", ki.Props{ "ext": ".json", }}, }, }}, {"SaveJSON", ki.Props{ "label": "Save to file", "icon": "file-save", "desc": "You can save and open styles to / from files to share, experiment, transfer, etc", "Args": ki.PropSlice{ {"File Name", ki.Props{ "ext": ".json", }}, }, }}, {"sep-std", ki.BlankProp{}}, {"ViewStd", ki.Props{ "desc": `Shows the standard styles that are compiled into the program (from <a href="https://github.com/alecthomas/chroma">github.com/alecthomas/chroma</a>). Save a style from there and load it into custom as a starting point for creating a variant of an existing style.`, "confirm": true, "updtfunc": func(sti interface{}, act *gi.Action) { act.SetActiveStateUpdt(sti.(*Styles) != &StdStyles) }, }}, }, }
StylesProps define the ToolBar and MenuBar for view
Functions ¶
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 InitHiTagNames ¶
func InitHiTagNames()
InitHiTagNames initializes the HiTagNames -- called during overall init
func MergeAvailStyles ¶
func MergeAvailStyles()
MergeAvailStyles updates AvailStyles as combination of std and custom styles
Types ¶
type HiTags ¶
type HiTags int
HiTags are highlighting tags -- one-to-one with chroma.Token but kept in simple sequential order for use as an enum -- with enums and the string interface the raw numbers don't matter so sequential is best
const ( // Used as an EOF marker / nil token EOFType HiTags = iota // Default background style. Background // Line numbers in output. LineNumbers // Line numbers in output when in table. LineNumbersTable // Line higlight style. LineHighlight // Line numbers table wrapper style. LineTable // Line numbers table TD wrapper style. LineTableTD // Input that could not be tokenised. Error // Other is used by the Delegate lexer to indicate which tokens should be handled by the delegate. Other // No highlighting. None // Keywords. Keyword KeywordConstant KeywordDeclaration KeywordNamespace KeywordPseudo KeywordReserved KeywordType // Names. Name NameAttribute NameBuiltin NameBuiltinPseudo NameClass NameConstant NameDecorator NameEntity NameException NameFunction NameFunctionMagic NameKeyword NameLabel NameNamespace NameOperator NameOther NamePseudo NameProperty NameTag NameVariable NameVariableAnonymous NameVariableClass NameVariableGlobal NameVariableInstance NameVariableMagic // Literals. Literal LiteralDate LiteralOther // Literal Strings. LiteralString LiteralStringAffix LiteralStringAtom LiteralStringBacktick LiteralStringBoolean LiteralStringChar LiteralStringDelimiter LiteralStringDoc LiteralStringDouble LiteralStringEscape LiteralStringHeredoc LiteralStringInterpol LiteralStringName LiteralStringOther LiteralStringRegex LiteralStringSingle LiteralStringSymbol // Literals Numbers. LiteralNumber LiteralNumberBin LiteralNumberFloat LiteralNumberHex LiteralNumberInteger LiteralNumberIntegerLong LiteralNumberOct // Operators. Operator OperatorWord // Punctuation. Punctuation // Comments. Comment CommentHashbang CommentMultiline CommentSingle CommentSpecial // Preprocessor "comments". CommentPreproc CommentPreprocFile // Generic tokens. Generic GenericDeleted GenericEmph GenericError GenericHeading GenericInserted GenericOutput GenericPrompt GenericStrong GenericSubheading GenericTraceback GenericUnderline // Text. Text TextWhitespace TextSymbol TextPunctuation // Our own custom types SpellErr HiTagsN )
HiTags values -- MUST keep this in correspondence with alecthomas/chroma for interoperability
func HiTagFromChroma ¶
HiTagFromChroma returns a HiTags tag from a chroma tag
func (HiTags) ClassName ¶
ClassName returns the . prefixed CSS classname of the tag style for styling, a CSS property should exist with this name
func (*HiTags) FromChroma ¶
FromChroma converts a chroma.TokenType to a HiTags type
func (*HiTags) FromString ¶
func (HiTags) InCategory ¶
func (HiTags) InSubCategory ¶
func (HiTags) MarshalJSON ¶
func (HiTags) MarshalText ¶
map keys require text marshaling:
func (HiTags) SubCategory ¶
func (*HiTags) UnmarshalJSON ¶
func (*HiTags) UnmarshalText ¶
type Style ¶
type Style map[HiTags]StyleEntry
Style is a full style map of styles for different HiTags tag values
func AvailStyle ¶
AvailStyle returns a style by name from the AvailStyles list -- if not found default is used as a fallback
func (*Style) FromChroma ¶
FromChroma copies styles from chroma
func (Style) Tag ¶
func (hs Style) Tag(tag HiTags) 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 HiTags) StyleEntry
TagRaw returns a StyleEntry for given tag without any inheritance of anything will be IsZero if not defined for this style
type StyleEntry ¶
type StyleEntry struct {
Color gi.Color `desc:"text color"`
Background gi.Color `desc:"background color"`
Border gi.Color `view:"-" desc:"border color? not sure what this is -- not really used"`
Bold Trilean `desc:"bold font"`
Italic Trilean `desc:"italic font"`
Underline Trilean `desc:"underline"`
NoInherit bool `desc:"don't inherit these settings from sub-category or category levels -- otherwise everthing with a Pass is inherited"`
}
StyleEntry is one value in the map of highilight style values
func StyleEntryFromChroma ¶
func StyleEntryFromChroma(ce chroma.StyleEntry) StyleEntry
StyleEntryFromChroma returns a new style entry from corresponding chroma version
func (*StyleEntry) FromChroma ¶
func (he *StyleEntry) FromChroma(ce chroma.StyleEntry)
FromChroma copies styles from chroma
func (StyleEntry) Inherit ¶
func (s StyleEntry) Inherit(ancestors ...StyleEntry) StyleEntry
Inherit styles from ancestors.
Ancestors should be provided from oldest, furthest away to newest, closest.
func (StyleEntry) IsZero ¶
func (s StyleEntry) IsZero() bool
func (StyleEntry) String ¶
func (se StyleEntry) String() string
func (StyleEntry) Sub ¶
func (s 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) ToProps ¶
func (se StyleEntry) ToProps() ki.Props
ToProps converts StyleEntry to ki.Props attributes.
type Styles ¶
Styles is a collection of styles
var AvailStyles Styles
AvailStyles are all highlighting styles
var StdStyles Styles
StdStyles are the styles from chroma package
func (*Styles) FromChroma ¶
FromChroma copies styles from chroma
func (*Styles) OpenPrefs ¶
OpenPrefs opens Styles from App standard prefs directory, using PrefsStylesFileName
type Trilean ¶
type Trilean uint8
Trilean value for StyleEntry value inheritance.