-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4coder_sloth_draw.cpp
More file actions
886 lines (777 loc) · 37.6 KB
/
4coder_sloth_draw.cpp
File metadata and controls
886 lines (777 loc) · 37.6 KB
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
function Cursor_Kind get_cursor_kind(String_Const_u8 str){
return (string_match(str, string_u8_litexpr("|")) ? Cursor_Thin :
string_match(str, string_u8_litexpr("_")) ? Cursor_Under :
string_match(str, string_u8_litexpr("L")) ? Cursor_Corner : Cursor_Rect);
}
function Rect_f32 rect_vsplit(Rect_f32 r, f32 t, b32 c){ return !c ? rect_split_left_right(r, t).min : rect_split_left_right_neg(r, t).max; }
function Rect_f32 rect_hsplit(Rect_f32 r, f32 t, b32 c){ return !c ? rect_split_top_bottom(r, t).min : rect_split_top_bottom_neg(r, t).max; }
function void
sloth_draw_file_bar(Application_Links *app, View_ID view_id, Buffer_ID buffer, Face_ID face_id, Rect_f32 bar) {
Scratch_Block scratch(app);
FColor base_color = fcolor_id(defcolor_base);
FColor pop2_color = fcolor_id(defcolor_pop2);
i64 cursor_position = view_get_cursor_pos(app, view_id);
Buffer_Cursor cursor = view_compute_cursor(app, view_id, seek_pos(cursor_position));
String_Const_u8 unique_name = push_buffer_unique_name(app, scratch, buffer);
i64 mark_position = view_get_mark_pos(app, view_id);
Range_i64 range = Ii64(cursor_position, mark_position);
Range_i64 cursor_range = get_line_range_from_pos(app, buffer, cursor_position);
Range_i64 mark_range = get_line_range_from_pos(app, buffer, mark_position);
Range_i64 range_lines = range_union(cursor_range, mark_range);
i64 line0 = get_line_number_from_pos(app, buffer, range.min);
i64 line1 = get_line_number_from_pos(app, buffer, range.max);
b32 passive = view_get_is_passive(app, view_id);
// NOTE(sloth): bg
draw_rectangle_fcolor(app, bar, 0.f, fcolor_id(defcolor_bar));
if(def_get_config_b32(vars_save_string_lit("enable_progress_bar"))) {
f32 progress = (f32)cursor.line / (f32)buffer_get_line_count(app, buffer);
Rect_f32 progress_bar_rect = {
bar.x0 + (bar.x1 - bar.x0) * progress,
bar.y0,
bar.x1,
bar.y1,
};
ARGB_Color progress_bar_color = fcolor_resolve(fcolor_id(lily_color_file_progress_bar));
draw_rectangle(app, progress_bar_rect, 0, progress_bar_color);
}
// NOTE(sloth): text line
Fancy_Line list = {0};
u8 space[3]; {
Dirty_State dirty = buffer_get_dirty_state(app, buffer);
String_u8 str = Su8(space, 0, 3);
if (HasFlag(dirty, DirtyState_UnsavedChanges)) {
string_append(&str, string_u8_litexpr("*"));
}
if (HasFlag(dirty, DirtyState_UnloadedChanges)) {
string_append(&str, string_u8_litexpr("!"));
}
if (dirty != 0) {
string_append(&str, string_u8_litexpr(" "));
}
push_fancy_string(scratch, &list, pop2_color, str.string);
}
push_fancy_string(scratch, &list, base_color, unique_name);
push_fancy_stringf(scratch, &list, base_color, ":%lld:%lld [ch:%lld|ln:%lld]", cursor.line, cursor.col, range.max - range.min, (i64) abs_f32((f32)line0 - line1));
Managed_Scope scope = buffer_get_managed_scope(app, buffer);
Line_Ending_Kind *eol_setting = scope_attachment(app, scope, buffer_eol_setting, Line_Ending_Kind);
switch (*eol_setting) {
case LineEndingKind_Binary: {
push_fancy_string(scratch, &list, base_color, string_u8_litexpr(" bin"));
break;
}
case LineEndingKind_LF: {
push_fancy_string(scratch, &list, base_color, string_u8_litexpr(" nix"));
break;
}
case LineEndingKind_CRLF: {
push_fancy_string(scratch, &list, base_color, string_u8_litexpr(" win"));
break;
}
}
View_ID aview = get_active_view(app, Access_Always);
b32 active = aview == view_id;
b32 next = view_id == get_next_view_looped_primary_panels(app, aview, Access_Always) && view_id != aview;
b32 prev = view_id == get_prev_view_looped_primary_panels(app, aview, Access_Always) && view_id != aview;
if (passive || active || next || prev)
push_fancy_string(scratch, &list, base_color, string_u8_litexpr(" ["));
if (prev)
push_fancy_string(scratch, &list, base_color, string_u8_litexpr("b"));
else
push_fancy_string(scratch, &list, 0, string_u8_litexpr(" "));
if (passive)
push_fancy_string(scratch, &list, base_color, string_u8_litexpr("p")); // NOTE(sloth): p is for passive, that's good enough for me
else
push_fancy_string(scratch, &list, 0, string_u8_litexpr(" "));
if (active)
push_fancy_string(scratch, &list, base_color, string_u8_litexpr("a"));
else
push_fancy_string(scratch, &list, 0, string_u8_litexpr(" "));
if (next)
push_fancy_string(scratch, &list, base_color, string_u8_litexpr("f"));
else
push_fancy_string(scratch, &list, 0, string_u8_litexpr(" "));
if (passive || active || next || prev)
push_fancy_string(scratch, &list, base_color, string_u8_litexpr("]"));
Vec2_f32 p = bar.p0 + V2f32(2.f, 2.f);
draw_fancy_line(app, face_id, fcolor_zero(), &list, p);
}
//~ NOTE(rjf): Divider Comments
String_Const_u8 strong_divider_comment_signifier = string_u8_litexpr("//~");
String_Const_u8 weak_divider_comment_signifier = string_u8_litexpr("//-");
function i64
_F4_Boundary_DividerComment(Application_Links *app, Buffer_ID buffer,
Side side, Scan_Direction direction, i64 pos,
String_Const_u8 signifier) {
i64 result = pos;
Scratch_Block scratch(app);
Token_Array tokens = get_token_array_from_buffer(app, buffer);
if(tokens.tokens != 0) {
Token_Iterator_Array it = token_iterator_pos(0, &tokens, pos);
switch(direction) {
case Scan_Forward: {
for(;token_it_inc_non_whitespace(&it);) {
Token *token = token_it_read(&it);
if(token == 0) {
break;
}
if(token->kind == TokenBaseKind_Comment) {
String_Const_u8 str = push_buffer_range(app, scratch, buffer, Ii64(token));
if(str.size >= signifier.size &&
string_match(string_substring(str, Ii64(0, signifier.size)), signifier)) {
result = token->pos;
break;
}
}
}
}break;
case Scan_Backward: {
for(;token_it_dec_non_whitespace(&it);) {
Token *token = token_it_read(&it);
if(token == 0) {
break;
}
if(token->kind == TokenBaseKind_Comment) {
String_Const_u8 str = push_buffer_range(app, scratch, buffer, Ii64(token));
if(str.size >= signifier.size &&
string_match(string_substring(str, Ii64(0, signifier.size)), signifier)) {
result = token->pos;
break;
}
}
}
}break;
}
}
return(result);
}
function i64
F4_Boundary_DividerComment(Application_Links *app, Buffer_ID buffer,
Side side, Scan_Direction direction, i64 pos,
String_Const_u8 signifier) {
return _F4_Boundary_DividerComment(app, buffer, side, direction, pos, strong_divider_comment_signifier);
}
CUSTOM_COMMAND_SIG(lily_find_major_divider_down)
CUSTOM_DOC("Find //~ divider comments below the cursor")
{
Scratch_Block scratch(app);
current_view_scan_move(app, Scan_Forward, push_boundary_list(scratch, F4_Boundary_DividerComment));
}
CUSTOM_COMMAND_SIG(lily_find_major_divider_up)
CUSTOM_DOC("Find //~ divider comments above the cursor")
{
Scratch_Block scratch(app);
current_view_scan_move(app, Scan_Backward, push_boundary_list(scratch, F4_Boundary_DividerComment));
}
function void
F4_RenderDividerComments(Application_Links *app, Buffer_ID buffer, View_ID view, Text_Layout_ID text_layout_id) {
if(!def_get_config_b32(vars_save_string_lit("f4_disable_divider_comments"))) {
ProfileScope(app, "[F4] Divider Comments");
Scratch_Block scratch(app);
Token_Array token_array = get_token_array_from_buffer(app, buffer);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
if(token_array.tokens != 0) {
i64 first_index = token_index_from_pos(&token_array, visible_range.first);
Token_Iterator_Array it = token_iterator_index(0, &token_array, first_index);
Token *token = 0;
for(;;) {
token = token_it_read(&it);
if(token->pos >= visible_range.one_past_last || !token || !token_it_inc_non_whitespace(&it))
break;
if(token->kind == TokenBaseKind_Comment) {
Rect_f32 comment_first_char_rect = text_layout_character_on_screen(app, text_layout_id, token->pos);
Rect_f32 comment_last_char_rect = text_layout_character_on_screen(app, text_layout_id, token->pos+token->size-1);
String_Const_u8 token_string = push_buffer_range(app, scratch, buffer, Ii64(token));
String_Const_u8 signifier_substring = string_substring(token_string, Ii64(0, 3));
f32 roundness = 4.f;
// NOTE(rjf): Strong dividers.
if(string_match(signifier_substring, strong_divider_comment_signifier)) {
Rect_f32 rect = {
comment_first_char_rect.x0,
comment_first_char_rect.y0-2,
10000,
comment_first_char_rect.y0,
};
draw_rectangle(app, rect, roundness, fcolor_resolve(fcolor_id(defcolor_comment)));
}
// NOTE(rjf): Weak dividers.
else if(string_match(signifier_substring, weak_divider_comment_signifier)) {
f32 dash_size = 8;
Rect_f32 rect = {
comment_last_char_rect.x1,
(comment_last_char_rect.y0 + comment_last_char_rect.y1)/2 - 1,
comment_last_char_rect.x1 + dash_size,
(comment_last_char_rect.y0 + comment_last_char_rect.y1)/2 + 1,
};
for(int i = 0; i < 1000; i += 1) {
draw_rectangle(app, rect, roundness, fcolor_resolve(fcolor_id(defcolor_comment)));
rect.x0 += dash_size*1.5f;
rect.x1 += dash_size*1.5f;
}
}
}
}
}
}
}
function void
sloth_draw_hex_color(Application_Links *app, View_ID view, Buffer_ID buffer, Text_Layout_ID text_layout_id){
Scratch_Block scratch(app);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
String_Const_u8 buffer_string = push_buffer_range(app, scratch, buffer, visible_range);
for (i64 i = 0; i+9 < range_size(visible_range); i += 1){
u8 *str = buffer_string.str+i;
bool s0 = str[0] != '0';
bool s1 = str[1] != 'x';
if (s0 || s1){ continue; }
b32 all_hex = true;
for (i64 j = 0; j < 8; j += 1){
u8 c = str[j+2];
bool is_digit = '0' <= c && c <= '9';
bool is_lower = 'a' <= c && c <= 'f';
bool is_upper = 'A' <= c && c <= 'F';
if (!(is_digit || is_lower || is_upper)) { all_hex=false; break; }
}
if (!all_hex){ continue; }
i64 pos = visible_range.min + i;
Rect_f32 r0 = text_layout_character_on_screen(app, text_layout_id, pos+0);
Rect_f32 r1 = text_layout_character_on_screen(app, text_layout_id, pos+9);
Rect_f32 rect = rect_inner(rect_union(r0, r1), -1.f);
ARGB_Color color = ARGB_Color(string_to_integer(SCu8(str+2, 8), 16));
u32 sum = ((color >> 16) & 0xFF) + ((color >> 8) & 0xFF) + (color & 0xFF);
ARGB_Color contrast = ARGB_Color(0xFF000000 | (i32(sum > 330)-1));
// draw_rectangle_outline(app, rect_inner(rect, -1.f), 4.f, 2.f, contrast);
draw_rectangle(app, rect, 4.f, color);
paint_text_color(app, text_layout_id, Ii64_size(pos, 10), contrast);
i += 9;
}
}
function void
sloth_draw_scopes(Application_Links *app, View_ID view, Buffer_ID buffer, Text_Layout_ID text_layout_id, f32 width){
Scratch_Block scratch(app);
i64 pos = view_get_cursor_pos(app, view); // Get current cursor position
Rect_f32 prev_clip = draw_set_clip(app, Rect_f32{}); // Save previous clip rect and set to full view
Rect_f32 view_rect = prev_clip;
view_rect.x0 -= 3.f; // Extend view slightly left for the scope indicators
Face_ID face_id = get_face_id(app, buffer);
Face_Metrics face_metrics = get_face_metrics(app, face_id);
// Get all nested scope ranges (brackets/parens) containing the cursor
Range_i64_Array ranges = get_enclosure_ranges(app, scratch, buffer, pos, FindNest_Scope);
for (i64 i = 0; i < ranges.count; i += 1){
// Color: first scope uses preprocessor color, others use ghost character color
FColor color = fcolor_id(i == 0 ? defcolor_preproc : defcolor_line_numbers_text);
Range_i64 range = ranges.ranges[i];
range.max -= 1;
// Check if range starts/ends with brackets or parens
u8 c0 = buffer_get_char(app, buffer, range.min);
u8 c1 = buffer_get_char(app, buffer, range.max);
if ((c0 != '{' && c0 != '(') || (c1 != '}' && c1 != ')')){ continue; }
// Color the actual bracket/paren characters
paint_text_color_pos(app, text_layout_id, range.min, color);
paint_text_color_pos(app, text_layout_id, range.max, color);
// Skip if opening and closing are on same line
i64 line0 = get_line_number_from_pos(app, buffer, range.min);
i64 line1 = get_line_number_from_pos(app, buffer, range.max);
if (line0 == line1){ continue; }
// NOTE(lily): Here's the important part for the scope visualization!
Rect_f32 scope_rect = qol_get_abs_block_rect(app, view, buffer, text_layout_id, range);
scope_rect.x0 += 3.f; // move to the right
// Create clipping rectangle for the rounded corner drawing
Rect_f32 clip_rect = scope_rect; // this would draw the whole rectangle
clip_rect.x1 += -5.f; // -1.f removes right
clip_rect.y0 += face_metrics.line_height; // +1.f removes top
clip_rect.y1 += -face_metrics.line_height; // -1.f removes bottom
draw_set_clip(app, rect_intersect(view_rect, clip_rect));
// This is where the actual rounded rectangle is drawn!
draw_rectangle_outline_fcolor(app, scope_rect, 5.f, 2.f, color);
}
// Rest of the function handles drawing the scope labels...
draw_set_clip(app, prev_clip);
Token_Array tokens = get_token_array_from_buffer(app, buffer);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
for (i64 i=0 ; i < ranges.count; i += 1){
Range_i64 range = ranges.ranges[ranges.count-1-i];
i64 line0 = get_line_number_from_pos(app, buffer, range.min);
i64 line1 = get_line_number_from_pos(app, buffer, range.max);
if (line0 == line1 || range.max >= visible_range.max){ continue; }
Token *start_token = 0;
Token_Iterator_Array it = token_iterator_pos(0, &tokens, range.min-1);
i64 paren_level = 0;
for (;;){
Token *token = token_it_read(&it);
if (token == 0){ break; }
paren_level += (token->kind == TokenBaseKind_ParentheticalClose);
paren_level -= (token->kind == TokenBaseKind_ParentheticalOpen);
if (paren_level == 0){
if (token->kind == TokenBaseKind_ScopeClose ||
(token->kind == TokenBaseKind_StatementClose && token->sub_kind != TokenCppKind_Colon)) {
break;
} else if (token->kind == TokenBaseKind_Identifier ||
token->kind == TokenBaseKind_Keyword ||
token->kind == TokenBaseKind_Comment ||
token->kind == TokenBaseKind_Control) {
start_token = token;
break;
}
}
if (!token_it_dec_non_whitespace(&it))
break;
}
//- Scope end text labels
if (start_token){
i64 line = get_line_number_from_pos(app, buffer, start_token->pos);
String_Const_u8 text = push_buffer_line(app, scratch, buffer, line);
i64 ws_count = 0;
for (u64 j = 0; j < text.size; j += 1){
if (text.str[j] > 32){ break; }
ws_count++;
}
text.str += ws_count;
text.size -= ws_count;
text.size -= (text.str[text.size-1] == '\r');
i64 end_pos = get_line_end_pos(app, buffer, line1)-1;
Rect_f32 end_rect = text_layout_character_on_screen(app, text_layout_id, end_pos);
// NOTE(sloth): the correct height of the scope labels is half the diff of the current font and the small face
Face_Metrics small_metrics = get_face_metrics(app, qol_small_face);
f32 diff = face_metrics.line_height - small_metrics.line_height;
Vec2_f32 p0 = V2f32(end_rect.x1 + face_metrics.space_advance, end_rect.y0 + (diff*.5f));
draw_string(app, qol_small_face, text, p0, fcolor_id(defcolor_ghost_character));
}
}
}
function void
sloth_draw_function_tooltip_inner(Application_Links *app, Arena *arena, Buffer_ID buffer, Code_Index_Note *note, Range_f32 x_range, i64 depth, Range_i64 range){
Range_i64 def_range = note->pos;
find_nest_side(app, note->file->buffer, def_range.end+1, FindNest_Paren|FindNest_Balanced|FindNest_EndOfToken, Scan_Forward, NestDelim_Close, &def_range.end);
String_Const_u8 sig = push_buffer_range(app, arena, note->file->buffer, def_range);
sig = string_condense_whitespace(arena, sig);
Face_Metrics metrics = get_face_metrics(app, qol_small_face);
f32 wid = 2.f;
f32 pad = 4.f;
Rect_f32 rect = {};
Vec2_f32 cur_p0 = V2f32(f32_floor32(qol_cur_cursor_pos.x), f32_floor32(qol_cur_cursor_pos.y));
rect.p0 = cur_p0 + V2f32(0, 2.f + depth*(1.f + metrics.line_height + pad + 2.f*wid));
rect.p1 = rect.p0 + V2f32(sig.size*metrics.normal_advance, wid + metrics.line_height);
f32 x_offset = ((rect.x1 > x_range.max)*(rect.x1 - x_range.max) -
(rect.x0 < x_range.min)*(rect.x0 - x_range.max));
rect.x0 -= x_offset;
rect.x1 -= x_offset;
rect = rect_inner(rect, -pad);
draw_rectangle_fcolor(app, rect, 3.f, fcolor_id(defcolor_back));
draw_rectangle_outline_fcolor(app, rect, 3.f, wid, fcolor_id(depth == 1 ? defcolor_cursor : defcolor_ghost_character));
draw_string(app, qol_small_face, sig, rect.p0 + (pad + wid)*V2f32(1,1), fcolor_id(defcolor_text_default));
}
function void
sloth_draw_function_tooltip(Application_Links *app, Buffer_ID buffer, Range_f32 x_range, i64 pos){
Token_Array tokens = get_token_array_from_buffer(app, buffer);
if (tokens.tokens == 0){ return; }
Token_Iterator_Array it = token_iterator_pos(0, &tokens, pos);
Token *token = token_it_read(&it);
if (token->kind == TokenBaseKind_ParentheticalOpen){
pos = token->pos + token->size;
} else if (token_it_dec_all(&it)){
token = token_it_read(&it);
if (token->kind == TokenBaseKind_ParentheticalClose && pos == token->pos + token->size){
pos = token->pos;
}
}
i64 count = 0;
Scratch_Block scratch(app);
Range_i64_Array ranges = get_enclosure_ranges(app, scratch, buffer, pos, FindNest_Paren);
code_index_lock();
for (i64 i=0; i < ranges.count; i += 1){
Scratch_Block temp(app, scratch);
Range_i64 range = ranges.ranges[i];
i64 f_pos = range.min-1;
Token_Iterator_Array cur_it = token_iterator_pos(0, &tokens, f_pos);
token = token_it_read(&cur_it);
if (token->kind == TokenBaseKind_Identifier){
String_Const_u8 lexeme = push_token_lexeme(app, temp, buffer, token);
Code_Index_Note *note = code_index_note_from_string(lexeme);
if (note == NULL){ continue; }
if (note->note_kind == CodeIndexNote_Function ||
note->note_kind == CodeIndexNote_Macro)
{
sloth_draw_function_tooltip_inner(app, temp, buffer, note, x_range, ++count, range);
}
}
}
code_index_unlock();
}
function void
sloth_draw_compile_errors(Application_Links *app, Buffer_ID buffer, Text_Layout_ID text_layout_id, Buffer_ID jump_buffer){
if (jump_buffer == 0){ return; }
Scratch_Block scratch(app);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
FColor cl_error = fcolor_blend(fcolor_id(defcolor_highlight_junk), 0.6f, fcolor_id(defcolor_text_default));
Managed_Scope scopes[2];
scopes[0] = buffer_get_managed_scope(app, jump_buffer);
scopes[1] = buffer_get_managed_scope(app, buffer);
Managed_Scope comp_scope = get_managed_scope_with_multiple_dependencies(app, scopes, ArrayCount(scopes));
Managed_Object *markers_object = scope_attachment(app, comp_scope, sticky_jump_marker_handle, Managed_Object);
i32 count = managed_object_get_item_count(app, *markers_object);
Marker *markers = push_array(scratch, Marker, count);
managed_object_load_data(app, *markers_object, 0, count, markers);
for (i32 i = 0; i < count; i += 1){
i64 line_number = get_line_number_from_pos(app, buffer, markers[i].pos);
Range_i64 line_range = get_line_pos_range(app, buffer, line_number);
if (!range_overlap(visible_range, line_range)){ continue; }
String_Const_u8 comp_line_string = push_buffer_line(app, scratch, jump_buffer, markers[i].line);
Parsed_Jump jump = parse_jump_location(comp_line_string);
if (!jump.success){ continue; }
i64 end_pos = get_line_end_pos(app, buffer, line_number)-1;
Rect_f32 end_rect = text_layout_character_on_screen(app, text_layout_id, end_pos);
Vec2_f32 p0 = V2f32(end_rect.x1, end_rect.y0 + 4.f);
String_Const_u8 error_string = string_skip(comp_line_string, jump.colon_position + 2);
draw_line_highlight(app, text_layout_id, line_number, fcolor_id(defcolor_highlight_junk));
draw_string(app, qol_small_face, error_string, p0, cl_error);
}
}
function void
sloth_draw_cursor_mark(Application_Links *app, View_ID view_id, b32 is_active_view,
Buffer_ID buffer, Text_Layout_ID text_layout_id,
f32 roundness, f32 outline_thickness){
b32 has_highlight_range = draw_highlight_range(app, view_id, buffer, text_layout_id, roundness);
i64 cursor_pos = view_get_cursor_pos(app, view_id);
i64 mark_pos = view_get_mark_pos(app, view_id);
Rect_f32 nxt_cursor_rect = text_layout_character_on_screen(app, text_layout_id, cursor_pos);
Rect_f32 cur_cursor_rect = Rf32_xy_wh(qol_cur_cursor_pos, rect_dim(nxt_cursor_rect));
if (is_active_view && nxt_cursor_rect.x1 > 0.f)
qol_nxt_cursor_pos = nxt_cursor_rect.p0;
if (!has_highlight_range){
Scratch_Block scratch(app);
Cursor_Kind cursor_kind = get_cursor_kind(def_get_config_string(scratch, vars_save_string_lit("cursor_style")));
Cursor_Kind mark_kind = get_cursor_kind(def_get_config_string(scratch, vars_save_string_lit("mark_style")));
ARGB_Color cl_cursor = fcolor_resolve(fcolor_id(defcolor_cursor, default_cursor_sub_id()));
ARGB_Color cl_mark = fcolor_resolve(fcolor_id(defcolor_mark));
if (is_active_view && cursor_kind == Cursor_Rect && rect_overlap(nxt_cursor_rect, cur_cursor_rect)){
paint_text_color_pos(app, text_layout_id, cursor_pos, fcolor_id(defcolor_at_cursor));
} else if (!is_active_view) {
draw_rectangle_outline(app, nxt_cursor_rect, roundness, outline_thickness, cl_cursor);
}
b32 b = cursor_pos < mark_pos;
b32 c = mark_pos <= cursor_pos;
f32 w = rect_width(cur_cursor_rect) - 1.f;
{ // NOTE(sloth): cursor
Vec2_f32 d = V2f32(c ? w : 0, 0);
Rect_f32 rect_shifted = Rf32(cur_cursor_rect.p0-d, cur_cursor_rect.p1-d);
switch (cursor_kind){
case Cursor_Rect: draw_rectangle(app, cur_cursor_rect, roundness, cl_cursor); break;
case Cursor_Thin: draw_rectangle(app, rect_vsplit(cur_cursor_rect, 1.f, 0), 0.f, cl_cursor); break;
case Cursor_Under: draw_rectangle(app, rect_hsplit(cur_cursor_rect, 3.f, 1), roundness, cl_cursor); break;
case Cursor_Corner: (draw_rectangle(app, rect_vsplit(cur_cursor_rect, 3.f, 0), roundness, cl_cursor),
draw_rectangle(app, rect_hsplit(rect_shifted, 3.f, c), roundness, cl_cursor)); break;
}
}
{ // NOTE(sloth): mark
Rect_f32 mark_rect = text_layout_character_on_screen(app, text_layout_id, mark_pos);
Vec2_f32 d = V2f32(b ? w : 0, 0);
Rect_f32 rect_shifted = Rf32(mark_rect.p0-d, mark_rect.p1-d);
switch (mark_kind){
case Cursor_Rect: draw_rectangle_outline(app, mark_rect, roundness, outline_thickness, cl_mark); break;
case Cursor_Thin: draw_rectangle(app, rect_vsplit(mark_rect, 1.f, 0), 0.f, cl_mark); break;
case Cursor_Under: draw_rectangle(app, rect_hsplit(mark_rect, 3.f, 1), roundness, cl_mark); break;
case Cursor_Corner: (draw_rectangle(app, rect_vsplit(mark_rect, outline_thickness, 0), roundness, cl_mark),
draw_rectangle(app, rect_hsplit(rect_shifted, outline_thickness, !c), roundness, cl_mark));
}
}
MC_render_cursors(app, view_id, text_layout_id);
}
}
function void
sloth_draw_comments(Application_Links *app, Buffer_ID buffer, Text_Layout_ID text_layout_id, Token_Array *array, Rect_f32 rect){
Scratch_Block scratch(app);
ARGB_Color cl_annotate = finalize_color(defcolor_comment_pop, 4);
b32 use_comment_annotations = def_get_config_b32(vars_save_string_lit("use_comment_annotations"));
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
i64 first_index = token_index_from_pos(array, visible_range.first);
Token_Iterator_Array it = token_iterator_index(buffer, array, first_index);
for (;;){
Temp_Memory_Block temp(scratch);
Token *token = token_it_read(&it);
if (token->pos >= visible_range.one_past_last)
break;
String_Const_u8 tail = {};
if (token_it_check_and_get_lexeme(app, scratch, &it, TokenBaseKind_Comment, &tail)){
// paint @annotations in comments
if (use_comment_annotations){
for (i64 i = 1; i < token->size; i += 1){
if (tail.str[i] != '@' || !character_is_whitespace(tail.str[i-1])){ continue; }
Range_i64 range = Ii64(i,i+1);
for (i64 j = i+1; j < token->size; j += 1) {
char ch = tail.str[j];
if (character_is_whitespace(ch) || !character_is_alpha_numeric(ch))
break;
range.max++;
}
if (range_size(range) > 1){
paint_text_color(app, text_layout_id, range + token->pos, cl_annotate);
}
}
}
}
if (!token_it_inc_non_whitespace(&it))
break;
}
}
function void
sloth_render_buffer(Application_Links *app, View_ID view_id, Face_ID face_id, Buffer_ID buffer, Text_Layout_ID text_layout_id, Rect_f32 rect) {
ProfileScope(app, "qol render buffer");
Scratch_Block scratch(app);
View_ID active_view = get_active_view(app, Access_Always);
b32 is_active_view = (active_view == view_id);
Rect_f32 prev_clip = draw_set_clip(app, rect);
Range_i64 visible_range = text_layout_get_visible_range(app, text_layout_id);
// NOTE(allen): Cursor shape
Face_Metrics metrics = get_face_metrics(app, face_id);
u64 cursor_roundness_100 = def_get_config_u64(app, vars_save_string_lit("cursor_roundness"));
f32 cursor_roundness = metrics.normal_advance*cursor_roundness_100*0.01f;
f32 mark_thickness = (f32)def_get_config_u64(app, vars_save_string_lit("mark_thickness"));
i64 cursor_pos = view_correct_cursor(app, view_id);
view_correct_mark(app, view_id);
// NOTE(allen): Scope highlight (must come before draw_comments)
b32 use_scope_highlight = def_get_config_b32(vars_save_string_lit("use_scope_highlight"));
if (use_scope_highlight){
Color_Array colors = finalize_color_array(defcolor_back_cycle);
draw_scope_highlight(app, buffer, text_layout_id, cursor_pos, colors.vals, colors.count);
}
// NOTE(allen): Token colorizing
Token_Array token_array = get_token_array_from_buffer(app, buffer);
if (token_array.tokens != 0){
sloth_draw_cpp_token_colors(app, view_id, buffer, text_layout_id, &token_array);
// NOTE(sloth): Scan for TODOs NOTEs DONEs IMPORTANTs @annotations //- and //~ divider-starters
b32 use_comment_keyword = def_get_config_b32(vars_save_string_lit("use_comment_keyword"));
if (use_comment_keyword) {
Comment_Highlight_Pair pairs[] = {
{string_u8_litexpr("NOTE"), finalize_color(defcolor_comment_pop, 0)},
{string_u8_litexpr("TODO"), finalize_color(defcolor_comment_pop, 1)},
{string_u8_litexpr("DONE"), finalize_color(defcolor_comment_pop, 2)},
{string_u8_litexpr("IMPORTANT"), finalize_color(defcolor_comment_pop, 3)},
{def_get_config_string(scratch, vars_save_string_lit("user_name")), finalize_color(lily_color_comment_user_name, 0)},
};
draw_comment_highlights(app, buffer, text_layout_id, &token_array, pairs, ArrayCount(pairs));
}
sloth_draw_comments(app, buffer, text_layout_id, &token_array, rect);
F4_RenderDividerComments(app, buffer, view_id, text_layout_id);
}
else{
paint_text_color_fcolor(app, text_layout_id, visible_range, fcolor_id(defcolor_text_default));
}
if (qol_col_cursor.pos >= 0){
Buffer_Seek seek = seek_line_col(qol_col_cursor.line, qol_col_cursor.col);
Buffer_Cursor cursor = buffer_compute_cursor(app, buffer, seek);
Rect_f32 col_rect = text_layout_character_on_screen(app, text_layout_id, cursor.pos);
if (col_rect.x1 > 0.f){
col_rect.y0 = rect.y0;
col_rect.y1 = rect.y1;
draw_rectangle_fcolor(app, col_rect, 0.f, fcolor_id(defcolor_highlight_cursor_line));
}
}
// NOTE(allen): Line highlight
b32 highlight_line_at_cursor = def_get_config_b32(vars_save_string_lit("highlight_line_at_cursor"));
if (highlight_line_at_cursor && is_active_view){
i64 line_number = get_line_number_from_pos(app, buffer, cursor_pos);
draw_line_highlight(app, text_layout_id, line_number, fcolor_id(defcolor_highlight_cursor_line));
}
sloth_draw_scopes(app, view_id, buffer, text_layout_id, metrics.normal_advance);
b32 use_error_highlight = def_get_config_b32(vars_save_string_lit("use_error_highlight"));
b32 use_jump_highlight = def_get_config_b32(vars_save_string_lit("use_jump_highlight"));
if (use_error_highlight || use_jump_highlight){
// NOTE(allen): Error highlight
String_Const_u8 name = string_u8_litexpr("*compilation*");
Buffer_ID compilation_buffer = get_buffer_by_name(app, name, Access_Always);
if (use_error_highlight){
sloth_draw_compile_errors(app, buffer, text_layout_id, compilation_buffer);
}
// NOTE(allen): Search highlight
if (use_jump_highlight){
Buffer_ID jump_buffer = get_locked_jump_buffer(app);
if (jump_buffer != compilation_buffer){
draw_jump_highlights(app, buffer, text_layout_id, jump_buffer,
fcolor_id(defcolor_highlight_white));
}
}
}
// NOTE(allen): Color parens
b32 use_paren_helper = def_get_config_b32(vars_save_string_lit("use_paren_helper"));
if (use_paren_helper) {
Color_Array colors = finalize_color_array(defcolor_text_cycle);
draw_paren_highlight(app, buffer, text_layout_id, cursor_pos, colors.vals, colors.count);
}
// NOTE(allen): Whitespace highlight
b64 show_whitespace = false;
view_get_setting(app, view_id, ViewSetting_ShowWhitespace, &show_whitespace);
if (show_whitespace) {
if (token_array.tokens == 0) {
draw_whitespace_highlight(app, buffer, text_layout_id, cursor_roundness);
} else {
draw_whitespace_highlight(app, text_layout_id, &token_array, cursor_roundness);
}
}
b32 show_hex_colors = def_get_config_b32(vars_save_string_lit("show_hex_colors"));
if (show_hex_colors){
sloth_draw_hex_color(app, view_id, buffer, text_layout_id);
}
// NOTE(allen): Cursor
switch (fcoder_mode){
case FCoderMode_Original:
{
Rect_f32 r = draw_set_clip(app, prev_clip);
sloth_draw_cursor_mark(app, view_id, is_active_view, buffer, text_layout_id, cursor_roundness, mark_thickness);
draw_set_clip(app, r);
}break;
case FCoderMode_NotepadLike:
{
draw_notepad_style_cursor_highlight(app, view_id, buffer, text_layout_id, cursor_roundness);
}break;
}
// NOTE(allen): Fade ranges
paint_fade_ranges(app, text_layout_id, buffer);
// NOTE(allen): put the actual text on the actual screen
draw_text_layout_default(app, text_layout_id);
if (rect_contains_point(rect, qol_cur_cursor_pos)){
sloth_draw_function_tooltip(app, buffer, If32(rect.x0, rect.x1), cursor_pos);
}
draw_set_clip(app, prev_clip);
}
function Rect_f32
sloth_draw_query_bars(Application_Links *app, Rect_f32 region, View_ID view_id, Face_ID face_id){
Face_Metrics face_metrics = get_face_metrics(app, face_id);
f32 line_height = face_metrics.line_height;
Query_Bar *space[32];
Query_Bar_Ptr_Array query_bars = {};
query_bars.ptrs = space;
if (get_active_query_bars(app, view_id, ArrayCount(space), &query_bars)){
for (i32 i = 0; i < query_bars.count; i += 1){
Rect_f32_Pair pair = layout_query_bar_on_bot(region, line_height, 1);
Query_Bar *query_bar = query_bars.ptrs[i];
Rect_f32 bar_rect = pair.max;
Scratch_Block scratch(app);
Fancy_Line list = {};
push_fancy_string(scratch, &list, fcolor_id(defcolor_pop1), query_bar->prompt);
push_fancy_string(scratch, &list, fcolor_id(defcolor_text_default), query_bar->string);
Vec2_f32 p = bar_rect.p0 + V2f32(2.f, 2.f);
p = draw_fancy_line(app, face_id, fcolor_zero(), &list, p);
if (i == 0){
draw_rectangle_fcolor(app, Rf32_xy_wh(p.x, p.y, 2.f, face_metrics.line_height), 0.f, fcolor_id(defcolor_cursor, 0));
}
region = pair.min;
}
}
return(region);
}
function void
sloth_render_caller(Application_Links *app, Frame_Info frame_info, View_ID view_id){
ProfileScope(app, "sloth render caller");
View_ID active_view = get_active_view(app, Access_Always);
b32 is_active_view = (active_view == view_id);
Rect_f32 region = draw_background_and_margin(app, view_id, is_active_view);
Rect_f32 prev_clip = draw_set_clip(app, region);
Buffer_ID buffer = view_get_buffer(app, view_id, Access_Always);
Face_ID face_id = get_face_id(app, buffer);
Face_Metrics face_metrics = get_face_metrics(app, face_id);
f32 line_height = face_metrics.line_height;
f32 normal_advance = face_metrics.normal_advance;
f32 digit_advance = face_metrics.decimal_digit_advance;
// NOTE(allen): query bars
region = sloth_draw_query_bars(app, region, view_id, face_id);
// NOTE(allen): file bar
b64 showing_file_bar = false;
if (view_get_setting(app, view_id, ViewSetting_ShowFileBar, &showing_file_bar) && showing_file_bar){
b32 on_top = def_get_config_b32(vars_save_string_lit("filebar_on_top"));
Rect_f32_Pair pair = (on_top ?
layout_file_bar_on_top(region, line_height) :
layout_file_bar_on_bot(region, line_height));
sloth_draw_file_bar(app, view_id, buffer, face_id, pair.e[1-on_top]);
region = pair.e[on_top];
}
f32 char_count = def_get_config_f32(app, vars_save_string_lit("scroll_margin_x"));
f32 line_count = def_get_config_f32(app, vars_save_string_lit("scroll_margin_y"));
Vec2_f32 margin = V2f32(char_count*normal_advance, line_count*line_height);
view_set_camera_bounds(app, view_id, margin, V2f32(1,1));
Buffer_Scroll scroll = view_get_buffer_scroll(app, view_id);
Buffer_Point_Delta_Result delta = delta_apply(app, view_id, frame_info.animation_dt, scroll);
if (!block_match_struct(&scroll.position, &delta.point)){
if (is_active_view){
qol_cur_cursor_pos -= view_point_difference(app, view_id, delta.point, scroll.position);
}
block_copy_struct(&scroll.position, &delta.point);
view_set_buffer_scroll(app, view_id, scroll, SetBufferScroll_NoCursorChange);
}
if (delta.still_animating){
animate_in_n_milliseconds(app, 0);
}
// NOTE(allen): FPS hud
if (show_fps_hud){
Rect_f32_Pair pair = layout_fps_hud_on_bottom(region, line_height);
draw_fps_hud(app, frame_info, face_id, pair.max);
region = pair.min;
animate_in_n_milliseconds(app, 1000);
}
// NOTE(allen): layout line numbers
b32 show_line_number_margins = def_get_config_b32(vars_save_string_lit("show_line_number_margins"));
Rect_f32 line_number_rect = {};
if (show_line_number_margins){
Rect_f32_Pair pair = layout_line_number_margin(app, buffer, region, digit_advance);
line_number_rect = pair.min;
region = pair.max;
}
region = rect_split_left_right(region, 4.f).max;
// NOTE(allen): begin buffer render
Buffer_Point buffer_point = scroll.position;
Text_Layout_ID text_layout_id = text_layout_create(app, buffer, region, buffer_point);
// NOTE(allen): draw line numbers
if (show_line_number_margins){
draw_line_number_margin(app, view_id, buffer, face_id, text_layout_id, line_number_rect);
}
// NOTE(allen): draw the buffer
sloth_render_buffer(app, view_id, face_id, buffer, text_layout_id, region);
if(is_active_view && global_bindings_hud_state.show_hud) {
//- NOTE(sloth): @hudactivation
mod_draw_bindings_hud(app, view_id, buffer, face_id);
}
text_layout_free(app, text_layout_id);
draw_set_clip(app, prev_clip);
}
function Rect_f32
sloth_buffer_region(Application_Links *app, View_ID view_id, Rect_f32 region){
Buffer_ID buffer = view_get_buffer(app, view_id, Access_Always);
Face_ID face_id = get_face_id(app, buffer);
Face_Metrics metrics = get_face_metrics(app, face_id);
f32 line_height = metrics.line_height;
f32 digit_advance = metrics.decimal_digit_advance;
// NOTE(allen): margins
region = rect_inner(region, 3.f);
// NOTE(allen): file bar
b64 showing_file_bar = false;
if (view_get_setting(app, view_id, ViewSetting_ShowFileBar, &showing_file_bar) &&
showing_file_bar){
b32 on_top = def_get_config_b32(vars_save_string_lit("filebar_on_top"));
Rect_f32_Pair pair = (on_top ?
layout_file_bar_on_top(region, line_height) :
layout_file_bar_on_bot(region, line_height));
region = pair.e[on_top];
}
// NOTE(allen): query bars
{
Query_Bar *space[32];
Query_Bar_Ptr_Array query_bars = {};
query_bars.ptrs = space;
if (get_active_query_bars(app, view_id, ArrayCount(space), &query_bars)){
Rect_f32_Pair pair = layout_query_bar_on_top(region, line_height, query_bars.count);
region = pair.max;
}
}
// NOTE(allen): FPS hud
if (show_fps_hud){
Rect_f32_Pair pair = layout_fps_hud_on_bottom(region, line_height);
region = pair.min;
}
// NOTE(allen): line numbers
b32 show_line_number_margins = def_get_config_b32(vars_save_string_lit("show_line_number_margins"));
if (show_line_number_margins){
Rect_f32_Pair pair = layout_line_number_margin(app, buffer, region, digit_advance);
region = pair.max;
}
region = rect_split_left_right(region, 4.f).max;
return(region);
}