-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4coder_qol_isearch.cpp
More file actions
257 lines (226 loc) · 8.7 KB
/
4coder_qol_isearch.cpp
File metadata and controls
257 lines (226 loc) · 8.7 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
#define str8lit string_u8_litexpr
struct History {
Arena arena;
String8List list = {0};
Node_String_Const_u8 *current;
};
global History global_history;
function void
qol_isearch(Application_Links* app, Scan_Direction start_scan, i64 first_pos, String_Const_u8 query_init)
{
History *history = &global_history;
if (history->arena.base_allocator == 0) {
history->arena = make_arena_system();
}
Arena *arena = &history->arena;
String8List *list = &history->list;
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
if (!buffer_exists(app, buffer)) {
return;
}
i64 buffer_size = buffer_get_size(app, buffer);
Query_Bar_Group group(app);
Query_Bar bar = {};
if (start_query_bar(app, &bar, 0) == 0) {
return;
}
Vec2_f32 old_margin = {};
Vec2_f32 old_push_in = {};
view_get_camera_bounds(app, view, &old_margin, &old_push_in);
Vec2_f32 margin = old_margin;
margin.y = clamp_bot(200.f, margin.y);
view_set_camera_bounds(app, view, margin, old_push_in);
Scan_Direction scan = start_scan;
i64 pos = first_pos;
u8 bar_string_space[256];
bar.string = SCu8(bar_string_space, query_init.size);
block_copy(bar.string.str, query_init.str, query_init.size);
String_Const_u8 isearch_str = string_u8_litexpr("I-Search: ");
String_Const_u8 rsearch_str = string_u8_litexpr("Reverse-I-Search: ");
u64 match_size = bar.string.size;
User_Input in = {};
for (;;) {
bar.prompt = (scan == Scan_Forward ? isearch_str : rsearch_str);
isearch__update_highlight(app, view, Ii64_size(pos, match_size));
in = get_next_input(app, EventPropertyGroup_Any, EventProperty_Escape);
if (in.abort) {
break;
}
Scratch_Block scratch(app);
String_Const_u8 string = to_writable(&in);
b32 string_change = false;
if (match_key_code(&in, KeyCode_Return) ||
match_key_code(&in, KeyCode_Tab)) { // NOTE(sloth): arrows?
if (is_modified(&in.event) &&
has_modifier(&in.event, KeyCode_Control)) {
if (!history->current && list->first) {
history->current = list->first;
}
String8Node c = *history->current;
String_Const_u8 s = c.string;
bar.string.size = s.size;
block_copy(bar.string.str, s.str, s.size);
history->current = history->current->next;
string_change = true; // NOTE(sloth): doesn't work without this (qol bug)
} else {
// NOTE(sloth): save last search onto the history
Node_String_Const_u8 *node = push_array(arena, Node_String_Const_u8, 1);
sll_stack_push(list->first, node);
node->string = push_string_copy(arena, bar.string);
list->node_count += 1;
list->total_size += node->string.size;
break;
}
} else if (is_modified(&in.event) &&
has_modifier(&in.event, KeyCode_Control) &&
match_key_code(&in, KeyCode_V)) {
clipboard_update_history_from_system(app, 0);
i32 count = clipboard_count(0);
if (count > 0){
Managed_Scope scope = view_get_managed_scope(app, view);
i32 *paste_index = scope_attachment(app, scope, view_paste_index_loc, i32);
if (paste_index != 0){
*paste_index = 0;
// NOTE(sloth): here's the actual pastebuffer contents
string = push_clipboard_index(scratch, 0, *paste_index);
if (string.size > 0){
// NOTE(sloth): @KILLERFEATURE FUCKING PASTE IN THE QUERY?! WHAT!?
String_u8 bar_string = Su8(bar.string, sizeof(bar_string_space));
string_append(&bar_string, string);
bar.string = bar_string.string;
string_change = true;
}
}
}
} else if (string.str != 0 && string.size > 0) {
String_u8 bar_string = Su8(bar.string, sizeof(bar_string_space));
string_append(&bar_string, string);
bar.string = bar_string.string;
string_change = true;
} else if (match_key_code(&in, KeyCode_Backspace)) {
String_Const_u8 old_string = bar.string;
b32 mod_ctl = has_modifier(&in.event.key.modifiers, KeyCode_Control);
b32 mod_sft = has_modifier(&in.event.key.modifiers, KeyCode_Shift);
if (is_unmodified_key(&in.event)) {
bar.string = backspace_utf8(bar.string);
} else if (mod_ctl && !mod_sft) {
bar.string = qol_ctrl_backspace_string(app, bar.string);
} else if (mod_ctl && mod_sft) {
bar.string.size = 0;
}
string_change = (bar.string.size < old_string.size);
}
b32 do_scan_action = false;
b32 do_scroll_wheel = false;
Scan_Direction change_scan = scan;
if (!string_change) {
if (match_key_code(&in, KeyCode_PageDown) || match_key_code(&in, KeyCode_Down)) {
change_scan = Scan_Forward;
do_scan_action = true;
} else if (match_key_code(&in, KeyCode_PageUp) || match_key_code(&in, KeyCode_Up)) {
change_scan = Scan_Backward;
do_scan_action = true;
} else {
// NOTE(allen): is the user trying to execute another command?
View_Context ctx = view_current_context(app, view);
Mapping* mapping = ctx.mapping;
Command_Map* map = mapping_get_map(mapping, ctx.map_id);
Command_Binding binding = map_get_binding_recursive(mapping, map, &in.event);
if (binding.custom != 0) {
if (binding.custom == search) {
change_scan = Scan_Forward;
do_scan_action = true;
} else if (binding.custom == reverse_search) {
change_scan = Scan_Backward;
do_scan_action = true;
} else {
Command_Metadata* metadata = get_command_metadata(binding.custom);
if (metadata != 0) {
if (metadata->is_ui) {
view_enqueue_command_function(app, view, binding.custom);
break;
}
}
binding.custom(app);
}
} else {
leave_current_input_unhandled(app);
}
}
}
if (string_change) {
switch (scan) {
case Scan_Forward: {
i64 new_pos = 0;
seek_string_insensitive_forward(app, buffer, pos - 1, 0, bar.string, &new_pos);
if (new_pos < buffer_size) {
pos = new_pos;
match_size = bar.string.size;
}
} break;
case Scan_Backward: {
i64 new_pos = 0;
seek_string_insensitive_backward(app, buffer, pos + 1, 0, bar.string, &new_pos);
if (new_pos >= 0) {
pos = new_pos;
match_size = bar.string.size;
}
} break;
}
} else if (do_scan_action) {
scan = change_scan;
switch (scan) {
case Scan_Forward: {
i64 new_pos = 0;
seek_string_insensitive_forward(app, buffer, pos, 0, bar.string, &new_pos);
if (new_pos < buffer_size) {
pos = new_pos;
match_size = bar.string.size;
}
} break;
case Scan_Backward: {
i64 new_pos = 0;
seek_string_insensitive_backward(app, buffer, pos, 0, bar.string, &new_pos);
if (new_pos >= 0) {
pos = new_pos;
match_size = bar.string.size;
}
} break;
}
} else if (do_scroll_wheel) {
mouse_wheel_scroll(app);
}
}
view_disable_highlight_range(app, view);
if (in.abort) {
u64 size = bar.string.size;
size = clamp_top(size, sizeof(previous_isearch_query) - 1);
block_copy(previous_isearch_query, bar.string.str, size);
previous_isearch_query[size] = 0;
view_set_cursor_and_preferred_x(app, view, seek_pos(first_pos));
}
view_set_camera_bounds(app, view, old_margin, old_push_in);
}
CUSTOM_COMMAND_SIG(qol_search_identifier)
CUSTOM_DOC("[QOL] I-search identifier under cursor")
{
Scratch_Block scratch(app);
View_ID view = get_active_view(app, Access_ReadVisible);
Buffer_ID buffer = view_get_buffer(app, view, Access_ReadVisible);
Token_Array array = get_token_array_from_buffer(app, buffer);
i64 cursor = view_get_cursor_pos(app, view);
Token* token = get_token_from_pos(app, &array, cursor);
String_Const_u8 query = { 0 };
if (token) {
query = push_token_lexeme(app, scratch, buffer, token);
qol_isearch(app, Scan_Forward, token->pos, query);
}
}
CUSTOM_COMMAND_SIG(qol_search)
CUSTOM_DOC("[QOL] I-search")
{
View_ID view = get_active_view(app, Access_ReadVisible);
i64 cursor = view_get_cursor_pos(app, view);
qol_isearch(app, Scan_Forward, cursor, string_u8_empty);
}