-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.go
executable file
·528 lines (408 loc) · 12.7 KB
/
editor.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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
package main
import (
"math"
"os"
"path/filepath"
imgui "github.com/AllenDang/cimgui-go"
"github.com/bloeys/gopad/settings"
)
const (
linesPerNode = 100
textPadding = 10
)
type Line struct {
// @TODO: This will explode on long lines.
// We can use the same strategy of line nodes here, where a line is something like
// type Line struct {
// Line [RunesPerLine]rune
// Next *Line
// }
chars []rune
}
type LinesNode struct {
Lines [linesPerNode]Line
Next *LinesNode
}
type Editor struct {
FileName string
FilePath string
FileContents string
MouseX int
MouseY int
SelectionStart int
SelectionLength int
IsModified bool
LinesHead *LinesNode
LineCount int
LineHeight float32
CharWidth float32
StartPos float32
}
type MousePosInfo struct {
//Global represents a grid on the whole window (i.e. 0,0 is the top left of the program window)
GridXGlobal int
//Global represents a grid on the whole window (i.e. 0,0 is the top left of the program window)
GridYGlobal int
//Editor represents a grid on the editor window (i.e. 0,0 is the top left of the editor window)
GridXEditor int
//Editor represents a grid on the editor window (i.e. 0,0 is the top left of the editor window)
GridYEditor int
//Line is the currently selected line
Line *Line
LineNum int
}
func (e *Editor) SetCursorPos(x, y int) {
e.MouseX = x
e.MouseY = y
}
func (e *Editor) SetStartPos(mouseDeltaNorm int32) {
e.StartPos = clampF32(e.StartPos+float32(-mouseDeltaNorm)*settings.ScrollSpeed, 0, float32(e.LineCount))
}
func (e *Editor) RefreshFontSettings() {
e.LineHeight = imgui.TextLineHeightWithSpacing()
//NOTE: Because of 'https://github.com/ocornut/imgui/issues/792', CalcTextSize returns slightly incorrect width
//values for sentences than to be expected with singleCharWidth*sentenceCharCount. For example, with 3 chars at width 10
//we expect width of 30 (for a fixed-width font), but instead we might get 29.
// This is fixed in the newer releases, but imgui-go hasn't updated yet.
//
//That's why instead of getting width of one char, we get the average width from the width of a sentence, which helps us position
//cursors properly for now
e.CharWidth = imgui.CalcTextSizeV("abcdefghijklmnopqrstuvwxyz", false, 1000).X / 26
}
func (e *Editor) RoundToGridX(x float32) float32 {
return clampF32(float32(math.Round(float64(x/e.CharWidth)))*e.CharWidth, 0, math.MaxFloat32)
}
func (e *Editor) RoundToGridY(x float32) float32 {
return clampF32(float32(math.Round(float64(x/e.LineHeight)))*e.LineHeight, 0, math.MaxFloat32)
}
func (e *Editor) UpdateAndDraw(drawStartPos, winSize *imgui.Vec2, newRunes []rune) {
//Draw window
imgui.SetNextWindowPos(*drawStartPos)
imgui.SetNextWindowSize(*winSize)
imgui.BeginV("editorText", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove)
imgui.PushStyleColorVec4(imgui.ColFrameBg, settings.EditorBgColor)
imgui.PushStyleColorVec4(imgui.ColTextSelectedBg, settings.TextSelectionColor)
imgui.SetNextItemWidth(winSize.X)
// We want different lables so multiple editors don't mess eaach other, but we don't want the label to show
// so we prefix the whole thing with ##
if imgui.InputTextMultiline("##"+e.FilePath, &e.FileContents, imgui.Vec2{X: winSize.X - winSize.X*0.02, Y: winSize.Y - winSize.Y*0.02}, imgui.InputTextFlagsNone|imgui.InputTextFlagsAllowTabInput, nil) {
e.IsModified = true
}
// @NOTE: Commented out until rewrite that doesn't use imgui as an editor is complete
// //Draw window
// imgui.SetNextWindowPos(*drawStartPos)
// imgui.SetNextWindowSize(*winSize)
// imgui.BeginV("editorText", nil, imgui.WindowFlagsNoCollapse|imgui.WindowFlagsNoDecoration|imgui.WindowFlagsNoMove)
// imgui.PushStyleColor(imgui.StyleColorFrameBg, settings.EditorBgColor)
// imgui.PushStyleColor(imgui.StyleColorTextSelectedBg, settings.TextSelectionColor)
// //Add padding to text
// drawStartPos.X += textPadding
// drawStartPos.Y += textPadding
// paddedDrawStartPos := *drawStartPos
// //Make edits
// posInfo := e.getPositions(&paddedDrawStartPos)
// e.Insert(&posInfo, newRunes)
// if input.KeyClickedCaptured(sdl.K_LEFT) {
// e.MoveMouseXByChars(-1, &posInfo)
// } else if input.KeyClickedCaptured(sdl.K_RIGHT) {
// e.MoveMouseXByChars(1, &posInfo)
// }
// if input.KeyClickedCaptured(sdl.K_UP) {
// e.MoveMouseYByLines(-1, &posInfo)
// } else if input.KeyClickedCaptured(sdl.K_DOWN) {
// e.MoveMouseYByLines(1, &posInfo)
// }
// if input.KeyClickedCaptured(sdl.K_BACKSPACE) {
// e.Delete(&posInfo, 1)
// }
// //Draw text
// dl := imgui.WindowDrawList()
// linesToDraw := int(winSize.Y / e.LineHeight)
// startLine := clampInt(int(e.StartPos), 0, e.LineCount)
// for i := startLine; i < startLine+linesToDraw; i++ {
// dl.AddText(*drawStartPos, imgui.PackedColorFromVec4(imgui.Vec4{X: 1, Y: 1, Z: 1, W: 1}), string(e.GetLine(0+i).chars))
// drawStartPos.Y += e.LineHeight
// }
// tabCount, charsToOffsetBy := getTabs(posInfo.Line, posInfo.GridXEditor)
// textWidth := float32(len(posInfo.Line.chars)-tabCount+tabCount*settings.TabSize) * e.CharWidth
// lineX := clampF32(float32(posInfo.GridXGlobal)+float32(charsToOffsetBy)*e.CharWidth, 0, paddedDrawStartPos.X+textWidth)
// lineStart := imgui.Vec2{
// X: lineX,
// Y: paddedDrawStartPos.Y + float32(posInfo.GridYEditor)*e.LineHeight - e.LineHeight*0.25,
// }
// lineEnd := imgui.Vec2{
// X: lineX,
// Y: paddedDrawStartPos.Y + float32(posInfo.GridYEditor)*e.LineHeight + e.LineHeight*0.75,
// }
// dl.AddLineV(lineStart, lineEnd, imgui.PackedColorFromVec4(settings.CursorColor), settings.CursorWidthFactor*e.CharWidth)
// // charAtCursor := getCharFromCursor(clickedLine, clickedColGridXEditor)
// // println("Chars:", "'"+charAtCursor+"'", ";", clickedColGridXEditor)
}
func (e *Editor) Insert(posInfo *MousePosInfo, rs []rune) {
if len(rs) == 0 {
return
}
e.IsModified = true
l := posInfo.Line
if len(l.chars) == 0 {
l.chars = append(l.chars, rs...)
return
}
charIndex := getCharIndexFromCursor(posInfo.Line, posInfo.GridXEditor)
if charIndex == -1 {
charIndex = 0
} else if charIndex == len(l.chars)-1 {
l.chars = append(l.chars, rs...)
return
} else {
charIndex++
}
//Make a new array that can accomodate the changes
c := l.chars
newLength := len(c) + len(rs)
l.chars = make([]rune, newLength)
//Copy the left half (before the changes), then copy the changes in the new space, and
//lastly copy the pushed elements to the right of the changes
copy(l.chars, c[:charIndex])
copy(l.chars[charIndex:], rs)
copy(l.chars[charIndex+len(rs):], c[charIndex:])
e.MoveMouseXByChars(len(rs), posInfo)
}
func (e *Editor) Delete(posInfo *MousePosInfo, count int) {
l := posInfo.Line
if len(l.chars) == 0 {
return
}
e.IsModified = true
if count >= len(l.chars) {
l.chars = []rune{}
return
}
charIndex := getCharIndexFromCursor(l, posInfo.GridXEditor)
if charIndex == -1 {
return
}
//Count tabs that will be deleted
tabCount := 0
for i := charIndex - count + 1; i < charIndex+1; i++ {
if l.chars[i] == '\t' {
tabCount++
}
}
l.chars = append(l.chars[:charIndex-count+1], l.chars[charIndex+1:]...)
if tabCount == 0 {
e.MoveMouseXByChars(-1, posInfo)
} else {
e.MoveMouseXByChars(-tabCount*settings.TabSize, posInfo)
}
}
func (e *Editor) MoveMouseXByChars(charCount int, posInfo *MousePosInfo) {
if posInfo.GridXEditor == 0 && charCount < 0 {
return
}
delta := float32(charCount) * e.CharWidth
e.MouseX = int(e.RoundToGridX(float32(e.MouseX) + delta))
posInfo.GridXGlobal = int(e.RoundToGridX(float32(posInfo.GridXGlobal) + delta))
posInfo.GridXEditor = int(e.RoundToGridX(float32(posInfo.GridXEditor) + delta))
}
func (e *Editor) MoveMouseYByLines(lineCount int, posInfo *MousePosInfo) {
if lineCount < 0 && posInfo.LineNum == 0 {
return
}
if lineCount > 0 && posInfo.LineNum == e.LineCount {
return
}
delta := float32(lineCount) * e.LineHeight
e.MouseY = int(e.RoundToGridY(float32(e.MouseY) + delta))
posInfo.GridYGlobal = int(e.RoundToGridY(float32(posInfo.GridYGlobal) + delta))
posInfo.GridYEditor = int(e.RoundToGridY(float32(posInfo.GridYEditor) + delta))
}
func (e *Editor) getPositions(paddedDrawStartPos *imgui.Vec2) MousePosInfo {
//Calculate position of cursor in window and grid coords.
//Window coords are as reported by SDL, but we correct for padding and snap to the nearest
//char window pos.
//
//Since Gopad only supports fixed-width fonts, we treat the text area as a grid with each
//cell having identical width and one char inside.
//
//'Global' suffix means the position is in window coords.
//'Editor' suffix means coords are within the text editor coords, where sidebar and tabs have been adjusted for
roundedMouseX := e.RoundToGridX(float32(e.MouseX))
gridXGlobal := clampInt(int(roundedMouseX), 0, math.MaxInt)
roundedMouseY := e.RoundToGridY(float32(e.MouseY))
gridYGlobal := clampInt(int(roundedMouseY), 0, math.MaxInt)
gridXEditor := int(
roundF32(
(float32(gridXGlobal) - paddedDrawStartPos.X) / e.CharWidth,
),
)
windowYEditor := clampInt(e.MouseY-int(paddedDrawStartPos.Y), 0, math.MaxInt)
gridYEditor := clampInt(windowYEditor/int(e.LineHeight), 0, e.LineCount)
startLineIndex := clampInt(int(e.StartPos), 0, e.LineCount)
return MousePosInfo{
GridXGlobal: gridXGlobal,
GridYGlobal: gridYGlobal,
GridXEditor: gridXEditor,
GridYEditor: gridYEditor,
Line: e.GetLine(startLineIndex + gridYEditor),
LineNum: startLineIndex + gridYEditor,
}
}
func getCharFromCursor(l *Line, cursorGridX int) string {
i := getCharIndexFromCursor(l, cursorGridX)
if i == -1 {
return ""
}
return string(l.chars[i])
}
func getCharIndexFromCursor(l *Line, cursorGridX int) int {
if cursorGridX <= 0 || len(l.chars) == 0 {
return -1
}
gridSize := 0
for i := 0; i < len(l.chars) && i <= cursorGridX; i++ {
if l.chars[i] == '\t' {
gridSize += settings.TabSize
} else {
gridSize++
}
if gridSize < cursorGridX {
continue
}
return i
}
if cursorGridX >= gridSize {
return len(l.chars) - 1
}
return -1
}
// TODO: The offset chars must be how many grid cols between cursor col and the nearest non-tab char.
func getTabs(l *Line, gridPosX int) (tabCount, charsToOffsetBy int) {
charIndex := getCharIndexFromCursor(l, gridPosX)
if charIndex == -1 {
return 0, 0
}
//gridSize represents the visual grid (e.g. \tHi has a visual grid size of 'tabSize+2')
gridSize := 0
for i := charIndex; i >= 0; i-- {
if l.chars[i] == '\t' {
tabCount++
gridSize += settings.TabSize
} else {
gridSize++
}
}
if l.chars[charIndex] != '\t' {
return tabCount, 0
}
return tabCount, gridSize - gridPosX
}
func (e *Editor) GetLine(lineNum int) *Line {
if lineNum > e.LineCount {
return &Line{
chars: []rune{},
}
}
curr := e.LinesHead
//Advance to correct node
nodeNum := lineNum / linesPerNode
lineNum -= nodeNum * linesPerNode
for nodeNum > 0 {
curr = curr.Next
nodeNum--
}
return &curr.Lines[lineNum]
}
func (e *Editor) GetLineCharCount(lineNum int) int {
curr := e.LinesHead
//Advance to correct node
nodeNum := lineNum / linesPerNode
lineNum -= nodeNum * linesPerNode
for nodeNum > 0 {
curr = curr.Next
nodeNum--
}
return len(curr.Lines[lineNum].chars)
}
func ParseLines(fileContents string) (*LinesNode, int) {
head := NewLineNode()
if len(fileContents) == 0 {
return head, 0
}
lineCount := 0
start := 0
end := 0
currLine := 0
currNode := head
// @PERF: Would be a lot faster to use something like bytes index
for i := 0; i < len(fileContents); i++ {
if fileContents[i] != '\n' {
end++
continue
}
lineCount++
currNode.Lines[currLine].chars = []rune(fileContents[start:end])
end++
start = end
currLine++
if currLine == linesPerNode {
currLine = 0
currNode.Next = NewLineNode()
currNode = currNode.Next
}
}
if fileContents[len(fileContents)-1] != '\n' {
lineCount++
currNode.Lines[currLine].chars = []rune(fileContents[start:end])
}
return head, lineCount
}
func NewLineNode() *LinesNode {
n := LinesNode{}
for i := 0; i < len(n.Lines); i++ {
n.Lines[i].chars = []rune{}
}
return &n
}
func clampF32(x, min, max float32) float32 {
if x > max {
return max
}
if x < min {
return min
}
return x
}
func clampInt(x, min, max int) int {
if x > max {
return max
}
if x < min {
return min
}
return x
}
func roundF32(x float32) float32 {
return float32(math.Round(float64(x)))
}
func NewScratchEditor() Editor {
e := Editor{
FileName: "**scratch**",
LinesHead: NewLineNode(),
}
return e
}
func NewEditor(fPath string) Editor {
b, err := os.ReadFile(fPath)
if err != nil {
panic(err)
}
e := Editor{
FileName: filepath.Base(fPath),
FilePath: fPath,
FileContents: string(b),
}
e.RefreshFontSettings()
e.LinesHead, e.LineCount = ParseLines(e.FileContents)
return e
}