1
0
Fork 0

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
}
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) {

View File

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

View File

@ -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]