histyle

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 14, 2024 License: BSD-3-Clause Imports: 23 Imported by: 0

Documentation

Overview

Package histyle 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 pi and cannot be imported there; is imported in texteditor.

Index

Constants

This section is empty.

Variables

View Source
var ChromaToTokensMap map[chroma.TokenType]token.Tokens

ChromaToTokensMap maps from chroma.TokenType to Tokens -- built from opposite map

View Source
var CustomStyles = Styles{}

CustomStyles are user's special styles

View Source
var Properties = map[token.Tokens]map[string]any{
	token.TextSpellErr: {
		"text-decoration": 1 << uint32(styles.DecoDottedUnderline),
	},
}

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

View Source
var SettingsStylesFilename = "hi_styles.json"

SettingsStylesFilename is the name of the preferences file in App prefs directory for saving / loading the custom styles

View Source
var StyleDefault = core.HiStyleName("emacs")

StyleDefault is the default highlighting style name -- can set this to whatever you want

View Source
var StyleNames []string

StyleNames are all the names of all the available highlighting styles

View Source
var StylesChanged = false

StylesChanged is used for gui updating while editing

View Source
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 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 MergeAvailStyles

func MergeAvailStyles()

MergeAvailStyles updates AvailStyles as combination of std and custom styles

func TokenFromChroma

func TokenFromChroma(ct chroma.TokenType) token.Tokens

FromChroma converts a chroma.TokenType to a parse token.Tokens

func TokenToChroma

func TokenToChroma(tok token.Tokens) chroma.TokenType

TokenToChroma converts to a chroma.TokenType

func View

func View(st *Styles)

View opens a view of highlighting styles

Types

type Style

type Style map[token.Tokens]*StyleEntry

Style is a full style map of styles for different token.Tokens tag values

func AvailableStyle added in v0.0.10

func AvailableStyle(nm core.HiStyleName) *Style

AvailableStyle returns a style by name from the AvailStyles list -- if not found default is used as a fallback

func (*Style) CopyFrom

func (hs *Style) CopyFrom(ss *Style)

CopyFrom copies a style from source style

func (Style) OpenJSON

func (hs Style) OpenJSON(filename core.Filename) error

Open hi style from a JSON-formatted file.

func (Style) SaveJSON

func (hs Style) SaveJSON(filename core.Filename) error

Save hi style to a JSON-formatted file.

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) ToCSS

func (hs Style) ToCSS() map[token.Tokens]string

ToCSS generates a CSS style sheet for this style, by token.Tokens tag

func (Style) ToProperties added in v0.0.10

func (hs Style) ToProperties() map[string]any

ToProperties generates a list of key-value properties for this style.

type StyleEntry

type StyleEntry struct {

	// text color
	Color color.RGBA

	// background color
	Background color.RGBA

	// border color? not sure what this is -- not really used
	Border color.RGBA `view:"-"`

	// bold font
	Bold Trilean

	// italic font
	Italic Trilean

	// underline
	Underline Trilean

	// don't inherit these settings from sub-category or category levels -- otherwise everything with a Pass is inherited
	NoInherit bool
}

StyleEntry is one value in the map of highlight style values

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) Norm

func (se *StyleEntry) Norm()

Norm normalizes the colors of the style entry such that they have consistent chromas and tones that guarantee sufficient text contrast.

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) ToProperties added in v0.0.10

func (se StyleEntry) ToProperties() map[string]any

ToProperties converts the StyleEntry to key-value properties.

type Styles

type Styles map[string]*Style

Styles is a collection of styles

var AvailableStyles Styles

AvailableStyles are all highlighting styles

var StandardStyles Styles

StandardStyles are the styles from chroma package

func (*Styles) Add

func (hs *Styles) Add() *Style

Add adds a new style to the list

func (*Styles) CopyFrom

func (hs *Styles) CopyFrom(os Styles)

CopyFrom copies styles from another collection

func (*Styles) Names

func (hs *Styles) Names() []string

Names outputs names of styles in collection

func (*Styles) OpenDefaults

func (hs *Styles) OpenDefaults() error

OpenDefaults opens the default highlighting styles (from chroma originally) These are encoded as an embed from defaults.histys

func (*Styles) OpenJSON

func (hs *Styles) OpenJSON(filename core.Filename) error

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 added in v0.0.10

func (hs *Styles) OpenSettings() error

OpenSettings opens Styles from Cogent Core standard prefs directory, using SettingsStylesFilename

func (*Styles) SaveAll

func (hs *Styles) SaveAll(dir core.Filename)

SaveAll saves all styles individually to chosen directory

func (*Styles) SaveJSON

func (hs *Styles) SaveJSON(filename core.Filename) error

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 added in v0.0.10

func (hs *Styles) SaveSettings() error

SaveSettings saves Styles to Cogent Core standard prefs directory, using SettingsStylesFilename

func (*Styles) ViewStandard added in v0.0.10

func (hs *Styles) ViewStandard()

ViewStandard shows the standard styles that are compiled into the program via chroma package

type Trilean

type Trilean int32 //enums:enum

Trilean value for StyleEntry value inheritance.

const (
	Pass Trilean = iota
	Yes
	No
)
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) Desc

func (i Trilean) Desc() string

Desc returns the description of the Trilean value.

func (Trilean) Int64

func (i Trilean) Int64() int64

Int64 returns the Trilean value as an int64.

func (Trilean) MarshalText

func (i Trilean) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface.

func (Trilean) Prefix

func (t Trilean) Prefix(s string) string

func (*Trilean) SetInt64

func (i *Trilean) SetInt64(in int64)

SetInt64 sets the Trilean value from an int64.

func (*Trilean) SetString

func (i *Trilean) SetString(s string) error

SetString sets the Trilean value from its string representation, and returns an error if the string is invalid.

func (Trilean) String

func (i Trilean) String() string

String returns the string representation of this Trilean value.

func (*Trilean) UnmarshalText

func (i *Trilean) UnmarshalText(text []byte) error

UnmarshalText implements the encoding.TextUnmarshaler interface.

func (Trilean) Values

func (i Trilean) Values() []enums.Enum

Values returns all possible values for the type Trilean.

type Value

type Value struct {
	views.ValueBase[*core.Button]
}

Value represents a core.HiStyleName with a button.

func (*Value) Config added in v0.0.8

func (v *Value) Config()

func (*Value) ConfigDialog

func (v *Value) ConfigDialog(d *core.Body) (bool, func())

func (*Value) Update added in v0.0.8

func (v *Value) Update()

Jump to

Keyboard shortcuts

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