Documentation
¶
Overview ¶
Package syntax provides syntax highlighting for source code, using the same approach as Orbiton. It tokenizes input with 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 CheckMultiLineString(trimmedLine string, inCodeBlock bool) (bool, bool)
- func ClearKeywords()
- func NewScanner(src []byte) *scanner.Scanner
- func NewScannerReader(src 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)
- func SingleLineCommentMarker(m mode.Mode) string
- type Annotator
- type Kind
- type Option
- type Printer
- type QuoteState
- func (q *QuoteState) Backtick() int
- func (q *QuoteState) BraCount() int
- func (q *QuoteState) ContainsMultiLineComments() bool
- func (q *QuoteState) HasSingleLineComment() bool
- func (q *QuoteState) InMultiLineComment() bool
- func (q *QuoteState) None() bool
- func (q *QuoteState) ParBraCount(line string) (int, int)
- func (q *QuoteState) ParCount() int
- func (q *QuoteState) Process(line string) (rune, rune)
- func (q *QuoteState) ProcessRune(r, prevRune, prevPrevRune rune)
- func (q *QuoteState) ResetVim()
- func (q *QuoteState) SetBacktick(v int)
- func (q *QuoteState) SetBraCount(v int)
- func (q *QuoteState) SetDoubleQuote(v int)
- func (q *QuoteState) SetHasSingleLineComment(v bool)
- func (q *QuoteState) SetParCount(v int)
- func (q *QuoteState) SetSingleQuote(v int)
- func (q *QuoteState) SetStartedMultiLineString(v bool)
- func (q *QuoteState) SetStoppedMultiLineComment(v bool)
- func (q *QuoteState) StartedMultiLineComment() bool
- func (q *QuoteState) StartedMultiLineString() bool
- func (q *QuoteState) StoppedMultiLineComment() bool
- 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 class names that match the color names of textoutput tags: https://github.com/xyproto/textoutput
var ( // Keywords contains the default syntax highlighting keywords Keywords = map[string]struct{}{}/* 190 elements not displayed */ )
Functions ¶
func AddAndRemoveKeywords ¶
func AddAndRemoveKeywords(addAndDel ...[]string)
AddAndRemoveKeywords first adds and then removes keywords.
func AddKeywords ¶
func AddKeywords(kws []string)
AddKeywords adds the given keywords so that they will be syntax highlighted.
func AddKeywordsAsUppercase ¶
func AddKeywordsAsUppercase(xs []string)
AddKeywordsAsUppercase adds uppercased versions of the given keywords.
func AdjustKeywords ¶
AdjustKeywords contains per-language adjustments to highlighting of keywords
func AsText ¶
AsText converts source code into a Text-highlighted version. It accepts optional configuration parameters to control rendering.
func CatBytes ¶
func CatBytes(sourceCodeData []byte, o *vt.TextOutput) error
CatBytes highlights sourceCodeData and writes it to stdout via the given TextOutput.
func CheckMultiLineString ¶ added in v1.13.0
CheckMultiLineString detects and updates the inCodeBlock state. For languages like Nim, Mojo, Python and Starlark.
func NewScanner ¶
NewScanner is a helper that takes a []byte src, wraps it in a reader and creates a Scanner.
func NewScannerReader ¶
NewScannerReader takes a reader src and creates a Scanner.
func RemoveKeywords ¶
func RemoveKeywords(kws []string)
RemoveKeywords removes keywords that should not be syntax highlighted.
func SetDefaultTextConfigFromEnv ¶
func SetDefaultTextConfigFromEnv()
SetDefaultTextConfigFromEnv will update DefaultTextConfig based on O_THEME.
func SetKeywords ¶
func SetKeywords(addAndDel ...[]string)
SetKeywords clears, then adds/removes keywords.
func SingleLineCommentMarker ¶ added in v1.13.0
SingleLineCommentMarker returns the string that starts a single-line comment for the given language mode.
Types ¶
type Annotator ¶
type Annotator interface {
Annotate(start int, kind Kind, tokText string) (*annotate.Annotation, error)
}
Annotator produces syntax highlighting annotations.
type Kind ¶
type Kind uint8
Kind represents a syntax highlighting kind (class) which will be assigned to tokens. A syntax highlighting scheme (style) maps text style properties to each token kind.
type Option ¶
type Option func(*TextConfig)
Option is a type of the function that can modify one or more of the options in the TextConfig structure.
type Printer ¶
Printer implements an interface to render highlighted output (see TextPrinter for the implementation of this interface).
type QuoteState ¶ added in v1.13.0
type QuoteState struct {
// contains filtered or unexported fields
}
QuoteState keeps track of if we're within a multi-line comment, single quotes, double quotes or multi-line quotes. Single line comments are not kept track of in the same way, they can be detected just by checking the current line. If one of the ints are > 0, the other ints should not be added to. MultiLine comments (/* ... */) are special. This could be a flag int instead
func NewQuoteState ¶ added in v1.13.0
func NewQuoteState(singleLineCommentMarker string, m mode.Mode, ignoreSingleQuotes bool) (*QuoteState, error)
NewQuoteState takes a singleLineCommentMarker (such as "//" or "#") and returns a pointer to a new QuoteState struct
func (*QuoteState) Backtick ¶ added in v1.13.0
func (q *QuoteState) Backtick() int
Backtick returns the backtick nesting count
func (*QuoteState) BraCount ¶ added in v1.13.0
func (q *QuoteState) BraCount() int
BraCount returns the square bracket nesting count
func (*QuoteState) ContainsMultiLineComments ¶ added in v1.13.0
func (q *QuoteState) ContainsMultiLineComments() bool
ContainsMultiLineComments returns true if the line both starts and stops multi-line comments
func (*QuoteState) HasSingleLineComment ¶ added in v1.13.0
func (q *QuoteState) HasSingleLineComment() bool
HasSingleLineComment returns true if this line has a single-line comment
func (*QuoteState) InMultiLineComment ¶ added in v1.13.0
func (q *QuoteState) InMultiLineComment() bool
InMultiLineComment returns true if we're currently within a multi-line comment
func (*QuoteState) None ¶ added in v1.13.0
func (q *QuoteState) None() bool
None returns true if we're not within ', "", `, /* ... */ or a single-line quote right now
func (*QuoteState) ParBraCount ¶ added in v1.13.0
func (q *QuoteState) ParBraCount(line string) (int, int)
ParBraCount will count the parenthesis and square brackets for a single line while skipping comments and multi-line strings and without modifying the QuoteState.
func (*QuoteState) ParCount ¶ added in v1.13.0
func (q *QuoteState) ParCount() int
ParCount returns the parenthesis nesting count
func (*QuoteState) Process ¶ added in v1.13.0
func (q *QuoteState) Process(line string) (rune, rune)
Process takes a line of text and modifies the current quote state accordingly, depending on which runes are encountered.
func (*QuoteState) ProcessRune ¶ added in v1.13.0
func (q *QuoteState) ProcessRune(r, prevRune, prevPrevRune rune)
ProcessRune is for processing single runes
func (*QuoteState) ResetVim ¶ added in v1.13.0
func (q *QuoteState) ResetVim()
ResetVim resets quote state fields for Vim comment handling
func (*QuoteState) SetBacktick ¶ added in v1.13.0
func (q *QuoteState) SetBacktick(v int)
SetBacktick sets the backtick nesting count
func (*QuoteState) SetBraCount ¶ added in v1.13.0
func (q *QuoteState) SetBraCount(v int)
SetBraCount sets the square bracket nesting count
func (*QuoteState) SetDoubleQuote ¶ added in v1.13.0
func (q *QuoteState) SetDoubleQuote(v int)
SetDoubleQuote sets the double quote nesting count
func (*QuoteState) SetHasSingleLineComment ¶ added in v1.13.0
func (q *QuoteState) SetHasSingleLineComment(v bool)
SetHasSingleLineComment sets the single-line comment flag
func (*QuoteState) SetParCount ¶ added in v1.13.0
func (q *QuoteState) SetParCount(v int)
SetParCount sets the parenthesis nesting count
func (*QuoteState) SetSingleQuote ¶ added in v1.13.0
func (q *QuoteState) SetSingleQuote(v int)
SetSingleQuote sets the single quote nesting count
func (*QuoteState) SetStartedMultiLineString ¶ added in v1.13.0
func (q *QuoteState) SetStartedMultiLineString(v bool)
SetStartedMultiLineString sets the started multi-line string flag
func (*QuoteState) SetStoppedMultiLineComment ¶ added in v1.13.0
func (q *QuoteState) SetStoppedMultiLineComment(v bool)
SetStoppedMultiLineComment sets the stopped multi-line comment flag
func (*QuoteState) StartedMultiLineComment ¶ added in v1.13.0
func (q *QuoteState) StartedMultiLineComment() bool
StartedMultiLineComment returns true if this line started a multi-line comment
func (*QuoteState) StartedMultiLineString ¶ added in v1.13.0
func (q *QuoteState) StartedMultiLineString() bool
StartedMultiLineString returns true if this line started a multi-line string
func (*QuoteState) StoppedMultiLineComment ¶ added in v1.13.0
func (q *QuoteState) StoppedMultiLineComment() bool
StoppedMultiLineComment returns true if this line stopped a multi-line comment
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 holds the Text class configuration to be used by annotators when highlighting code.
func LightTextConfigByName ¶
func LightTextConfigByName(name string) TextConfig
LightTextConfigByName will return the TextConfig for the given theme name, preferring the light variant when one exists.
func NewDarkBlueEditTextConfig ¶
func NewDarkBlueEditTextConfig() TextConfig
NewDarkBlueEditTextConfig returns the TextConfig for the dark "blueedit" theme.
func NewDarkVSTextConfig ¶
func NewDarkVSTextConfig() TextConfig
NewDarkVSTextConfig returns the TextConfig for the dark "vs" theme.
func NewDefaultTextConfig ¶
func NewDefaultTextConfig() TextConfig
NewDefaultTextConfig returns the TextConfig for the default Orbiton theme.
func NewLightBlueEditTextConfig ¶
func NewLightBlueEditTextConfig() TextConfig
NewLightBlueEditTextConfig returns the TextConfig for the light "blueedit" theme.
func NewLightVSTextConfig ¶
func NewLightVSTextConfig() TextConfig
NewLightVSTextConfig returns the TextConfig for the light "vs" theme.
func NewLitmusTextConfig ¶
func NewLitmusTextConfig() TextConfig
NewLitmusTextConfig returns the TextConfig for the "litmus" theme.
func NewNoColorTextConfig ¶
func NewNoColorTextConfig() TextConfig
NewNoColorTextConfig returns an empty TextConfig with no colors.
func NewOrbTextConfig ¶
func NewOrbTextConfig() TextConfig
NewOrbTextConfig returns the TextConfig for the "orb" theme.
func NewPinetreeTextConfig ¶
func NewPinetreeTextConfig() TextConfig
NewPinetreeTextConfig returns the TextConfig for the "pinetree" theme.
func NewRedBlackTextConfig ¶
func NewRedBlackTextConfig() TextConfig
NewRedBlackTextConfig returns the TextConfig for the "redblack" theme.
func NewSynthwaveTextConfig ¶
func NewSynthwaveTextConfig() TextConfig
NewSynthwaveTextConfig returns the TextConfig for the "synthwave" theme.
func NewTealTextConfig ¶
func NewTealTextConfig() TextConfig
NewTealTextConfig returns the TextConfig for the "teal" theme.
func NewZuluTextConfig ¶
func NewZuluTextConfig() TextConfig
NewZuluTextConfig returns the TextConfig for the "zulu" theme.
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 will return the TextConfig selected by the O_THEME (or default) 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 set class for a given token Kind.
type TextPrinter ¶
type TextPrinter TextConfig
TextPrinter implements Printer interface and is used to produce Text-based highligher.