From ee99e553f00ed91d4e174dc58c00701a9cbe15d0 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 4 Oct 2023 14:22:51 -0400 Subject: [PATCH] Highlight string literals --- editor/document/row.go | 40 ++++++++++++++++++++++++++++++++--- editor/highlight/constants.go | 6 +++++- editor/highlight/fn.go | 20 ------------------ editor/highlight/syntax.go | 35 +++++++++++++++++++++++++++--- 4 files changed, 74 insertions(+), 27 deletions(-) delete mode 100644 editor/highlight/fn.go diff --git a/editor/document/row.go b/editor/document/row.go index 1381359..aa13b57 100644 --- a/editor/document/row.go +++ b/editor/document/row.go @@ -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 diff --git a/editor/highlight/constants.go b/editor/highlight/constants.go index f07a536..92dcf1f 100644 --- a/editor/highlight/constants.go +++ b/editor/highlight/constants.go @@ -5,8 +5,12 @@ package highlight // ---------------------------------------------------------------------------- const ( Normal = iota + String Number Match ) -const HighlightNumbers = (1 << 0) +const ( + HighlightNumbers = 1 << 0 + HighlightStrings = 1 << 1 +) diff --git a/editor/highlight/fn.go b/editor/highlight/fn.go deleted file mode 100644 index 0620c03..0000000 --- a/editor/highlight/fn.go +++ /dev/null @@ -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 -} diff --git a/editor/highlight/syntax.go b/editor/highlight/syntax.go index b8b6530..dabd12b 100644 --- a/editor/highlight/syntax.go +++ b/editor/highlight/syntax.go @@ -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"},