package main
import (
- "github.com/gdamore/tcell"
+ "github.com/zyedidia/tcell"
"io/ioutil"
"path/filepath"
"regexp"
"privoxy-filter",
"puppet",
"python",
+ "r",
"reST",
"rpmspec",
"ruby",
// and returns them. It also returns the filetype of the file
func GetRules(buf *Buffer) ([]SyntaxRule, string) {
for r := range syntaxFiles {
- if r[0] != nil && r[0].MatchString(buf.path) {
+ if r[0] != nil && r[0].MatchString(buf.Path) {
// Check if the syntax statement matches the extension
return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype
- } else if r[1] != nil && r[1].MatchString(buf.lines[0]) {
+ } else if r[1] != nil && r[1].MatchString(buf.Lines[0]) {
// Check if the header statement matches the first line
return LoadRulesFromFile(syntaxFiles[r].text, syntaxFiles[r].filename), syntaxFiles[r].filetype
}
// so map[3] represents the style of the third character
type SyntaxMatches [][]tcell.Style
-// Match takes a buffer and returns the syntax matches a map specifying how it should be syntax highlighted
-// We need to check the start-end regexes for the entire buffer every time Match is called, but for the
-// non start-end rules, we only have to update the updateLines provided by the view
+// Match takes a buffer and returns the syntax matches: a 2d array specifying how it should be syntax highlighted
+// We match the rules from up `synLinesUp` lines and down `synLinesDown` lines
func Match(v *View) SyntaxMatches {
- buf := v.buf
- rules := v.buf.rules
+ buf := v.Buf
+ rules := v.Buf.rules
- viewStart := v.topline
- viewEnd := v.topline + v.height
- if viewEnd > len(buf.lines) {
- viewEnd = len(buf.lines)
+ viewStart := v.Topline
+ viewEnd := v.Topline + v.height
+ if viewEnd > buf.NumLines {
+ viewEnd = buf.NumLines
}
- // updateStart := v.updateLines[0]
- // updateEnd := v.updateLines[1]
- //
- // if updateEnd > len(buf.lines) {
- // updateEnd = len(buf.lines)
- // }
- // if updateStart < 0 {
- // updateStart = 0
- // }
- lines := buf.lines[viewStart:viewEnd]
- // updateLines := buf.lines[updateStart:updateEnd]
+ lines := buf.Lines[viewStart:viewEnd]
matches := make(SyntaxMatches, len(lines))
for i, line := range lines {
}
// We don't actually check the entire buffer, just from synLinesUp to synLinesDown
- totalStart := v.topline - synLinesUp
- totalEnd := v.topline + v.height + synLinesDown
+ totalStart := v.Topline - synLinesUp
+ totalEnd := v.Topline + v.height + synLinesDown
if totalStart < 0 {
totalStart = 0
}
- if totalEnd > len(buf.lines) {
- totalEnd = len(buf.lines)
+ if totalEnd > buf.NumLines {
+ totalEnd = buf.NumLines
}
- str := strings.Join(buf.lines[totalStart:totalEnd], "\n")
- startNum := ToCharPos(0, totalStart, v.buf)
+ str := strings.Join(buf.Lines[totalStart:totalEnd], "\n")
+ startNum := ToCharPos(0, totalStart, v.Buf)
- toplineNum := ToCharPos(0, v.topline, v.buf)
+ toplineNum := ToCharPos(0, v.Topline, v.Buf)
for _, rule := range rules {
if rule.startend {
if i < toplineNum {
continue
}
- colNum, lineNum := FromCharPosStart(toplineNum, 0, v.topline, i, buf)
+ colNum, lineNum := FromCharPosStart(toplineNum, 0, v.Topline, i, buf)
if lineNum == -1 || colNum == -1 {
continue
}
if indicies := rule.regex.FindAllStringIndex(line, -1); indicies != nil {
for _, value := range indicies {
for i := value[0]; i < value[1]; i++ {
- // matches[lineN+updateStart][i] = rule.style
matches[lineN][i] = rule.style
}
}