/
syntax.go
201 lines (186 loc) · 6.23 KB
/
syntax.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"strings"
termutil "github.com/japanoise/termbox-util"
"github.com/nsf/termbox-go"
glisp "github.com/zhemao/glisp/interpreter"
"github.com/zyedidia/highlight"
)
var defs []*highlight.Def
// These functions implement highlight's LineStates interface for EditorBuffer
func (buf *EditorBuffer) Line(n int) string {
return buf.Rows[n].Render
}
func (buf *EditorBuffer) LinesNum() int {
return buf.NumRows
}
func (buf *EditorBuffer) State(n int) highlight.State {
return buf.Rows[n].HlState
}
func (buf *EditorBuffer) SetState(n int, s highlight.State) {
buf.Rows[n].HlState = s
}
func (buf *EditorBuffer) SetMatch(n int, m highlight.LineMatch) {
buf.Rows[n].HlMatches = m
}
// End interface functions
func (buf *EditorBuffer) Highlight() {
if buf.Highlighter == nil {
return
}
buf.Highlighter.HighlightStates(buf)
buf.Highlighter.HighlightMatches(buf, 0, buf.NumRows)
}
func getColorForGroup(group highlight.Group) termbox.Attribute {
color := termbox.ColorDefault
switch group {
case 255:
// Special case here for search results
color = termbox.AttrReverse
case highlight.Groups["type.extended"]:
color = termbox.ColorDefault
case highlight.Groups["preproc"], highlight.Groups["special"], highlight.Groups["constant.specialChar"]:
color = termbox.ColorYellow
case highlight.Groups["comment"], highlight.Groups["preproc.shebang"]:
color = termbox.ColorBlue
case highlight.Groups["constant.string"], highlight.Groups["constant"], highlight.Groups["constant.number"], highlight.Groups["constant.specialChar"], highlight.Groups["constant.bool"]:
color = termbox.ColorRed
case highlight.Groups["type"]:
color = termbox.ColorGreen
case highlight.Groups["identifier"]:
color = termbox.ColorCyan
case highlight.Groups["statement"]:
color = termbox.ColorMagenta
case highlight.Groups["todo"]:
color = termbox.ColorRed | termbox.AttrReverse
default:
color = termbox.ColorDefault
}
return color
}
func (row *EditorRow) PrintWCursor(x, y, offset, runeoff, sx int, ts string, buf *EditorBuffer) {
if buf.regionActive && buf.region.startl <= row.idx && row.idx < buf.region.endl {
for i := x; i <= sx; i++ {
termbox.SetCell(i, y, ' ', termbox.AttrReverse, termbox.ColorDefault)
}
if buf.region.startl < row.idx {
termutil.PrintstringColored(termbox.AttrReverse, ts, x, y)
return
}
}
color := termbox.ColorDefault
os := 0
ri := 0
for in, ru := range ts {
if x+os >= sx {
termutil.PrintRune(x+os-1, y, '→', termbox.ColorDefault)
return
}
if offset+os == buf.rx {
termbox.SetCursor(x+os, y)
}
if Global.NoSyntax || buf.Highlighter == nil {
color = termbox.ColorDefault
} else if group, ok := row.HlMatches[ri+offset]; ok {
color = getColorForGroup(group)
} else if in == 0 && runeoff != 0 {
groupi, oki := row.HlMatches[offset]
for i := 1; !oki && i <= offset; i++ {
groupi, oki = row.HlMatches[offset-i]
}
color = getColorForGroup(groupi)
}
// See comment in original function
if buf.regionActive &&
((row.idx == buf.region.startl && buf.region.startl == buf.region.endl && offset+os < buf.region.endc && offset+os >= buf.region.startc) ||
(buf.region.startl != buf.region.endl && ((row.idx == buf.region.startl && offset+os >= buf.region.startc) || (row.idx == buf.region.endl && offset+os < buf.region.endc)))) {
termutil.PrintRune(x+os, y, ru, termbox.AttrReverse)
} else {
termutil.PrintRune(x+os, y, ru, color)
}
os += termutil.Runewidth(ru)
ri++
}
if buf.cx == row.Size {
termbox.SetCursor(x+os, y)
}
}
func (row *EditorRow) Print(x, y, offset, runeoff, sx int, ts string, buf *EditorBuffer) {
if buf.regionActive && buf.region.startl <= row.idx && row.idx < buf.region.endl {
for i := x; i <= sx; i++ {
termbox.SetCell(i, y, ' ', termbox.AttrReverse, termbox.ColorDefault)
}
if buf.region.startl < row.idx {
termutil.PrintstringColored(termbox.AttrReverse, ts, x, y)
return
}
}
color := termbox.ColorDefault
os := 0
ri := 0
for in, ru := range ts {
if x+os >= sx {
termutil.PrintRune(x+os-1, y, '→', termbox.ColorDefault)
return
}
if Global.NoSyntax || buf.Highlighter == nil {
color = termbox.ColorDefault
} else if group, ok := row.HlMatches[ri+offset]; ok {
color = getColorForGroup(group)
} else if in == 0 && runeoff != 0 {
groupi, oki := row.HlMatches[offset]
for i := 1; !oki && i <= offset; i++ {
groupi, oki = row.HlMatches[offset-i]
}
color = getColorForGroup(groupi)
}
// Extremely insane boolean, but it basically is asking if we're in the region.
// Could kick this out to a function, but it would be just as unreadable.
// 1st line is "If the region is active"
// 2nd line is "If the start & end are the same, and we're in between the first and last character"
// 3rd line is "If the start & end are not the same and we're within the region"
if buf.regionActive &&
((row.idx == buf.region.startl && buf.region.startl == buf.region.endl && offset+os < buf.region.endc && offset+os >= buf.region.startc) ||
(buf.region.startl != buf.region.endl && ((row.idx == buf.region.startl && offset+os >= buf.region.startc) || (row.idx == buf.region.endl && offset+os < buf.region.endc)))) {
termutil.PrintRune(x+os, y, ru, termbox.AttrReverse)
} else {
termutil.PrintRune(x+os, y, ru, color)
}
os += termutil.Runewidth(ru)
ri++
}
}
func LoadSyntaxDefs() {
for _, fname := range AssetNames() {
if strings.HasSuffix(fname, ".yaml") {
input := MustAsset(fname)
d, err := highlight.ParseDef(input)
if err != nil {
continue
}
defs = append(defs, d)
}
}
highlight.ResolveIncludes(defs)
}
func editorSelectSyntaxHighlight(buf *EditorBuffer, env *glisp.Glisp) {
if strings.HasSuffix(buf.Filename, "gomacs.lisp") {
buf.Highlighter = highlight.NewHighlighter(highlight.DetectFiletype(defs, "gomacs.zy", []byte{}))
buf.MajorMode = buf.Highlighter.Def.FileType
ExecHooksForMode(env, buf.MajorMode)
buf.Highlight()
return
}
var first []byte
if buf.NumRows > 0 {
first = []byte(buf.Rows[0].Data)
}
buf.Highlighter = highlight.NewHighlighter(highlight.DetectFiletype(defs, buf.Filename, first))
if buf.Highlighter != nil {
buf.MajorMode = buf.Highlighter.Def.FileType
ExecHooksForMode(env, buf.MajorMode)
} else {
buf.MajorMode = "Unknown"
}
buf.Highlight()
}