Improve api

This commit is contained in:
Zachary Yedidia
2017-02-18 21:29:17 -05:00
parent 0f7ed208c1
commit 6dfc8e081f
5 changed files with 37 additions and 9 deletions
+1 -1
View File
@@ -124,7 +124,7 @@ var defs []*highlight.Def
... ...
def := highlight.DetectFiletype(defs, filename, firstLine) def := highlight.DetectFiletype(defs, filename, firstLine)
fmt.Println("Filetype is", def.ft) fmt.Println("Filetype is", def.FileType)
``` ```
For a full example, see the [`syncat`](./examples) example which acts like cat but will syntax highlight the output (if highlight recognizes the filetype). For a full example, see the [`syncat`](./examples) example which acts like cat but will syntax highlight the output (if highlight recognizes the filetype).
+1 -1
View File
@@ -39,7 +39,7 @@ func main() {
h := highlight.NewHighlighter(def) h := highlight.NewHighlighter(def)
matches := h.Highlight(string(fileSrc)) matches := h.HighlightString(string(fileSrc))
lines := strings.Split(string(fileSrc), "\n") lines := strings.Split(string(fileSrc), "\n")
for lineN, l := range lines { for lineN, l := range lines {
+4 -1
View File
@@ -12,5 +12,8 @@ func DetectFiletype(defs []*Def, filename string, firstLine []byte) *Def {
} }
} }
return nil emptyDef := new(Def)
emptyDef.FileType = "Unknown"
emptyDef.rules = new(Rules)
return emptyDef
} }
+29 -4
View File
@@ -21,7 +21,7 @@ func combineLineMatch(src, dst LineMatch) LineMatch {
type State *Region type State *Region
type LineStates interface { type LineStates interface {
Lines() [][]byte LineData() [][]byte
State(lineN int) State State(lineN int) State
SetState(lineN int, s State) SetState(lineN int, s State)
} }
@@ -173,7 +173,7 @@ func (h *Highlighter) highlightEmptyRegion(start int, canMatchEnd bool, lineNum
return highlights return highlights
} }
func (h *Highlighter) Highlight(input string) []LineMatch { func (h *Highlighter) HighlightString(input string) []LineMatch {
lines := strings.Split(input, "\n") lines := strings.Split(input, "\n")
var lineMatches []LineMatch var lineMatches []LineMatch
@@ -190,10 +190,35 @@ func (h *Highlighter) Highlight(input string) []LineMatch {
return lineMatches return lineMatches
} }
func (h *Highlighter) ReHighlight(input LineStates, startline int) []LineMatch { func (h *Highlighter) Highlight(input LineStates, startline int) []LineMatch {
lines := input.Lines() lines := input.LineData()
var lineMatches []LineMatch var lineMatches []LineMatch
for i := startline; i < len(lines); i++ {
line := []byte(lines[i])
if i == 0 || h.lastRegion == nil {
lineMatches = append(lineMatches, h.highlightEmptyRegion(0, true, i, line))
} else {
lineMatches = append(lineMatches, h.highlightRegion(0, true, i, line, h.lastRegion))
}
curState := h.lastRegion
input.SetState(i, curState)
}
return lineMatches
}
func (h *Highlighter) ReHighlight(input LineStates, startline int) []LineMatch {
lines := input.LineData()
var lineMatches []LineMatch
h.lastRegion = nil
if startline > 0 {
h.lastRegion = input.State(startline - 1)
}
for i := startline; i < len(lines); i++ { for i := startline; i < len(lines); i++ {
line := []byte(lines[i]) line := []byte(lines[i])
+2 -2
View File
@@ -12,7 +12,7 @@ import (
// on filename or header (the first line of the file) // on filename or header (the first line of the file)
// Then it has the rules which define how to highlight the file // Then it has the rules which define how to highlight the file
type Def struct { type Def struct {
ft string FileType string
ftdetect []*regexp.Regexp ftdetect []*regexp.Regexp
rules *Rules rules *Rules
} }
@@ -68,7 +68,7 @@ func ParseDef(input []byte) (s *Def, err error) {
if k == "filetype" { if k == "filetype" {
filetype := v.(string) filetype := v.(string)
s.ft = filetype s.FileType = filetype
} else if k == "detect" { } else if k == "detect" {
ftdetect := v.(map[interface{}]interface{}) ftdetect := v.(map[interface{}]interface{})
if len(ftdetect) >= 1 { if len(ftdetect) >= 1 {