forked from tsoding/ded
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy patheditor.c
544 lines (476 loc) · 15.2 KB
/
editor.c
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
#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include "./editor.h"
#include "./common.h"
void editor_backspace(Editor *e)
{
if (e->searching) {
if (e->search.count > 0) {
e->search.count -= 1;
}
} else {
if (e->cursor > e->data.count) {
e->cursor = e->data.count;
}
if (e->cursor == 0) return;
memmove(
&e->data.items[e->cursor - 1],
&e->data.items[e->cursor],
e->data.count - e->cursor
);
e->cursor -= 1;
e->data.count -= 1;
editor_retokenize(e);
}
}
void editor_delete(Editor *e)
{
if (e->searching) return;
if (e->cursor >= e->data.count) return;
memmove(
&e->data.items[e->cursor],
&e->data.items[e->cursor + 1],
e->data.count - e->cursor - 1
);
e->data.count -= 1;
editor_retokenize(e);
}
// TODO: make sure that you always have new line at the end of the file while saving
// https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206
Errno editor_save_as(Editor *e, const char *file_path)
{
printf("Saving as %s...\n", file_path);
Errno err = write_entire_file(file_path, e->data.items, e->data.count);
if (err != 0) return err;
e->file_path.count = 0;
sb_append_cstr(&e->file_path, file_path);
sb_append_null(&e->file_path);
return 0;
}
Errno editor_save(const Editor *e)
{
assert(e->file_path.count > 0);
printf("Saving as %s...\n", e->file_path.items);
return write_entire_file(e->file_path.items, e->data.items, e->data.count);
}
Errno editor_load_from_file(Editor *e, const char *file_path)
{
printf("Loading %s\n", file_path);
e->data.count = 0;
Errno err = read_entire_file(file_path, &e->data);
if (err != 0) return err;
e->cursor = 0;
editor_retokenize(e);
e->file_path.count = 0;
sb_append_cstr(&e->file_path, file_path);
sb_append_null(&e->file_path);
return 0;
}
size_t editor_cursor_row(const Editor *e)
{
assert(e->lines.count > 0);
for (size_t row = 0; row < e->lines.count; ++row) {
Line line = e->lines.items[row];
if (line.begin <= e->cursor && e->cursor <= line.end) {
return row;
}
}
return e->lines.count - 1;
}
void editor_move_line_up(Editor *e)
{
editor_stop_search(e);
size_t cursor_row = editor_cursor_row(e);
size_t cursor_col = e->cursor - e->lines.items[cursor_row].begin;
if (cursor_row > 0) {
Line next_line = e->lines.items[cursor_row - 1];
size_t next_line_size = next_line.end - next_line.begin;
if (cursor_col > next_line_size) cursor_col = next_line_size;
e->cursor = next_line.begin + cursor_col;
}
}
void editor_move_line_down(Editor *e)
{
editor_stop_search(e);
size_t cursor_row = editor_cursor_row(e);
size_t cursor_col = e->cursor - e->lines.items[cursor_row].begin;
if (cursor_row < e->lines.count - 1) {
Line next_line = e->lines.items[cursor_row + 1];
size_t next_line_size = next_line.end - next_line.begin;
if (cursor_col > next_line_size) cursor_col = next_line_size;
e->cursor = next_line.begin + cursor_col;
}
}
void editor_move_char_left(Editor *e)
{
editor_stop_search(e);
if (e->cursor > 0) e->cursor -= 1;
}
void editor_move_char_right(Editor *e)
{
editor_stop_search(e);
if (e->cursor < e->data.count) e->cursor += 1;
}
void editor_move_word_left(Editor *e)
{
editor_stop_search(e);
while (e->cursor > 0 && !isalnum(e->data.items[e->cursor - 1])) {
e->cursor -= 1;
}
while (e->cursor > 0 && isalnum(e->data.items[e->cursor - 1])) {
e->cursor -= 1;
}
}
void editor_move_word_right(Editor *e)
{
editor_stop_search(e);
while (e->cursor < e->data.count && !isalnum(e->data.items[e->cursor])) {
e->cursor += 1;
}
while (e->cursor < e->data.count && isalnum(e->data.items[e->cursor])) {
e->cursor += 1;
}
}
void editor_insert_char(Editor *e, char x)
{
editor_insert_buf(e, &x, 1);
}
void editor_insert_buf(Editor *e, char *buf, size_t buf_len)
{
if (e->searching) {
sb_append_buf(&e->search, buf, buf_len);
bool matched = false;
for (size_t pos = e->cursor; pos < e->data.count; ++pos) {
if (editor_search_matches_at(e, pos)) {
e->cursor = pos;
matched = true;
break;
}
}
if (!matched) e->search.count -= buf_len;
} else {
if (e->cursor > e->data.count) {
e->cursor = e->data.count;
}
for (size_t i = 0; i < buf_len; ++i) {
da_append(&e->data, '\0');
}
memmove(
&e->data.items[e->cursor + buf_len],
&e->data.items[e->cursor],
e->data.count - e->cursor - buf_len
);
memcpy(&e->data.items[e->cursor], buf, buf_len);
e->cursor += buf_len;
editor_retokenize(e);
}
}
void editor_retokenize(Editor *e)
{
// Lines
{
e->lines.count = 0;
Line line;
line.begin = 0;
for (size_t i = 0; i < e->data.count; ++i) {
if (e->data.items[i] == '\n') {
line.end = i;
da_append(&e->lines, line);
line.begin = i + 1;
}
}
line.end = e->data.count;
da_append(&e->lines, line);
}
// Syntax Highlighting
{
e->tokens.count = 0;
Lexer l = lexer_new(e->atlas, e->data.items, e->data.count);
Token t = lexer_next(&l);
while (t.kind != TOKEN_END) {
da_append(&e->tokens, t);
t = lexer_next(&l);
}
}
}
bool editor_line_starts_with(Editor *e, size_t row, size_t col, const char *prefix)
{
size_t prefix_len = strlen(prefix);
if (prefix_len == 0) {
return true;
}
Line line = e->lines.items[row];
if (col + prefix_len - 1 >= line.end) {
return false;
}
for (size_t i = 0; i < prefix_len; ++i) {
if (prefix[i] != e->data.items[line.begin + col + i]) {
return false;
}
}
return true;
}
const char *editor_line_starts_with_one_of(Editor *e, size_t row, size_t col, const char **prefixes, size_t prefixes_count)
{
for (size_t i = 0; i < prefixes_count; ++i) {
if (editor_line_starts_with(e, row, col, prefixes[i])) {
return prefixes[i];
}
}
return NULL;
}
void editor_render(SDL_Window *window, Free_Glyph_Atlas *atlas, Simple_Renderer *sr, Editor *editor)
{
int w, h;
SDL_GetWindowSize(window, &w, &h);
float max_line_len = 0.0f;
sr->resolution = vec2f(w, h);
sr->time = (float) SDL_GetTicks() / 1000.0f;
// Render selection
{
simple_renderer_set_shader(sr, SHADER_FOR_COLOR);
if (editor->selection) {
for (size_t row = 0; row < editor->lines.count; ++row) {
size_t select_begin_chr = editor->select_begin;
size_t select_end_chr = editor->cursor;
if (select_begin_chr > select_end_chr) {
SWAP(size_t, select_begin_chr, select_end_chr);
}
Line line_chr = editor->lines.items[row];
if (select_begin_chr < line_chr.begin) {
select_begin_chr = line_chr.begin;
}
if (select_end_chr > line_chr.end) {
select_end_chr = line_chr.end;
}
if (select_begin_chr <= select_end_chr) {
Vec2f select_begin_scr = vec2f(0, -((float)row + CURSOR_OFFSET) * FREE_GLYPH_FONT_SIZE);
free_glyph_atlas_measure_line_sized(
atlas, editor->data.items + line_chr.begin, select_begin_chr - line_chr.begin,
&select_begin_scr);
Vec2f select_end_scr = select_begin_scr;
free_glyph_atlas_measure_line_sized(
atlas, editor->data.items + select_begin_chr, select_end_chr - select_begin_chr,
&select_end_scr);
Vec4f selection_color = vec4f(.25, .25, .25, 1);
simple_renderer_solid_rect(sr, select_begin_scr, vec2f(select_end_scr.x - select_begin_scr.x, FREE_GLYPH_FONT_SIZE), selection_color);
}
}
}
simple_renderer_flush(sr);
}
Vec2f cursor_pos = vec2fs(0.0f);
{
size_t cursor_row = editor_cursor_row(editor);
Line line = editor->lines.items[cursor_row];
size_t cursor_col = editor->cursor - line.begin;
cursor_pos.y = -((float)cursor_row + CURSOR_OFFSET) * FREE_GLYPH_FONT_SIZE;
cursor_pos.x = free_glyph_atlas_cursor_pos(
atlas,
editor->data.items + line.begin, line.end - line.begin,
vec2f(0.0, cursor_pos.y),
cursor_col
);
}
// Render search
{
if (editor->searching) {
simple_renderer_set_shader(sr, SHADER_FOR_COLOR);
Vec4f selection_color = vec4f(.10f, .10f, .25, 1);
Vec2f p1 = cursor_pos;
Vec2f p2 = p1;
free_glyph_atlas_measure_line_sized(editor->atlas, editor->search.items, editor->search.count, &p2);
simple_renderer_solid_rect(sr, p1, vec2f(p2.x - p1.x, FREE_GLYPH_FONT_SIZE), selection_color);
simple_renderer_flush(sr);
}
}
// Render text
{
simple_renderer_set_shader(sr, SHADER_FOR_TEXT);
for (size_t i = 0; i < editor->tokens.count; ++i) {
Token token = editor->tokens.items[i];
Vec2f pos = token.position;
Vec4f color = vec4fs(1);
switch (token.kind) {
case TOKEN_PREPROC:
color = hex_to_vec4f(0x95A99FFF);
break;
case TOKEN_KEYWORD:
color = hex_to_vec4f(0xFFDD33FF);
break;
case TOKEN_COMMENT:
color = hex_to_vec4f(0xCC8C3CFF);
break;
case TOKEN_STRING:
color = hex_to_vec4f(0x73c936ff);
break;
default:
{}
}
free_glyph_atlas_render_line_sized(atlas, sr, token.text, token.text_len, &pos, color);
// TODO: the max_line_len should be calculated based on what's visible on the screen right now
if (max_line_len < pos.x) max_line_len = pos.x;
}
simple_renderer_flush(sr);
}
// Render cursor
simple_renderer_set_shader(sr, SHADER_FOR_COLOR);
{
float CURSOR_WIDTH = 5.0f;
Uint32 CURSOR_BLINK_THRESHOLD = 500;
Uint32 CURSOR_BLINK_PERIOD = 1000;
Uint32 t = SDL_GetTicks() - editor->last_stroke;
sr->verticies_count = 0;
if (t < CURSOR_BLINK_THRESHOLD || t/CURSOR_BLINK_PERIOD%2 != 0) {
simple_renderer_solid_rect(
sr,
cursor_pos, vec2f(CURSOR_WIDTH, FREE_GLYPH_FONT_SIZE),
vec4fs(1));
}
simple_renderer_flush(sr);
}
// Update camera
{
if (max_line_len > 1000.0f) {
max_line_len = 1000.0f;
}
float target_scale = w/3/(max_line_len*0.75); // TODO: division by 0
Vec2f target = cursor_pos;
float offset = 0.0f;
if (target_scale > 3.0f) {
target_scale = 3.0f;
} else {
offset = cursor_pos.x - w/3/sr->camera_scale;
if (offset < 0.0f) offset = 0.0f;
target = vec2f(w/3/sr->camera_scale + offset, cursor_pos.y);
}
sr->camera_vel = vec2f_mul(
vec2f_sub(target, sr->camera_pos),
vec2fs(2.0f));
sr->camera_scale_vel = (target_scale - sr->camera_scale) * 2.0f;
sr->camera_pos = vec2f_add(sr->camera_pos, vec2f_mul(sr->camera_vel, vec2fs(DELTA_TIME)));
sr->camera_scale = sr->camera_scale + sr->camera_scale_vel * DELTA_TIME;
}
}
void editor_update_selection(Editor *e, bool shift)
{
if (e->searching) return;
if (shift) {
if (!e->selection) {
e->selection = true;
e->select_begin = e->cursor;
}
} else {
if (e->selection) {
e->selection = false;
}
}
}
void editor_clipboard_copy(Editor *e)
{
if (e->searching) return;
if (e->selection) {
size_t begin = e->select_begin;
size_t end = e->cursor;
if (begin > end) SWAP(size_t, begin, end);
e->clipboard.count = 0;
sb_append_buf(&e->clipboard, &e->data.items[begin], end - begin + 1);
sb_append_null(&e->clipboard);
if (SDL_SetClipboardText(e->clipboard.items) < 0) {
fprintf(stderr, "ERROR: SDL ERROR: %s\n", SDL_GetError());
}
}
}
void editor_clipboard_paste(Editor *e)
{
char *text = SDL_GetClipboardText();
size_t text_len = strlen(text);
if (text_len > 0) {
editor_insert_buf(e, text, text_len);
} else {
fprintf(stderr, "ERROR: SDL ERROR: %s\n", SDL_GetError());
}
SDL_free(text);
}
void editor_start_search(Editor *e)
{
if (e->searching) {
for (size_t pos = e->cursor + 1; pos < e->data.count; ++pos) {
if (editor_search_matches_at(e, pos)) {
e->cursor = pos;
break;
}
}
} else {
e->searching = true;
if (e->selection) {
e->selection = false;
// TODO: put the selection into the search automatically
} else {
e->search.count = 0;
}
}
}
void editor_stop_search(Editor *e)
{
e->searching = false;
}
bool editor_search_matches_at(Editor *e, size_t pos)
{
if (e->data.count - pos < e->search.count) return false;
for (size_t i = 0; i < e->search.count; ++i) {
if (e->search.items[i] != e->data.items[pos + i]) {
return false;
}
}
return true;
}
void editor_move_to_begin(Editor *e)
{
editor_stop_search(e);
e->cursor = 0;
}
void editor_move_to_end(Editor *e)
{
editor_stop_search(e);
e->cursor = e->data.count;
}
void editor_move_to_line_begin(Editor *e)
{
editor_stop_search(e);
size_t row = editor_cursor_row(e);
e->cursor = e->lines.items[row].begin;
}
void editor_move_to_line_end(Editor *e)
{
editor_stop_search(e);
size_t row = editor_cursor_row(e);
e->cursor = e->lines.items[row].end;
}
void editor_move_paragraph_up(Editor *e)
{
editor_stop_search(e);
size_t row = editor_cursor_row(e);
while (row > 0 && e->lines.items[row].end - e->lines.items[row].begin <= 1) {
row -= 1;
}
while (row > 0 && e->lines.items[row].end - e->lines.items[row].begin > 1) {
row -= 1;
}
e->cursor = e->lines.items[row].begin;
}
void editor_move_paragraph_down(Editor *e)
{
editor_stop_search(e);
size_t row = editor_cursor_row(e);
while (row + 1 < e->lines.count && e->lines.items[row].end - e->lines.items[row].begin <= 1) {
row += 1;
}
while (row + 1 < e->lines.count && e->lines.items[row].end - e->lines.items[row].begin > 1) {
row += 1;
}
e->cursor = e->lines.items[row].begin;
}