Skip to content

Commit

Permalink
syntaxes can now specify foreground and background; closes #39
Browse files Browse the repository at this point in the history
  • Loading branch information
felixangell committed Jun 19, 2018
1 parent b942c22 commit a634897
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 36 deletions.
7 changes: 4 additions & 3 deletions cfg/config.go
Expand Up @@ -35,9 +35,10 @@ type FileAssociations struct {
}

type SyntaxCriteria struct {
Colour int `toml:"colouring"`
Match []string `toml:"match"`
Pattern string `toml:"pattern"`
Foreground uint32 `toml:"foreground"`
Background uint32 `toml:"background"`
Match []string `toml:"match"`
Pattern string `toml:"pattern"`

CompiledPattern *regexp.Regexp
MatchList map[string]bool
Expand Down
30 changes: 15 additions & 15 deletions cfg/syntax.go
Expand Up @@ -16,31 +16,31 @@ func init() {
// TOML
RegisterSyntax("toml", `[syntax.toml]
[syntax.declaration]
colouring = 0xf8f273
foreground = 0xf8f273
pattern = '(\[)(.*)(\])'
[syntax.identifier]
colouring = 0xf0a400
foreground = 0xf0a400
pattern = '\b([a-z]|[A-Z])+(_|([a-z]|[A-Z])+)*\b'
[syntax.symbol]
match = ["="]
colouring = 0xf8f273
foreground = 0xf8f273
`)

// C LANGUAGE SYNTAX HIGHLIGHTING

RegisterSyntax("c", `[syntax.c]
[syntax.type]
colouring = 0xf8f273
foreground = 0xf8f273
match = [
"int", "char", "bool", "float", "double", "void",
"uint8_t", "uint16_t", "uint32_t", "uint64_t",
"int8_t", "int16_t", "int32_t", "int64_t", "const"
]
[syntax.keyword]
colouring = 0xf0a400
foreground = 0xf0a400
match = [
"for", "break", "if", "else", "continue", "return",
"goto", "extern", "const", "typedef",
Expand All @@ -50,15 +50,15 @@ match = [
]
[syntax.string_literal]
colouring = 0x4b79fc
foreground = 0x4b79fc
pattern = "\"([^\\\"]|\\.)*\""
[syntax.directive]
colouring = 0xf0a400
foreground = 0xf0a400
pattern = "^\\s*#\\s*include\\s+(?:<[^>]*>|\"[^\"]*\")\\s*"
[syntax.symbol]
colouring = 0xf0a400
foreground = 0xf0a400
match = [
"+=", "-=", "*=", "/=", ">>", "<<", "==", "!=",
">=", "<=", "||", "&&",
Expand All @@ -67,14 +67,14 @@ match = [
]
[syntax.comment]
colouring = 0x4b79fc
foreground = 0x4b79fc
pattern = '//.*'`)

// GO LANGUAGE SYNTAX HIGHLIGHTING

RegisterSyntax("go", `[syntax.go]
[syntax.keyword]
colouring = 0xf0a400
foreground = 0xf0a400
match = [
"break", "default", "func", "interface", "select",
"case", "defer", "go", "map", "struct",
Expand All @@ -84,7 +84,7 @@ match = [
]
[syntax.type]
colouring = 0xf8f273
foreground = 0xf8f273
match = [
"int", "string", "uint", "rune",
"int8", "int16", "int32", "int64",
Expand All @@ -94,15 +94,15 @@ match = [
]
[syntax.comment]
colouring = 0x4b79fc
foreground = 0x4b79fc
pattern = '//.*'
[syntax.string_literal]
colouring = 0x4b79fc
foreground = 0x4b79fc
pattern = "\"([^\\\"]|\\.)*\""
[syntax.symbol]
colouring = 0xf0a400
foreground = 0xf0a400
match = [
"+=", "-=", "*=", "/=", ">>", "<<", "==", "!=", ":=",
">=", "<=", "||", "&&",
Expand All @@ -112,7 +112,7 @@ match = [

RegisterSyntax("md", `[syntax.md]
[syntax.header]
colouring = 0xff00ff
foreground = 0xff00ff
pattern = '(?m)^#{1,6}.*'
`)

Expand Down
52 changes: 34 additions & 18 deletions gui/buffer.go
Expand Up @@ -1236,16 +1236,16 @@ func (b *Buffer) processInput(pred func(r int) bool) bool {
}

type syntaxRuneInfo struct {
background int
foreground int
background uint32
foreground uint32
length int
}

// dimensions of the last character we rendered
var last_w, last_h int

// runs up a lexer instance
func lexFindMatches(matches *map[int]syntaxRuneInfo, currLine string, toMatch map[string]bool, bg int, fg int) {
func lexFindMatches(matches *map[int]syntaxRuneInfo, currLine string, toMatch map[string]bool, bg uint32, fg uint32) {
// start up a lexer instance and
// lex the line.
lexer := lex.New(currLine)
Expand All @@ -1254,20 +1254,28 @@ func lexFindMatches(matches *map[int]syntaxRuneInfo, currLine string, toMatch ma

for _, tok := range tokenStream {
if _, ok := toMatch[tok.Lexeme]; ok {
(*matches)[tok.Start] = syntaxRuneInfo{bg, -1, len(tok.Lexeme)}
(*matches)[tok.Start] = syntaxRuneInfo{bg, fg, len(tok.Lexeme)}
}
}
}

type charColouring struct {
bg uint32
fg uint32
}

func (b *Buffer) syntaxHighlightLine(currLine string) map[int]syntaxRuneInfo {
matches := map[int]syntaxRuneInfo{}

subjects := make([]*cfg.SyntaxCriteria, len(b.languageInfo.Syntax))
colours := make([]int, len(b.languageInfo.Syntax))
colours := make([]charColouring, len(b.languageInfo.Syntax))

idx := 0
for _, criteria := range b.languageInfo.Syntax {
colours[idx] = criteria.Colour
colours[idx] = charColouring{
criteria.Background,
criteria.Foreground,
}
subjects[idx] = criteria
idx++
}
Expand All @@ -1282,16 +1290,22 @@ func (b *Buffer) syntaxHighlightLine(currLine string) map[int]syntaxRuneInfo {
if matched != nil {
if _, ok := matches[charIndex]; !ok {
matchedStrLen := (matched[1] - matched[0])
matches[charIndex+matched[0]] = syntaxRuneInfo{colours[syntaxIndex], -1, matchedStrLen}

colouring := colours[syntaxIndex]
matches[charIndex+matched[0]] = syntaxRuneInfo{
colouring.bg,
colouring.fg,
matchedStrLen,
}

charIndex += matchedStrLen
continue
}
}
}
} else {
background := colours[syntaxIndex]
foreground := 0
lexFindMatches(&matches, currLine, syntax.MatchList, background, foreground)
colouring := colours[syntaxIndex]
lexFindMatches(&matches, currLine, syntax.MatchList, colouring.bg, colouring.fg)
}
}

Expand Down Expand Up @@ -1387,7 +1401,7 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) {
matches = b.syntaxHighlightLine(string(currLine))
}

var colorStack []int32
var colorStack []charColouring

// TODO move this into a struct
// or something.
Expand Down Expand Up @@ -1415,30 +1429,32 @@ func (b *Buffer) renderAt(ctx *strife.Renderer, rx int, ry int) {

if info, ok := matches[idx]; ok {
if colorStack == nil || len(colorStack) == 0 {
colorStack = make([]int32, info.length)
colorStack = make([]charColouring, info.length)
for i := 0; i < info.length; i++ {
colorStack[i] = int32(info.background)
colorStack[i] = charColouring{info.background, info.foreground}
}
}
}

characterColor := b.buffOpts.foreground
characterColor := charColouring{0, b.buffOpts.foreground}

if len(colorStack) > 0 {
var a uint32
a, colorStack = uint32(colorStack[len(colorStack)-1]), colorStack[:len(colorStack)-1]
a := colorStack[len(colorStack)-1]
colorStack = colorStack[:len(colorStack)-1]
characterColor = a
}

if b.HasFocus() && (b.curs.x-b.cam.x) == (x_col-1) && (b.curs.y-b.cam.y) == y_col {
characterColor = b.buffOpts.cursorInvert
characterColor = charColouring{0, b.buffOpts.cursorInvert}
}

lineHeight := last_h + pad
xPos := b.ex + (rx + ((x_col - 1) * last_w))
yPos := b.ey + (ry + (y_col * lineHeight)) + halfPad

ctx.SetColor(strife.HexRGB(characterColor))
// todo render background

ctx.SetColor(strife.HexRGB(characterColor.fg))
last_w, last_h = ctx.String(string(char), xPos, yPos)

if DEBUG_MODE {
Expand Down

0 comments on commit a634897

Please sign in to comment.