gilo/editor/highlight/highlight.go
Timothy Warren 79846e372a
All checks were successful
timw4mail/gilo/pipeline/head This commit looks good
Complete Step 181
2023-10-06 14:56:51 -04:00

138 lines
3.1 KiB
Go

package highlight
import (
"strings"
"timshome.page/gilo/terminal"
)
// ----------------------------------------------------------------------------
// !Syntax Highlighting Constants
// ----------------------------------------------------------------------------
const (
Normal = iota
Comment
MLComment
Keyword1
Keyword2
String
Number
Match
)
const (
DoNumbers = 1 << 0
DoStrings = 1 << 1
)
// ------------------------------------------------------------------
// Syntax Type -> Color Mapping
// ------------------------------------------------------------------
var syntaxColorMap = map[int]string{
Comment: terminal.FGCyan,
MLComment: terminal.FGBrightCyan,
Keyword1: terminal.FGYellow,
Keyword2: terminal.FGGreen,
String: terminal.FGBrightMagenta,
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
FileMatch []string
LineCommentStart string
MultilineCommentStart string
MultilineCommentEnd string
Keywords1 []string
Keywords2 []string
Flags int
}
// HLDB - The "database" of syntax types
var HLDB = []*Syntax{{
"c",
[]string{".c", ".h", ".cpp"},
"//",
"/*",
"*/",
[]string{
"switch", "if", "while", "for", "break", "continue", "return",
"else", "struct", "union", "typedef", "static", "enum",
"class", "case",
},
[]string{"int",
"long", "double", "float", "char", "unsigned", "signed", "void",
"#define", "#endif", "#error", "#if", "#ifdef", "#ifndef", "#include",
"#undef",
},
DoNumbers | DoStrings,
}, {
"go",
[]string{".go", "go.mod"},
"//",
"/*",
"*/",
[]string{
"break", "case", "chan", "const", "continue", "default", "defer",
"else", "fallthrough", "for", "func", "go", "goto", "if",
"import", "interface", "map", "package", "range", "return", "select",
"struct", "switch", "type", "var", "iota",
},
[]string{
"bool", "uint8", "uint16", "uint32", "uint64", "int8", "int16",
"int32", "int64", "float32", "float64", "complex64", "complex128",
"byte", "rune", "uint", "int", "uintptr", "string",
"struct", "type", "map",
},
DoNumbers | DoStrings,
}, {
"makefile",
[]string{"Makefile", "makefile", "justfile"},
"#",
"",
"",
[]string{},
[]string{},
0,
}}
func GetSyntaxByFilename(filename string) *Syntax {
if filename == "" {
return nil
}
var ext string = ""
extInd := strings.LastIndex(filename, ".")
if extInd > -1 {
ext = filename[extInd:len(filename)]
}
for i := 0; i < len(HLDB); i++ {
s := HLDB[i]
for j := range s.FileMatch {
if s.FileMatch[j] == ext || strings.Contains(filename, s.FileMatch[j]) {
return s
}
}
}
return nil
}