/
jssyntax.go
346 lines (324 loc) · 8.32 KB
/
jssyntax.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package renderer
import (
"bytes"
"image"
"image/draw"
"strings"
"unicode"
"github.com/driusan/de/demodel"
"github.com/driusan/de/renderer"
"golang.org/x/image/font"
"golang.org/x/image/math/fixed"
)
type JSSyntax struct {
renderer.DefaultSizeCalcer
renderer.DefaultImageMapper
}
func (rd *JSSyntax) InvalidateCache() {
rd.DefaultSizeCalcer.InvalidateCache()
rd.DefaultImageMapper.InvalidateCache()
}
func (rd *JSSyntax) CanRender(buf *demodel.CharBuffer) bool {
return strings.HasSuffix(buf.Filename, ".js")
}
func (rd *JSSyntax) RenderInto(dst draw.Image, buf *demodel.CharBuffer, viewport image.Rectangle) error {
bounds := dst.Bounds()
writer := font.Drawer{
Dst: dst,
Src: &image.Uniform{renderer.TextColour},
Dot: fixed.P(bounds.Min.X, bounds.Min.Y+renderer.MonoFontAscent.Floor()),
Face: renderer.MonoFontFace,
}
runes := bytes.Runes(buf.Buffer)
var inLineComment, inMultilineComment, inString, inCharString, inStringLiteral bool
// Some characters (like a terminating quote) only change the active colour
//after being rendered.
var nextColor image.Image
for i, r := range runes {
if inStringLiteral {
if r == '`' {
nextColor = &image.Uniform{renderer.TextColour}
inStringLiteral = false
}
} else {
switch r {
case '\n':
if inLineComment && !inMultilineComment && !inString {
inLineComment = false
writer.Src = &image.Uniform{renderer.TextColour}
}
case '\'':
if !IsEscaped(i, runes) {
if inCharString {
// end of a string, colourize the quote too.
nextColor = &image.Uniform{renderer.TextColour}
inCharString = false
} else if !inLineComment && !inMultilineComment && !inString && !inStringLiteral {
inCharString = true
writer.Src = &image.Uniform{renderer.StringColour}
}
}
case '"':
if !IsEscaped(i, runes) {
if inString {
inString = false
nextColor = &image.Uniform{renderer.TextColour}
} else if !inLineComment && !inMultilineComment && !inCharString && !inStringLiteral {
inString = true
writer.Src = &image.Uniform{renderer.StringColour}
}
}
case '`':
// \ doesn't mean anything special inside of a string literal in Go. Don't check if it's
// escaped.
//if !IsEscaped(i, runes) {
if inStringLiteral {
inStringLiteral = false
nextColor = &image.Uniform{renderer.TextColour}
} else if !inLineComment && !inMultilineComment && !inCharString && !inString {
inStringLiteral = true
writer.Src = &image.Uniform{renderer.StringColour}
}
//}
case '/':
if i+2 < len(runes) && string(runes[i:i+2]) == "//" {
if !inCharString && !inMultilineComment && !inString {
inLineComment = true
writer.Src = &image.Uniform{renderer.CommentColour}
}
} else if i+2 < len(runes) && string(runes[i:i+2]) == "/*" {
if !inCharString && !inString {
inMultilineComment = true
writer.Src = &image.Uniform{renderer.CommentColour}
}
}
if i > 1 && inMultilineComment && i+1 < len(runes) && string(runes[i-1:i+1]) == "*/" {
nextColor = &image.Uniform{renderer.TextColour}
inMultilineComment = false
}
case ' ', '\t':
if !inCharString && !inMultilineComment && !inString && !inLineComment {
writer.Src = &image.Uniform{renderer.TextColour}
}
default:
if !inCharString && !inMultilineComment && !inString && !inLineComment && !inStringLiteral {
if IsLanguageKeyword(i, runes) {
writer.Src = &image.Uniform{renderer.KeywordColour}
} else if IsLanguageType(i, runes) {
writer.Src = &image.Uniform{renderer.BuiltinTypeColour}
} else if StartsLanguageDeliminator(r) {
writer.Src = &image.Uniform{renderer.TextColour}
}
}
}
}
runeRectangle := image.Rectangle{}
runeRectangle.Min.X = writer.Dot.X.Ceil()
runeRectangle.Min.Y = writer.Dot.Y.Ceil() - renderer.MonoFontAscent.Floor() + 1
switch r {
case '\t':
runeRectangle.Max.X = runeRectangle.Min.X + 8*renderer.MonoFontGlyphWidth.Ceil()
case '\n':
runeRectangle.Max.X = viewport.Max.X
default:
runeRectangle.Max.X = runeRectangle.Min.X + renderer.MonoFontGlyphWidth.Ceil()
}
runeRectangle.Max.Y = runeRectangle.Min.Y + renderer.MonoFontHeight.Ceil() + 1
if runeRectangle.Min.Y > viewport.Max.Y {
// exit the loop early, we've already gotten past the part that we care about.
return nil
}
// Don't draw or calculate the image map if we're outside of the viewport. We can't
// break out, because things not being drawn might still affect the rendering (ie.
// the start of the screen might be in the middle of a comment that needs to be syntax
// highlighted)
// im.IMap = append(im.IMap, renderer.ImageLoc{runeRectangle, uint(i)})
if runeRectangle.Intersect(viewport) != image.ZR {
if uint(i) >= buf.Dot.Start && uint(i) <= buf.Dot.End {
// it's in dot, so highlight the background (unless it's outside of the viewport
// clipping rectangle)
draw.Draw(
dst,
image.Rectangle{
runeRectangle.Min.Sub(viewport.Min),
runeRectangle.Max.Sub(viewport.Min)},
&image.Uniform{renderer.TextHighlight},
image.ZP,
draw.Src,
)
}
}
switch r {
case '\t':
writer.Dot.X += renderer.MonoFontGlyphWidth * 8
continue
case '\n':
writer.Dot.Y += renderer.MonoFontHeight
writer.Dot.X = fixed.I(bounds.Min.X)
continue
}
if runeRectangle.Max.Y > viewport.Min.Y {
// as a hack to align viewport.Min with dst.Min, we subtract it from
// Dot before drawing, then add it back.
writer.Dot.X -= fixed.I(viewport.Min.X)
writer.Dot.Y -= fixed.I(viewport.Min.Y)
writer.DrawString(string(r))
writer.Dot.X += fixed.I(viewport.Min.X)
writer.Dot.Y += fixed.I(viewport.Min.Y)
}
if nextColor != nil {
writer.Src = nextColor
nextColor = nil
}
}
return nil
}
func StartsLanguageDeliminator(r rune) bool {
switch r {
case '+', '-', '*', '/', '%',
'&', '|', '^',
'<', '>', '=', '!',
':', '.',
'(', ')', '[', ']', '{', '}',
',', ';':
return true
}
return unicode.IsSpace(r)
}
func IsLanguageKeyword(pos int, runes []rune) bool {
if pos > 0 {
prev := runes[pos-1]
if !unicode.IsSpace(prev) && !StartsLanguageDeliminator(prev) {
return false
}
}
keywords := []string{
"abstract",
"arguments",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"debugger",
"default",
"delete",
"do",
"double",
"else",
"enum",
"eval",
"export",
"extends",
"false",
"final",
"finally",
"float",
"for",
"function",
"goto",
"if",
"implements",
"import",
"in",
"instanceof",
"int",
"interface",
"let",
"long",
"native",
"new",
"null",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"true",
"try",
"typeof",
"var",
"void",
"volatile",
"while",
"with",
"yield",
}
for _, keyword := range keywords {
keyLen := len(keyword)
// Guard against an out of bounds exception at the end of the file
if len(runes) > pos+keyLen+1 {
if unicode.IsSpace(runes[pos+keyLen]) ||
StartsLanguageDeliminator(runes[pos+keyLen]) {
if string(runes[pos:pos+keyLen]) == keyword {
return true
}
}
}
}
return false
}
func IsLanguageType(pos int, runes []rune) bool {
if pos < 3 {
return false
}
if !StartsLanguageDeliminator(runes[pos-1]) {
return false
}
if len(runes) > pos+4 {
if StartsLanguageDeliminator(runes[pos+3]) {
switch string(runes[pos : pos+3]) {
case "int":
return true
}
}
}
if len(runes) > pos+5 {
if StartsLanguageDeliminator(runes[pos+4]) {
switch string(runes[pos : pos+4]) {
case "int8", "bool", "byte", "rune", "uint":
return true
}
}
}
if len(runes) > pos+6 {
if StartsLanguageDeliminator(runes[pos+5]) {
switch string(runes[pos : pos+5]) {
case "uint8", "int16", "int32", "int64", "error":
return true
}
}
}
if len(runes) > pos+7 {
if StartsLanguageDeliminator(runes[pos+6]) {
switch string(runes[pos : pos+6]) {
case "uint16", "uint32", "uint64", "string":
return true
}
}
}
return false
}
func IsEscaped(pos int, runes []rune) bool {
if pos == 0 {
return false
}
isEscaped := false
for i := pos - 1; i >= 0 && runes[i] == '\\'; i-- {
isEscaped = !isEscaped
}
return isEscaped
}