-
-
Notifications
You must be signed in to change notification settings - Fork 21.5k
/
Copy pathcode_edit.cpp
3648 lines (3085 loc) · 127 KB
/
code_edit.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
/**************************************************************************/
/* code_edit.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "code_edit.h"
#include "code_edit.compat.inc"
#include "core/os/keyboard.h"
#include "core/string/string_builder.h"
#include "core/string/ustring.h"
#include "scene/theme/theme_db.h"
void CodeEdit::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_THEME_CHANGED: {
set_gutter_width(main_gutter, get_line_height());
set_gutter_width(line_number_gutter, (line_number_digits + 1) * theme_cache.font->get_char_size('0', theme_cache.font_size).width);
set_gutter_width(fold_gutter, get_line_height() / 1.2);
} break;
case NOTIFICATION_DRAW: {
RID ci = get_canvas_item();
const Size2 size = get_size();
const bool caret_visible = is_caret_visible();
const bool rtl = is_layout_rtl();
const int row_height = get_line_height();
if (line_length_guideline_columns.size() > 0) {
const int xmargin_beg = theme_cache.style_normal->get_margin(SIDE_LEFT) + get_total_gutter_width();
const int xmargin_end = size.width - theme_cache.style_normal->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
const float char_size = theme_cache.font->get_char_size('0', theme_cache.font_size).width;
for (int i = 0; i < line_length_guideline_columns.size(); i++) {
const int xoffset = xmargin_beg + char_size * (int)line_length_guideline_columns[i] - get_h_scroll();
if (xoffset > xmargin_beg && xoffset < xmargin_end) {
Color guideline_color = (i == 0) ? theme_cache.line_length_guideline_color : theme_cache.line_length_guideline_color * Color(1, 1, 1, 0.5);
if (rtl) {
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(size.width - xoffset, 0), Point2(size.width - xoffset, size.height), guideline_color);
continue;
}
RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(xoffset, 0), Point2(xoffset, size.height), guideline_color);
}
}
}
bool code_completion_below = false;
if (caret_visible && code_completion_active && code_completion_options.size() > 0) {
const int code_completion_options_count = code_completion_options.size();
const int lines = MIN(code_completion_options_count, theme_cache.code_completion_max_lines);
const Size2 icon_area_size(row_height, row_height);
code_completion_rect.size.width = code_completion_longest_line + theme_cache.code_completion_icon_separation + icon_area_size.width + 2;
code_completion_rect.size.height = lines * row_height;
const Point2 caret_pos = get_caret_draw_pos();
const int total_height = theme_cache.code_completion_style->get_minimum_size().y + code_completion_rect.size.height;
const bool can_fit_completion_above = (caret_pos.y - row_height > total_height);
const bool can_fit_completion_below = (caret_pos.y + row_height + total_height <= get_size().height);
if (!can_fit_completion_below && can_fit_completion_above) {
code_completion_rect.position.y = (caret_pos.y - total_height - row_height) + theme_cache.line_spacing;
} else {
code_completion_rect.position.y = caret_pos.y + (theme_cache.line_spacing / 2.0f);
code_completion_below = true;
}
const int scroll_width = code_completion_options_count > theme_cache.code_completion_max_lines ? theme_cache.code_completion_scroll_width : 0;
const int code_completion_base_width = theme_cache.font->get_string_size(code_completion_base, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
if (caret_pos.x - code_completion_base_width + code_completion_rect.size.width + scroll_width > get_size().width) {
code_completion_rect.position.x = get_size().width - code_completion_rect.size.width - scroll_width;
} else {
code_completion_rect.position.x = caret_pos.x - code_completion_base_width;
}
draw_style_box(theme_cache.code_completion_style, Rect2(code_completion_rect.position - theme_cache.code_completion_style->get_offset(), code_completion_rect.size + theme_cache.code_completion_style->get_minimum_size() + Size2(scroll_width, 0)));
if (theme_cache.code_completion_background_color.a > 0.01) {
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(code_completion_rect.position, code_completion_rect.size + Size2(scroll_width, 0)), theme_cache.code_completion_background_color);
}
code_completion_scroll_rect.position = code_completion_rect.position + Vector2(code_completion_rect.size.width, 0);
code_completion_scroll_rect.size = Vector2(scroll_width, code_completion_rect.size.height);
code_completion_line_ofs = CLAMP((code_completion_force_item_center < 0 ? code_completion_current_selected : code_completion_force_item_center) - lines / 2, 0, code_completion_options_count - lines);
RenderingServer::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(code_completion_rect.position.x, code_completion_rect.position.y + (code_completion_current_selected - code_completion_line_ofs) * row_height), Size2(code_completion_rect.size.width, row_height)), theme_cache.code_completion_selected_color);
for (int i = 0; i < lines; i++) {
int l = code_completion_line_ofs + i;
ERR_CONTINUE(l < 0 || l >= code_completion_options_count);
Ref<TextLine> tl;
tl.instantiate();
tl->add_string(code_completion_options[l].display, theme_cache.font, theme_cache.font_size);
int yofs = (row_height - tl->get_size().y) / 2;
Point2 title_pos(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height + yofs);
/* Draw completion icon if it is valid. */
const Ref<Texture2D> &icon = code_completion_options[l].icon;
Rect2 icon_area(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height, icon_area_size.width, icon_area_size.height);
if (icon.is_valid()) {
Size2 icon_size = icon_area.size * 0.7;
icon->draw_rect(ci, Rect2(icon_area.position + (icon_area.size - icon_size) / 2, icon_size));
}
title_pos.x = icon_area.position.x + icon_area.size.width + theme_cache.code_completion_icon_separation;
tl->set_width(code_completion_rect.size.width - (icon_area_size.x + theme_cache.code_completion_icon_separation));
if (rtl) {
if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
draw_rect(Rect2(Point2(code_completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
}
tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
} else {
if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
draw_rect(Rect2(Point2(code_completion_rect.position.x + code_completion_rect.size.width - icon_area_size.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
}
tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
}
Point2 match_pos = Point2(code_completion_rect.position.x + icon_area_size.x + theme_cache.code_completion_icon_separation, code_completion_rect.position.y + i * row_height);
for (int j = 0; j < code_completion_options[l].matches.size(); j++) {
Pair<int, int> match_segment = code_completion_options[l].matches[j];
int match_offset = theme_cache.font->get_string_size(code_completion_options[l].display.substr(0, match_segment.first), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
int match_len = theme_cache.font->get_string_size(code_completion_options[l].display.substr(match_segment.first, match_segment.second), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
draw_rect(Rect2(match_pos + Point2(match_offset, 0), Size2(match_len, row_height)), theme_cache.code_completion_existing_color);
}
tl->draw(ci, title_pos, code_completion_options[l].font_color);
}
/* Draw a small scroll rectangle to show a position in the options. */
if (scroll_width) {
Color scroll_color = is_code_completion_scroll_hovered || is_code_completion_scroll_pressed ? theme_cache.code_completion_scroll_hovered_color : theme_cache.code_completion_scroll_color;
float r = (float)theme_cache.code_completion_max_lines / code_completion_options_count;
float o = (float)code_completion_line_ofs / code_completion_options_count;
draw_rect(Rect2(code_completion_rect.position.x + code_completion_rect.size.width, code_completion_rect.position.y + o * code_completion_rect.size.y, scroll_width, code_completion_rect.size.y * r), scroll_color);
}
}
/* Code hint */
if (caret_visible && !code_hint.is_empty() && (!code_completion_active || (code_completion_below != code_hint_draw_below))) {
const int font_height = theme_cache.font->get_height(theme_cache.font_size);
Vector<String> code_hint_lines = code_hint.split("\n");
int line_count = code_hint_lines.size();
int max_width = 0;
for (int i = 0; i < line_count; i++) {
max_width = MAX(max_width, theme_cache.font->get_string_size(code_hint_lines[i], HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x);
}
Size2 minsize = theme_cache.code_hint_style->get_minimum_size() + Size2(max_width, line_count * font_height + (theme_cache.line_spacing * line_count - 1));
int offset = theme_cache.font->get_string_size(code_hint_lines[0].substr(0, code_hint_lines[0].find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
if (code_hint_xpos == -0xFFFF) {
code_hint_xpos = get_caret_draw_pos().x - offset;
}
Point2 hint_ofs = Vector2(code_hint_xpos, get_caret_draw_pos().y);
if (code_hint_draw_below) {
hint_ofs.y += theme_cache.line_spacing / 2.0f;
} else {
hint_ofs.y -= (minsize.y + row_height) - theme_cache.line_spacing;
}
draw_style_box(theme_cache.code_hint_style, Rect2(hint_ofs, minsize));
int yofs = 0;
for (int i = 0; i < line_count; i++) {
const String &line = code_hint_lines[i];
int begin = 0;
int end = 0;
if (line.contains(String::chr(0xFFFF))) {
begin = theme_cache.font->get_string_size(line.substr(0, line.find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
end = theme_cache.font->get_string_size(line.substr(0, line.rfind(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
}
Point2 round_ofs = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(0, theme_cache.font->get_ascent(theme_cache.font_size) + font_height * i + yofs);
round_ofs = round_ofs.round();
draw_string(theme_cache.font, round_ofs, line.replace(String::chr(0xFFFF), ""), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size, theme_cache.code_hint_color);
if (end > 0) {
// Draw an underline for the currently edited function parameter.
const Vector2 b = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(begin, font_height + font_height * i + yofs);
draw_line(b, b + Vector2(end - begin, 0), theme_cache.code_hint_color, 2);
// Draw a translucent text highlight as well.
const Rect2 highlight_rect = Rect2(
b - Vector2(0, font_height),
Vector2(end - begin, font_height));
draw_rect(highlight_rect, theme_cache.code_hint_color * Color(1, 1, 1, 0.2));
}
yofs += theme_cache.line_spacing;
}
}
} break;
case NOTIFICATION_DRAG_BEGIN: {
cancel_code_completion();
} break;
case NOTIFICATION_MOUSE_EXIT: {
queue_redraw();
} break;
}
}
void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
Ref<InputEventMouseButton> mb = p_gui_input;
if (mb.is_valid()) {
/* Ignore mouse clicks in IME input mode. */
if (has_ime_text()) {
return;
}
if (is_code_completion_scroll_pressed && mb->get_button_index() == MouseButton::LEFT) {
is_code_completion_scroll_pressed = false;
accept_event();
queue_redraw();
return;
}
if (is_code_completion_drag_started && !mb->is_pressed()) {
is_code_completion_drag_started = false;
accept_event();
queue_redraw();
return;
}
if (code_completion_active && code_completion_rect.has_point(mb->get_position())) {
if (!mb->is_pressed()) {
accept_event();
return;
}
is_code_completion_drag_started = true;
switch (mb->get_button_index()) {
case MouseButton::WHEEL_UP: {
if (code_completion_current_selected > 0) {
code_completion_current_selected--;
code_completion_force_item_center = -1;
queue_redraw();
}
} break;
case MouseButton::WHEEL_DOWN: {
if (code_completion_current_selected < code_completion_options.size() - 1) {
code_completion_current_selected++;
code_completion_force_item_center = -1;
queue_redraw();
}
} break;
case MouseButton::LEFT: {
if (code_completion_force_item_center == -1) {
code_completion_force_item_center = code_completion_current_selected;
}
code_completion_current_selected = CLAMP(code_completion_line_ofs + (mb->get_position().y - code_completion_rect.position.y) / get_line_height(), 0, code_completion_options.size() - 1);
if (mb->is_double_click()) {
confirm_code_completion();
}
queue_redraw();
} break;
default:
break;
}
accept_event();
return;
} else if (code_completion_active && code_completion_scroll_rect.has_point(mb->get_position())) {
if (mb->get_button_index() != MouseButton::LEFT) {
accept_event();
return;
}
if (mb->is_pressed()) {
is_code_completion_drag_started = true;
is_code_completion_scroll_pressed = true;
_update_scroll_selected_line(mb->get_position().y);
queue_redraw();
}
accept_event();
return;
}
cancel_code_completion();
set_code_hint("");
if (mb->is_pressed()) {
Vector2i mpos = mb->get_position();
if (is_layout_rtl()) {
mpos.x = get_size().x - mpos.x;
}
Point2i pos = get_line_column_at_pos(mpos, false);
int line = pos.y;
int col = pos.x;
if (line != -1 && mb->get_button_index() == MouseButton::LEFT) {
if (is_line_folded(line)) {
int wrap_index = get_line_wrap_index_at_column(line, col);
if (wrap_index == get_line_wrap_count(line)) {
int eol_icon_width = theme_cache.folded_eol_icon->get_width();
int left_margin = get_total_gutter_width() + eol_icon_width + get_line_width(line, wrap_index) - get_h_scroll();
if (mpos.x > left_margin && mpos.x <= left_margin + eol_icon_width + 3) {
unfold_line(line);
return;
}
}
}
}
} else {
if (mb->get_button_index() == MouseButton::LEFT) {
if (mb->is_command_or_control_pressed() && !symbol_lookup_word.is_empty()) {
Vector2i mpos = mb->get_position();
if (is_layout_rtl()) {
mpos.x = get_size().x - mpos.x;
}
Point2i pos = get_line_column_at_pos(mpos, false);
int line = pos.y;
int col = pos.x;
if (line != -1) {
emit_signal(SNAME("symbol_lookup"), symbol_lookup_word, line, col);
}
return;
}
}
}
}
Ref<InputEventMouseMotion> mm = p_gui_input;
if (mm.is_valid()) {
Vector2i mpos = mm->get_position();
if (is_layout_rtl()) {
mpos.x = get_size().x - mpos.x;
}
if (symbol_lookup_on_click_enabled) {
if (mm->is_command_or_control_pressed() && mm->get_button_mask().is_empty()) {
symbol_lookup_pos = get_line_column_at_pos(mpos);
symbol_lookup_new_word = get_word_at_pos(mpos);
if (symbol_lookup_new_word != symbol_lookup_word) {
emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
}
} else if (!mm->is_command_or_control_pressed() || (!mm->get_button_mask().is_empty() && symbol_lookup_pos != get_line_column_at_pos(mpos))) {
set_symbol_lookup_word_as_valid(false);
}
}
bool scroll_hovered = code_completion_scroll_rect.has_point(mpos);
if (is_code_completion_scroll_hovered != scroll_hovered) {
is_code_completion_scroll_hovered = scroll_hovered;
accept_event();
queue_redraw();
}
if (is_code_completion_scroll_pressed) {
_update_scroll_selected_line(mpos.y);
accept_event();
queue_redraw();
return;
}
if (code_completion_active && code_completion_rect.has_point(mm->get_position())) {
accept_event();
return;
}
}
Ref<InputEventKey> k = p_gui_input;
if (TextEdit::alt_input(p_gui_input)) {
accept_event();
return;
}
bool update_code_completion = false;
if (!k.is_valid()) {
// MouseMotion events should not be handled by TextEdit logic if we're
// currently clicking and dragging from the code completion panel.
if (!mm.is_valid() || !is_code_completion_drag_started) {
TextEdit::gui_input(p_gui_input);
}
return;
}
/* Ctrl + Hover symbols */
bool mac_keys = OS::get_singleton()->has_feature("macos") || OS::get_singleton()->has_feature("web_macos") || OS::get_singleton()->has_feature("web_ios");
if ((mac_keys && k->get_keycode() == Key::META) || (!mac_keys && k->get_keycode() == Key::CTRL)) {
if (symbol_lookup_on_click_enabled) {
if (k->is_pressed() && !is_dragging_cursor()) {
symbol_lookup_new_word = get_word_at_pos(get_local_mouse_pos());
if (symbol_lookup_new_word != symbol_lookup_word) {
emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
}
} else {
set_symbol_lookup_word_as_valid(false);
}
}
return;
}
/* If a modifier has been pressed, and nothing else, return. */
if (!k->is_pressed() || k->get_keycode() == Key::CTRL || k->get_keycode() == Key::ALT || k->get_keycode() == Key::SHIFT || k->get_keycode() == Key::META || k->get_keycode() == Key::CAPSLOCK) {
return;
}
// Allow unicode handling if:
// No modifiers are pressed (except Shift and CapsLock)
bool allow_unicode_handling = !(k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
/* AUTO-COMPLETE */
if (code_completion_enabled && k->is_action("ui_text_completion_query", true)) {
request_code_completion(true);
accept_event();
return;
}
if (code_completion_active) {
if (k->is_action("ui_up", true)) {
if (code_completion_current_selected > 0) {
code_completion_current_selected--;
} else {
code_completion_current_selected = code_completion_options.size() - 1;
}
code_completion_force_item_center = -1;
queue_redraw();
accept_event();
return;
}
if (k->is_action("ui_down", true)) {
if (code_completion_current_selected < code_completion_options.size() - 1) {
code_completion_current_selected++;
} else {
code_completion_current_selected = 0;
}
code_completion_force_item_center = -1;
queue_redraw();
accept_event();
return;
}
if (k->is_action("ui_page_up", true)) {
code_completion_current_selected = MAX(0, code_completion_current_selected - theme_cache.code_completion_max_lines);
code_completion_force_item_center = -1;
queue_redraw();
accept_event();
return;
}
if (k->is_action("ui_page_down", true)) {
code_completion_current_selected = MIN(code_completion_options.size() - 1, code_completion_current_selected + theme_cache.code_completion_max_lines);
code_completion_force_item_center = -1;
queue_redraw();
accept_event();
return;
}
if (k->is_action("ui_text_caret_line_start", true) || k->is_action("ui_text_caret_line_end", true)) {
cancel_code_completion();
}
if (k->is_action("ui_text_completion_replace", true) || k->is_action("ui_text_completion_accept", true)) {
confirm_code_completion(k->is_action("ui_text_completion_replace", true));
accept_event();
return;
}
if (k->is_action("ui_cancel", true)) {
cancel_code_completion();
accept_event();
return;
}
if (k->is_action("ui_text_backspace", true)) {
backspace();
_filter_code_completion_candidates_impl();
accept_event();
return;
}
if (k->is_action("ui_left", true) || k->is_action("ui_right", true)) {
update_code_completion = true;
} else {
update_code_completion = (allow_unicode_handling && k->get_unicode() >= 32);
}
if (!update_code_completion) {
cancel_code_completion();
}
}
/* MISC */
if (!code_hint.is_empty() && k->is_action("ui_cancel", true)) {
set_code_hint("");
accept_event();
return;
}
if (allow_unicode_handling && k->get_unicode() == ')') {
set_code_hint("");
}
/* Indentation */
if (k->is_action("ui_text_indent", true)) {
do_indent();
accept_event();
return;
}
if (k->is_action("ui_text_dedent", true)) {
unindent_lines();
accept_event();
return;
}
// Override new line actions, for auto indent.
if (k->is_action("ui_text_newline_above", true)) {
_new_line(false, true);
accept_event();
return;
}
if (k->is_action("ui_text_newline_blank", true)) {
_new_line(false);
accept_event();
return;
}
if (k->is_action("ui_text_newline", true)) {
_new_line();
accept_event();
return;
}
// Remove shift, otherwise actions will not match.
k = k->duplicate();
k->set_shift_pressed(false);
if (k->is_action("ui_text_caret_up", true) ||
k->is_action("ui_text_caret_down", true) ||
k->is_action("ui_text_caret_line_start", true) ||
k->is_action("ui_text_caret_line_end", true) ||
k->is_action("ui_text_caret_page_up", true) ||
k->is_action("ui_text_caret_page_down", true)) {
set_code_hint("");
}
TextEdit::gui_input(p_gui_input);
if (update_code_completion) {
_filter_code_completion_candidates_impl();
}
}
/* General overrides */
Control::CursorShape CodeEdit::get_cursor_shape(const Point2 &p_pos) const {
if (!symbol_lookup_word.is_empty()) {
return CURSOR_POINTING_HAND;
}
if ((code_completion_active && code_completion_rect.has_point(p_pos)) || (!is_editable() && (!is_selecting_enabled() || get_line_count() == 0))) {
return CURSOR_ARROW;
}
if (code_completion_active && code_completion_scroll_rect.has_point(p_pos)) {
return CURSOR_ARROW;
}
Point2i pos = get_line_column_at_pos(p_pos, false);
int line = pos.y;
int col = pos.x;
if (line != -1 && is_line_folded(line)) {
int wrap_index = get_line_wrap_index_at_column(line, col);
if (wrap_index == get_line_wrap_count(line)) {
int eol_icon_width = theme_cache.folded_eol_icon->get_width();
int left_margin = get_total_gutter_width() + eol_icon_width + get_line_width(line, wrap_index) - get_h_scroll();
if (p_pos.x > left_margin && p_pos.x <= left_margin + eol_icon_width + 3) {
return CURSOR_POINTING_HAND;
}
}
}
return TextEdit::get_cursor_shape(p_pos);
}
/* Text manipulation */
// Overridable actions
void CodeEdit::_handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) {
start_action(EditAction::ACTION_TYPING);
Vector<int> caret_edit_order = get_caret_index_edit_order();
for (const int &i : caret_edit_order) {
if (p_caret != -1 && p_caret != i) {
continue;
}
bool had_selection = has_selection(i);
String selection_text = (had_selection ? get_selected_text(i) : "");
if (had_selection) {
delete_selection(i);
}
// Remove the old character if in overtype mode and no selection.
if (is_overtype_mode_enabled() && !had_selection) {
// Make sure we don't try and remove empty space.
if (get_caret_column(i) < get_line(get_caret_line(i)).length()) {
remove_text(get_caret_line(i), get_caret_column(i), get_caret_line(i), get_caret_column(i) + 1);
}
}
const char32_t chr[2] = { (char32_t)p_unicode, 0 };
if (auto_brace_completion_enabled) {
int cl = get_caret_line(i);
int cc = get_caret_column(i);
if (had_selection) {
insert_text_at_caret(chr, i);
String close_key = get_auto_brace_completion_close_key(chr);
if (!close_key.is_empty()) {
insert_text_at_caret(selection_text + close_key, i);
set_caret_column(get_caret_column(i) - 1, i == 0, i);
}
} else {
int caret_move_offset = 1;
int post_brace_pair = cc < get_line(cl).length() ? _get_auto_brace_pair_close_at_pos(cl, cc) : -1;
if (has_string_delimiter(chr) && cc > 0 && !is_symbol(get_line(cl)[cc - 1]) && post_brace_pair == -1) {
insert_text_at_caret(chr, i);
} else if (cc < get_line(cl).length() && !is_symbol(get_line(cl)[cc])) {
insert_text_at_caret(chr, i);
} else if (post_brace_pair != -1 && auto_brace_completion_pairs[post_brace_pair].close_key[0] == chr[0]) {
caret_move_offset = auto_brace_completion_pairs[post_brace_pair].close_key.length();
} else if (is_in_comment(cl, cc) != -1 || (is_in_string(cl, cc) != -1 && has_string_delimiter(chr))) {
insert_text_at_caret(chr, i);
} else {
insert_text_at_caret(chr, i);
int pre_brace_pair = _get_auto_brace_pair_open_at_pos(cl, cc + 1);
if (pre_brace_pair != -1) {
insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
}
}
set_caret_column(cc + caret_move_offset, i == 0, i);
}
} else {
insert_text_at_caret(chr, i);
}
}
end_action();
}
void CodeEdit::_backspace_internal(int p_caret) {
if (!is_editable()) {
return;
}
if (has_selection(p_caret)) {
delete_selection(p_caret);
return;
}
begin_complex_operation();
Vector<int> caret_edit_order = get_caret_index_edit_order();
for (const int &i : caret_edit_order) {
if (p_caret != -1 && p_caret != i) {
continue;
}
int cc = get_caret_column(i);
int cl = get_caret_line(i);
if (cc == 0 && cl == 0) {
continue;
}
if (cl > 0 && _is_line_hidden(cl - 1)) {
unfold_line(get_caret_line(i) - 1);
}
int prev_line = cc ? cl : cl - 1;
int prev_column = cc ? (cc - 1) : (get_line(cl - 1).length());
merge_gutters(prev_line, cl);
if (auto_brace_completion_enabled && cc > 0) {
int idx = _get_auto_brace_pair_open_at_pos(cl, cc);
if (idx != -1) {
prev_column = cc - auto_brace_completion_pairs[idx].open_key.length();
if (_get_auto_brace_pair_close_at_pos(cl, cc) == idx) {
remove_text(prev_line, prev_column, cl, cc + auto_brace_completion_pairs[idx].close_key.length());
} else {
remove_text(prev_line, prev_column, cl, cc);
}
set_caret_line(prev_line, false, true, 0, i);
set_caret_column(prev_column, i == 0, i);
adjust_carets_after_edit(i, prev_line, prev_column, cl, cc + auto_brace_completion_pairs[idx].close_key.length());
continue;
}
}
// For space indentation we need to do a basic unindent if there are no chars to the left, acting the same way as tabs.
if (indent_using_spaces && cc != 0) {
if (get_first_non_whitespace_column(cl) >= cc) {
prev_column = cc - _calculate_spaces_till_next_left_indent(cc);
prev_line = cl;
}
}
remove_text(prev_line, prev_column, cl, cc);
set_caret_line(prev_line, false, true, 0, i);
set_caret_column(prev_column, i == 0, i);
adjust_carets_after_edit(i, prev_line, prev_column, cl, cc);
}
merge_overlapping_carets();
end_complex_operation();
}
/* Indent management */
void CodeEdit::set_indent_size(const int p_size) {
ERR_FAIL_COND_MSG(p_size <= 0, "Indend size must be greater than 0.");
if (indent_size == p_size) {
return;
}
indent_size = p_size;
if (indent_using_spaces) {
indent_text = String(" ").repeat(p_size);
} else {
indent_text = "\t";
}
set_tab_size(p_size);
}
int CodeEdit::get_indent_size() const {
return indent_size;
}
void CodeEdit::set_indent_using_spaces(const bool p_use_spaces) {
indent_using_spaces = p_use_spaces;
if (indent_using_spaces) {
indent_text = String(" ").repeat(indent_size);
} else {
indent_text = "\t";
}
}
bool CodeEdit::is_indent_using_spaces() const {
return indent_using_spaces;
}
void CodeEdit::set_auto_indent_enabled(bool p_enabled) {
auto_indent = p_enabled;
}
bool CodeEdit::is_auto_indent_enabled() const {
return auto_indent;
}
void CodeEdit::set_auto_indent_prefixes(const TypedArray<String> &p_prefixes) {
auto_indent_prefixes.clear();
for (int i = 0; i < p_prefixes.size(); i++) {
const String prefix = p_prefixes[i];
auto_indent_prefixes.insert(prefix[0]);
}
}
TypedArray<String> CodeEdit::get_auto_indent_prefixes() const {
TypedArray<String> prefixes;
for (const char32_t &E : auto_indent_prefixes) {
prefixes.push_back(String::chr(E));
}
return prefixes;
}
void CodeEdit::do_indent() {
if (!is_editable()) {
return;
}
if (has_selection()) {
indent_lines();
return;
}
if (!indent_using_spaces) {
insert_text_at_caret("\t");
return;
}
begin_complex_operation();
Vector<int> caret_edit_order = get_caret_index_edit_order();
for (const int &i : caret_edit_order) {
int spaces_to_add = _calculate_spaces_till_next_right_indent(get_caret_column(i));
if (spaces_to_add > 0) {
insert_text_at_caret(String(" ").repeat(spaces_to_add), i);
}
}
end_complex_operation();
}
void CodeEdit::indent_lines() {
if (!is_editable()) {
return;
}
begin_complex_operation();
Vector<int> caret_edit_order = get_caret_index_edit_order();
for (const int &c : caret_edit_order) {
// This value informs us by how much we changed selection position by indenting right.
// Default is 1 for tab indentation.
int selection_offset = 1;
int start_line = get_caret_line(c);
int end_line = start_line;
if (has_selection(c)) {
start_line = get_selection_from_line(c);
end_line = get_selection_to_line(c);
// Ignore the last line if the selection is not past the first column.
if (get_selection_to_column(c) == 0) {
selection_offset = 0;
end_line--;
}
}
for (int i = start_line; i <= end_line; i++) {
const String line_text = get_line(i);
if (line_text.size() == 0 && has_selection(c)) {
continue;
}
if (!indent_using_spaces) {
set_line(i, '\t' + line_text);
continue;
}
// We don't really care where selection is - we just need to know indentation level at the beginning of the line.
// Since we will add this many spaces, we want to move the whole selection and caret by this much.
int spaces_to_add = _calculate_spaces_till_next_right_indent(get_first_non_whitespace_column(i));
set_line(i, String(" ").repeat(spaces_to_add) + line_text);
selection_offset = spaces_to_add;
}
// Fix selection and caret being off after shifting selection right.
if (has_selection(c)) {
select(start_line, get_selection_from_column(c) + selection_offset, get_selection_to_line(c), get_selection_to_column(c) + selection_offset, c);
}
set_caret_column(get_caret_column(c) + selection_offset, false, c);
}
end_complex_operation();
queue_redraw();
}
void CodeEdit::unindent_lines() {
if (!is_editable()) {
return;
}
begin_complex_operation();
Vector<int> caret_edit_order = get_caret_index_edit_order();
for (const int &c : caret_edit_order) {
// Moving caret and selection after unindenting can get tricky because
// changing content of line can move caret and selection on its own (if new line ends before previous position of either)
// therefore we just remember initial values and at the end of the operation offset them by number of removed characters.
int removed_characters = 0;
int initial_selection_end_column = 0;
int initial_cursor_column = get_caret_column(c);
int start_line = get_caret_line(c);
int end_line = start_line;
if (has_selection(c)) {
start_line = get_selection_from_line(c);
end_line = get_selection_to_line(c);
// Ignore the last line if the selection is not past the first column.
initial_selection_end_column = get_selection_to_column(c);
if (initial_selection_end_column == 0) {
end_line--;
}
}
bool first_line_edited = false;
bool last_line_edited = false;
for (int i = start_line; i <= end_line; i++) {
String line_text = get_line(i);
if (line_text.begins_with("\t")) {
line_text = line_text.substr(1, line_text.length());
set_line(i, line_text);
removed_characters = 1;
first_line_edited = (i == start_line) ? true : first_line_edited;
last_line_edited = (i == end_line) ? true : last_line_edited;
continue;
}
if (line_text.begins_with(" ")) {
// When unindenting we aim to remove spaces before line that has selection no matter what is selected.
// Here we remove only enough spaces to align text to nearest full multiple of indentation_size.
// In case where selection begins at the start of indentation_size multiple we remove whole indentation level.
int spaces_to_remove = _calculate_spaces_till_next_left_indent(get_first_non_whitespace_column(i));
line_text = line_text.substr(spaces_to_remove, line_text.length());
set_line(i, line_text);
removed_characters = spaces_to_remove;
first_line_edited = (i == start_line) ? true : first_line_edited;
last_line_edited = (i == end_line) ? true : last_line_edited;
}
}
if (has_selection(c)) {
// Fix selection being off by one on the first line.
if (first_line_edited) {
select(get_selection_from_line(c), get_selection_from_column(c) - removed_characters, get_selection_to_line(c), initial_selection_end_column, c);
}
// Fix selection being off by one on the last line.
if (last_line_edited) {
select(get_selection_from_line(c), get_selection_from_column(c), get_selection_to_line(c), initial_selection_end_column - removed_characters, c);
}
}
set_caret_column(initial_cursor_column - removed_characters, false, c);
}
end_complex_operation();
queue_redraw();
}
void CodeEdit::convert_indent(int p_from_line, int p_to_line) {
if (!is_editable()) {
return;
}
// Check line range.
p_from_line = (p_from_line < 0) ? 0 : p_from_line;
p_to_line = (p_to_line < 0) ? get_line_count() - 1 : p_to_line;
ERR_FAIL_COND(p_from_line >= get_line_count());
ERR_FAIL_COND(p_to_line >= get_line_count());
ERR_FAIL_COND(p_to_line < p_from_line);
// Store caret states.
Vector<int> caret_columns;
Vector<Pair<int, int>> from_selections;
Vector<Pair<int, int>> to_selections;
caret_columns.resize(get_caret_count());
from_selections.resize(get_caret_count());
to_selections.resize(get_caret_count());
for (int c = 0; c < get_caret_count(); c++) {
caret_columns.write[c] = get_caret_column(c);
// Set "selection_from_line" to -1 to allow checking if there was a selection later.
if (!has_selection(c)) {
from_selections.write[c].first = -1;
continue;
}
from_selections.write[c].first = get_selection_from_line(c);
from_selections.write[c].second = get_selection_from_column(c);
to_selections.write[c].first = get_selection_to_line(c);
to_selections.write[c].second = get_selection_to_column(c);
}
// Check lines within range.
const char32_t from_indent_char = indent_using_spaces ? '\t' : ' ';
int size_diff = indent_using_spaces ? indent_size - 1 : -(indent_size - 1);
bool changed_indentation = false;
for (int i = p_from_line; i <= p_to_line; i++) {
String line = get_line(i);
if (line.length() <= 0) {