Highlight single-line comments (up to step 172)

This commit is contained in:
Timothy Warren 2023-10-04 15:08:04 -04:00
parent ee99e553f0
commit ca2fcdbede
3 changed files with 34 additions and 10 deletions

View File

@ -122,18 +122,32 @@ func (r *Row) updateSyntax() {
return return
} }
var scsIndex int = -1
scs := s.LineCommentStart
if len(scs) > 0 {
scsIndex = strings.Index(string(r.render), scs)
}
prevSep := true prevSep := true
inString := '0' inString := '0'
for i < r.RenderSize() { for i < r.RenderSize() {
ch := r.render[i] ch := r.render[i]
prevHl := highlight.Normal prevHl := highlight.Normal
if i > 0 { if i > 0 {
prevHl = r.Hl[i-1] prevHl = r.Hl[i-1]
} }
ip1 := 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 { if s.Flags&highlight.HighlightStrings == highlight.HighlightStrings {
// At the start of a string literal // At the start of a string literal
if inString == '0' && (ch == '"' || ch == '\'') { if inString == '0' && (ch == '"' || ch == '\'') {
@ -166,6 +180,7 @@ func (r *Row) updateSyntax() {
} }
} }
// Numeric literals
if s.Flags&highlight.HighlightNumbers == highlight.HighlightNumbers { if s.Flags&highlight.HighlightNumbers == highlight.HighlightNumbers {
if (unicode.IsDigit(ch) && (prevSep || prevHl == highlight.Number)) || if (unicode.IsDigit(ch) && (prevSep || prevHl == highlight.Number)) ||
(ch == '.' && prevHl == highlight.Number) { (ch == '.' && prevHl == highlight.Number) {

View File

@ -5,6 +5,7 @@ package highlight
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
const ( const (
Normal = iota Normal = iota
Comment
String String
Number Number
Match Match

View File

@ -10,10 +10,11 @@ import (
// ------------------------------------------------------------------ // ------------------------------------------------------------------
var syntaxColorMap = map[int]string{ var syntaxColorMap = map[int]string{
String: terminal.FGMagenta, Comment: terminal.FGCyan,
Number: terminal.FGRed, String: terminal.FGMagenta,
Match: terminal.FGBlue, Number: terminal.FGRed,
Normal: terminal.DefaultFGColor, Match: terminal.FGBlue,
Normal: terminal.DefaultFGColor,
} }
// SyntaxToColor Take a highlighting type and map it to // SyntaxToColor Take a highlighting type and map it to
@ -32,23 +33,27 @@ func SyntaxToColor(hl int) string {
// ------------------------------------------------------------------ // ------------------------------------------------------------------
type Syntax struct { type Syntax struct {
FileType string FileType string
FileMatch []string FileMatch []string
Flags int LineCommentStart string
Flags int
} }
// HLDB - The "database" of syntax types // HLDB - The "database" of syntax types
var HLDB = []*Syntax{{ var HLDB = []*Syntax{{
"c", "c",
[]string{".c", ".h", ".cpp"}, []string{".c", ".h", ".cpp"},
"//",
HighlightNumbers | HighlightStrings, HighlightNumbers | HighlightStrings,
}, { }, {
"go", "go",
[]string{".go", "go.mod"}, []string{".go", "go.mod"},
"//",
HighlightNumbers | HighlightStrings, HighlightNumbers | HighlightStrings,
}, { }, {
"makefile", "makefile",
[]string{"Makefile", "makefile"}, []string{"Makefile", "makefile", "justfile"},
"#",
0, 0,
}} }}
@ -57,8 +62,11 @@ func GetSyntaxByFilename(filename string) *Syntax {
return nil return nil
} }
var ext string = ""
extInd := strings.LastIndex(filename, ".") extInd := strings.LastIndex(filename, ".")
ext := filename[extInd:len(filename)] if extInd > -1 {
ext = filename[extInd:len(filename)]
}
for i := 0; i < len(HLDB); i++ { for i := 0; i < len(HLDB); i++ {
s := HLDB[i] s := HLDB[i]