From ca2fcdbedec701960915d137e10068766068c3b1 Mon Sep 17 00:00:00 2001 From: Timothy Warren Date: Wed, 4 Oct 2023 15:08:04 -0400 Subject: [PATCH] Highlight single-line comments (up to step 172) --- editor/document/row.go | 17 ++++++++++++++++- editor/highlight/constants.go | 1 + editor/highlight/syntax.go | 26 +++++++++++++++++--------- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/editor/document/row.go b/editor/document/row.go index aa13b57..9b96ce9 100644 --- a/editor/document/row.go +++ b/editor/document/row.go @@ -122,18 +122,32 @@ func (r *Row) updateSyntax() { return } + var scsIndex int = -1 + scs := s.LineCommentStart + if len(scs) > 0 { + scsIndex = strings.Index(string(r.render), scs) + } + prevSep := true inString := '0' for i < r.RenderSize() { ch := r.render[i] prevHl := highlight.Normal - if i > 0 { prevHl = r.Hl[i-1] } ip1 := i + 1 + // Single line comments + if inString == '0' && scsIndex == i { + for j := scsIndex; j < r.RenderSize(); j++ { + r.Hl[j] = highlight.Comment + } + break + } + + // String literals if s.Flags&highlight.HighlightStrings == highlight.HighlightStrings { // At the start of a string literal if inString == '0' && (ch == '"' || ch == '\'') { @@ -166,6 +180,7 @@ func (r *Row) updateSyntax() { } } + // Numeric literals if s.Flags&highlight.HighlightNumbers == highlight.HighlightNumbers { if (unicode.IsDigit(ch) && (prevSep || prevHl == highlight.Number)) || (ch == '.' && prevHl == highlight.Number) { diff --git a/editor/highlight/constants.go b/editor/highlight/constants.go index 92dcf1f..d744e36 100644 --- a/editor/highlight/constants.go +++ b/editor/highlight/constants.go @@ -5,6 +5,7 @@ package highlight // ---------------------------------------------------------------------------- const ( Normal = iota + Comment String Number Match diff --git a/editor/highlight/syntax.go b/editor/highlight/syntax.go index dabd12b..4d85bf1 100644 --- a/editor/highlight/syntax.go +++ b/editor/highlight/syntax.go @@ -10,10 +10,11 @@ import ( // ------------------------------------------------------------------ var syntaxColorMap = map[int]string{ - String: terminal.FGMagenta, - Number: terminal.FGRed, - Match: terminal.FGBlue, - Normal: terminal.DefaultFGColor, + Comment: terminal.FGCyan, + String: terminal.FGMagenta, + Number: terminal.FGRed, + Match: terminal.FGBlue, + Normal: terminal.DefaultFGColor, } // SyntaxToColor Take a highlighting type and map it to @@ -32,23 +33,27 @@ func SyntaxToColor(hl int) string { // ------------------------------------------------------------------ type Syntax struct { - FileType string - FileMatch []string - Flags int + FileType string + FileMatch []string + LineCommentStart string + Flags int } // HLDB - The "database" of syntax types var HLDB = []*Syntax{{ "c", []string{".c", ".h", ".cpp"}, + "//", HighlightNumbers | HighlightStrings, }, { "go", []string{".go", "go.mod"}, + "//", HighlightNumbers | HighlightStrings, }, { "makefile", - []string{"Makefile", "makefile"}, + []string{"Makefile", "makefile", "justfile"}, + "#", 0, }} @@ -57,8 +62,11 @@ func GetSyntaxByFilename(filename string) *Syntax { return nil } + var ext string = "" extInd := strings.LastIndex(filename, ".") - ext := filename[extInd:len(filename)] + if extInd > -1 { + ext = filename[extInd:len(filename)] + } for i := 0; i < len(HLDB); i++ { s := HLDB[i]