forked from 4coder-archive/4coder_fleury
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4coder_fleury_base_commands.cpp
1767 lines (1570 loc) · 64.3 KB
/
4coder_fleury_base_commands.cpp
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
//~ NOTE(rjf): @f4_base_commands
// TODO(rjf): Remove once Allen adds official version.
CUSTOM_COMMAND_SIG(f4_leave_event_unhandled)
CUSTOM_DOC("when bound to keystroke, ensures the event falls through to text insertion")
{
leave_current_input_unhandled(app);
}
internal void
F4_Search(Application_Links *app, Scan_Direction dir)
{
Scratch_Block scratch(app);
View_ID view = get_active_view(app, Access_Read);
Buffer_ID buffer = view_get_buffer(app, view, Access_Read);
if(view && buffer)
{
i64 cursor = view_get_cursor_pos(app, view);
i64 mark = view_get_mark_pos(app, view);
i64 cursor_line = get_line_number_from_pos(app, buffer, cursor);
i64 mark_line = get_line_number_from_pos(app, buffer, mark);
String_Const_u8 query_init = (fcoder_mode != FCoderMode_NotepadLike || cursor == mark || cursor_line != mark_line) ? SCu8() : push_buffer_range(app, scratch, buffer, Ii64(cursor, mark));
isearch(app, dir, cursor, query_init);
}
}
CUSTOM_COMMAND_SIG(f4_search)
CUSTOM_DOC("Searches the current buffer forward. If something is highlighted, will fill search query with it.")
{
F4_Search(app, Scan_Forward);
}
CUSTOM_COMMAND_SIG(f4_reverse_search)
CUSTOM_DOC("Searches the current buffer backwards. If something is highlighted, will fill search query with it.")
{
F4_Search(app, Scan_Backward);
}
CUSTOM_COMMAND_SIG(f4_write_text_input)
CUSTOM_DOC("Inserts whatever text was used to trigger this command.")
{
write_text_input(app);
F4_PowerMode_CharacterPressed();
User_Input in = get_current_input(app);
String_Const_u8 insert = to_writable(&in);
F4_PowerMode_Spawn(app, get_active_view(app, Access_ReadWriteVisible), insert.str ? insert.str[0] : 0);
}
CUSTOM_COMMAND_SIG(f4_write_text_and_auto_indent)
CUSTOM_DOC("Inserts text and auto-indents the line on which the cursor sits if any of the text contains 'layout punctuation' such as ;:{}()[]# and new lines.")
{
write_text_and_auto_indent(app);
F4_PowerMode_CharacterPressed();
User_Input in = get_current_input(app);
String_Const_u8 insert = to_writable(&in);
F4_PowerMode_Spawn(app, get_active_view(app, Access_ReadWriteVisible), insert.str ? insert.str[0] : 0);
}
CUSTOM_COMMAND_SIG(f4_write_zero_struct)
CUSTOM_DOC("At the cursor, insert a ' = {0};'.")
{
write_string(app, string_u8_litexpr(" = {0};"));
F4_PowerMode_CharacterPressed();
F4_PowerMode_Spawn(app, get_active_view(app, Access_ReadWriteVisible), 0);
}
CUSTOM_COMMAND_SIG(f4_home)
CUSTOM_DOC("Goes to the beginning of the line.")
{
seek_pos_of_visual_line(app, Side_Min);
View_ID view = get_active_view(app, Access_ReadWriteVisible);
Buffer_Scroll scroll = view_get_buffer_scroll(app, view);
scroll.target.pixel_shift.x = 0;
view_set_buffer_scroll(app, view, scroll, SetBufferScroll_NoCursorChange);
}
CUSTOM_COMMAND_SIG(f4_toggle_battery_saver)
CUSTOM_DOC("Toggles battery saving mode.")
{
global_battery_saver = !global_battery_saver;
}
CUSTOM_COMMAND_SIG(f4_toggle_compilation_expand)
CUSTOM_DOC("Expand the compilation window.")
{
Buffer_ID buffer = view_get_buffer(app, global_compilation_view, Access_Always);
Face_ID face_id = get_face_id(app, buffer);
Face_Metrics metrics = get_face_metrics(app, face_id);
if(global_compilation_view_expanded ^= 1)
{
view_set_split_pixel_size(app, global_compilation_view, (i32)(metrics.line_height*32.f));
}
else
{
view_set_split_pixel_size(app, global_compilation_view, (i32)(metrics.line_height*4.f));
}
}
internal void
F4_GoToDefinition(Application_Links *app, F4_Index_Note *note, b32 same_panel)
{
if(note != 0 && note->file != 0)
{
View_ID view = get_active_view(app, Access_Always);
Rect_f32 region = view_get_buffer_region(app, view);
f32 view_height = rect_height(region);
Buffer_ID buffer = note->file->buffer;
if(!same_panel)
{
view = get_next_view_looped_primary_panels(app, view, Access_Always);
}
point_stack_push_view_cursor(app, view);
view_set_buffer(app, view, buffer, 0);
i64 line_number = get_line_number_from_pos(app, buffer, note->range.min);
Buffer_Scroll scroll = view_get_buffer_scroll(app, view);
scroll.position.line_number = line_number;
scroll.target.line_number = line_number;
scroll.position.pixel_shift.y = scroll.target.pixel_shift.y = -view_height*0.5f;
view_set_buffer_scroll(app, view, scroll, SetBufferScroll_SnapCursorIntoView);
view_set_cursor(app, view, seek_pos(note->range.min));
view_set_mark(app, view, seek_pos(note->range.min));
}
}
internal F4_Index_Note *
F4_FindMostIntuitiveNoteInDuplicateChain(F4_Index_Note *note, Buffer_ID cursor_buffer, i64 cursor_pos)
{
F4_Index_Note *result = note;
if(note != 0)
{
F4_Index_Note *best_note_based_on_cursor = 0;
for(F4_Index_Note *candidate = note; candidate; candidate = candidate->next)
{
F4_Index_File *file = candidate->file;
if(file != 0)
{
if(cursor_buffer == file->buffer &&
candidate->range.min <= cursor_pos && cursor_pos <= candidate->range.max)
{
if(candidate->next)
{
best_note_based_on_cursor = candidate->next;
break;
}
else
{
best_note_based_on_cursor = note;
break;
}
}
}
}
if(best_note_based_on_cursor)
{
result = best_note_based_on_cursor;
}
else if(note->flags & F4_Index_NoteFlag_Prototype)
{
for(F4_Index_Note *candidate = note; candidate; candidate = candidate->next)
{
if(!(candidate->flags & F4_Index_NoteFlag_Prototype))
{
result = candidate;
break;
}
}
}
}
return result;
}
CUSTOM_COMMAND_SIG(f4_go_to_definition)
CUSTOM_DOC("Goes to the definition of the identifier under the cursor.")
{
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
Scratch_Block scratch(app);
String_Const_u8 string = push_token_or_word_under_active_cursor(app, scratch);
F4_Index_Note *note = F4_Index_LookupNote(string);
note = F4_FindMostIntuitiveNoteInDuplicateChain(note, buffer, view_get_cursor_pos(app, view));
F4_GoToDefinition(app, note, 0);
}
CUSTOM_COMMAND_SIG(f4_go_to_definition_same_panel)
CUSTOM_DOC("Goes to the definition of the identifier under the cursor in the same panel.")
{
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
Scratch_Block scratch(app);
String_Const_u8 string = push_token_or_word_under_active_cursor(app, scratch);
F4_Index_Note *note = F4_Index_LookupNote(string);
note = F4_FindMostIntuitiveNoteInDuplicateChain(note, buffer, view_get_cursor_pos(app, view));
F4_GoToDefinition(app, note, 1);
}
internal void
_F4_PushListerOptionForNote(Application_Links *app, Arena *arena, Lister *lister, F4_Index_Note *note)
{
if(note && note->file)
{
F4_Index_File *file = note->file;
Buffer_ID buffer = file->buffer;
Tiny_Jump *jump = push_array(arena, Tiny_Jump, 1);
jump->buffer = buffer;
jump->pos = note->range.first;
String_Const_u8 buffer_name = push_buffer_unique_name(app, arena, buffer);
String_Const_u8 name = push_stringf(arena, "[%.*s] %.*s", string_expand(buffer_name), string_expand(note->string));
String_Const_u8 sort = S8Lit("");
switch(note->kind)
{
case F4_Index_NoteKind_Type:
{
sort = push_stringf(arena, "type [%s] [%s]",
note->flags & F4_Index_NoteFlag_Prototype ? "prototype" : "def",
note->flags & F4_Index_NoteFlag_SumType ? "sum" : "product");
}break;
case F4_Index_NoteKind_Function:
{
sort = push_stringf(arena, "function [%s]", note->flags & F4_Index_NoteFlag_Prototype ? "prototype" : "def");
}break;
case F4_Index_NoteKind_Macro:
{
sort = S8Lit("macro");
}break;
case F4_Index_NoteKind_Constant:
{
sort = S8Lit("constant");
}break;
case F4_Index_NoteKind_CommentTag:
{
sort = S8Lit("comment tag");
}break;
case F4_Index_NoteKind_CommentToDo:
{
sort = S8Lit("TODO");
}break;
default: break;
}
lister_add_item(lister, name, sort, jump, 0);
}
}
internal void
F4_JumpToLocation(Application_Links *app, View_ID view, Buffer_ID buffer, i64 pos)
{
// NOTE(rjf): This function was ripped from 4coder's jump_to_location. It was copied
// and modified so that jumping to a location didn't cause a selection in notepad-like
// mode.
view_set_active(app, view);
Buffer_Seek seek = seek_pos(pos);
set_view_to_location(app, view, buffer, seek);
if (auto_center_after_jumps)
{
center_view(app);
}
view_set_cursor(app, view, seek);
view_set_mark(app, view, seek);
}
CUSTOM_UI_COMMAND_SIG(f4_search_for_definition__project_wide)
CUSTOM_DOC("List all definitions in the index and jump to the one selected by the user.")
{
char *query = "Index (Project):";
Scratch_Block scratch(app);
Lister_Block lister(app, scratch);
lister_set_query(lister, query);
lister_set_default_handlers(lister);
F4_Index_Lock();
{
for (Buffer_ID buffer = get_buffer_next(app, 0, Access_Always);
buffer != 0; buffer = get_buffer_next(app, buffer, Access_Always))
{
F4_Index_File *file = F4_Index_LookupFile(app, buffer);
if(file != 0)
{
for(F4_Index_Note *note = file->first_note; note; note = note->next_sibling)
{
_F4_PushListerOptionForNote(app, scratch, lister, note);
}
}
}
}
F4_Index_Unlock();
Lister_Result l_result = run_lister(app, lister);
Tiny_Jump result = {};
if (!l_result.canceled && l_result.user_data != 0){
block_copy_struct(&result, (Tiny_Jump*)l_result.user_data);
}
if (result.buffer != 0)
{
View_ID view = get_this_ctx_view(app, Access_Always);
point_stack_push_view_cursor(app, view);
F4_JumpToLocation(app, view, result.buffer, result.pos);
}
}
CUSTOM_UI_COMMAND_SIG(f4_search_for_definition__current_file)
CUSTOM_DOC("List all definitions in the current file and jump to the one selected by the user.")
{
char *query = "Index (File):";
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
Scratch_Block scratch(app);
Lister_Block lister(app, scratch);
lister_set_query(lister, query);
lister_set_default_handlers(lister);
F4_Index_Lock();
{
F4_Index_File *file = F4_Index_LookupFile(app, buffer);
if(file != 0)
{
for(F4_Index_Note *note = file->first_note; note; note = note->next_sibling)
{
_F4_PushListerOptionForNote(app, scratch, lister, note);
}
}
}
F4_Index_Unlock();
Lister_Result l_result = run_lister(app, lister);
Tiny_Jump result = {};
if (!l_result.canceled && l_result.user_data != 0){
block_copy_struct(&result, (Tiny_Jump*)l_result.user_data);
}
if (result.buffer != 0)
{
View_ID view_id = get_this_ctx_view(app, Access_Always);
point_stack_push_view_cursor(app, view_id);
F4_JumpToLocation(app, view_id, result.buffer, result.pos);
}
}
CUSTOM_COMMAND_SIG(f4_toggle_enclosure_side)
CUSTOM_DOC("Moves the cursor between the open/close brace/paren/bracket of the closest enclosure.")
{
View_ID view = get_active_view(app, Access_Always);
Buffer_ID buffer = view_get_buffer(app, view, Access_Always);
i64 pos = view_get_cursor_pos(app, view);
// NOTE(rjf): Adjust position if it's on the start or end of an enclosure.
{
Token_Array tokens = get_token_array_from_buffer(app, buffer);
Token_Iterator_Array it = token_iterator_pos(0, &tokens, pos);
Token *token = token_it_read(&it);
if(token)
{
if(token->kind == TokenBaseKind_ScopeOpen ||
token->kind == TokenBaseKind_ParentheticalOpen)
{
pos += 1;
goto end;
}
}
token_it_dec_all(&it);
token = token_it_read(&it);
if(token)
{
if(token->kind == TokenBaseKind_ScopeClose ||
token->kind == TokenBaseKind_ParentheticalClose)
{
pos -= 1;
goto end;
}
}
end:;
}
Scratch_Block scratch(app);
Range_i64_Array ranges = get_enclosure_ranges(app, scratch, buffer, pos,
FindNest_Scope | FindNest_Paren);
if(ranges.count > 0)
{
Range_i64 nearest_range = ranges.ranges[0];
if(pos == nearest_range.min+1)
{
pos = nearest_range.max;
}
else
{
pos = nearest_range.min;
}
view_set_cursor(app, view, seek_pos(pos));
no_mark_snap_to_cursor_if_shift(app, view);
}
}
CUSTOM_UI_COMMAND_SIG(f4_open_project)
CUSTOM_DOC("Open a project by navigating to the project file.")
{
for(;;)
{
Scratch_Block scratch(app);
View_ID view = get_this_ctx_view(app, Access_Always);
File_Name_Result result = get_file_name_from_user(app, scratch, "Open Project:", view);
if (result.canceled) break;
String_Const_u8 file_name = result.file_name_activated;
if (file_name.size == 0)
{
file_name = result.file_name_in_text_field;
}
if (file_name.size == 0) break;
String_Const_u8 path = result.path_in_text_field;
String_Const_u8 full_file_name = push_u8_stringf(scratch, "%.*s/%.*s",
string_expand(path), string_expand(file_name));
if (result.is_folder)
{
set_hot_directory(app, full_file_name);
continue;
}
if(character_is_slash(file_name.str[file_name.size - 1]))
{
File_Attributes attribs = system_quick_file_attributes(scratch, full_file_name);
if (HasFlag(attribs.flags, FileAttribute_IsDirectory)){
set_hot_directory(app, full_file_name);
continue;
}
if (string_looks_like_drive_letter(file_name)){
set_hot_directory(app, file_name);
continue;
}
if (query_create_folder(app, file_name)){
set_hot_directory(app, full_file_name);
continue;
}
break;
}
set_hot_directory(app, full_file_name);
load_project(app);
break;
}
}
CUSTOM_COMMAND_SIG(f4_setup_new_project)
CUSTOM_DOC("Sets up a blank 4coder project provided some user folder.")
{
Scratch_Block scratch(app);
Query_Bar_Group bar_group(app);
// NOTE(rjf): Query user for project folder.
u8 project_folder_absolute[1024];
{
Query_Bar path_bar = {};
path_bar.prompt = string_u8_litexpr("Absolute Path To Project Folder: ");
path_bar.string = SCu8(project_folder_absolute, (u64)0);
path_bar.string_capacity = sizeof(project_folder_absolute);
if(query_user_string(app, &path_bar))
{
String_Const_u8 full_file_name = push_u8_stringf(scratch, "%.*s/",
string_expand(path_bar.string));
set_hot_directory(app, full_file_name);
String_Const_u8 project_file_path = push_u8_stringf(scratch, "%.*s/project.4coder", string_expand(path_bar.string));
FILE *file = fopen((char *)project_file_path.str, "wb");
if(file)
{
char *string = R"PROJ(version(1);
project_name = "New Project";
patterns =
{
"*.c",
"*.cpp",
"*.jai",
"*.odin",
"*.zig",
"*.h",
"*.inc",
"*.bat",
"*.sh",
"*.4coder",
"*.txt",
};
blacklist_patterns =
{
".*",
};
load_paths =
{
{
{ {"."}, .recursive = true, .relative = true }, .os = "win"
},
};
command_list =
{
{
.name = "build",
.out = "*compilation*",
.footer_panel = true,
.save_dirty_files = true,
.cursor_at_end = false,
.cmd =
{
{ "echo Windows build command not implemented for 4coder project.", .os = "win" },
{ "echo Linux build command not implemented for 4coder project.", .os = "linux" },
},
},
{
.name = "run",
.out = "*compilation*",
.footer_panel = true,
.save_dirty_files = true,
.cursor_at_end = false,
.cmd =
{
{ "echo Windows run command not implemented for 4coder project.", .os = "win" },
{ "echo Linux run command not implemented for 4coder project.", .os = "linux" },
},
},
};
fkey_command[1] = "build";
fkey_command[2] = "run";
)PROJ";
fprintf(file, "%s", string);
fclose(file);
load_project(app);
}
else
{
// TODO(rjf): Error.
}
}
}
load_project(app);
}
function i64
F4_Boundary_TokenAndWhitespace(Application_Links *app, Buffer_ID buffer,
Side side, Scan_Direction direction, i64 pos)
{
i64 result = boundary_non_whitespace(app, buffer, side, direction, pos);
Token_Array tokens = get_token_array_from_buffer(app, buffer);
if (tokens.tokens != 0){
switch (direction){
case Scan_Forward:
{
i64 buffer_size = buffer_get_size(app, buffer);
result = buffer_size;
if(tokens.count > 0)
{
Token_Iterator_Array it = token_iterator_pos(0, &tokens, pos);
Token *token = token_it_read(&it);
if(token == 0)
{
break;
}
// NOTE(rjf): Comments/Strings
if(token->kind == TokenBaseKind_Comment ||
token->kind == TokenBaseKind_LiteralString)
{
result = boundary_non_whitespace(app, buffer, side, direction, pos);
break;
}
// NOTE(rjf): All other cases.
else
{
if (token->kind == TokenBaseKind_Whitespace)
{
// token_it_inc_non_whitespace(&it);
// token = token_it_read(&it);
}
if (side == Side_Max){
result = token->pos + token->size;
token_it_inc_all(&it);
Token *ws = token_it_read(&it);
if(ws != 0 && ws->kind == TokenBaseKind_Whitespace &&
get_line_number_from_pos(app, buffer, ws->pos + ws->size) ==
get_line_number_from_pos(app, buffer, token->pos))
{
result = ws->pos + ws->size;
}
}
else{
if (token->pos <= pos){
token_it_inc_non_whitespace(&it);
token = token_it_read(&it);
}
if (token != 0){
result = token->pos;
}
}
}
}
}break;
case Scan_Backward:
{
result = 0;
if (tokens.count > 0){
Token_Iterator_Array it = token_iterator_pos(0, &tokens, pos);
Token *token = token_it_read(&it);
Token_Iterator_Array it2 = it;
token_it_dec_non_whitespace(&it2);
Token *token2 = token_it_read(&it2);
// NOTE(rjf): Comments/Strings
if(token->kind == TokenBaseKind_Comment ||
token->kind == TokenBaseKind_LiteralString ||
(token2 &&
token2->kind == TokenBaseKind_Comment ||
token2->kind == TokenBaseKind_LiteralString))
{
result = boundary_non_whitespace(app, buffer, side, direction, pos);
break;
}
if (token->kind == TokenBaseKind_Whitespace){
token_it_dec_non_whitespace(&it);
token = token_it_read(&it);
}
if (token != 0){
if (side == Side_Min){
if (token->pos >= pos){
token_it_dec_non_whitespace(&it);
token = token_it_read(&it);
}
result = token->pos;
}
else{
if (token->pos + token->size >= pos){
token_it_dec_non_whitespace(&it);
token = token_it_read(&it);
}
result = token->pos + token->size;
}
}
}
}break;
}
}
return(result);
}
// TODO(rjf): Replace with the final one from Jack's layer.
function i64
F4_Boundary_CursorTokenOrBlankLine_TEST(Application_Links *app, Buffer_ID buffer,
Side side, Scan_Direction direction, i64 pos)
{
Scratch_Block scratch(app);
Range_i64_Array scopes = get_enclosure_ranges(app, scratch, buffer, pos, FindNest_Scope);
// NOTE(jack): The outermost scope
Range_i64 outer_scope = scopes.ranges[scopes.count - 1];
// NOTE(jack): As we are issuing a move command here I will assume that buffer is the active buffer.
View_ID view = get_active_view(app, Access_Always);
i64 active_cursor_pos = view_get_cursor_pos(app, view);
Token_Array tokens = get_token_array_from_buffer(app, buffer);
Token_Iterator_Array active_cursor_it = token_iterator_pos(0, &tokens, active_cursor_pos);
Token *active_cursor_token = token_it_read(&active_cursor_it);
String_Const_u8 cursor_string = push_buffer_range(app, scratch, buffer, Ii64(active_cursor_token));
i64 cursor_offset = pos - active_cursor_token->pos;
// NOTE(jack): If the cursor token is not an identifier, we will move to empty lines
i64 result = get_pos_of_blank_line_grouped(app, buffer, direction, pos);
result = view_get_character_legal_pos_from_pos(app, view, result);
if (tokens.tokens != 0)
{
// NOTE(jack): if the the cursor token is an identifier, and we are inside of a scope
// perform the cursor occurance movement.
if (active_cursor_token->kind == TokenBaseKind_Identifier && !(scopes.count == 0))
{
// NOTE(jack): Reset result to prevent token movement to escape to blank line movement
// when you are on the first/last token in the outermost scope.
result = pos;
Token_Iterator_Array it = token_iterator_pos(0, &tokens, pos);
for (;;)
{
b32 done = false;
// NOTE(jack): Incremenet first so we dont move to the same cursor that the cursor is on.
switch (direction)
{
// NOTE(jack): I am using it.ptr->pos because its easier than reading the token with
// token_it_read
case Scan_Forward:
{
if (!token_it_inc_non_whitespace(&it) || it.ptr->pos >= outer_scope.end) {
done = true;
}
} break;
case Scan_Backward:
{
if (!token_it_dec_non_whitespace(&it) || it.ptr->pos < outer_scope.start) {
done = true;
}
} break;
}
if (!done)
{
Token *token = token_it_read(&it);
String_Const_u8 token_string = push_buffer_range(app, scratch, buffer, Ii64(token));
if (string_match(cursor_string, token_string)) {
result = token->pos + cursor_offset;
break;
}
}
else
{
break;
}
}
}
}
return result ;
}
CUSTOM_COMMAND_SIG(f4_move_left)
CUSTOM_DOC("Moves the cursor one character to the left.")
{
Scratch_Block scratch(app);
Input_Modifier_Set mods = system_get_keyboard_modifiers(scratch);
View_ID view = get_active_view(app, Access_ReadVisible);
if(fcoder_mode != FCoderMode_NotepadLike || view_get_cursor_pos(app, view) == view_get_mark_pos(app, view) ||
has_modifier(&mods, KeyCode_Shift))
{
view_set_cursor_by_character_delta(app, view, -1);
}
no_mark_snap_to_cursor_if_shift(app, view);
}
CUSTOM_COMMAND_SIG(f4_move_right)
CUSTOM_DOC("Moves the cursor one character to the right.")
{
Scratch_Block scratch(app);
Input_Modifier_Set mods = system_get_keyboard_modifiers(scratch);
View_ID view = get_active_view(app, Access_ReadVisible);
if(fcoder_mode != FCoderMode_NotepadLike || view_get_cursor_pos(app, view) == view_get_mark_pos(app, view) ||
has_modifier(&mods, KeyCode_Shift))
{
view_set_cursor_by_character_delta(app, view, +1);
}
no_mark_snap_to_cursor_if_shift(app, view);
}
CUSTOM_COMMAND_SIG(f4_move_up_token_occurrence)
CUSTOM_DOC("Moves the cursor to the previous occurrence of the token that the cursor is over.")
{
Scratch_Block scratch(app);
current_view_scan_move(app, Scan_Backward, push_boundary_list(scratch, F4_Boundary_CursorTokenOrBlankLine_TEST));
}
CUSTOM_COMMAND_SIG(f4_move_down_token_occurrence)
CUSTOM_DOC("Moves the cursor to the next occurrence of the token that the cursor is over.")
{
Scratch_Block scratch(app);
current_view_scan_move(app, Scan_Forward, push_boundary_list(scratch, F4_Boundary_CursorTokenOrBlankLine_TEST));
}
CUSTOM_COMMAND_SIG(f4_move_right_token_boundary)
CUSTOM_DOC("Seek right for boundary between alphanumeric characters and non-alphanumeric characters.")
{
Scratch_Block scratch(app);
current_view_scan_move(app, Scan_Forward, push_boundary_list(scratch, F4_Boundary_TokenAndWhitespace));
}
CUSTOM_COMMAND_SIG(f4_move_left_token_boundary)
CUSTOM_DOC("Seek left for boundary between alphanumeric characters and non-alphanumeric characters.")
{
Scratch_Block scratch(app);
current_view_scan_move(app, Scan_Backward, push_boundary_list(scratch, F4_Boundary_TokenAndWhitespace));
}
CUSTOM_COMMAND_SIG(f4_backspace_token_boundary)
CUSTOM_DOC("Deletes left to a token boundary.")
{
Scratch_Block scratch(app);
current_view_boundary_delete(app, Scan_Backward, push_boundary_list(scratch, F4_Boundary_TokenAndWhitespace));
}
CUSTOM_COMMAND_SIG(f4_delete_token_boundary)
CUSTOM_DOC("Deletes right to a token boundary.")
{
Scratch_Block scratch(app);
current_view_boundary_delete(app, Scan_Forward, push_boundary_list(scratch, F4_Boundary_TokenAndWhitespace));
}
CUSTOM_COMMAND_SIG(f4_backspace_alpha_numeric_or_camel_boundary)
CUSTOM_DOC("Deletes left to a alphanumeric or camel boundary.")
{
Scratch_Block scratch(app);
current_view_boundary_delete(app, Scan_Backward, push_boundary_list(scratch,
boundary_alpha_numeric,
boundary_alpha_numeric_camel));
}
CUSTOM_COMMAND_SIG(f4_delete_alpha_numeric_or_camel_boundary)
CUSTOM_DOC("Deletes right to an alphanumeric or camel boundary.")
{
Scratch_Block scratch(app);
current_view_boundary_delete(app, Scan_Forward, push_boundary_list(scratch,
boundary_alpha_numeric,
boundary_alpha_numeric_camel));
}
CUSTOM_COMMAND_SIG(f4_home_first_non_whitespace)
CUSTOM_DOC("Goes to the beginning of the line.")
{
View_ID view = get_active_view(app, Access_Read);
Buffer_ID buffer = view_get_buffer(app, view, Access_Read);
if(view && buffer)
{
i64 start_pos = view_get_cursor_pos(app, view);
seek_pos_of_visual_line(app, Side_Min);
i64 end_pos = view_get_cursor_pos(app, view);
i64 line = get_line_number_from_pos(app, buffer, start_pos);
// NOTE(rjf): If we are on the first column, go to the first non-whitespace
// in the line.
if(start_pos == end_pos && start_pos == get_line_start_pos(app, buffer, line))
{
Scratch_Block scratch(app);
String_Const_u8 string = push_buffer_line(app, scratch, buffer, line);
for(u64 i = 0; i < string.size; i += 1)
{
if(!character_is_whitespace(string.str[i]))
{
view_set_cursor_by_character_delta(app, view, (i64)i);
break;
}
}
}
// NOTE(rjf): If we hit any non-whitespace, move to the first possible
// non-whitespace instead of the front of the line entirely.
else
{
Scratch_Block scratch(app);
String_Const_u8 string = push_buffer_range(app, scratch, buffer, Ii64(start_pos, end_pos));
b32 skipped_non_whitespace = 0;
{
for(i64 i = string.size-1; i >= 0; i -= 1)
{
if(!character_is_whitespace(string.str[i]))
{
skipped_non_whitespace = 1;
break;
}
}
}
if(skipped_non_whitespace)
{
for(i64 i = 0; i < (i64)string.size; i += 1)
{
if(!character_is_whitespace(string.str[i]))
{
view_set_cursor_by_character_delta(app, view, i);
break;
}
}
}
}
// NOTE(rjf): Scroll all the way left.
{
Buffer_Scroll scroll = view_get_buffer_scroll(app, view);
scroll.target.pixel_shift.x = 0;
view_set_buffer_scroll(app, view, scroll, SetBufferScroll_NoCursorChange);
}
}
}
function void
F4_ReIndentLine(Application_Links *app, Buffer_ID buffer, i64 line, i64 indent_delta)
{
Scratch_Block scratch(app);
String_Const_u8 line_string = push_buffer_line(app, scratch, buffer, line);
i64 line_start_pos = get_line_start_pos(app, buffer, line);
Range_i64 line_indent_range = Ii64(0, 0);
i64 tabs_at_beginning = 0;
i64 spaces_at_beginning = 0;
for(u64 i = 0; i < line_string.size; i += 1)
{
if(line_string.str[i] == '\t')
{
tabs_at_beginning += 1;
}
else if(character_is_whitespace(line_string.str[i]))
{
spaces_at_beginning += 1;
}
else if(!character_is_whitespace(line_string.str[i]))
{
line_indent_range.max = (i64)i;
break;
}
}
// NOTE(rjf): Indent lines.
{
Range_i64 indent_range =
{
line_indent_range.min + line_start_pos,
line_indent_range.max + line_start_pos,
};
i64 indent_width = (i64)def_get_config_u64(app, vars_save_string_lit("indent_width"));
b32 indent_with_tabs = def_get_config_b32(vars_save_string_lit("indent_with_tabs"));
i64 spaces_per_indent_level = indent_width;
i64 indent_level = spaces_at_beginning / spaces_per_indent_level + tabs_at_beginning;
i64 new_indent_level = indent_level + indent_delta;
String_Const_u8 indent_string = indent_with_tabs ? S8Lit("\t") : push_stringf(scratch, "%.*s", Min(indent_width, 16),
" ");
buffer_replace_range(app, buffer, indent_range, S8Lit(""));
for(i64 i = 0; i < new_indent_level; i += 1)
{
buffer_replace_range(app, buffer, Ii64(line_start_pos), indent_string);
}
}
}
internal void
F4_ReIndentLineRange(Application_Links *app, Buffer_ID buffer, Range_i64 range, i64 indent_delta)
{
for(i64 i = range.min; i <= range.max; i += 1)
{
F4_ReIndentLine(app, buffer, i, indent_delta);
}
}
internal Range_i64
F4_LineRangeFromPosRange(Application_Links *app, Buffer_ID buffer, Range_i64 pos_range)
{
Range_i64 lines_range =
Ii64(get_line_number_from_pos(app, buffer, pos_range.min),
get_line_number_from_pos(app, buffer, pos_range.max));
return lines_range;
}
internal Range_i64
F4_PosRangeFromLineRange(Application_Links *app, Buffer_ID buffer, Range_i64 line_range)
{
if(line_range.min > line_range.max)
{
i64 swap = line_range.max;
line_range.max = line_range.min;
line_range.min = swap;
}
Range_i64 pos_range =
Ii64(get_line_start_pos(app, buffer, line_range.min),
get_line_end_pos(app, buffer, line_range.max));
return pos_range;
}
internal void
F4_ReIndentPosRange(Application_Links *app, Buffer_ID buffer, Range_i64 range, i64 indent_delta)
{