-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4coder_default_hooks.cpp
More file actions
495 lines (412 loc) · 17.1 KB
/
Copy path4coder_default_hooks.cpp
File metadata and controls
495 lines (412 loc) · 17.1 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
/*
4coder_default_hooks.cpp - Sets up the hooks for the default framework.
TYPE: 'internal-for-default-system'
*/
// TOP
#if !defined(FCODER_DEFAULT_HOOKS_CPP)
#define FCODER_DEFAULT_HOOKS_CPP
#include "4coder_default_framework.h"
#include "4coder_helper/4coder_bind_helper.h"
#include "4coder_project_commands.cpp"
#include "languages/4coder_language_haxe.h"
#include "languages/4coder_language_cpp.h"
#include "languages/4coder_language_rust.h"
#include "languages/4coder_language_cs.h"
#include "languages/4coder_language_java.h"
CUSTOM_COMMAND_SIG(set_bindings_choose);
CUSTOM_COMMAND_SIG(set_bindings_default);
CUSTOM_COMMAND_SIG(set_bindings_mac_default);
static Named_Mapping named_maps_values[] = {
{make_lit_string("choose") , set_bindings_choose },
{make_lit_string("default") , set_bindings_default },
{make_lit_string("mac-default") , set_bindings_mac_default },
};
START_HOOK_SIG(default_start){
named_maps = named_maps_values;
named_map_count = ArrayCount(named_maps_values);
default_4coder_initialize(app);
default_4coder_side_by_side_panels(app, files, file_count);
if (automatically_load_project){
load_project(app);
}
// no meaning for return
return(0);
}
// NOTE(allen|a4.0.9): All command calls can now go through this hook
// If this hook is not implemented a default behavior of calling the
// command is used. It is important to note that paste_next does not
// work without this hook.
// NOTE(allen|a4.0.10): As of this version the word_complete command
// also relies on this particular command caller hook.
COMMAND_CALLER_HOOK(default_command_caller){
View_Summary view = get_active_view(app, AccessAll);
view_paste_index[view.view_id].next_rewrite = 0;
exec_command(app, cmd);
view_paste_index[view.view_id].rewrite = view_paste_index[view.view_id].next_rewrite;
return(0);
}
HOOK_SIG(default_exit){
// if this returns zero it cancels the exit.
return(1);
}
HOOK_SIG(default_view_adjust){
int32_t count = 0;
int32_t new_wrap_width = 0;
for (View_Summary view = get_view_first(app, AccessAll);
view.exists;
get_view_next(app, &view, AccessAll)){
new_wrap_width += view.view_region.x1 - view.view_region.x0;
++count;
}
new_wrap_width /= count;
new_wrap_width = (int32_t)(new_wrap_width * .9f);
int32_t new_min_base_width = (int32_t)(new_wrap_width * .77f);
if (automatically_adjust_wrapping){
adjust_all_buffer_wrap_widths(app, new_wrap_width, new_min_base_width);
default_wrap_width = new_wrap_width;
default_min_base_width = new_min_base_width;
}
// no meaning for return
return(0);
}
BUFFER_NAME_RESOLVER_SIG(default_buffer_name_resolution){
if (conflict_count > 1){
// List of unresolved conflicts
Partition *part = &global_part;
Temp_Memory temp = begin_temp_memory(part);
int32_t *unresolved = push_array(part, int32_t, conflict_count);
if (unresolved == 0) return;
int32_t unresolved_count = conflict_count;
for (int32_t i = 0; i < conflict_count; ++i){
unresolved[i] = i;
}
// Resolution Loop
int32_t x = 0;
for (;;){
// Resolution Pass
++x;
for (int32_t i = 0; i < unresolved_count; ++i){
int32_t conflict_index = unresolved[i];
Buffer_Name_Conflict_Entry *conflict = &conflicts[conflict_index];
int32_t len = conflict->base_name_len;
if (len < 0){
len = 0;
}
if (len > conflict->unique_name_capacity){
len = conflict->unique_name_capacity;
}
conflict->unique_name_len_in_out = len;
memcpy(conflict->unique_name_in_out, conflict->base_name, len);
if (conflict->file_name != 0){
String s_file_name = make_string(conflict->file_name, conflict->file_name_len);
s_file_name = path_of_directory(s_file_name);
s_file_name.size -= 1;
char *end = s_file_name.str + s_file_name.size;
for (int32_t j = 0; j < x; ++j){
s_file_name = path_of_directory(s_file_name);
if (j + 1 < x){
s_file_name.size -= 1;
}
if (s_file_name.size <= 0){
s_file_name.size = 0;
break;
}
}
char *start = s_file_name.str + s_file_name.size;
String uniqueifier = make_string(start, (int32_t)(end - start));
String builder = make_string_cap(conflict->unique_name_in_out,
conflict->unique_name_len_in_out,
conflict->unique_name_capacity);
append(&builder, " <");
append(&builder, uniqueifier);
append(&builder, ">");
conflict->unique_name_len_in_out = builder.size;
}
}
// Conflict Check Pass
bool32 has_conflicts = false;
for (int32_t i = 0; i < unresolved_count; ++i){
int32_t conflict_index = unresolved[i];
Buffer_Name_Conflict_Entry *conflict = &conflicts[conflict_index];
String conflict_name = make_string(conflict->unique_name_in_out,
conflict->unique_name_len_in_out);
bool32 hit_conflict = false;
if (conflict->file_name != 0){
for (int32_t j = 0; j < unresolved_count; ++j){
if (i == j) continue;
int32_t conflict_j_index = unresolved[j];
Buffer_Name_Conflict_Entry *conflict_j = &conflicts[conflict_j_index];
if (match(conflict_name, make_string(conflict_j->unique_name_in_out,
conflict_j->unique_name_len_in_out))){
hit_conflict = true;
break;
}
}
}
if (hit_conflict){
has_conflicts = true;
}
else{
--unresolved_count;
unresolved[i] = unresolved[unresolved_count];
--i;
}
}
if (!has_conflicts){
break;
}
}
end_temp_memory(temp);
}
}
OPEN_FILE_HOOK_SIG(default_file_settings){
// NOTE(allen|a4.0.8): The get_parameter_buffer was eliminated
// and instead the buffer is passed as an explicit parameter through
// the function call. That is where buffer_id comes from here.
Buffer_Summary buffer = get_buffer(app, buffer_id, AccessAll);
Assert(buffer.exists);
bool32 treat_as_code = false;
bool32 treat_as_todo = false;
bool32 wrap_lines = true;
bool32 lex_without_strings = false;
int32_t extension_count = 0;
char **extension_list = get_current_code_extensions(&extension_count);
Parse_Context_ID parse_context_id = 0;
if (buffer.file_name != 0 && buffer.size < (16 << 20)){
String name = make_string(buffer.file_name, buffer.file_name_len);
String ext = file_extension(name);
for (int32_t i = 0; i < extension_count; ++i){
if (match(ext, extension_list[i])){
treat_as_code = true;
if (match(ext, "cs")){
if (parse_context_language_cs == 0){
init_language_cs(app);
}
parse_context_id = parse_context_language_cs;
}
if (match(ext, "java")){
if (parse_context_language_java == 0){
init_language_java(app);
}
parse_context_id = parse_context_language_java;
}
if (match(ext, "rs")){
if (parse_context_language_rust == 0){
init_language_rust(app);
}
parse_context_id = parse_context_language_rust;
lex_without_strings = true;
}
if (match(ext, "hx")){
if (parse_context_language_haxe == 0){
init_language_haxe(app);
}
parse_context_id = parse_context_language_haxe;
}
if (match(ext, "cpp") || match(ext, "h") || match(ext, "c") || match(ext, "hpp") || match(ext, "cc")){
if (parse_context_language_cpp == 0){
init_language_cpp(app);
}
parse_context_id = parse_context_language_cpp;
}
// TODO(NAME): Real GLSL highlighting
if (match(ext, "glsl")){
if (parse_context_language_cpp == 0){
init_language_cpp(app);
}
parse_context_id = parse_context_language_cpp;
}
// TODO(NAME): Real Objective-C highlighting
if (match(ext, "m")){
if (parse_context_language_cpp == 0){
init_language_cpp(app);
}
parse_context_id = parse_context_language_cpp;
}
break;
}
}
if (!treat_as_code){
String lead_name = front_of_directory(name);
if (match_insensitive(lead_name, "todo.txt")){
treat_as_todo = true;
}
}
}
if (treat_as_code){
wrap_lines = false;
}
if (buffer.file_name == 0){
wrap_lines = false;
}
int32_t map_id = (treat_as_code)?((int32_t)default_code_map):((int32_t)mapid_file);
buffer_set_setting(app, &buffer, BufferSetting_WrapPosition, default_wrap_width);
buffer_set_setting(app, &buffer, BufferSetting_MinimumBaseWrapPosition, default_min_base_width);
buffer_set_setting(app, &buffer, BufferSetting_MapID, map_id);
buffer_set_setting(app, &buffer, BufferSetting_ParserContext, parse_context_id);
if (treat_as_todo){
buffer_set_setting(app, &buffer, BufferSetting_WrapLine, true);
buffer_set_setting(app, &buffer, BufferSetting_LexWithoutStrings, true);
buffer_set_setting(app, &buffer, BufferSetting_VirtualWhitespace, true);
}
else if (treat_as_code && enable_code_wrapping && buffer.size < (128 << 10)){
// NOTE(allen|a4.0.12): There is a little bit of grossness going on here.
// If we set BufferSetting_Lex to true, it will launch a lexing job.
// If a lexing job is active when we set BufferSetting_VirtualWhitespace, the call can fail.
// Unfortunantely without tokens virtual whitespace doesn't really make sense.
// So for now I have it automatically turning on lexing when virtual whitespace is turned on.
// Cleaning some of that up is a goal for future versions.
if (lex_without_strings){
buffer_set_setting(app, &buffer, BufferSetting_LexWithoutStrings, true);
}
buffer_set_setting(app, &buffer, BufferSetting_WrapLine, true);
buffer_set_setting(app, &buffer, BufferSetting_VirtualWhitespace, true);
}
else{
buffer_set_setting(app, &buffer, BufferSetting_WrapLine, wrap_lines);
buffer_set_setting(app, &buffer, BufferSetting_Lex, treat_as_code);
}
// no meaning for return
return(0);
}
OPEN_FILE_HOOK_SIG(default_new_file){
Buffer_Summary buffer = get_buffer(app, buffer_id, AccessOpen);
char str[] = "/*\nNew File\n*/\n\n\n";
buffer_replace_range(app, &buffer, 0, 0, str, sizeof(str)-1);
// no meaning for return
return(0);
}
OPEN_FILE_HOOK_SIG(default_file_save){
Buffer_Summary buffer = get_buffer(app, buffer_id, AccessAll);
Assert(buffer.exists);
#if defined(FCODER_AUTO_INDENT_CPP)
int32_t is_virtual = 0;
if (automatically_indent_text_on_save && buffer_get_setting(app, &buffer, BufferSetting_VirtualWhitespace, &is_virtual)){
if (is_virtual){
buffer_auto_indent(app, &global_part, &buffer, 0, buffer.size, DEF_TAB_WIDTH, DEFAULT_INDENT_FLAGS | AutoIndent_FullTokens);
}
}
#endif
// no meaning for return
return(0);
}
OPEN_FILE_HOOK_SIG(default_end_file){
Buffer_Summary buffer = get_buffer(app, buffer_id, AccessAll);
Assert(buffer.exists);
char space[1024];
String str = make_fixed_width_string(space);
append(&str, "Ending file: ");
append(&str, make_string(buffer.buffer_name, buffer.buffer_name_len));
append(&str, "\n");
print_message(app, str.str, str.size);
// no meaning for return
return(0);
}
// NOTE(allen|a4.0.9): The input filter allows you to modify the input
// to a frame before 4coder starts processing it at all.
//
// Right now it only has access to the mouse state, but it will be
// extended to have access to the key presses soon.
INPUT_FILTER_SIG(default_suppress_mouse_filter){
if (suppressing_mouse){
*mouse = null_mouse_state;
mouse->x = -100;
mouse->y = -100;
}
}
// NOTE(allen|a4): scroll rule information
//
// The parameters:
// target_x, target_y
// This is where the view would like to be for the purpose of
// following the cursor, doing mouse wheel work, etc.
//
// scroll_x, scroll_y
// These are pointers to where the scrolling actually is. If you bind
// the scroll rule it is you have to update these in some way to move
// the actual location of the scrolling.
//
// view_id
// This corresponds to which view is computing it's new scrolling position.
// This id DOES correspond to the views that View_Summary contains.
// This will always be between 1 and 16 (0 is a null id).
// See below for an example of having state that carries across scroll udpates.
//
// is_new_target
// If the target of the view is different from the last target in either x or y
// this is true, otherwise it is false.
//
// The return:
// Should be true if and only if scroll_x or scroll_y are changed.
//
// Don't try to use the app pointer in a scroll rule, you're asking for trouble.
//
// If you don't bind scroll_rule, nothing bad will happen, yo will get default
// 4coder scrolling behavior.
//
struct Scroll_Velocity{
float x, y;
};
Scroll_Velocity scroll_velocity_[16] = {0};
Scroll_Velocity *scroll_velocity = scroll_velocity_ - 1;
static int32_t
smooth_camera_step(float target, float *current, float *vel, float S, float T){
int32_t result = 0;
float curr = *current;
float v = *vel;
if (curr != target){
if (curr > target - .1f && curr < target + .1f){
curr = target;
v = 1.f;
}
else{
float L = curr + T*(target - curr);
int32_t sign = (target > curr) - (target < curr);
float V = curr + sign*v;
if (sign > 0) curr = (L<V)?(L):(V);
else curr = (L>V)?(L):(V);
if (curr == V){
v *= S;
}
}
*current = curr;
*vel = v;
result = 1;
}
return(result);
}
SCROLL_RULE_SIG(smooth_scroll_rule){
Scroll_Velocity *velocity = scroll_velocity + view_id;
int32_t result = 0;
if (velocity->x == 0.f){
velocity->x = 1.f;
velocity->y = 1.f;
}
if (smooth_camera_step(target_y, scroll_y, &velocity->y, 80.f, 1.f/2.f)){
result = 1;
}
if (smooth_camera_step(target_x, scroll_x, &velocity->x, 80.f, 1.f/2.f)){
result = 1;
}
return(result);
}
static void
set_all_default_hooks(Bind_Helper *context){
set_hook(context, hook_exit, default_exit);
set_hook(context, hook_view_size_change, default_view_adjust);
set_start_hook(context, default_start);
set_open_file_hook(context, default_file_settings);
set_new_file_hook(context, default_new_file);
set_save_file_hook(context, default_file_save);
#if defined(FCODER_STICKY_JUMP)
set_end_file_hook(context, end_file_close_jump_list);
#else
set_end_file_hook(context, default_end_file);
#endif
set_command_caller(context, default_command_caller);
set_input_filter(context, default_suppress_mouse_filter);
set_scroll_rule(context, smooth_scroll_rule);
set_buffer_name_resolver(context, default_buffer_name_resolution);
}
#endif
// BOTTOM