Documentation
¶
Overview ¶
Package syntax provides syntax highlighting for source code using the same approach as Orbiton. It tokenizes input via text/scanner, classifies tokens into kinds, and wraps them in color tags that can be converted to ANSI escape codes by the vt package.
Theme selection is supported via the O_THEME (or THEME) environment variable.
Index ¶
- Variables
- func AddAndRemoveKeywords(addAndDel ...[]string)
- func AddKeywords(kws []string)
- func AddKeywordsAsUppercase(xs []string)
- func AdjustKeywords(m mode.Mode)
- func Annotate(src []byte, a Annotator, m mode.Mode) (annotate.Annotations, error)
- func AsText(src []byte, m mode.Mode, options ...Option) ([]byte, error)
- func CatBytes(sourceCodeData []byte, o *vt.TextOutput) error
- func ClearKeywords()
- func NewScanner(src []byte) *scanner.Scanner
- func NewScannerReader(r io.Reader) *scanner.Scanner
- func Print(s *scanner.Scanner, w io.Writer, p Printer, m mode.Mode) error
- func RemoveKeywords(kws []string)
- func SetDefaultTextConfigFromEnv()
- func SetKeywords(addAndDel ...[]string)
- type Annotator
- type Kind
- type Option
- type Printer
- type TextAnnotator
- type TextConfig
- func LightTextConfigByName(name string) TextConfig
- func NewDarkBlueEditTextConfig() TextConfig
- func NewDarkVSTextConfig() TextConfig
- func NewDefaultTextConfig() TextConfig
- func NewLightBlueEditTextConfig() TextConfig
- func NewLightVSTextConfig() TextConfig
- func NewLitmusTextConfig() TextConfig
- func NewNoColorTextConfig() TextConfig
- func NewOrbTextConfig() TextConfig
- func NewPinetreeTextConfig() TextConfig
- func NewRedBlackTextConfig() TextConfig
- func NewSynthwaveTextConfig() TextConfig
- func NewTealTextConfig() TextConfig
- func NewZuluTextConfig() TextConfig
- func TextConfigByName(name string) TextConfig
- func TextConfigFromEnv() TextConfig
- type TextPrinter
Constants ¶
This section is empty.
Variables ¶
var DefaultTextConfig = TextConfig{ AndOr: "red", AngleBracket: "red", AssemblyEnd: "lightyellow", Class: "white", Comment: "darkgray", Decimal: "red", Dollar: "white", Keyword: "red", Literal: "white", Mut: "magenta", Plaintext: "white", Private: "red", Protected: "red", Public: "red", Punctuation: "red", Self: "magenta", Star: "white", Static: "lightyellow", String: "lightwhite", Tag: "white", TextAttrName: "white", TextAttrValue: "white", TextTag: "white", Type: "white", Whitespace: "", }
DefaultTextConfig provides color names matching Orbiton's default theme.
var ( // Keywords contains the default syntax highlighting keywords Keywords = map[string]struct{}{}/* 190 elements not displayed */ )
Functions ¶
func AddAndRemoveKeywords ¶
func AddAndRemoveKeywords(addAndDel ...[]string)
AddAndRemoveKeywords adds then removes keywords from the global Keywords map.
func AddKeywords ¶
func AddKeywords(kws []string)
AddKeywords adds keywords to the global Keywords map.
func AddKeywordsAsUppercase ¶
func AddKeywordsAsUppercase(xs []string)
AddKeywordsAsUppercase adds uppercased keywords to the global Keywords map.
func AdjustKeywords ¶
AdjustKeywords applies per-language keyword adjustments, matching Orbiton's logic.
func CatBytes ¶
func CatBytes(sourceCodeData []byte, o *vt.TextOutput) error
CatBytes highlights sourceCodeData and writes it to stdout via the given TextOutput.
func NewScanner ¶
NewScanner returns a scanner.Scanner configured for syntax highlighting.
func NewScannerReader ¶
NewScannerReader returns a scanner.Scanner configured for syntax highlighting from r.
func RemoveKeywords ¶
func RemoveKeywords(kws []string)
RemoveKeywords removes keywords from the global Keywords map.
func SetDefaultTextConfigFromEnv ¶
func SetDefaultTextConfigFromEnv()
SetDefaultTextConfigFromEnv updates DefaultTextConfig based on O_THEME.
func SetKeywords ¶
func SetKeywords(addAndDel ...[]string)
SetKeywords clears, then adds/removes keywords.
Types ¶
type Annotator ¶
type Annotator interface {
Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)
}
Annotator produces syntax highlighting annotations.
type TextAnnotator ¶
type TextAnnotator TextConfig
TextAnnotator wraps TextConfig to implement Annotator.
func (TextAnnotator) Annotate ¶
func (a TextAnnotator) Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)
Annotate returns an annotation for the given token.
type TextConfig ¶
type TextConfig struct {
AndOr string
AngleBracket string
AssemblyEnd string
Class string
Comment string
Decimal string
Dollar string
Keyword string
Literal string
Mut string
Plaintext string
Private string
Protected string
Public string
Punctuation string
Self string
Star string
Static string
String string
Tag string
TextAttrName string
TextAttrValue string
TextTag string
Type string
Whitespace string
}
TextConfig maps token kinds to color tag names used by vt.TextOutput.DarkTags.
func LightTextConfigByName ¶
func LightTextConfigByName(name string) TextConfig
LightTextConfigByName returns the TextConfig for the given theme name, preferring the light variant when one exists.
func NewDarkBlueEditTextConfig ¶
func NewDarkBlueEditTextConfig() TextConfig
func NewDarkVSTextConfig ¶
func NewDarkVSTextConfig() TextConfig
func NewDefaultTextConfig ¶
func NewDefaultTextConfig() TextConfig
func NewLightBlueEditTextConfig ¶
func NewLightBlueEditTextConfig() TextConfig
func NewLightVSTextConfig ¶
func NewLightVSTextConfig() TextConfig
func NewLitmusTextConfig ¶
func NewLitmusTextConfig() TextConfig
func NewNoColorTextConfig ¶
func NewNoColorTextConfig() TextConfig
func NewOrbTextConfig ¶
func NewOrbTextConfig() TextConfig
func NewPinetreeTextConfig ¶
func NewPinetreeTextConfig() TextConfig
func NewRedBlackTextConfig ¶
func NewRedBlackTextConfig() TextConfig
func NewSynthwaveTextConfig ¶
func NewSynthwaveTextConfig() TextConfig
func NewTealTextConfig ¶
func NewTealTextConfig() TextConfig
func NewZuluTextConfig ¶
func NewZuluTextConfig() TextConfig
func TextConfigByName ¶
func TextConfigByName(name string) TextConfig
TextConfigByName returns the TextConfig for the given theme name. If the name is not recognized, the default TextConfig is returned.
func TextConfigFromEnv ¶
func TextConfigFromEnv() TextConfig
TextConfigFromEnv returns the TextConfig selected by the O_THEME (or THEME) environment variable, falling back to the default. If NO_COLOR is set, an empty TextConfig (no colors) is returned. If O_LIGHT is set, light theme variants are preferred.
func (TextConfig) GetClass ¶
func (c TextConfig) GetClass(kind Kind) string
GetClass returns the color tag name for a given token kind.
type TextPrinter ¶
type TextPrinter TextConfig
TextPrinter wraps TextConfig to implement Printer, emitting color tags.