-
-
Notifications
You must be signed in to change notification settings - Fork 81
/
ved.v
2208 lines (2118 loc) · 51.3 KB
/
ved.v
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
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by a GPL license
// that can be found in the LICENSE file.
module main
import gg
import gx
import os
import time
import uiold
import clipboard
const exe_dir = os.dir(os.executable())
const home_dir = os.home_dir()
const settings_dir = os.join_path(home_dir, '.ved')
const codeblog_path = os.join_path(home_dir, 'code', 'blog')
const syntax_dir = os.join_path(settings_dir, 'syntax')
const session_path = os.join_path(settings_dir, 'session')
const workspaces_path = os.join_path(settings_dir, 'workspaces')
const timer_path = os.join_path(settings_dir, 'timer')
const tasks_path = os.join_path(settings_dir, 'tasks')
const config_path = os.join_path(settings_dir, 'conf.toml')
const config_path2 = os.join_path(settings_dir, 'config.json')
const max_nr_workspaces = 10
@[heap]
struct Ved {
mut:
win_width int
win_height int
nr_splits int
page_height int
views []View
cur_split int
view &View = unsafe { nil }
mode EditorMode
just_switched bool // for keydown/char events to avoid dup keys
prev_key gg.KeyCode
prev_key_str string // for `ci(` etc, no `(` in gg.KeyCode
prev_cmd string
prev_insert string // for `.` (re-enter the text that was just entered via cw etc)
all_git_files []string
top_tasks []string
gg &gg.Context = unsafe { nil }
query string
search_query string
query_type QueryType
workspace string
workspace_idx int
workspaces []string
ylines []string // for y, yy
git_diff_plus string // short git diff stat top right
git_diff_minus string
syntaxes []Syntax
current_syntax_idx int
chunks []Chunk
is_building bool
timer Timer
task_start_unix i64
cur_task string
words []string
file_y_pos map[string]int // to save current line for each file s
refresh bool = true
char_width int
is_ml_comment bool
gg_lines []string
gg_pos int
cfg Config
cb &clipboard.Clipboard = unsafe { nil }
open_paths [][]string // all open files (tabs) per workspace: open_paths[workspace_idx] == ['a.txt', b.v']
prev_y int // for jumping back ('')
now time.Time // cached value of time.now() to avoid calling it for every frame
search_history []string
search_idx int
cq_in_a_row int
search_dir string // for cmd+/ search in the entire directory where the current file is located
search_dir_idx int // for looping thru search dir files
error_line string // is displayed at the bottom
autocomplete_info AutocompleteInfo
autocomplete_cache map[string][]AutocompleteField // autocomplete_cache["v.checker.Checker"] == [{"AnonFn", "void"}, {"cur_anon_fn", "AnonFn"}]
debug_info string
debugger Debugger
cur_fn_name string // Always displayed on the top bar
// debugger_output DebuggerOutput
}
// For syntax highlighting
enum ChunkKind {
a_string = 1
a_comment = 2
a_key = 3
a_lit = 4
}
enum EditorMode {
normal = 0
insert = 1
query = 2
visual = 3
timer = 4
autocomplete = 5
debugger = 6
}
struct Chunk {
start int
end int
typ ChunkKind
}
struct ViSize {
width int
height int
}
const help_text = '
Usage: ved [options] [files]
Options:
-h, --help Display this information.
-window <window-name> Launch in a window.
-dark Launch in dark mode.
-two_splits
'
const fpath = os.resource_abs_path('RobotoMono-Regular.ttf')
const args = os.args.clone()
const is_window = '-window' in args
@[console]
fn main() {
if '-h' in args || '--help' in args {
println(help_text)
return
}
if !os.is_dir(settings_dir) {
os.mkdir(settings_dir) or { panic(err) }
}
mut size := gg.screen_size()
if size.width == 0 || size.height == 0 {
size = $if small_window ? { gg.Size{770, 480} } $else { gg.Size{2560, 1440} }
}
println('size=${size}')
mut ved := &Ved{
win_width: size.width
win_height: size.height
// nr_splits: nr_splits
// nr_splits: nr_splits
cur_split: 0
mode: .normal
cb: clipboard.new()
open_paths: [][]string{len: max_nr_workspaces}
}
ved.handle_segfault()
// ved.cfg.set_settings(config_path)
println('CONFIG')
println(ved.cfg)
ved.load_config2()
ved.cfg.set_default_values()
ved.nr_splits = ved.get_nr_splits_from_screen_size(size.width, size.height)
ved.calc_nr_splits_from_text_size()
println('splits per w = ${ved.nr_splits}')
println('height=${size.height}')
ved.load_syntaxes()
ved.gg = gg.new_context(
width: size.width
height: size.height // borderless_window: !is_window
fullscreen: !is_window
window_title: 'Ved'
create_window: true
user_data: ved
scale: 2
bg_color: ved.cfg.bgcolor
frame_fn: frame
event_fn: on_event
keydown_fn: key_down
char_fn: on_char
font_path: fpath
ui_mode: true
)
println('full screen=${!is_window}')
ved.timer = new_timer(mut ved.gg)
ved.load_all_tasks()
// TODO linux and windows
// C.AXUIElementCreateApplication(234)
uiold.reg_key_ved()
// Open workspaces or a file
$if debug {
println('args:')
println(args)
}
mut cur_dir := os.getwd()
if cur_dir.ends_with('/ved.app/Contents/Resources') {
cur_dir = cur_dir.replace('/ved.app/Contents/Resources', '')
}
mut first_launch := false
if args.len == 1 {
// No args, open previous saved workspaces.
if workspaces := os.read_lines(workspaces_path) {
for workspace in workspaces {
ved.add_workspace(workspace)
}
} else {
first_launch = true
ved.add_workspace('.')
}
ved.open_workspace(0)
}
// Open a single text file
else if args.len == 2 && os.is_file(args.last()) {
path := args[args.len - 1]
if !os.exists(path) {
println('file "${path}" does not exist')
exit(1)
}
println('PATH="${path}" cur_dir="${cur_dir}"')
if !os.is_dir(path) && !path.starts_with('-') {
mut workspace := os.dir(path)
ved.add_workspace(workspace)
ved.open_workspace(0)
ved.view.open_file(path, 0)
}
}
// Open multiple workspaces
else {
println('open multiple workspaces')
for i, arg in args {
println(arg)
if i == 0 {
continue
}
if arg.starts_with('-') {
continue
}
// relative path
if !arg.starts_with('/') {
ved.add_workspace(cur_dir + '/' + arg)
} else {
// absolute path
ved.add_workspace(arg)
}
}
if ved.workspaces.len == 0 {
first_launch = true
ved.add_workspace(cur_dir)
}
ved.open_workspace(0)
}
ved.load_session()
ved.load_timer()
println('first_launch=${first_launch}')
if ved.workspaces.len == 1 && first_launch && !os.exists(session_path) {
ved.view.open_file(os.join_path(exe_dir, 'welcome.txt'), 0)
}
spawn ved.loop()
ved.refresh = true
ved.gg.run()
}
fn on_event(e &gg.Event, mut ved Ved) {
ved.refresh = true
ved.win_height = gg.window_size().height
ved.win_width = gg.window_size().width
if e.typ == .mouse_scroll {
if e.scroll_y < -0.2 {
ved.view.j()
} else if e.scroll_y > 0.2 {
ved.view.k()
}
}
// FIXME: The rounding math here cause the Y coord to be unintuitive sometimes.
if e.typ == .mouse_down {
if ved.cfg.disable_mouse {
return
}
mut view := ved.view
mut current_line := ''
if view.y > 0 && view.y < view.lines.len {
current_line = view.lines[view.y]
}
current_line_split := current_line.split('\t')
mut leading_tabs := 0
for i := 0; i < current_line_split.len; i++ {
if current_line_split[i] == '' {
leading_tabs++
}
}
// Focus the pane currently under the cursor before continuing
for i := 0; i < ved.nr_splits; i++ {
sw := ved.split_width()
starting_x := 2 * i * sw
ending_x := 2 * (i + 1) * sw
if e.mouse_x > starting_x && e.mouse_x < ending_x {
ved.cur_split = i
ved.update_view()
}
}
clicked_y := int((e.mouse_y / ved.cfg.line_height - 1.5) / 2) + ved.view.from
if clicked_y >= view.lines.len {
if view.lines.len == 0 {
view.set_y(0)
} else {
view.set_y(view.lines.len - 1)
}
} else if clicked_y < 0 {
view.set_y(1)
} else {
view.set_y(clicked_y)
}
// Wow, that's a lot of math that is probably pretty hard to parse.
// In the future I need to separate this into several variables,
// and perhaps even its own function.
clicked_x := int(((e.mouse_x - ved.cur_split * ved.split_width() * 2 - view.padding_left) / ved.cfg.char_width) / 2 - 3 - leading_tabs * 3)
if view.lines.len <= 0 {
return
}
if clicked_x > view.lines[view.y].len {
view.x = view.lines[view.y].len
} else if clicked_x < 0 {
view.x = 0
} else {
view.x = clicked_x
}
}
}
fn (ved &Ved) split_width() int {
mut split_width := ved.win_width / ved.nr_splits // + 60
if split_width < 300 {
split_width = ved.win_width
}
return split_width
}
fn frame(mut ved Ved) {
// if !ved.refresh {
// return
// }
// println('frame() ${time.now()}')
ved.gg.begin()
ved.draw()
if ved.mode == .timer {
ved.timer.draw()
}
ved.gg.end()
ved.refresh = false
}
fn (ved &Ved) draw_cursor(cursor_x int, y int) {
mut width := ved.cfg.char_width
// println('CURSOR WIDTH=${ved.cfg.char_width}')
match ved.cfg.cursor_style {
.block {
width = ved.cfg.char_width
}
.beam {
width = 1
}
.variable {
if ved.mode == .insert {
width = 1
} else if ved.mode == .visual {
// FIXME: This looks terrible.
// ved.gg.draw_rect_filled(cursor_x, y, 1, ved.cfg.line_height, ved.cfg.cursor_color)
// ved.gg.draw_rect_filled(cursor_x + ved.cfg.char_width, y, 1, ved.cfg.line_height, ved.cfg.cursor_color)
} else {
width = ved.cfg.char_width
}
}
}
ved.gg.draw_rect_empty(cursor_x, y, width, ved.cfg.line_height, ved.cfg.cursor_color)
}
fn (ved &Ved) calc_cursor_x() int {
line := ved.view.line()
// Tab offset for cursor
mut cursor_tab_off := 0
for i := 0; i < line.len && i < ved.view.x; i++ {
// if rune != '\t' {
if int(line[i]) != ved.cfg.tab {
break
}
cursor_tab_off++
}
from := ved.workspace_idx * ved.nr_splits
split_width := ved.split_width()
line_x := split_width * (ved.cur_split - from) + ved.view.padding_left + 10
mut cursor_x := line_x + (ved.view.x + cursor_tab_off * ved.cfg.tab_size) * ved.cfg.char_width
if cursor_tab_off > 0 {
// If there's a tab, need to shift the cursor to the left by nr of tabsl
cursor_x -= ved.cfg.char_width * cursor_tab_off
}
return cursor_x
}
fn (ved &Ved) calc_cursor_y() int {
y := (ved.view.y - ved.view.from) * ved.cfg.line_height + ved.cfg.line_height
return y
}
fn (mut ved Ved) draw() {
mut view := ved.view
split_width := ved.split_width()
ved.page_height = ved.win_height / ved.cfg.line_height - 1
view.page_height = ved.page_height
// Splits from and to
from, to := ved.get_splits_from_to()
// Not a full refresh? Means we need to refresh only current split.
if !ved.refresh {
// split_x := split_width * (ved.cur_split - from)
// ved.gg.draw_rect_filled(split_x, 0, split_width - 1, ved.win_height, ved.cfg.bgcolor)
}
// Coords
y := ved.calc_cursor_y()
// Cur line
line_x := split_width * (ved.cur_split - from) + ved.view.padding_left + 10
line_width := split_width - ved.view.padding_left - 10
ved.gg.draw_rect_filled(line_x, y, line_width, ved.cfg.line_height, ved.cfg.vcolor)
// V selection
mut v_from := ved.view.vstart + 1
mut v_to := ved.view.vend + 1
if view.vend < view.vstart {
// Swap start and end if we go beyond the start
v_from = ved.view.vend + 1
v_to = ved.view.vstart + 1
}
for yy := v_from; yy <= v_to; yy++ {
ved.gg.draw_rect_filled(line_x, (yy - ved.view.from) * ved.cfg.line_height, line_width,
ved.cfg.line_height, ved.cfg.vcolor)
}
// Black title background
ved.gg.draw_rect_filled(0, 0, ved.win_width, ved.cfg.line_height, ved.cfg.title_color)
// Current split has dark blue title
// ved.gg.draw_rect_filled(split_x, 0, split_width, ved.cfg.line_height, gx.rgb(47, 11, 105))
// Title (file paths)
for i := to - 1; i >= from; i-- {
v := ved.views[i]
mut name := v.short_path
if v.changed && !v.path.ends_with('/out') {
name = '${name} [+]'
}
ved.gg.draw_text(ved.split_x(i - from) + v.padding_left + 10, 1, name, ved.cfg.file_name_cfg)
}
// Git diff stats
if ved.git_diff_plus != '+' {
ved.gg.draw_text(ved.win_width - 400, 1, ved.git_diff_plus, ved.cfg.plus_cfg)
}
if ved.git_diff_minus != '-' {
ved.gg.draw_text(ved.win_width - 350, 1, ved.git_diff_minus, ved.cfg.minus_cfg)
}
// Workspaces
nr_spaces := ved.workspaces.len
cur_space := ved.workspace_idx + 1
space_name := short_space(ved.workspace)
ved.gg.draw_text(ved.win_width - 220, 1, '[${space_name}]', ved.cfg.file_name_cfg)
ved.gg.draw_text(ved.win_width - 100, 1, '${cur_space}/${nr_spaces}', ved.cfg.file_name_cfg)
// Time
ved.gg.draw_text(ved.win_width - 50, 1, ved.now.hhmm(), ved.cfg.file_name_cfg)
// ved.gg.draw_text(ved.win_width - 550, 1, now.hhmmss(), file_name_cfg)
// vim top right next to current time
/*
if ved.start_unix > 0 {
minutes := '1m' //ved.timer.minutes()
ved.gg.draw_text(ved.win_width - 300, 1, '${minutes}m' !,
ved.cfg.file_name_cfg)
}
*/
if ved.cur_task != '' {
// Draw current task
task_text_width := ved.cur_task.len * ved.cfg.char_width
task_x := ved.win_width - split_width - task_text_width - 70
// ved.timer.gg.draw_text(task_x, 1, ved.timer.cur_task.to_upper(), file_name_cfg)
ved.gg.draw_text(task_x, 1, ved.cur_task, ved.cfg.file_name_cfg)
// Draw current task time
task_time_x := (ved.nr_splits - 1) * split_width - 50
ved.gg.draw_text(task_time_x, 1, '${ved.task_minutes()}m', ved.cfg.file_name_cfg)
}
// Draw pomodoro timer
if ved.timer.pom_is_started {
ved.gg.draw_text(split_width - 50, 1, '${ved.pomodoro_minutes()}m', ved.cfg.file_name_cfg)
}
// Draw "i" in insert mode
if ved.mode == .insert {
ved.gg.draw_text(5, 1, '-i-', ved.cfg.file_name_cfg)
}
// Draw "v" in visual mode
if ved.mode == .visual {
ved.gg.draw_text(5, 1, '-v-', ved.cfg.file_name_cfg)
}
// Splits
// println('\nsplit from=$from to=$to nrviews=$ved.views.len refresh=$ved.refresh')
for i := to - 1; i >= from; i-- {
// J or K is pressed (full refresh disabled for performance), only redraw current split
if !ved.refresh && i != ved.cur_split {
// continue
}
// t := glfw.get_time()
ved.draw_split(i, from)
// println('draw split $i: ${ glfw.get_time() - t }')
}
// Cur fn name (top right of current split)
cur_fn_width := ved.cfg.char_width * ved.cur_fn_name.len
cur_fn_x := (ved.cur_split % ved.nr_splits + 1) * split_width - cur_fn_width - 3
cur_fn_y := ved.cfg.line_height
ved.gg.draw_rect(
x: cur_fn_x
y: cur_fn_y
w: cur_fn_width
h: ved.cfg.line_height
color: ved.cfg.bgcolor // gx.rgb(40, 40, 40)
)
ved.gg.draw_text(cur_fn_x, cur_fn_y, ved.cur_fn_name, ved.cfg.comment_cfg)
// Debugger variables
if ved.mode == .debugger && ved.debugger.output.vars.len > 0 {
ved.draw_debugger_variables()
}
// Cursor
mut cursor_x := ved.calc_cursor_x()
ved.draw_cursor(cursor_x, y)
// ved.gg.draw_text_def(cursor_x + 500, y - 1, 'tab=$cursor_tab_off x=$cursor_x view_x=$ved.view.x')
// query window
if ved.mode == .query {
ved.draw_query()
} else if ved.mode == .autocomplete {
ved.draw_autocomplete_window()
}
// Big red error line at the bottom
if ved.error_line != '' {
ved.gg.draw_rect_filled(0, ved.win_height - ved.cfg.line_height, ved.win_width,
ved.cfg.line_height, ved.cfg.errorbgcolor)
ved.gg.draw_text(3, ved.win_height - ved.cfg.line_height, ved.error_line, gx.TextCfg{
size: ved.cfg.text_size
color: gx.white
align: gx.align_left
})
}
}
fn (ved &Ved) split_x(i int) int {
return ved.split_width() * i
}
fn (mut ved Ved) draw_split(i int, split_from int) {
view := ved.views[i]
ved.is_ml_comment = false
split_width := ved.split_width()
split_x := split_width * (i - split_from)
// Vertical split line
ved.gg.draw_line(split_x, ved.cfg.line_height + 1, split_x, ved.win_height, ved.cfg.split_color)
// Lines
mut line_nr := 1 // relative y
for j := view.from; j < view.from + ved.page_height && j < view.lines.len; j++ {
line := view.lines[j]
if line.len > 5000 {
println('line len too big! views[${i}].lines[${j}] (${line.len}) path=${ved.view.path}')
continue
}
x := split_x + view.padding_left
y := line_nr * ved.cfg.line_height
// Error bg
if view.error_y == j {
ved.gg.draw_rect_filled(x + 10, y - 1, split_width - view.padding_left - 10,
ved.cfg.line_height, ved.cfg.errorbgcolor)
}
// Breakpoint red circle
if view.breakpoints.contains(j) {
ved.gg.draw_circle_filled(split_x + 3, y + ved.cfg.line_height / 2 - 1, 5,
gx.red)
}
// Breakpoint yellow line
if ved.mode == .debugger && ved.cur_split == i && ved.debugger.output.line_nr != 0
&& ved.debugger.output.line_nr == j + 1 {
line_width := split_width - view.padding_left - 10
ved.gg.draw_rect_filled(x + 10, y, line_width, ved.cfg.line_height, breakpoint_color)
}
// Line number
line_number := j + 1
ved.gg.draw_text(x + 3, y, '${line_number}', ved.cfg.line_nr_cfg)
// Tab offset
mut line_x := x + 10
mut nr_tabs := 0
// for k := 0; k < line.len; k++ {
for c in line {
if c != `\t` {
break
}
nr_tabs++
line_x += ved.cfg.char_width * ved.cfg.tab_size
}
mut s := line[nr_tabs..] // tabs have been skipped, remove them from the string
if s == '' {
line_nr++
continue
}
// Number of chars to display in this view
// mut max := (split_width - view.padding_left - ved.cfg.char_width * TAB_SIZE *
// nr_tabs) / ved.cfg.char_width - 1
max := ved.max_chars(i, nr_tabs)
if view.y == j {
// Display entire line if its current
// if line.len > max {
// ved.gg.draw_rect_filled(line_x, y - 1, ved.win_width, line_height, vcolor)
// }
// max = line.len
}
// if s.contains('width :=') {
// println('"$s" max=$max')
//}
// Handle utf8 codepoints
// old_len := s.len
if s.len != s.len_utf8() {
u := s.runes()
if max > 0 && max < u.len {
s = u[..max].string()
}
} else {
if max > 0 && max < s.len {
s = s[..max]
}
}
if view.hl_on {
// println('line="$s" nrtabs=$nr_tabs line_x=$line_x')
ved.draw_text_line(line_x, y, s, os.file_ext(view.path))
} else {
ved.gg.draw_text(line_x, y, s, ved.cfg.txt_cfg)
}
// if old_len != s.len {
// ved.draw_text_line(line_x, y + 1, '!!!')
//}
line_nr++
}
}
fn (ved &Ved) max_chars(view_idx int, nr_tabs int) int {
width := ved.split_width() - ved.views[view_idx].padding_left - ved.cfg.char_width * ved.cfg.tab_size * nr_tabs
return width / ved.cfg.char_width - 1
}
fn (mut ved Ved) add_chunk(typ ChunkKind, start int, end int) {
chunk := Chunk{
typ: typ
start: start
end: end
}
ved.chunks << chunk
}
fn (mut ved Ved) draw_text_line(x int, y int, line string, ext string) {
// mcomment := get_mcomment_by_ext(os.file_ext(ved.view.path))
mcomment := get_mcomment_by_ext(ext)
// Red/green test hack
/*
if line.contains('[32m') && line.contains('PASS') {
ved.gg.draw_text(x, y, line[5..], ved.cfg.green_cfg)
return
} else if line.contains('[31m') && line.contains('FAIL') {
ved.gg.draw_text(x, y, line[5..], ved.cfg.red_cfg)
return
}
*/
// } else if line[0] == `-` {
ved.chunks = []
cur_syntax := ved.syntaxes[ved.current_syntax_idx] or { Syntax{} }
// TODO use runes for everything to fix keyword + 2+ byte rune words
for i := 0; i < line.len; i++ {
start := i
// Comment // #
if i > 0 && line[i - 1] == `/` && line[i] == `/` {
ved.add_chunk(.a_comment, start - 1, line.len)
break
}
if line[i] == `#` {
ved.add_chunk(.a_comment, start, line.len)
break
}
// Comment /*
// (unless it's /* line */ which is a single line)
if i > 0 && line[i - 1] == mcomment.start1 && line[i] == mcomment.start2
&& !(line[line.len - 2] == mcomment.end1 && line[line.len - 1] == mcomment.end2) {
// All after /* is a comment
ved.add_chunk(.a_comment, start, line.len)
ved.is_ml_comment = true
break
}
// End of /**/
if i > 0 && line[i - 1] == mcomment.end1 && line[i] == mcomment.end2 {
// All before */ is still a comment
ved.add_chunk(.a_comment, 0, start + 1)
ved.is_ml_comment = false
break
}
// String
if line[i] == `'` {
i++
for i < line.len - 1 && line[i] != `'` {
i++
}
if i >= line.len {
i = line.len - 1
}
ved.add_chunk(.a_string, start, i + 1)
}
if line[i] == `"` {
i++
for i < line.len - 1 && line[i] != `"` {
i++
}
if i >= line.len {
i = line.len - 1
}
ved.add_chunk(.a_string, start, i + 1)
}
// Key
for i < line.len && is_alpha_underscore(int(line[i])) {
i++
}
word := line[start..i]
// println('word="$word"')
if word in cur_syntax.literals {
// println('$word is key')
ved.add_chunk(.a_lit, start, i)
// println('adding key. len=$ved.chunks.len')
} else if word in cur_syntax.keywords {
// println('$word is key')
ved.add_chunk(.a_key, start, i)
// println('adding key. len=$ved.chunks.len')
}
}
if ved.is_ml_comment {
ved.gg.draw_text(x, y, line, ved.cfg.comment_cfg)
return
}
if ved.chunks.len == 0 {
// println('no chunks')
ved.gg.draw_text(x, y, line, ved.cfg.txt_cfg)
return
}
mut pos := 0
// println('"$line" nr chunks=$ved.chunks.len')
// TODO use runes
// runes := msg.runes.slice_fast(chunk.pos, chunk.end)
// txt := join_strings(runes)
for i, chunk in ved.chunks {
// println('chunk #$i start=$chunk.start end=$chunk.end typ=$chunk.typ')
// Initial text chunk (not necessarily initial, but the one right before current chunk,
// since we don't have a seperate chunk for text)
if chunk.start > pos {
s := line[pos..chunk.start]
ved.gg.draw_text(x + pos * ved.cfg.char_width, y, s, ved.cfg.txt_cfg)
}
// Keyword, literal, string etc
typ := chunk.typ
cfg := match typ {
.a_key { ved.cfg.key_cfg }
.a_lit { ved.cfg.lit_cfg }
.a_string { ved.cfg.string_cfg }
.a_comment { ved.cfg.comment_cfg }
}
s := line[chunk.start..chunk.end]
ved.gg.draw_text(x + chunk.start * ved.cfg.char_width, y, s, cfg)
pos = chunk.end
// Final text chunk
if i == ved.chunks.len - 1 && chunk.end < line.len {
final := line[chunk.end..line.len]
ved.gg.draw_text(x + pos * ved.cfg.char_width, y, final, ved.cfg.txt_cfg)
}
}
}
fn key_down(key gg.KeyCode, mod gg.Modifier, mut ved Ved) {
super := mod == .super
shift := mod == .shift
if key == .escape {
if ved.mode == .visual {
ved.exit_visual()
}
ved.mode = .normal
}
// Reset error line
ved.view.error_y = -1
ved.error_line = ''
match ved.mode {
.normal { ved.key_normal(key, mod) }
.visual { ved.key_visual(key, super, shift) }
.insert { ved.key_insert(key, mod) }
.query { ved.key_query(key, super) }
.timer { ved.timer.key_down(key, super) }
.autocomplete { ved.key_insert(key, mod) }
.debugger { ved.key_normal(key, mod) }
}
ved.gg.refresh_ui()
}
@[manualfree]
fn on_char(code u32, mut ved Ved) {
if ved.just_switched {
ved.just_switched = false
return
}
mut buf := [5]u8{}
s := unsafe { utf32_to_str_no_malloc(code, mut &buf[0]) }
// s := utf32_to_str(code)
// println('s="$s" code="$code"')
match ved.mode {
.insert, .autocomplete {
ved.char_insert(s)
}
.query {
ved.gg_pos = -1
ved.char_query(s)
}
.normal {
// on char on normal only for replace with r
if !ved.just_switched && ved.prev_key == .r {
if s != 'r' {
ved.view.r(s)
ved.prev_key = gg.KeyCode.invalid
ved.prev_cmd = 'r'
ved.prev_insert = s.clone()
}
return
}
ved.prev_key_str = s // for `ci(` etc, no `(` in gg.KeyCode
}
else {}
}
}
/*
fn (ved &Ved) is_in_blog() bool {
return ved.view.path.contains('/blog/') && ved.view.path.contains('20')
}
*/
fn (ved &Ved) git_commit() {
text := ved.query
dir := ved.workspace
os.system('git -C ${dir} commit -am "${text}"')
// os.system('gitter $dir')
}
fn (mut ved Ved) key_insert(key gg.KeyCode, mod gg.Modifier) {
super := mod == .super || mod == .ctrl
// shift := mod == .shift
match key {
.backspace {
ved.just_switched = true // prevent backspace symbol being added in char handler
ved.view.backspace()
}
.enter {
if false && ved.mode == .autocomplete {
// Pressed enter in autocomplete mode, insert text from selected suggested field
ved.insert_suggested_field()
} else {
ved.view.enter()
}
}
.escape {
ved.mode = .normal
}
.tab {
line := ved.view.line()
if line.ends_with('p ') {
ved.view.insert_text("rintln('')")
ved.view.x -= 2
} else {
ved.view.insert_text('\t')
}
}
.left {
if ved.view.x > 0 {
ved.view.x--
}
}
.right {
ved.view.l()
}
.up {
ved.view.k()
// ved.refresh = false
}
.down {
ved.view.j()
// ved.refresh = false
}
else {}
}
if (key == .l || key == .s) && super {
ved.view.save_file()
ved.mode = .normal
return
}
if super && key == .u {
ved.mode = .normal
ved.key_u()
return
}
// Insert macro TODO customize
if super && key == .g {
ved.view.insert_text('<code></code>')
ved.view.x -= 7
}
// Autocomplete
if key == .n && super {
ved.ctrl_n()
return
}
if key == .v && super {
ved.view.insert_text(ved.cb.paste())
ved.just_switched = true
}
}
fn (mut ved Ved) ctrl_n() {
line := ved.view.line()
mut i := ved.view.x - 1
end := i
for i > 0 && is_alpha_underscore(int(line[i])) {
i--
}
if !is_alpha_underscore(int(line[i])) {
i++
}
mut word := line[i..end + 1]
word = word.trim_space()
// Dont autocomplete if fewer than 3 chars
if word.len < 3 {
return
}
for map_word in ved.words {
// If any word starts with our subword, add the rest
if map_word.starts_with(word) {
ved.view.insert_text(map_word[word.len..])
ved.just_switched = true
return
}
}
}
fn (mut ved Ved) key_normal(key gg.KeyCode, mod gg.Modifier) {
super := mod == .super || mod == .ctrl
shift := mod == .shift
// println('mod=')
// println(int(mod))
shift_and_super := int(mod) == 9
mut view := ved.view
ved.refresh = true
if ved.prev_key == .r {
return
}
if ved.prev_cmd == 'ci' {
println('CALLING CI PREV S=${ved.prev_key_str}')
view.ci(key)
return
}
match key {
.enter {
// Full screen => window
if false && super {
ved.nr_splits = 1
ved.win_width = 600
ved.win_height = 500
// glfw.post_empty_event()
}
}
.period {
if shift {
// >
ved.view.shift_right()
} else {
ved.dot()
}
}
.comma {
if shift {
// <
ved.view.shift_left()
} else if super {
//
// ved.cfg.reload_config()
// ved.update_view()
}
}
.slash {
ved.search_query = ''
ved.mode = .query