-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4coder_sloth_hooks.cpp
More file actions
269 lines (215 loc) · 9.88 KB
/
4coder_sloth_hooks.cpp
File metadata and controls
269 lines (215 loc) · 9.88 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
CUSTOM_COMMAND_SIG(qol_startup)
CUSTOM_DOC("QOL command for responding to a startup event")
{
ProfileScope(app, "qol startup");
User_Input input = get_current_input(app);
if (match_core_code(&input, CoreCode_Startup)){
String_Const_u8_Array file_names = input.event.core.file_names;
load_themes_default_folder(app);
default_4coder_initialize(app, file_names);
if (file_names.count == 1)
default_4coder_one_panel(app, file_names);
else
default_4coder_side_by_side_panels(app, file_names);
String_ID global_map_id = vars_save_string_lit("keys_global");
String_ID file_map_id = vars_save_string_lit("keys_file");
String_ID code_map_id = vars_save_string_lit("keys_code");
sloth_setup_essential_mapping(&framework_mapping, global_map_id, file_map_id, code_map_id);
sloth_setup_default_mapping(&framework_mapping, global_map_id, file_map_id, code_map_id);
b32 auto_load = def_get_config_b32(vars_save_string_lit("automatically_load_project"));
if (auto_load) {
load_project(app);
}
qol_temp_buffer = create_buffer(app, string_u8_litexpr("*qol_temp*"),
BufferCreate_Background | BufferCreate_AlwaysNew | BufferCreate_NeverAttachToFile);
buffer_set_setting(app, qol_temp_buffer, BufferSetting_Unimportant, true);
buffer_set_setting(app, qol_temp_buffer, BufferSetting_Unkillable, true);
buffer_set_setting(app, qol_temp_buffer, BufferSetting_ReadOnly, false);
qol_snippet_init(app);
qol_lister_init(app);
}
def_audio_init();
def_enable_virtual_whitespace = def_get_config_b32(vars_save_string_lit("enable_virtual_whitespace"));
clear_all_layouts(app);
Face_Description desc = get_global_face_description(app);
desc.parameters.pt_size -= 4;
qol_small_face = try_create_new_face(app, &desc);
String_Const_u8 non_word_chars = string_u8_litexpr(" \t\n/\\()\"':,.;<>~!@#$%^&*|+=[]{}`?-");
for (u64 i = 0; i < non_word_chars.size; i += 1){
Character_Predicate pred = character_predicate_from_character(non_word_chars.str[i]);
character_predicate_non_word = character_predicate_or(&pred, &character_predicate_non_word);
}
character_predicate_word = character_predicate_not(&character_predicate_non_word);
Scratch_Block scratch(app);
set_active_color(get_color_table_by_name(def_get_config_string(scratch, vars_save_string_lit("default_theme_name"))));
qol_cur_colors = qol_color_table_init(app);
qol_nxt_colors = qol_color_table_init(app);
qol_color_table_copy(qol_cur_colors, active_color_table);
qol_color_table_copy(qol_nxt_colors, active_color_table);
}
function void
sloth_tick(Application_Links *app, Frame_Info frame_info){
default_tick(app, frame_info);
f32 dt = frame_info.animation_dt;
qol_interp(qol_cur_cursor_pos, qol_nxt_cursor_pos, dt, 1e-28f);
if (!near_zero(qol_cur_cursor_pos - qol_nxt_cursor_pos, 0.5f)){
animate_in_n_milliseconds(app, 0);
}
qol_tick_colors(app, frame_info);
MC_tick_inner(app, frame_info);
qol_tick_lister(app, frame_info);
}
BUFFER_HOOK_SIG(qol_file_save)
{
default_file_save(app, buffer_id);
Scratch_Block scratch(app);
String_Const_u8 path = push_buffer_file_name(app, scratch, buffer_id);
String_Const_u8 name = string_front_of_path(path);
if (qol_is_theme_file(name)) {
Color_Table color_table = make_color_table(app, &global_theme_arena);
Config *config = theme_parse__buffer(app, scratch, buffer_id, &global_theme_arena, &color_table);
String_Const_u8 error_text = config_stringize_errors(app, scratch, config);
comp_error(app, error_text);
if (error_text.size > 0) {
print_message(app, error_text);
} else {
qol_color_table_copy(qol_nxt_colors, color_table);
}
}
if (string_match(name, string_u8_litexpr("config.4coder"))) {
View_ID view = get_active_view(app, Access_Always);
view_enqueue_command_function(app, view, qol_reload_config);
}
if (string_match(name, string_u8_litexpr("project.4coder"))) {
View_ID view = get_active_view(app, Access_Always);
view_enqueue_command_function(app, view, qol_reload_project);
}
if (string_match(name, string_u8_litexpr("bindings.4coder"))) {
View_ID view = get_active_view(app, Access_Always);
view_enqueue_command_function(app, view, qol_reload_bindings);
}
return 0;
}
CUSTOM_COMMAND_SIG(sloth_view_input_handler)
CUSTOM_DOC("QOL Input consumption loop for views")
{
Scratch_Block scratch(app);
default_input_handler_init(app, scratch);
View_ID view = get_this_ctx_view(app, Access_Always);
Managed_Scope scope = view_get_managed_scope(app, view);
for (;;){
// NOTE(allen): Get input
User_Input input = get_next_input(app, EventPropertyGroup_Any, 0);
if (input.abort){
break;
}
ProfileScopeNamed(app, "before view input", view_input_profile);
// NOTE(allen): Mouse Suppression
Event_Property event_properties = get_event_properties(&input.event);
if (suppressing_mouse && (event_properties & EventPropertyGroup_AnyMouseEvent) != 0){
continue;
}
// NOTE(allen): Get binding
if (implicit_map_function == 0){
implicit_map_function = default_implicit_map;
}
Implicit_Map_Result map_result = implicit_map_function(app, 0, 0, &input.event);
if (map_result.command == 0){
leave_current_input_unhandled(app);
continue;
}
// NOTE(allen): Run the command and pre/post command stuff
qol_pre_command(app, scope);
ProfileCloseNow(view_input_profile);
map_result.command(app);
ProfileScope(app, "after view input");
qol_post_command(app, scope);
}
}
BUFFER_EDIT_RANGE_SIG(sloth_buffer_edit_range)
{
// signature: Application_Links *app, Buffer_ID buffer_id, Range_i64 new_range, Range_Cursor old_cursor_range
MC_buffer_edit_range_inner(app, buffer_id, new_range, old_cursor_range);
ProfileScope(app, "default edit range");
Range_i64 old_range = Ii64(old_cursor_range.min.pos, old_cursor_range.max.pos);
buffer_shift_fade_ranges(buffer_id, old_range.max, (new_range.max - old_range.max));
{
code_index_lock();
Code_Index_File *file = code_index_get_file(buffer_id);
if (file != 0){
code_index_shift(file, old_range, range_size(new_range));
}
code_index_unlock();
}
i64 insert_size = range_size(new_range);
i64 text_shift = replace_range_shift(old_range, insert_size);
Scratch_Block scratch(app);
Managed_Scope scope = buffer_get_managed_scope(app, buffer_id);
Async_Task *lex_task_ptr = scope_attachment(app, scope, buffer_lex_task, Async_Task);
Base_Allocator *allocator = managed_scope_allocator(app, scope);
b32 do_full_relex = false;
if (async_task_is_running_or_pending(&global_async_system, *lex_task_ptr)){
async_task_cancel(app, &global_async_system, *lex_task_ptr);
buffer_unmark_as_modified(buffer_id);
do_full_relex = true;
*lex_task_ptr = 0;
}
Token_Array *ptr = scope_attachment(app, scope, attachment_tokens, Token_Array);
if (ptr != 0 && ptr->tokens != 0){
ProfileBlockNamed(app, "attempt resync", profile_attempt_resync);
i64 token_index_first = token_relex_first(ptr, old_range.first, 1);
i64 token_index_resync_guess =
token_relex_resync(ptr, old_range.one_past_last, 16);
if (token_index_resync_guess - token_index_first >= 4000){
do_full_relex = true;
}
else{
Token *token_first = ptr->tokens + token_index_first;
Token *token_resync = ptr->tokens + token_index_resync_guess;
Range_i64 relex_range = Ii64(token_first->pos, token_resync->pos + token_resync->size + text_shift);
String_Const_u8 partial_text = push_buffer_range(app, scratch, buffer_id, relex_range);
Token_List relex_list = lex_full_input_cpp(scratch, partial_text);
if (relex_range.one_past_last < buffer_get_size(app, buffer_id)){
token_drop_eof(&relex_list);
}
Token_Relex relex = token_relex(relex_list, relex_range.first - text_shift, ptr->tokens, token_index_first, token_index_resync_guess);
ProfileCloseNow(profile_attempt_resync);
if (!relex.successful_resync){
do_full_relex = true;
}
else{
ProfileBlock(app, "apply resync");
i64 token_index_resync = relex.first_resync_index;
Range_i64 head = Ii64(0, token_index_first);
Range_i64 replaced = Ii64(token_index_first, token_index_resync);
Range_i64 tail = Ii64(token_index_resync, ptr->count);
i64 resynced_count = (token_index_resync_guess + 1) - token_index_resync;
i64 relexed_count = relex_list.total_count - resynced_count;
i64 tail_shift = relexed_count - (token_index_resync - token_index_first);
i64 new_tokens_count = ptr->count + tail_shift;
Token *new_tokens = base_array(allocator, Token, new_tokens_count);
Token *old_tokens = ptr->tokens;
block_copy_array_shift(new_tokens, old_tokens, head, 0);
token_fill_memory_from_list(new_tokens + replaced.first, &relex_list, relexed_count);
for (i64 i = 0, index = replaced.first; i < relexed_count; i += 1, index += 1){
new_tokens[index].pos += relex_range.first;
}
for (i64 i = tail.first; i < tail.one_past_last; i += 1){
old_tokens[i].pos += text_shift;
}
block_copy_array_shift(new_tokens, ptr->tokens, tail, tail_shift);
base_free(allocator, ptr->tokens);
ptr->tokens = new_tokens;
ptr->count = new_tokens_count;
ptr->max = new_tokens_count;
buffer_mark_as_modified(buffer_id);
}
}
}
if (do_full_relex){
*lex_task_ptr = async_task_no_dep(&global_async_system, do_full_lex_async,
make_data_struct(&buffer_id));
}
// no meaning for return
return(0);
}