-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcodeview.go
193 lines (163 loc) · 4.74 KB
/
codeview.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
package main
import (
"bytes"
"fmt"
"go/scanner"
"go/token"
"image/color"
"log"
"strings"
"github.com/fogleman/gg"
)
type CodeView struct {
alloc *Allocation
// Information regarding the source code it contains
fset *token.FileSet
filename string
source []byte
sourceLines int
sourceWidth int
tokens []CodeViewToken
// Information regarding the typography
fontFace string
fontSize float64
lineHeight float64
}
type CodeViewToken struct {
pos token.Pos
tok token.Token
lit string
}
func (tok *CodeViewToken) Len() int {
switch tok.tok {
case token.ADD, token.SUB, token.MUL, token.QUO, token.REM, token.AND,
token.OR, token.XOR, token.LSS, token.GTR, token.ASSIGN, token.NOT,
token.LPAREN, token.LBRACK, token.LBRACE, token.COMMA, token.PERIOD,
token.RPAREN, token.RBRACK, token.RBRACE, token.SEMICOLON, token.COLON:
// One-character operators
return 1
case token.SHL, token.SHR, token.AND_NOT, token.ADD_ASSIGN, token.SUB_ASSIGN,
token.MUL_ASSIGN, token.QUO_ASSIGN, token.REM_ASSIGN, token.AND_ASSIGN,
token.OR_ASSIGN, token.XOR_ASSIGN, token.LAND, token.LOR, token.ARROW,
token.INC, token.DEC, token.EQL, token.NEQ, token.LEQ, token.GEQ,
token.DEFINE:
// Two-character operators
return 2
case token.SHL_ASSIGN, token.SHR_ASSIGN, token.AND_NOT_ASSIGN, token.ELLIPSIS:
// Three-character operators
return 3
}
return len(tok.lit)
}
func NewCodeView(filename string, source []byte) *CodeView {
codeview := &CodeView{
filename: filename,
source: source,
fontFace: "/usr/share/fonts/TTF/DejaVuSansMono.ttf",
fontSize: 16,
lineHeight: 24,
}
// Get line count
lines := bytes.Split(source, []byte("\n"))
for line := range lines {
width := len(lines[line])
if width > codeview.sourceWidth {
codeview.sourceWidth = width
}
codeview.sourceLines++
}
// Tokenize
codeview.fset = token.NewFileSet()
scan := scanner.Scanner{}
file := codeview.fset.AddFile(filename, codeview.fset.Base(), len(source))
scan.Init(file, source, nil, scanner.ScanComments)
for {
pos, tok, lit := scan.Scan()
if tok == token.EOF {
break
}
codeview.tokens = append(codeview.tokens, CodeViewToken{pos, tok, lit})
}
return codeview
}
func (c *CodeView) Render(context *gg.Context) {
alloc := c.Allocation()
context.SetColor(PaperColorScheme.Back)
context.DrawRectangle(0, 0, alloc.Width, alloc.Height)
context.FillPreserve()
context.Clip()
defer context.ResetClip()
context.SetColor(PaperColorScheme.Text)
if err := context.LoadFontFace(c.fontFace, c.fontSize); err != nil {
log.Print(err)
return
}
lineY := c.lineHeight / 2
lines := bytes.Split(c.source, []byte("\n"))
currentTokIndex := 0
for lineIndex, line := range lines {
// Keep track of the indices to change colors
type ColorMarker struct {
Index int
Color color.Color
}
fmt.Printf("\nline %d of %d: %s\n", lineIndex+1, len(lines), line)
// Add all tokens from this line
markers := []ColorMarker{{0, PaperColorScheme.Text}}
for currentTokIndex < len(c.tokens) {
tok := c.tokens[currentTokIndex]
pos := c.fset.Position(tok.pos)
if pos.Line != lineIndex+1 {
// There are no more tokens on this line
break
}
indexStart := pos.Column - 1
indexEnd := indexStart + tok.Len()
if indexEnd > len(line) {
indexEnd = len(line)
}
fmt.Printf("adding token %q %q index %d:%d\n", tok.tok, tok.lit, indexStart, indexEnd)
markers = append(markers,
ColorMarker{indexStart, PaperColorScheme.Tokens[tok.tok]},
ColorMarker{indexEnd, PaperColorScheme.Text})
currentTokIndex++
}
markers = append(markers, ColorMarker{len(line), PaperColorScheme.Text})
// Iterate each consecutive pair of markers that has text in between
var lineX float64
for index := 0; index < len(markers)-1; index++ {
markerA, markerB := markers[index], markers[index+1]
if markerA.Index == markerB.Index {
continue
}
str := string(line[markerA.Index:markerB.Index])
str = strings.Replace(str, "\t", " ", -1)
width, _ := context.MeasureString(str)
context.SetColor(markerA.Color)
context.DrawStringAnchored(str, lineX, lineY, 0, 0.5)
lineX += width
}
lineY += c.lineHeight
}
context.Fill()
}
func (c *CodeView) Measure(orient Orientation, forsize float64) (minimum, natural float64) {
switch orient {
case Horizontal:
// Natural and minimum size is the width of the longest line
minimum = float64(c.sourceWidth) * c.fontSize
natural = minimum
case Vertical:
// Natural size is the number of lines times the line height
// Minimum size is 3 lines
minimum = 3 * c.lineHeight
natural = float64(c.sourceLines) * c.lineHeight
}
return
}
func (c *CodeView) Allocate(alloc *Allocation) {
c.alloc = alloc
}
func (c *CodeView) Allocation() Allocation {
return *c.alloc
}