This repository has been archived by the owner on Jan 4, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 98
/
pixel-vision-ui-text-editor-v2.lua
executable file
·1530 lines (1204 loc) · 44.2 KB
/
pixel-vision-ui-text-editor-v2.lua
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
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
LoadScript("code-highlighter")
LoadScript("lume")
function EditorUI:CreateTextEditor(rect, text, toolTip, font, colorOffset)
-- local colorShift = colorOffset or 44
-- local text = text or ""
local data = self:CreateData(rect, nil, toolTip)
data.highlighterTheme = {
text = 15, -- White
selection = 14, -- Yellow,
keyword = 13, --
number = 2, --
comment = 6, --
string = 11, --
api = 7, --
callback = 8, --
escape = 15, --
disabled = 5, --
}
if(highlighter ~= nil) then
highlighter:setSyntax("lua")
highlighter:setTheme(data.highlighterTheme)
end
data.lastCX = 0
data.editable = true
data.endOfLineOffset = 1
data.viewPort = NewRect(data.rect.x, data.rect.y, data.rect.w, data.rect.h)
data.cursorPos = {x = 0, y = 0, color = 0}
data.inputDelay = .10
data.flavorBack = 0
data.theme = {
bg = 0, --Background Color
cursor = 0, --Cursor Color
cursorBG = 15 --Cursor BG Color
}
data.cx, data.cy = 1, 1 --Cursor Position
data.fw = self.spriteSize.x
data.fh = self.spriteSize.y --The font character size
data.vx, data.vy = 1, 1 --View postions
data.mflag = false --Mouse flag
data.btimer = 0 --The cursor blink timer
data.btime = 0.5 --The cursor blink time
data.bflag = true --The cursor is blinking atm ?
data.stimer = 0 -- The scroll timer when the mouse is dragging up
data.stime = 0.1 -- The speed of up scrolling when the mouse is dragging up
data.sflag = {x = 0, y = 0} -- Vector for scroll. 0 for no scroll, 1 for scroll down, -1 for scroll up.
data.undoStack = {} -- Keep a stack of undo info, each one is {data, state}
data.redoStack = {} -- Keep a stack of redo info, each one is {data, state}
data.colorize = false --Color lua syntax
data.autoDeselect = true
data.buffer = {}
data.invalidateLine = true
data.invalidateBuffer = true
data.invalidText = true
data.lastKeyCounter = 0
data.tabChar = " "
data.lastKey = ""
-- Set up the draw arguments
data.drawMode = DrawMode.TilemapCache
data.colorOffset = 0
data.spacing = 0
data.font = font or "large"
data.charSize = NewPoint(8, 8)
data.bgMaskDrawArguments = {
data.rect.x,
data.rect.y,
data.rect.w,
data.rect.h,
data.theme.bg,
DrawMode.TilemapCache
}
data.lineMaskDrawArguments = {
data.rect.x,
data.rect.y,
data.rect.w,
data.rect.h,
data.theme.bg,
DrawMode.TilemapCache
}
data.cursorBGDrawArguments = {
0,
0,
data.charSize.X,
data.charSize.Y,
data.theme.cursorBG,
DrawMode.Sprite
}
data.cursorDrawArguments = {
data.blinkChar,
0,
0,
DrawMode.SpriteAbove,
data.font,
data.theme.cursor,
data.spacing
}
-- Create input callbacks. These can be overridden to add special functionality to each input field
data.captureInput = function()
return InputString()
end
data.keymap = {
["return"] = function(targetData)
if targetData.sxs then self:TextEditorDeleteSelection(targetData) end
self:TextEditorInsertNewLine(targetData)
end,
["left"] = function(targetData)
self:TextEditorDeselect(targetData)
local flag = false
targetData.cx = targetData.cx - 1
if targetData.cx < 1 then
if targetData.cy > 1 then
targetData.cy = targetData.cy - 1
targetData.cx = targetData.buffer[targetData.cy]:len() + 1
flag = true
end
end
self:TextEditorResetCursorBlink(targetData)
self:TextEditorCheckPosition(targetData)-- or flag then
self:TextEditorDrawLineNum(targetData)
end,
["right"] = function(targetData)
self:TextEditorDeselect(targetData)
local flag = false
targetData.cx = targetData.cx + 1
if targetData.cx > targetData.buffer[targetData.cy]:len() + 1 then
if targetData.buffer[targetData.cy + 1] then
targetData.cy = targetData.cy + 1
targetData.cx = 1
flag = true
end
end
self:TextEditorResetCursorBlink(targetData)
self:TextEditorCheckPosition(targetData)-- or flag then
self:TextEditorDrawLineNum(targetData)
end,
["shift-up"] = function(targetData)
--in case we want to reduce shift selection
if targetData.cy == 1 then
--we stay in buffer
return
end
if targetData.sxs then
--there is an existing selection to update
targetData.cy = targetData.cy - 1
self:TextEditorCheckPosition(targetData)
targetData.sye = targetData.cy
targetData.sxe = math.min(targetData.cx, #targetData.buffer[targetData.cy])
else
targetData.sxs = targetData.cx
targetData.sys = targetData.cy
targetData.cy = targetData.cy - 1
self:TextEditorCheckPosition(targetData)
targetData.sye = targetData.cy
targetData.sxe = math.min(targetData.cx, #targetData.buffer[targetData.cy])
end
self:TextEditorInvalidateBuffer(targetData)
self:TextEditorDrawLineNum(targetData)
end,
["shift-ctrl-f"] = function(targetData)
self:TextEditorSearchPreviousFunction(targetData)
end,
["ctrl-f"] = function(targetData)
self:TextEditorSearchNextFunction(targetData)
end,
["shift-down"] = function(targetData)
--last line check, we do not go further than buffer
if #targetData.buffer == targetData.cy then
return
end
if targetData.sxs then
targetData.cy = targetData.cy + 1
self:TextEditorCheckPosition(targetData)
targetData.sye = targetData.cy
targetData.sxe = math.min(targetData.cx, #targetData.buffer[targetData.cy])
else
targetData.sxs = targetData.cx
targetData.sys = targetData.cy
targetData.cy = targetData.cy + 1
self:TextEditorCheckPosition(targetData)
targetData.sye = targetData.cy
targetData.sxe = math.min(targetData.cx, #targetData.buffer[targetData.cy])
end
self:TextEditorInvalidateBuffer(targetData)
self:TextEditorDrawLineNum(targetData)
end,
["shift-right"] = function(targetData)
--last line check, we do not go further than buffer
if #targetData.buffer == targetData.cy and targetData.cx == #targetData.buffer[targetData.cy] then
return
end
local originalcx, originalcy = targetData.cx, targetData.cy
targetData.cx = targetData.cx + 1
if targetData.cx > targetData.buffer[targetData.cy]:len() + 1 then
if targetData.buffer[targetData.cy + 1] then
targetData.cy = targetData.cy + 1
targetData.cx = 1
end
end
self:TextEditorCheckPosition(targetData)
if targetData.sxs then
targetData.sye = targetData.cy
targetData.sxe = math.min(targetData.cx, #targetData.buffer[targetData.cy])
else
targetData.sxs = originalcx
targetData.sys = originalcy
targetData.sye = targetData.cy
targetData.sxe = math.min(targetData.cx, #targetData.buffer[targetData.cy])
end
self:TextEditorInvalidateBuffer(targetData)
self:TextEditorDrawLineNum(targetData)
end,
["shift-left"] = function(targetData)
--last line check, we do not go further than buffer
if 0 == targetData.cy and targetData.cx <= 1 then
return
end
local originalcx, originalcy = targetData.cx, targetData.cy
targetData.cx = targetData.cx - 1
if targetData.cx < 1 then
if targetData.cy > 1 then
targetData.cy = targetData.cy - 1
targetData.cx = targetData.buffer[targetData.cy]:len() + 1
end
end
self:TextEditorCheckPosition(targetData)
if targetData.sxs then
targetData.sye = targetData.cy
targetData.sxe = math.min(targetData.cx, #targetData.buffer[targetData.cy])
else
targetData.sxs = originalcx
targetData.sys = originalcy
targetData.sye = targetData.cy
targetData.sxe = math.min(targetData.cx, #targetData.buffer[targetData.cy])
end
self:TextEditorInvalidateBuffer(targetData)
self:TextEditorDrawLineNum(targetData)
end,
["up"] = function(targetData)
self:TextEditorDeselect(targetData)
targetData.cy = targetData.cy - 1
self:TextEditorResetCursorBlink(targetData)
self:TextEditorCheckPosition(targetData)
self:TextEditorDrawLineNum(targetData)
end,
["down"] = function(targetData)
self:TextEditorDeselect(targetData)
targetData.cy = targetData.cy + 1
self:TextEditorResetCursorBlink(targetData)
self:TextEditorCheckPosition(targetData)
self:TextEditorDrawLineNum(targetData)
end,
["backspace"] = function(targetData)
if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
if targetData.sxs then self:TextEditorDeleteSelection(targetData) return end
if targetData.cx == 1 and targetData.cy == 1 then return end
local lineChange
targetData.cx, targetData.cy, lineChange = self:TextEditorDeleteCharAt(targetData, targetData.cx - 1, targetData.cy)
self:TextEditorResetCursorBlink(targetData)
self:TextEditorCheckPosition(targetData)-- or lineChange then self:TextEditorDrawBuffer(targetData) else self:TextEditorDrawLine(targetData) end
self:TextEditorDrawLineNum(targetData)
end,
["delete"] = function(targetData)
if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
if targetData.sxs then self:TextEditorDeleteSelection(targetData) return end
local lineChange
targetData.cx, targetData.cy, lineChange = self:TextEditorDeleteCharAt(targetData, targetData.cx, targetData.cy)
self:TextEditorResetCursorBlink(targetData)
self:TextEditorCheckPosition(targetData)-- or lineChange then self:TextEditorDrawBuffer(targetData) else self:TextEditorDrawLine(targetData) end
self:TextEditorDrawLineNum(targetData)
end,
["home"] = function(targetData) self:TextEditorGotoLineStart(targetData) end,
["end"] = function(targetData) self:TextEditorGotoLineEnd(targetData) end,
["pageup"] = function(targetData)
targetData.vy = targetData.vy - targetData.tiles.h
targetData.cy = targetData.cy - targetData.tiles.h
if targetData.vy < 1 then targetData.vy = 1 end
if targetData.cy < 1 then targetData.cy = 1 end
self:TextEditorResetCursorBlink(targetData)
self:TextEditorInvalidateBuffer(targetData)
-- self:TextEditorDrawBuffer(targetData)
self:TextEditorDrawLineNum(targetData)
end,
["pagedown"] = function(targetData)
targetData.vy = targetData.vy + targetData.tiles.h
targetData.cy = targetData.cy + targetData.tiles.h
local bottom = #targetData.buffer - targetData.tiles.h + 1
if targetData.vy > bottom then targetData.vy = bottom end
if targetData.cy > bottom then targetData.cy = bottom end
self:TextEditorResetCursorBlink(targetData)
self:TextEditorInvalidateBuffer(targetData)
-- self:TextEditorDrawBuffer(targetData)
self:TextEditorDrawLineNum(targetData)
end,
["tab"] = function(targetData)
self:TextEditorTextInput(targetData, targetData.tabChar)
end,
["ctrl-k"] = function(targetData)
if targetData.incsearch == true then
self:TextEditorSearchTextAndNavigate(targetData, targetData.cy)
end
end,
["ctrl-x"] = function(targetData)
if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
self:TextEditorCutText(targetData)
end,
["ctrl-c"] = function(targetData) self:TextEditorCopyText(targetData) end,
["ctrl-v"] = function(targetData)
if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
self:TextEditorPasteText(targetData)
end,
["ctrl-a"] = function(targetData) self:TextEditorSelectAll(targetData) end,
["ctrl-z"] = function(targetData)
if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
self:TextEditorUndo(targetData)
end,
["shift-ctrl-z"] = function(targetData)
if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
self:TextEditorRedo(targetData)
end,
["ctrl-y"] = function(targetData)
if targetData.readonly then _systemMessage("The file is readonly !", 1, 9, 4) return end
self:TextEditorRedo(targetData)
end
}
return data
end
function EditorUI:TextEditorMoveCursor(data, x, y, color)
if(x ~= nil) then
data.cursorPos.x = x
end
if(y ~= nil) then
-- Offset this by 1 since the buffer is not 0 based
data.cursorPos.y = y - 1
end
if(color ~= nil) then
data.cursorPos.color = color
end
return data.cursorPos
end
function EditorUI:TextEditorCursorColor(data, value)
data.cursorPos.color = value
end
function EditorUI:TextEditorDrawCharactersAtCursor(data, text, x, y)
x = x or (data.cursorPos.x * data.charSize.X)
y = y or (data.cursorPos.y * data.charSize.Y)
local length = #text
local tmpChar = ""
for i = 1, length do
data.cursorPos.x = data.cursorPos.x + 1
tmpChar = text:sub(i, i)
if(x >= 0 and tmpChar ~= " ") then
if(x > (data.viewPort.width - 8)) then
return
end
local drawArguments = {
tmpChar,
data.rect.x + x,
data.rect.y + y,
DrawMode.TilemapCache,
data.font,
data.enabled == true and data.cursorPos.color or data.highlighterTheme.disabled,
data.spacing
}
self:NewDraw("DrawText", drawArguments)
end
-- Increase X to the next character
x = x + data.charSize.X
end
end
--A usefull print function with color support !
function EditorUI:TextEditorDrawColoredTextAtCursor(data, tbl)
if type(tbl) == "string" then
self:TextEditorCursorColor(data, data.highlighterTheme.text)
self:TextEditorDrawCharactersAtCursor(data, tbl)--, false, true)
else
for i = 1, #tbl, 2 do
local col = tbl[i]
local txt = tbl[i + 1]
self:TextEditorCursorColor(data, col)
self:TextEditorDrawCharactersAtCursor(data, txt)--, false, true)--Disable auto newline
end
end
end
--Check the position of the cursor so the view includes it
function EditorUI:TextEditorCheckPosition(data)
local flag = false --Flag if the whole buffer requires redrawing
-- Clamp the y position between 1 and the length of the buffer
data.cy = Clamp(data.cy, 1, #data.buffer)
if data.cy > data.tiles.h + data.vy - 1 then --Passed the screen to the bottom
data.vy = data.cy - (data.tiles.h - 1); flag = true
elseif data.cy < data.vy then --Passed the screen to the top
if data.cy < 1 then data.cy = 1 end
data.vy = data.cy; flag = true
end
--X position checking--
if data.buffer[data.cy]:len() < data.cx - 1 then data.cx = data.buffer[data.cy]:len() + data.endOfLineOffset end --Passed the end of the line !
data.cx = Clamp(data.cx, 1, data.buffer[data.cy]:len() + data.endOfLineOffset)
if data.cx > data.tiles.w + (data.vx - 1) then --Passed the screen to the right
data.vx = data.cx - (data.tiles.w - 1); flag = true
elseif data.cx < data.vx then --Passed the screen to the left
if data.cx < 1 then data.cx = 1 end
data.vx = data.cx; flag = true
end
if(flag) then
self:TextEditorInvalidateBuffer(data)
end
return flag
end
-- Make the cursor visible and reset the blink timer
function EditorUI:TextEditorResetCursorBlink(data)
data.btimer = 0
data.bflag = true
end
--Draw the cursor blink
function EditorUI:TextEditorDrawBlink(data)
-- Don't blink if there is a selection or the cursor is off the screen
if data.sxs or data.cx > (data.vx + (data.tiles.w - 1)) then return end
if data.cy - data.vy < 0 or data.cy - data.vy > data.tiles.h then return end
if data.bflag then
local char = data.buffer[data.cy]:sub(data.cx, data.cx)
-- Need to replace empty characters with a space
if(char == "" or char == "\r") then
char = " "
end
data.cursorDrawArguments[1] = char
data.cursorDrawArguments[2] = data.rect.x + (data.cx - data.vx) * (data.fw)
data.cursorDrawArguments[3] = data.rect.y + (data.cy - data.vy) * (data.fh)
data.cursorBGDrawArguments[1] = data.cursorDrawArguments[2]
data.cursorBGDrawArguments[2] = data.cursorDrawArguments[3]
self:NewDraw("DrawRect", data.cursorBGDrawArguments)
self:NewDraw("DrawText", data.cursorDrawArguments)
end
end
--Draw the code on the screen
function EditorUI:TextEditorDrawBuffer(data)
if data.invalidateBuffer == false then return end
self:TextEditorResetBufferValidation(data)
local vbuffer = lume.slice(data.buffer, data.vy, data.vy + data.tiles.h - 1) --Visible buffer
local cbuffer = (data.colorize and highlighter ~= nil) and highlighter:highlightLines(vbuffer, data.vy) or vbuffer
self:NewDraw("DrawRect", data.bgMaskDrawArguments)
for k, l in ipairs(cbuffer) do
-- Draw the line first for the background
self:TextEditorMoveCursor(data, - (data.vx - 2) - 1, k, - 1)
self:TextEditorDrawColoredTextAtCursor(data, l)
local sxs, sys, sxe, sye = self:TextEditorGetOrderedSelect(data)
if sxs and data.vy + k - 1 >= sys and data.vy + k - 1 <= sye then --Selection
self:TextEditorMoveCursor(data, - (data.vx - 2) - 1, k, data.highlighterTheme.selection)
local linelen, skip = vbuffer[k]:len(), 0
if data.vy + k - 1 == sys then --Selection start
skip = sxs - 1
self:TextEditorMoveCursor(data, skip - (data.vx - 2) - 1)
linelen = linelen - skip
end
if data.vy + k - 1 == sye then --Selection end
linelen = sxe - skip
end
if data.vy + k - 1 < sye then --Not the end of the selection
linelen = linelen + 1
end
-- Highlight start
local hs = data.vx + data.cursorPos.x
local he = hs + linelen - 1
local char = data.buffer[data.vy + k - 1]:sub(hs, he)
if(char == "") then
char = " "
end
self:TextEditorDrawCharactersAtCursor(data, char)
end
end
end
function EditorUI:TextEditorDrawLine(data)
-- If the line hasn't been invalidated don't render it
if data.invalidateLine == false then return end
self:TextEditorResetLineValidation(data)
-- If there is a selection we want to draw the buffer instead of the line
if(data.sxs) then
self:TextEditorInvalidateBuffer(data)
return
end
-- get the line's new width to see if it's larger than the max counter
data.maxLineWidth = math.max(#data.buffer[data.cy], data.maxLineWidth)
-- Reset validation
if data.cy - data.vy < 0 or data.cy - data.vy > data.tiles.h - 1 then return end
local cline, colateral
if (data.colorize and highlighter ~= nil) then
cline, colateral = highlighter:highlightLine(data.buffer[data.cy], data.cy)
end
if not cline then cline = data.buffer[data.cy] end
local y = (data.cy - data.vy + 1) * (data.fh)
data.lineMaskDrawArguments[2] = data.rect.y + y - data.charSize.Y
data.lineMaskDrawArguments[4] = data.fh
self:NewDraw("DrawRect", data.lineMaskDrawArguments)
self:TextEditorMoveCursor(data, - (data.vx - 2) - 1, y / data.charSize.Y, data.theme.bg)
if not colateral then
self:TextEditorDrawColoredTextAtCursor(data, cline)
else
self:TextEditorInvalidateBuffer(data)
end
end
function EditorUI:TextEditorInvalidateLine(data)
data.invalidateLine = true
end
function EditorUI:TextEditorResetLineValidation(data)
data.invalidateLine = false
end
function EditorUI:TextEditorInvalidateBuffer(data)
data.invalidateBuffer = true
end
function EditorUI:TextEditorResetBufferValidation(data)
data.invalidateBuffer = false
end
function EditorUI:TextEditorInvalidateText(data)
data.invalidText = true
end
function EditorUI:TextEditorResetTextValidation(data)
data.invalidText = false
end
--Clear the selection just incase
function EditorUI:TextEditorDeselect(data)
if data.sxs then
--print(data.name, "Deselect")
data.sxs, data.sys, data.sxe, data.sye = nil, nil, nil, nil
self:TextEditorInvalidateBuffer(data)
end
end
function EditorUI:TextEditorGetOrderedSelect(data)
if data.sxs then
if data.sye < data.sys then
return data.sxe, data.sye, data.sxs, data.sys
elseif data.sye == data.sys and data.sxe < data.sxs then
return data.sxe, data.sys, data.sxs, data.sye
else
return data.sxs, data.sys, data.sxe, data.sye
end
else
return false
end
end
function EditorUI:TextEditorDrawLineNum(data)
local linestr = "LINE "..tostring(data.cy).."/"..tostring(#data.buffer).." CHAR "..tostring(data.cx - 1).."/"..tostring(data.buffer[data.cy]:len())
end
function EditorUI:TextEditorSearchNextFunction(data)
for i, t in ipairs(data.buffer)
do
if i > data.cy then
if string.find(t, "function ") then
data.cy = i
self:TextEditorCheckPosition(data)
-- Force the buffer to redraw
self:TextEditorInvalidateBuffer(data)
break
end
end
end
end
function EditorUI:TextEditorSearchPreviousFunction(data)
highermatch = -1
for i, t in ipairs(data.buffer)
do
if i < data.cy then
if string.find(t, "function ") then
highermatch = i
end
end
end
if highermatch > - 1 then
data.cy = highermatch
data.vy = highermatch
self:TextEditorCheckPosition(data)
-- Force the buffer to redraw
self:TextEditorInvalidateBuffer(data)
end
end
function EditorUI:TextEditorSearchTextAndNavigate(data, from_line)
for i, t in ipairs(data.buffer)
do
if from_line ~= nil and i > from_line then
if string.find(t, data.searchtxt) then
data.cy = i
data.vy = i
self:TextEditorCheckPosition(data)
self:TextEditorInvalidateBuffer(data)
break
end
end
end
end
function EditorUI:TextEditorTextInput(data, t)
if data.incsearch then
if data.searchtxt == nil then data.searchtxt = "" end
data.searchtxt = data.searchtxt..t
-- note on -1 : that way if search is on line , still works
-- and also ok for ctrl k
self:TextEditorSearchTextAndNavigate(data, data.cy - 1)
self:TextEditorDrawIncSearchState(data)
else
self:TextEditorBeginUndoable(data)
local delsel
if data.sxs then self:TextEditorDeleteSelection(data); delsel = true end
data.buffer[data.cy] = data.buffer[data.cy]:sub(0, data.cx - 1)..t..data.buffer[data.cy]:sub(data.cx, - 1)
data.cx = data.cx + t:len()
-- Update the line length
data.maxLineWidth = math.max(#data.buffer[data.cy], data.maxLineWidth)
self:TextEditorResetCursorBlink(data)
self:TextEditorInvalidateLine(data)
self:TextEditorCheckPosition(data)
self:TextEditorDrawLineNum(data)
self:TextEditorEndUndoable(data)
self:TextEditorInvalidateText(data)
end
end
function EditorUI:TextEditorGotoLineStart(data)
self:TextEditorDeselect(data)
data.cx = 1
self:TextEditorResetCursorBlink(data)
if self:TextEditorCheckPosition(data) then self:TextEditorDrawBuffer(data) else self:TextEditorDrawLine(data) end
end
function EditorUI:TextEditorGotoLineEnd(data)
self:TextEditorDeselect(data)
data.cx = data.buffer[data.cy]:len() + 1
self:TextEditorResetCursorBlink(data)
if self:TextEditorCheckPosition(data) then self:TextEditorDrawBuffer(data) else self:TextEditorDrawLine(data) end
end
function EditorUI:TextEditorInsertNewLine(data)
self:TextEditorBeginUndoable(data)
local newLine = data.buffer[data.cy]:sub(data.cx, - 1)
data.buffer[data.cy] = data.buffer[data.cy]:sub(0, data.cx - 1)
local snum = string.find(data.buffer[data.cy].."a", "%S") --Number of spaces
snum = snum and snum - 1 or 0
newLine = string.rep(" ", snum)..newLine
data.cx, data.cy = snum + 1, data.cy + 1
if data.cy > #data.buffer then
table.insert(data.buffer, newLine)
else
data.buffer = lume.concat(lume.slice(data.buffer, 0, data.cy - 1), {newLine}, lume.slice(data.buffer, data.cy, - 1)) --Insert between 2 different lines
end
self:TextEditorInvalidateBuffer(data)
self:TextEditorResetCursorBlink(data)
self:TextEditorCheckPosition(data)
-- self:TextEditorDrawBuffer(data)
-- self:TextEditorDrawLineNum(data)
self:TextEditorEndUndoable(data)
self:TextEditorInvalidateText(data)
end
-- Delete the char from the given coordinates.
-- If out of bounds, it'll merge the line with the previous or next as it suits
-- Returns the coordinates of the deleted character, adjusted if lines were changed
-- and a boolean "true" if other lines changed and redrawing the Buffer is needed
function EditorUI:TextEditorDeleteCharAt(data, x, y)
self:TextEditorBeginUndoable(data)
local lineChange = false
-- adjust "y" if out of bounds, just as failsafe
if y < 1 then y = 1 elseif y > #data.buffer then y = #data.buffer end
-- newline before the start of line == newline at end of previous line
if y > 1 and x < 1 then
y = y - 1
x = data.buffer[y]:len() + 1
end
-- join with next line (delete newline) when deleting past the boundaries of the line
if x > data.buffer[y]:len() and y < #data.buffer then
data.buffer[y] = data.buffer[y]..data.buffer[y + 1]
data.buffer = lume.concat(lume.slice(data.buffer, 0, y), lume.slice(data.buffer, y + 2, - 1))
lineChange = true
else
data.buffer[y] = data.buffer[y]:sub(0, x - 1) .. data.buffer[y]:sub(x + 1, - 1)
end
if lineChange then self:TextEditorInvalidateBuffer(data) else self:TextEditorInvalidateLine(data) end
self:TextEditorEndUndoable(data)
self:TextEditorInvalidateText(data)
return x, y, lineChange
end
--Will delete the current selection
function EditorUI:TextEditorDeleteSelection(data)
if not data.sxs then return end --If not selection just return back.
local sxs, sys, sxe, sye = self:TextEditorGetOrderedSelect(data)
self:TextEditorBeginUndoable(data)
local lnum, slength = sys, sye + 1
while lnum < slength do
if lnum == sys and lnum == sye then --Single line selection
data.buffer[lnum] = data.buffer[lnum]:sub(1, sxs - 1) .. data.buffer[lnum]:sub(sxe + 1, - 1)
lnum = lnum + 1
elseif lnum == sys then
data.buffer[lnum] = data.buffer[lnum]:sub(1, sxs - 1)
lnum = lnum + 1
elseif lnum == slength - 1 then
data.buffer[lnum - 1] = data.buffer[lnum - 1] .. data.buffer[lnum]:sub(sxe + 1, - 1)
data.buffer = lume.concat(lume.slice(data.buffer, 1, lnum - 1), lume.slice(data.buffer, lnum + 1, - 1))
slength = slength - 1
else --Middle line
data.buffer = lume.concat(lume.slice(data.buffer, 1, lnum - 1), lume.slice(data.buffer, lnum + 1, - 1))
slength = slength - 1
end
end
data.cx, data.cy = sxs, sys
self:TextEditorCheckPosition(data)
self:TextEditorDeselect(data)
self:TextEditorInvalidateBuffer(data)
self:TextEditorEndUndoable(data)
self:TextEditorInvalidateText(data)
end
--Copy selection text (Only if selecting)
function EditorUI:TextEditorCopyText(data)
local sxs, sys, sxe, sye = self:TextEditorGetOrderedSelect(data)
if sxs then --If there are any selection
local clipbuffer = {}
for lnum = sys, sye do
local line = data.buffer[lnum]
if lnum == sys and lnum == sye then --Single line selection
line = line:sub(sxs, sxe)
elseif lnum == sys then
line = line:sub(sxs, - 1)
elseif lnum == sye then
line = line:sub(1, sxe)
end
table.insert(clipbuffer, line)
end
local clipdata = table.concat(clipbuffer, "\n")
self:TextEditorClipboard(clipdata)
end
end
--Cut selection text
function EditorUI:TextEditorCutText(data)
if data.sxs then
self:TextEditorCopyText(data)
self:TextEditorDeleteSelection(data)
end
end
-- Paste the text from the clipboard
function EditorUI:TextEditorPasteText(data)
self:TextEditorBeginUndoable(data)
if data.sxs then self:TextEditorDeleteSelection(data) end
local text = self:TextEditorClipboard()
if(text == nil) then
return
end
text = text:gsub("\t", " ") -- tabs mess up the layout, replace them with spaces
local firstLine = true
for line in string.gmatch(text.."\n", "([^\r\n]*)\r?\n") do
if not firstLine then
self:TextEditorInsertNewLine(data) data.cx = 1
else
firstLine = false
end
self:TextEditorTextInput(data, line)
end
if self:TextEditorCheckPosition(data) then self:TextEditorDrawBuffer(data) else self:TextEditorDrawLine(data) end
self:TextEditorDrawLineNum(data)
self:TextEditorEndUndoable(data)
self:TextEditorInvalidateText(data)
end
--Select all text
function EditorUI:TextEditorSelectAll(data)
data.sxs, data.sys = 1, 1
data.sye = #data.buffer
data.sxe = data.buffer[data.sye]:len()
self:TextEditorInvalidateBuffer(data)
end
-- Call :TextEditorBeginUndoable(data) right before doing any modification to the
-- text in the editor. It will capture the current state of the editor's
-- contents (data) and the state of the cursor, selection, etc. (state)
-- so it can be restored later.
-- NOTE: Make sure to balance each call to :TextEditorBeginUndoable(data) with a call
-- to :TextEditorEndUndoable(data). They can nest fine, just don't forget one.
function EditorUI:TextEditorBeginUndoable(data)
if data.currentUndo then
-- we have already stashed the data & state, just track how deep we are
data.currentUndo.count = data.currentUndo.count + 1
else
-- make a new in-progress undo
data.currentUndo = {
count = 1, -- here is where we track nested begin/endUndoable calls
data = self:TextEditorExport(data),
state = self:TextEditorGetState(data)
}
end
end
-- Call :TextEditorEndUndoable(data) after each modification to the text in the editor.
function EditorUI:TextEditorEndUndoable(data)
-- We might be inside several nested calls to begin/endUndoable
data.currentUndo.count = data.currentUndo.count - 1
-- If this was the last of the nesting
if data.currentUndo.count == 0 then
-- then push the undo onto the undo stack.
table.insert(data.undoStack, {
data.currentUndo.data,
data.currentUndo.state
})
-- clear the redo stack
data.redoStack = {}
data.currentUndo = nil
end
end
-- Perform an undo. This will pop one entry off the undo
-- stack and restore the editor's contents & cursor state.
function EditorUI:TextEditorUndo(data)
if #data.undoStack == 0 then
-- beep?
return
end
-- pull one entry from the undo stack
local text, state = unpack(table.remove(data.undoStack))
-- push a new entry onto the redo stack
table.insert(data.redoStack, {
self:TextEditorExport(data),
self:TextEditorGetState(data)
})
-- restore the editor contents