syntax highlighting configs are back... make it work again!

This commit is contained in:
Felix Angell 2021-01-10 21:59:31 +00:00
parent 68c3a94c12
commit 88696f37c9
3 changed files with 159 additions and 133 deletions

View File

@ -15,6 +15,7 @@ import (
"math" "math"
"os" "os"
"path" "path"
"regexp"
"runtime" "runtime"
"strings" "strings"
"time" "time"
@ -1205,15 +1206,18 @@ type syntaxRuneInfo struct {
// dimensions of the last character we rendered // dimensions of the last character we rendered
var lastCharW, lastCharH int var lastCharW, lastCharH int
func lexFindMatches(matches *map[int]syntaxRuneInfo, currLine string, toMatch map[string]bool, bg uint32, fg uint32) { func lexFindMatches(matches *map[int]syntaxRuneInfo, currLine string, toMatch []string, bg uint32, fg uint32) {
lexer := lex.New(currLine) lexer := lex.New(currLine)
tokenStream := lexer.Tokenize() tokenStream := lexer.Tokenize()
for _, tok := range tokenStream { for _, tok := range tokenStream {
if _, ok := toMatch[tok.Lexeme]; ok { // hashset for match.
for _, ms := range toMatch {
if strings.Compare(ms, tok.Lexeme) == 0 {
(*matches)[tok.Start] = syntaxRuneInfo{bg, fg, len(tok.Lexeme)} (*matches)[tok.Start] = syntaxRuneInfo{bg, fg, len(tok.Lexeme)}
} }
} }
} }
}
type charColouring struct { type charColouring struct {
bg uint32 bg uint32
@ -1267,9 +1271,11 @@ func (b *Buffer) syntaxHighlightLine(currLine string) map[int]syntaxRuneInfo {
for syntaxIndex, syntax := range subjects { for syntaxIndex, syntax := range subjects {
if syntax.Pattern != "" { if syntax.Pattern != "" {
for charIndex := 0; charIndex < len(currLine); charIndex++ { for charIndex := 0; charIndex < len(currLine); charIndex++ {
a := string(currLine[charIndex:]) a := currLine[charIndex:]
matched := syntax.CompiledPattern.FindStringIndex(a) patt := lazyCompileRegExPattern(syntax.Pattern)
matched := patt.FindStringIndex(a)
if matched != nil { if matched != nil {
if _, ok := matches[charIndex]; !ok { if _, ok := matches[charIndex]; !ok {
matchedStrLen := matched[1] - matched[0] matchedStrLen := matched[1] - matched[0]
@ -1288,7 +1294,7 @@ func (b *Buffer) syntaxHighlightLine(currLine string) map[int]syntaxRuneInfo {
} }
} else { } else {
colouring := colours[syntaxIndex] colouring := colours[syntaxIndex]
lexFindMatches(&matches, currLine, syntax.MatchList, colouring.bg, colouring.fg) lexFindMatches(&matches, currLine, syntax.Match, colouring.bg, colouring.fg)
} }
} }
@ -1300,6 +1306,21 @@ func (b *Buffer) syntaxHighlightLine(currLine string) map[int]syntaxRuneInfo {
return matches return matches
} }
var patts = map[string]*regexp.Regexp{}
// FIXME
func lazyCompileRegExPattern(pattern string) *regexp.Regexp {
if cachedPattern, ok := patts[pattern]; ok {
return cachedPattern
}
compiledPattern, err := regexp.Compile(pattern)
if err != nil {
panic(err)
}
patts[pattern] = compiledPattern
return compiledPattern
}
func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) { func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) {
x, y := b.GetPos() x, y := b.GetPos()
w, h := b.GetSize() w, h := b.GetSize()

View File

@ -3,8 +3,8 @@ package cfg
import ( import (
"errors" "errors"
"log" "log"
"regexp"
"strconv" "strconv"
"strings"
) )
type PhiEditorConfig struct { type PhiEditorConfig struct {
@ -14,15 +14,25 @@ type PhiEditorConfig struct {
Theme *ThemeConfig `toml:"theme"` Theme *ThemeConfig `toml:"theme"`
Associations map[string]FileAssociations `toml:"file_associations"` Associations map[string]FileAssociations `toml:"file_associations"`
Commands map[string]Command `toml:"commands"` Commands map[string]Command `toml:"commands"`
LanguageAssociations map[string]*LanguageSyntaxConfig
associations map[string]*LanguageSyntaxConfig
} }
// GetSyntaxConfig returns a pointer to the parsed // GetSyntaxConfig returns a pointer to the parsed
// syntax language file for the given file extension // syntax language file for the given file extension
// e.g. what syntax def we need for a .cpp file or a .h file // e.g. what syntax def we need for a .cpp file or a .h file
func (p *PhiEditorConfig) GetSyntaxConfig(ext string) (*LanguageSyntaxConfig, error) { func (p *PhiEditorConfig) GetSyntaxConfig(ext string) (*LanguageSyntaxConfig, error) {
if val, ok := p.associations[ext]; ok { // fixme relationship is wrong way round so for now we have to iterate over keys.. very st upid
var theKey string
for key, assoc := range p.Associations {
for _, assocExt := range assoc.Extensions {
if strings.Compare(ext, assocExt) == 0 {
theKey = key
}
}
}
if val, ok := p.LanguageAssociations[theKey]; ok {
return val, nil return val, nil
} }
return nil, errors.New("no language for extension '" + ext + "'") return nil, errors.New("no language for extension '" + ext + "'")
@ -37,9 +47,6 @@ type SyntaxCriteria struct {
Background uint32 `toml:"background"` Background uint32 `toml:"background"`
Match []string `toml:"match"` Match []string `toml:"match"`
Pattern string `toml:"pattern"` Pattern string `toml:"pattern"`
CompiledPattern *regexp.Regexp
MatchList map[string]bool
} }
type Command struct { type Command struct {
@ -196,8 +203,11 @@ func NewDefaultConfig() *PhiEditorConfig {
"md": {Extensions: []string{".md", ".markdown"}}, "md": {Extensions: []string{".md", ".markdown"}},
"toml": {Extensions: []string{".toml"}}, "toml": {Extensions: []string{".toml"}},
}, },
LanguageAssociations: map[string]*LanguageSyntaxConfig{
// TODO syntax defaults "go": GoConfig(),
associations: map[string]*LanguageSyntaxConfig{}, "c": CConfig(),
"md": MarkdownConfig(),
"toml": TOMLConfig(),
},
} }
} }

View File

@ -4,119 +4,114 @@ type LanguageSyntaxConfig struct {
Syntax map[string]*SyntaxCriteria `toml:"syntax"` Syntax map[string]*SyntaxCriteria `toml:"syntax"`
} }
func NewLanguageSyntaxConfig() *LanguageSyntaxConfig { func MarkdownConfig() *LanguageSyntaxConfig {
return &LanguageSyntaxConfig{Syntax: map[string]*SyntaxCriteria{}} return &LanguageSyntaxConfig{Syntax: map[string]*SyntaxCriteria{
"header": {
Foreground: 0xff00ff,
Pattern: "(?m)^#{1,6}.*",
},
}}
} }
var DefaultSyntaxSet = map[string]string{} func TOMLConfig() *LanguageSyntaxConfig {
return &LanguageSyntaxConfig{Syntax: map[string]*SyntaxCriteria{
func RegisterSyntax(name string, syntaxTomlDef string) { "declaration": {
DefaultSyntaxSet[name] = syntaxTomlDef Foreground: 0xf8f273,
Pattern: `(\[)(.*)(\])`,
},
"identifier": {
Foreground: 0xf0a400,
Pattern: `\b([a-z]|[A-Z])+(_|([a-z]|[A-Z])+)*\b`,
},
"symbol": {
Match: []string{"="},
Foreground: 0xf8f273,
},
}}
} }
func init() { func CConfig() *LanguageSyntaxConfig {
// TOML return &LanguageSyntaxConfig{Syntax: map[string]*SyntaxCriteria{
RegisterSyntax("toml", `[syntax.toml] "type": {
[syntax.declaration] Foreground: 0xf8f273,
foreground = 0xf8f273 Match: []string{
pattern = '(\[)(.*)(\])'
[syntax.identifier]
foreground = 0xf0a400
pattern = '\b([a-z]|[A-Z])+(_|([a-z]|[A-Z])+)*\b'
[syntax.symbol]
match = ["="]
foreground = 0xf8f273
`)
// C LANGUAGE SYNTAX HIGHLIGHTING
RegisterSyntax("c", `[syntax.c]
[syntax.type]
foreground = 0xf8f273
match = [
"int", "char", "bool", "float", "double", "void", "int", "char", "bool", "float", "double", "void",
"uint8_t", "uint16_t", "uint32_t", "uint64_t", "uint8_t", "uint16_t", "uint32_t", "uint64_t",
"int8_t", "int16_t", "int32_t", "int64_t", "const" "int8_t", "int16_t", "int32_t", "int64_t", "const",
] },
},
[syntax.keyword] "keyword": {
foreground = 0xf0a400 Foreground: 0xf0a400,
match = [ Match: []string{
"for", "break", "if", "else", "continue", "return", "for", "break", "if", "else", "continue", "return",
"goto", "extern", "const", "typedef", "goto", "extern", "const", "typedef",
"struct", "union", "register", "enum", "struct", "union", "register", "enum",
"do", "static", "sizeof", "volatile", "unsigned", "do", "static", "sizeof", "volatile", "unsigned",
"switch", "case", "default" "switch", "case", "default",
] },
},
[syntax.string_literal] "string_literal": {
foreground = 0x4b79fc Foreground: 0x4b79fc,
pattern = "\"([^\\\"]|\\.)*\"" Pattern: `\"([^\\\"]|\\.)*\"`,
},
[syntax.directive] "directive": {
foreground = 0xf0a400 Foreground: 0xf0a400,
pattern = "^\\s*#\\s*include\\s+(?:<[^>]*>|\"[^\"]*\")\\s*" Pattern: `^\\s*#\\s*include\\s+(?:<[^>]*>|\"[^\"]*\")\\s*`,
},
[syntax.symbol] "symbol": {
foreground = 0xf0a400 Foreground: 0xf0a400,
match = [ Match: []string{
"+=", "-=", "*=", "/=", ">>", "<<", "==", "!=", "+=", "-=", "*=", "/=", ">>", "<<", "==", "!=",
">=", "<=", "||", "&&", ">=", "<=", "||", "&&",
"=", ":", ";", "*", "&", "+", "-", "/", "%", "=", ":", ";", "*", "&", "+", "-", "/", "%",
"^", "#", "!", "@", "<", ">", ".", "," "^", "#", "!", "@", "<", ">", ".", ",",
] },
},
"comment": {
Foreground: 0x4b79fc,
Pattern: `//.*`,
},
}}
}
[syntax.comment] func GoConfig() *LanguageSyntaxConfig {
foreground = 0x4b79fc return &LanguageSyntaxConfig{Syntax: map[string]*SyntaxCriteria{
pattern = '//.*'`) "keyword": {
Foreground: 0xf0a400,
// GO LANGUAGE SYNTAX HIGHLIGHTING Match: []string{
RegisterSyntax("go", `[syntax.go]
[syntax.keyword]
foreground = 0xf0a400
match = [
"break", "default", "func", "interface", "select", "break", "default", "func", "interface", "select",
"case", "defer", "go", "map", "struct", "case", "defer", "go", "map", "struct",
"chan", "else", "goto", "package", "switch", "chan", "else", "goto", "package", "switch",
"const", "fallthrough", "if", "range", "type", "const", "fallthrough", "if", "range", "type",
"continue", "for", "import", "return", "var", "continue", "for", "import", "return", "var",
] },
},
[syntax.type] "type": {
foreground = 0xf8f273 Foreground: 0xf8f273,
match = [ Match: []string{
"int", "string", "uint", "rune", "int", "string", "uint", "rune",
"int8", "int16", "int32", "int64", "int8", "int16", "int32", "int64",
"uint8", "uint16", "uint32", "uint64", "uint8", "uint16", "uint32", "uint64",
"byte", "float32", "float64", "complex64", "byte", "float32", "float64", "complex64",
"complex128", "uintptr", "complex128", "uintptr",
] },
},
[syntax.comment] "comment": {
foreground = 0x4b79fc Foreground: 0x4b79fc,
pattern = '//.*' Pattern: `//.*`,
},
[syntax.string_literal] "string_literal": {
foreground = 0x4b79fc Foreground: 0x4b79fc,
pattern = "\"([^\\\"]|\\.)*\"" Pattern: `\"([^\\\"]|\\.)*\"`,
},
[syntax.symbol] "symbol": {
foreground = 0xf0a400 Foreground: 0xf0a400,
match = [ Match: []string{
"+=", "-=", "*=", "/=", ">>", "<<", "==", "!=", ":=", "+=", "-=", "*=", "/=", ">>", "<<", "==", "!=", ":=",
">=", "<=", "||", "&&", ">=", "<=", "||", "&&",
"=", ":", ";", "*", "&", "+", "-", "/", "%", "=", ":", ";", "*", "&", "+", "-", "/", "%",
"^", "#", "!", "@", "<", ">", ".", "," "^", "#", "!", "@", "<", ">", ".", ",",
]`) },
},
RegisterSyntax("md", `[syntax.md] }}
[syntax.header]
foreground = 0xff00ff
pattern = '(?m)^#{1,6}.*'
`)
// your syntax here!
} }