From 28dec9874b3d0320f167e3af663bdfd50e658590 Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Sun, 16 Jul 2023 17:00:58 +0100 Subject: [PATCH 1/3] Fixes. --- highlighter.go | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/highlighter.go b/highlighter.go index eea429a..72ee42a 100644 --- a/highlighter.go +++ b/highlighter.go @@ -5,6 +5,7 @@ import ( "strings" "unicode/utf8" "path/filepath" + "os/ioutil" ) // RunePos returns the rune index of a given byte index @@ -61,7 +62,7 @@ func NewHighlighter(def *Def) *Highlighter { // color's group (represented as one byte) type LineMatch map[int]Group -func ParseSyntaxFiles (dir string, defs *[]*highlight.Def) error{ +func ParseSyntaxFiles (dir string, defs *[]*Def) error{ pattern := "*.yaml" files, err := filepath.Glob(dir + "/" + pattern) if err != nil { @@ -74,14 +75,14 @@ func ParseSyntaxFiles (dir string, defs *[]*highlight.Def) error{ return err } - d, err := highlight.ParseDef(file) + d, err := ParseDef(file) if err != nil { - log.Output(1, err.Error()) + return err continue } *defs = append(*defs, d) - return nil } + return nil } func findIndex(regex *regexp.Regexp, skip *regexp.Regexp, str []rune, canMatchStart, canMatchEnd bool) []int { @@ -223,6 +224,11 @@ func (h *Highlighter) highlightRegion(highlights LineMatch, start int, canMatchE } func (h *Highlighter) highlightEmptyRegion(highlights LineMatch, start int, canMatchEnd bool, lineNum int, line []rune, statesOnly bool) LineMatch { + if h.Def.rules == nil { + return nil + } + + if len(line) == 0 { if canMatchEnd { h.lastRegion = nil From c25570670078bf10b2100491ce77a38d6aa5e040 Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Sun, 16 Jul 2023 17:03:46 +0100 Subject: [PATCH 2/3] . --- highlighter.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/highlighter.go b/highlighter.go index 72ee42a..1f93403 100644 --- a/highlighter.go +++ b/highlighter.go @@ -5,7 +5,7 @@ import ( "strings" "unicode/utf8" "path/filepath" - "os/ioutil" + "io/ioutil" ) // RunePos returns the rune index of a given byte index From e93520cea49962194d97bff8a3ee41e463a4a549 Mon Sep 17 00:00:00 2001 From: Jesse Portnoy Date: Sun, 16 Jul 2023 17:21:03 +0100 Subject: [PATCH 3/3] Fix example to use the new parse helper function --- examples/go.mod | 1 + examples/go.sum | 2 ++ examples/syncat.go | 28 ++++++++++++---------------- 3 files changed, 15 insertions(+), 16 deletions(-) diff --git a/examples/go.mod b/examples/go.mod index 2cd13bb..6e55a4f 100644 --- a/examples/go.mod +++ b/examples/go.mod @@ -8,6 +8,7 @@ require ( ) require ( + github.com/jessp01/highlight v0.0.0-20230716160321-8969d1f05a45 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.17 // indirect golang.org/x/sys v0.6.0 // indirect diff --git a/examples/go.sum b/examples/go.sum index 22bff5e..a8532f4 100644 --- a/examples/go.sum +++ b/examples/go.sum @@ -1,5 +1,7 @@ github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= +github.com/jessp01/highlight v0.0.0-20230716160321-8969d1f05a45 h1:XKfU3AV4wfMbzX0jlqSRXYdsPueivQhHgSqlyUjf28g= +github.com/jessp01/highlight v0.0.0-20230716160321-8969d1f05a45/go.mod h1:Kgvk5uZPhFlZvcS8u0zOB38emtWGrUgnfOvjINkvhyM= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= diff --git a/examples/syncat.go b/examples/syncat.go index 78ba538..47f1e50 100644 --- a/examples/syncat.go +++ b/examples/syncat.go @@ -6,9 +6,10 @@ import ( "io/ioutil" "os" "strings" + "log" "github.com/fatih/color" - "github.com/zyedidia/highlight" + "github.com/jessp01/highlight" ) func main() { @@ -17,24 +18,19 @@ func main() { return } - var defs []*highlight.Def gopath := os.Getenv("GOPATH") - files, lerr := ioutil.ReadDir(gopath + "/src/github.com/zyedidia/highlight/syntax_files") - if lerr != nil { - fmt.Println(lerr) - return + + var syn_dir string + if gopath == "" { + syn_dir = "/etc/highlight" + }else{ + syn_dir = gopath + "/src/github.com/jessp01/highlight/syntax_files" } - for _, f := range files { - if strings.HasSuffix(f.Name(), ".yaml") { - input, _ := ioutil.ReadFile(gopath + "/src/github.com/zyedidia/highlight/syntax_files/" + f.Name()) - d, err := highlight.ParseDef(input) - if err != nil { - fmt.Println(err) - continue - } - defs = append(defs, d) - } + var defs []*highlight.Def + err := highlight.ParseSyntaxFiles (syn_dir, &defs) + if err != nil { + log.Fatal(err) } highlight.ResolveIncludes(defs)