1
0
Fork 0

Highlight string literals
timw4mail/gilo/pipeline/head This commit looks good Details

This commit is contained in:
Timothy Warren 2023-10-04 14:22:51 -04:00
parent 4fdb0a607c
commit ee99e553f0
4 changed files with 74 additions and 27 deletions

View File

@ -112,8 +112,6 @@ func (r *Row) update() {
func (r *Row) updateSyntax() {
i := 0
s := r.parent.Syntax
prevSep := true
r.Hl = make([]int, r.RenderSize())
for x := range r.Hl {
r.Hl[x] = highlight.Normal
@ -124,6 +122,8 @@ func (r *Row) updateSyntax() {
return
}
prevSep := true
inString := '0'
for i < r.RenderSize() {
ch := r.render[i]
prevHl := highlight.Normal
@ -132,7 +132,41 @@ func (r *Row) updateSyntax() {
prevHl = r.Hl[i-1]
}
if s.Flags&highlight.HighlightNumbers == 1 {
ip1 := i + 1
if s.Flags&highlight.HighlightStrings == highlight.HighlightStrings {
// At the start of a string literal
if inString == '0' && (ch == '"' || ch == '\'') {
inString = ch
r.Hl[i] = highlight.String
i++
continue
}
// In an existing string
if inString != '0' {
r.Hl[i] = highlight.String
// Handle when a quote is escaped inside a string
if ch == '\\' && ip1 < r.RenderSize() {
r.Hl[ip1] = highlight.String
i += 2
continue
}
// This quote mark matches the beginning of the string
// so now the string is completed
if ch == inString {
inString = '0'
}
i++
prevSep = true
continue
}
}
if s.Flags&highlight.HighlightNumbers == highlight.HighlightNumbers {
if (unicode.IsDigit(ch) && (prevSep || prevHl == highlight.Number)) ||
(ch == '.' && prevHl == highlight.Number) {
r.Hl[i] = highlight.Number

View File

@ -5,8 +5,12 @@ package highlight
// ----------------------------------------------------------------------------
const (
Normal = iota
String
Number
Match
)
const HighlightNumbers = (1 << 0)
const (
HighlightNumbers = 1 << 0
HighlightStrings = 1 << 1
)

View File

@ -1,20 +0,0 @@
package highlight
import "timshome.page/gilo/terminal"
var syntaxColorMap = map[int]string{
Number: terminal.FGRed,
Match: terminal.FGBlue,
Normal: terminal.DefaultFGColor,
}
// SyntaxToColor Take a highlighting type and map it to
// an ANSI color escape code for display
func SyntaxToColor(hl int) string {
color := syntaxColorMap[hl]
if len(color) == 0 {
color = terminal.DefaultFGColor
}
return color
}

View File

@ -1,6 +1,35 @@
package highlight
import "strings"
import (
"strings"
"timshome.page/gilo/terminal"
)
// ------------------------------------------------------------------
// Syntax Type -> Color Mapping
// ------------------------------------------------------------------
var syntaxColorMap = map[int]string{
String: terminal.FGMagenta,
Number: terminal.FGRed,
Match: terminal.FGBlue,
Normal: terminal.DefaultFGColor,
}
// SyntaxToColor Take a highlighting type and map it to
// an ANSI color escape code for display
func SyntaxToColor(hl int) string {
color := syntaxColorMap[hl]
if len(color) == 0 {
color = terminal.DefaultFGColor
}
return color
}
// ------------------------------------------------------------------
// File Type -> Syntax Mapping
// ------------------------------------------------------------------
type Syntax struct {
FileType string
@ -12,11 +41,11 @@ type Syntax struct {
var HLDB = []*Syntax{{
"c",
[]string{".c", ".h", ".cpp"},
HighlightNumbers,
HighlightNumbers | HighlightStrings,
}, {
"go",
[]string{".go", "go.mod"},
HighlightNumbers,
HighlightNumbers | HighlightStrings,
}, {
"makefile",
[]string{"Makefile", "makefile"},