Debian Codegraph @

scene/gui/code_edit.cpp

raw symbols SCIP
1 /**************************************************************************/
2 /* code_edit.cpp */
3 /**************************************************************************/
4 /* This file is part of: */
5 /* GODOT ENGINE */
6 /* https://godotengine.org */
7 /**************************************************************************/
8 /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9 /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10 /* */
11 /* Permission is hereby granted, free of charge, to any person obtaining */
12 /* a copy of this software and associated documentation files (the */
13 /* "Software"), to deal in the Software without restriction, including */
14 /* without limitation the rights to use, copy, modify, merge, publish, */
15 /* distribute, sublicense, and/or sell copies of the Software, and to */
16 /* permit persons to whom the Software is furnished to do so, subject to */
17 /* the following conditions: */
18 /* */
19 /* The above copyright notice and this permission notice shall be */
20 /* included in all copies or substantial portions of the Software. */
21 /* */
22 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23 /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24 /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25 /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26 /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27 /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28 /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29 /**************************************************************************/
30
31 #include "code_edit.h"
32 #include "code_edit.compat.inc"
33
34 #include "core/config/project_settings.h"
35 #include "core/os/keyboard.h"
36 #include "core/string/string_builder.h"
37 #include "core/string/translation_server.h"
38 #include "core/string/ustring.h"
39 #include "scene/theme/theme_db.h"
40
41 void CodeEdit::_apply_project_settings() {
42 symbol_tooltip_timer->set_wait_time(GLOBAL_GET_CACHED(double, "gui/timers/tooltip_delay_sec"));
43 }
44
45 void CodeEdit::_notification(int p_what) {
46 switch (p_what) {
47 case NOTIFICATION_READY: {
48 _apply_project_settings();
49 #ifdef TOOLS_ENABLED
50 if (Engine::get_singleton()->is_editor_hint()) {
51 ProjectSettings::get_singleton()->connect("settings_changed", callable_mp(this, &CodeEdit::_apply_project_settings));
52 }
53 #endif // TOOLS_ENABLED
54 } break;
55
56 case NOTIFICATION_THEME_CHANGED: {
57 set_gutter_width(main_gutter, get_line_height());
58 _update_line_number_gutter_width();
59 set_gutter_width(fold_gutter, get_line_height() / 1.2);
60 _clear_line_number_text_cache();
61 } break;
62
63 case NOTIFICATION_TRANSLATION_CHANGED:
64 [[fallthrough]];
65 case NOTIFICATION_LAYOUT_DIRECTION_CHANGED:
66 [[fallthrough]];
67 case NOTIFICATION_VISIBILITY_CHANGED: {
68 // Avoid having many hidden text editors with unused cache filling up memory.
69 _clear_line_number_text_cache();
70 } break;
71
72 case NOTIFICATION_DRAW: {
73 RID ci = get_text_canvas_item();
74 const bool caret_visible = is_caret_visible();
75 const bool rtl = is_layout_rtl();
76 const int row_height = get_line_height();
77
78 if (caret_visible) {
79 const bool draw_code_completion = code_completion_active && !code_completion_options.is_empty();
80 const bool draw_code_hint = !code_hint.is_empty();
81
82 /* Code hint */
83 Size2 code_hint_minsize;
84 if (draw_code_hint) {
85 const int font_height = theme_cache.font->get_height(theme_cache.font_size);
86
87 Vector<String> code_hint_lines = code_hint.split("\n");
88 int line_count = code_hint_lines.size();
89
90 int max_width = 0;
91 for (int i = 0; i < line_count; i++) {
92 max_width = MAX(max_width, theme_cache.font->get_string_size(code_hint_lines[i], HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x);
93 }
94 code_hint_minsize = theme_cache.code_hint_style->get_minimum_size() + Size2(max_width, line_count * font_height + (theme_cache.line_spacing * line_count - 1));
95
96 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;
97 if (code_hint_xpos == -0xFFFF) {
98 code_hint_xpos = get_caret_draw_pos().x - offset;
99 }
100 Point2 hint_ofs = Vector2(code_hint_xpos, get_caret_draw_pos().y);
101 if (code_hint_draw_below) {
102 hint_ofs.y += theme_cache.line_spacing / 2.0f;
103 } else {
104 hint_ofs.y -= (code_hint_minsize.y + row_height) - theme_cache.line_spacing;
105 }
106
107 theme_cache.code_hint_style->draw(ci, Rect2(hint_ofs, code_hint_minsize));
108
109 int yofs = 0;
110 for (int i = 0; i < line_count; i++) {
111 const String &line = code_hint_lines[i];
112
113 int begin = 0;
114 int end = 0;
115 if (line.contains(String::chr(0xFFFF))) {
116 begin = theme_cache.font->get_string_size(line.substr(0, line.find(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
117 end = theme_cache.font->get_string_size(line.substr(0, line.rfind(String::chr(0xFFFF))), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
118 }
119
120 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);
121 round_ofs = round_ofs.round();
122 theme_cache.font->draw_string(ci, round_ofs, line.remove_char(0xFFFF), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size, theme_cache.code_hint_color);
123 if (end > 0) {
124 // Draw an underline for the currently edited function parameter.
125 const Vector2 b = hint_ofs + theme_cache.code_hint_style->get_offset() + Vector2(begin, font_height + font_height * i + yofs);
126 RS::get_singleton()->canvas_item_add_line(ci, b, b + Vector2(end - begin, 0), theme_cache.code_hint_color, 2);
127
128 // Draw a translucent text highlight as well.
129 const Rect2 highlight_rect = Rect2(
130 b - Vector2(0, font_height),
131 Vector2(end - begin, font_height));
132 RS::get_singleton()->canvas_item_add_rect(ci, highlight_rect, theme_cache.code_hint_color * Color(1, 1, 1, 0.2));
133 }
134 yofs += theme_cache.line_spacing;
135 }
136 }
137
138 /* Code completion */
139 if (draw_code_completion) {
140 const int code_completion_options_count = code_completion_options.size();
141 int lines = MIN(code_completion_options_count, theme_cache.code_completion_max_lines);
142 const Size2 icon_area_size(row_height, row_height);
143
144 code_completion_rect.size.width = code_completion_longest_line + theme_cache.code_completion_icon_separation + icon_area_size.width + 2;
145 code_completion_rect.size.height = lines * row_height;
146
147 const Point2 caret_pos = get_caret_draw_pos();
148 int total_height = theme_cache.code_completion_style->get_minimum_size().y + code_completion_rect.size.height;
149 int min_y = caret_pos.y - row_height;
150 int max_y = caret_pos.y + row_height + total_height;
151 if (draw_code_hint) {
152 if (code_hint_draw_below) {
153 max_y += code_hint_minsize.y;
154 } else {
155 min_y -= code_hint_minsize.y;
156 }
157 }
158
159 const bool can_fit_completion_above = min_y > total_height;
160 const bool can_fit_completion_below = max_y <= get_size().height;
161
162 bool should_place_above = !can_fit_completion_below && can_fit_completion_above;
163
164 if (!can_fit_completion_below && !can_fit_completion_above) {
165 const int space_above = caret_pos.y - row_height;
166 const int space_below = get_size().height - caret_pos.y;
167 should_place_above = space_above > space_below;
168
169 // Reduce the line count and recalculate heights to better fit the completion popup.
170 int space_avail;
171 if (should_place_above) {
172 space_avail = space_above - theme_cache.code_completion_style->get_minimum_size().y;
173 } else {
174 space_avail = space_below - theme_cache.code_completion_style->get_minimum_size().y;
175 }
176
177 int max_lines_fit = MAX(1, space_avail / row_height);
178 lines = MIN(lines, max_lines_fit);
179 code_completion_rect.size.height = lines * row_height;
180 total_height = theme_cache.code_completion_style->get_minimum_size().y + code_completion_rect.size.height;
181 }
182
183 if (should_place_above) {
184 code_completion_rect.position.y = (caret_pos.y - total_height - row_height) + theme_cache.line_spacing;
185 if (draw_code_hint && !code_hint_draw_below) {
186 code_completion_rect.position.y -= code_hint_minsize.y;
187 }
188 } else {
189 code_completion_rect.position.y = caret_pos.y + (theme_cache.line_spacing / 2.0f);
190 if (draw_code_hint && code_hint_draw_below) {
191 code_completion_rect.position.y += code_hint_minsize.y;
192 }
193 }
194
195 const int scroll_width = code_completion_options_count > theme_cache.code_completion_max_lines ? theme_cache.code_completion_scroll_width : 0;
196 const int code_completion_base_width = theme_cache.font->get_string_size(code_completion_base, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width;
197 if (caret_pos.x - code_completion_base_width + code_completion_rect.size.width + scroll_width > get_size().width) {
198 code_completion_rect.position.x = get_size().width - code_completion_rect.size.width - scroll_width;
199 } else {
200 code_completion_rect.position.x = caret_pos.x - code_completion_base_width;
201 }
202
203 theme_cache.code_completion_style->draw(ci, 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)));
204 if (theme_cache.code_completion_background_color.a > 0.01) {
205 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);
206 }
207
208 code_completion_scroll_rect.position = code_completion_rect.position + Vector2(code_completion_rect.size.width, 0);
209 code_completion_scroll_rect.size = Vector2(scroll_width, code_completion_rect.size.height);
210
211 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);
212 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);
213
214 const String &lang = _get_locale();
215 for (int i = 0; i < lines; i++) {
216 int l = code_completion_line_ofs + i;
217 ERR_CONTINUE(l < 0 || l >= code_completion_options_count);
218
219 Ref<TextLine> tl;
220 tl.instantiate();
221 tl->add_string(code_completion_options[l].display, theme_cache.font, theme_cache.font_size, lang);
222
223 int yofs = (row_height - tl->get_size().y) / 2;
224 Point2 title_pos(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height + yofs);
225
226 /* Draw completion icon if it is valid. */
227 const Ref<Texture2D> &icon = code_completion_options[l].icon;
228 Rect2 icon_area(code_completion_rect.position.x, code_completion_rect.position.y + i * row_height, icon_area_size.width, icon_area_size.height);
229 if (icon.is_valid()) {
230 Size2 icon_size = icon_area.size * 0.7;
231 icon->draw_rect(ci, Rect2(icon_area.position + (icon_area.size - icon_size) / 2, icon_size));
232 }
233 title_pos.x = icon_area.position.x + icon_area.size.width + theme_cache.code_completion_icon_separation;
234
235 tl->set_width(code_completion_rect.size.width - (icon_area_size.x + theme_cache.code_completion_icon_separation));
236 if (rtl) {
237 if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
238 RS::get_singleton()->canvas_item_add_rect(ci, Rect2(Point2(code_completion_rect.position.x, icon_area.position.y), icon_area_size), (Color)code_completion_options[l].default_value);
239 }
240 tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_RIGHT);
241 } else {
242 if (code_completion_options[l].default_value.get_type() == Variant::COLOR) {
243 const Color color = code_completion_options[l].default_value;
244 const Rect2 rect = Rect2(Point2(code_completion_rect.position.x + code_completion_rect.size.width - icon_area_size.x, icon_area.position.y), icon_area_size);
245 if (color.a < 1.0) {
246 theme_cache.completion_color_bg->draw_rect(ci, rect, true);
247 }
248
249 RS::get_singleton()->canvas_item_add_rect(ci, rect, color);
250 }
251 tl->set_horizontal_alignment(HORIZONTAL_ALIGNMENT_LEFT);
252 }
253
254 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);
255
256 for (int j = 0; j < code_completion_options[l].matches.size(); j++) {
257 Pair<int, int> match_segment = code_completion_options[l].matches[j];
258 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;
259 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;
260
261 RS::get_singleton()->canvas_item_add_rect(ci, Rect2(match_pos + Point2(match_offset, 0), Size2(match_len, row_height)), theme_cache.code_completion_existing_color);
262 }
263 tl->draw(ci, title_pos, code_completion_options[l].font_color);
264 }
265
266 /* Draw a small scroll rectangle to show a position in the options. */
267 if (scroll_width) {
268 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;
269
270 float r = (float)theme_cache.code_completion_max_lines / code_completion_options_count;
271 float o = (float)code_completion_line_ofs / code_completion_options_count;
272 RS::get_singleton()->canvas_item_add_rect(ci, 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);
273 }
274 }
275 }
276 } break;
277
278 case NOTIFICATION_DRAG_BEGIN: {
279 cancel_code_completion();
280 } break;
281
282 case NOTIFICATION_MOUSE_EXIT: {
283 symbol_tooltip_timer->stop();
284 } break;
285 }
286 }
287
288 void CodeEdit::_draw_guidelines() {
289 if (line_length_guideline_columns.is_empty()) {
290 return;
291 }
292
293 RID ci = get_canvas_item();
294 const Size2 size = get_size();
295 const bool rtl = is_layout_rtl();
296
297 Ref<StyleBox> style = is_editable() ? theme_cache.style_normal : theme_cache.style_readonly;
298 const int xmargin_beg = style->get_margin(SIDE_LEFT) + get_total_gutter_width();
299 const int xmargin_end = size.width - style->get_margin(SIDE_RIGHT) - (is_drawing_minimap() ? get_minimap_width() : 0);
300
301 for (int i = 0; i < line_length_guideline_columns.size(); i++) {
302 const int column_pos = theme_cache.font->get_string_size(String("0").repeat((int)line_length_guideline_columns[i]), HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).x;
303 const int xoffset = xmargin_beg + column_pos - get_h_scroll();
304 if (xoffset > xmargin_beg && xoffset < xmargin_end) {
305 Color guideline_color = (i == 0) ? theme_cache.line_length_guideline_color : theme_cache.line_length_guideline_color * Color(1, 1, 1, 0.5);
306 if (rtl) {
307 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(size.width - xoffset, 0), Point2(size.width - xoffset, size.height), guideline_color);
308 continue;
309 }
310 RenderingServer::get_singleton()->canvas_item_add_line(ci, Point2(xoffset, 0), Point2(xoffset, size.height), guideline_color);
311 }
312 }
313 }
314
315 void CodeEdit::gui_input(const Ref<InputEvent> &p_gui_input) {
316 Ref<InputEventPanGesture> pan_gesture = p_gui_input;
317 if (pan_gesture.is_valid() && code_completion_active && code_completion_rect.has_point(pan_gesture->get_position())) {
318 const real_t delta = pan_gesture->get_delta().y;
319 code_completion_pan_offset += delta;
320 if (code_completion_pan_offset <= -1.0) {
321 if (code_completion_current_selected > 0) {
322 code_completion_current_selected--;
323 code_completion_force_item_center = -1;
324 queue_redraw();
325 }
326 code_completion_pan_offset = 0;
327 } else if (code_completion_pan_offset >= +1.0) {
328 if (code_completion_current_selected < code_completion_options.size() - 1) {
329 code_completion_current_selected++;
330 code_completion_force_item_center = -1;
331 queue_redraw();
332 }
333 code_completion_pan_offset = 0;
334 }
335 accept_event();
336 return;
337 }
338
339 Ref<InputEventMouseButton> mb = p_gui_input;
340 if (mb.is_valid()) {
341 // Ignore mouse clicks in IME input mode, let TextEdit handle it.
342 if (has_ime_text()) {
343 TextEdit::gui_input(p_gui_input);
344 return;
345 }
346
347 if (is_code_completion_scroll_pressed && mb->get_button_index() == MouseButton::LEFT) {
348 is_code_completion_scroll_pressed = false;
349 accept_event();
350 queue_redraw();
351 return;
352 }
353
354 if (is_code_completion_drag_started && !mb->is_pressed()) {
355 is_code_completion_drag_started = false;
356 accept_event();
357 queue_redraw();
358 return;
359 }
360
361 if (code_completion_active && code_completion_rect.has_point(mb->get_position())) {
362 if (!mb->is_pressed()) {
363 accept_event();
364 return;
365 }
366 is_code_completion_drag_started = true;
367
368 switch (mb->get_button_index()) {
369 case MouseButton::WHEEL_UP: {
370 if (code_completion_current_selected > 0) {
371 code_completion_current_selected--;
372 code_completion_force_item_center = -1;
373 code_completion_pan_offset = 0.0f;
374 queue_redraw();
375 }
376 } break;
377 case MouseButton::WHEEL_DOWN: {
378 if (code_completion_current_selected < code_completion_options.size() - 1) {
379 code_completion_current_selected++;
380 code_completion_force_item_center = -1;
381 code_completion_pan_offset = 0.0f;
382 queue_redraw();
383 }
384 } break;
385 case MouseButton::LEFT: {
386 if (code_completion_force_item_center == -1) {
387 code_completion_force_item_center = code_completion_current_selected;
388 }
389
390 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);
391 code_completion_pan_offset = 0.0f;
392 if (mb->is_double_click()) {
393 confirm_code_completion();
394 }
395 queue_redraw();
396 } break;
397 default:
398 break;
399 }
400
401 accept_event();
402 return;
403 } else if (code_completion_active && code_completion_scroll_rect.has_point(mb->get_position())) {
404 if (mb->get_button_index() != MouseButton::LEFT) {
405 accept_event();
406 return;
407 }
408
409 if (mb->is_pressed()) {
410 is_code_completion_drag_started = true;
411 is_code_completion_scroll_pressed = true;
412
413 _update_scroll_selected_line(mb->get_position().y);
414 queue_redraw();
415 }
416
417 accept_event();
418 return;
419 }
420
421 cancel_code_completion();
422 set_code_hint("");
423
424 if (mb->is_pressed()) {
425 Vector2i mpos = mb->get_position();
426 if (is_layout_rtl()) {
427 mpos.x = get_size().x - mpos.x;
428 }
429
430 Point2i pos = get_line_column_at_pos(mpos, false);
431 int line = pos.y;
432 int col = pos.x;
433
434 if (line != -1 && mb->get_button_index() == MouseButton::LEFT) {
435 if (is_line_folded(line)) {
436 int wrap_index = get_line_wrap_index_at_column(line, col);
437 if (wrap_index == get_line_wrap_count(line)) {
438 int eol_icon_width = theme_cache.folded_eol_icon->get_width();
439 int left_margin = get_total_gutter_width() + eol_icon_width + get_line_width(line, wrap_index) - get_h_scroll();
440 if (mpos.x > left_margin && mpos.x <= left_margin + eol_icon_width + 3) {
441 unfold_line(line);
442 return;
443 }
444 }
445 }
446 }
447 } else {
448 if (mb->get_button_index() == MouseButton::LEFT) {
449 if (mb->is_command_or_control_pressed() && !symbol_lookup_word.is_empty()) {
450 Vector2i mpos = mb->get_position();
451 if (is_layout_rtl()) {
452 mpos.x = get_size().x - mpos.x;
453 }
454
455 Point2i pos = get_line_column_at_pos(mpos, false, false);
456 int line = pos.y;
457 int col = pos.x;
458
459 if (line != -1) {
460 emit_signal(SNAME("symbol_lookup"), symbol_lookup_word, line, col);
461 }
462 // Don't return here to pass event to TextEdit so it can clean up the mouse pressed state.
463 }
464 }
465 }
466 }
467
468 Ref<InputEventMouseMotion> mm = p_gui_input;
469 if (mm.is_valid()) {
470 Vector2i mpos = mm->get_position();
471 if (is_layout_rtl()) {
472 mpos.x = get_size().x - mpos.x;
473 }
474
475 if (symbol_lookup_on_click_enabled) {
476 if (mm->is_command_or_control_pressed() && mm->get_button_mask().is_empty()) {
477 symbol_lookup_pos = get_line_column_at_pos(mpos, false, false);
478 symbol_lookup_new_word = get_lookup_word(symbol_lookup_pos.y, symbol_lookup_pos.x);
479 if (symbol_lookup_new_word != symbol_lookup_word) {
480 emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
481 }
482 } else if (!mm->is_command_or_control_pressed() || (!mm->get_button_mask().is_empty() && symbol_lookup_pos != get_line_column_at_pos(mpos, false, false))) {
483 set_symbol_lookup_word_as_valid(false);
484 }
485 }
486
487 if (symbol_tooltip_on_hover_enabled) {
488 symbol_tooltip_pos = get_line_column_at_pos(mpos, false, false);
489 symbol_tooltip_word = get_lookup_word(symbol_tooltip_pos.y, symbol_tooltip_pos.x);
490 symbol_tooltip_timer->start();
491 }
492
493 bool scroll_hovered = code_completion_scroll_rect.has_point(mpos);
494 if (is_code_completion_scroll_hovered != scroll_hovered) {
495 is_code_completion_scroll_hovered = scroll_hovered;
496 accept_event();
497 queue_redraw();
498 }
499
500 if (is_code_completion_scroll_pressed) {
501 _update_scroll_selected_line(mpos.y);
502 accept_event();
503 queue_redraw();
504 return;
505 }
506
507 if (code_completion_active && code_completion_rect.has_point(mm->get_position())) {
508 accept_event();
509 return;
510 }
511 }
512
513 Ref<InputEventKey> k = p_gui_input;
514 if (TextEdit::alt_input(p_gui_input)) {
515 accept_event();
516 return;
517 }
518
519 bool update_code_completion = false;
520 if (k.is_null()) {
521 // MouseMotion events should not be handled by TextEdit logic if we're
522 // currently clicking and dragging from the code completion panel.
523 if (mm.is_null() || !is_code_completion_drag_started) {
524 TextEdit::gui_input(p_gui_input);
525 }
526 return;
527 }
528
529 /* Ctrl + Hover symbols */
530 bool mac_keys = OS::prefer_meta_over_ctrl();
531 if ((mac_keys && k->get_keycode() == Key::META) || (!mac_keys && k->get_keycode() == Key::CTRL)) {
532 if (symbol_lookup_on_click_enabled) {
533 if (k->is_pressed() && !is_dragging_cursor()) {
534 Point2i lookup_pos = get_line_column_at_pos(get_local_mouse_pos(), false, false);
535 symbol_lookup_new_word = get_lookup_word(lookup_pos.y, lookup_pos.x);
536 if (symbol_lookup_new_word != symbol_lookup_word) {
537 emit_signal(SNAME("symbol_validate"), symbol_lookup_new_word);
538 }
539 } else {
540 set_symbol_lookup_word_as_valid(false);
541 }
542 }
543 return;
544 }
545
546 /* If a modifier has been pressed, and nothing else, return. */
547 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) {
548 return;
549 }
550
551 // Allow unicode handling if:
552 // No modifiers are pressed (except Shift and CapsLock)
553 bool allow_unicode_handling = !(k->is_ctrl_pressed() || k->is_alt_pressed() || k->is_meta_pressed());
554
555 /* AUTO-COMPLETE */
556 if (code_completion_enabled && k->is_action("ui_text_completion_query", true)) {
557 request_code_completion(true);
558 accept_event();
559 return;
560 }
561
562 if (code_completion_active) {
563 if (k->is_action("ui_up", true)) {
564 if (code_completion_current_selected > 0) {
565 code_completion_current_selected--;
566 } else {
567 code_completion_current_selected = code_completion_options.size() - 1;
568 }
569 code_completion_force_item_center = -1;
570 code_completion_pan_offset = 0.0f;
571 queue_redraw();
572 accept_event();
573 return;
574 }
575 if (k->is_action("ui_down", true)) {
576 if (code_completion_current_selected < code_completion_options.size() - 1) {
577 code_completion_current_selected++;
578 } else {
579 code_completion_current_selected = 0;
580 }
581 code_completion_force_item_center = -1;
582 code_completion_pan_offset = 0.0f;
583 queue_redraw();
584 accept_event();
585 return;
586 }
587 if (k->is_action("ui_page_up", true)) {
588 code_completion_current_selected = MAX(0, code_completion_current_selected - theme_cache.code_completion_max_lines);
589 code_completion_force_item_center = -1;
590 code_completion_pan_offset = 0.0f;
591 queue_redraw();
592 accept_event();
593 return;
594 }
595 if (k->is_action("ui_page_down", true)) {
596 code_completion_current_selected = MIN(code_completion_options.size() - 1, code_completion_current_selected + theme_cache.code_completion_max_lines);
597 code_completion_force_item_center = -1;
598 code_completion_pan_offset = 0.0f;
599 queue_redraw();
600 accept_event();
601 return;
602 }
603 if (k->is_action("ui_text_caret_line_start", true) || k->is_action("ui_text_caret_line_end", true)) {
604 cancel_code_completion();
605 }
606 if (k->is_action("ui_text_completion_replace", true) || k->is_action("ui_text_completion_accept", true)) {
607 confirm_code_completion(k->is_action("ui_text_completion_replace", true));
608 accept_event();
609 return;
610 }
611 if (k->is_action("ui_cancel", true)) {
612 cancel_code_completion();
613 accept_event();
614 return;
615 }
616 if (k->is_action("ui_text_backspace", true)) {
617 backspace();
618 _filter_code_completion_candidates_impl();
619 accept_event();
620 return;
621 }
622
623 if (k->is_action("ui_left", true) || k->is_action("ui_right", true)) {
624 update_code_completion = true;
625 } else {
626 update_code_completion = (allow_unicode_handling && k->get_unicode() >= 32);
627 }
628 }
629
630 /* MISC */
631 if (!code_hint.is_empty() && k->is_action("ui_cancel", true)) {
632 set_code_hint("");
633 accept_event();
634 return;
635 }
636 if (allow_unicode_handling && k->get_unicode() == ')') {
637 set_code_hint("");
638 }
639
640 /* Indentation */
641 if (k->is_action("ui_text_indent", true)) {
642 do_indent();
643 accept_event();
644 return;
645 }
646
647 if (k->is_action("ui_text_dedent", true)) {
648 unindent_lines();
649 accept_event();
650 return;
651 }
652
653 // Override new line actions, for auto indent.
654 if (k->is_action("ui_text_newline_above", true)) {
655 _new_line(false, true);
656 accept_event();
657 return;
658 }
659 if (k->is_action("ui_text_newline_blank", true)) {
660 _new_line(false);
661 accept_event();
662 return;
663 }
664 if (k->is_action("ui_text_newline", true)) {
665 _new_line();
666 accept_event();
667 return;
668 }
669
670 // Remove shift, otherwise actions will not match.
671 k = k->duplicate();
672 k->set_shift_pressed(false);
673
674 if (k->is_action("ui_text_caret_up", true) ||
675 k->is_action("ui_text_caret_down", true) ||
676 k->is_action("ui_text_caret_line_start", true) ||
677 k->is_action("ui_text_caret_line_end", true) ||
678 k->is_action("ui_text_caret_page_up", true) ||
679 k->is_action("ui_text_caret_page_down", true)) {
680 set_code_hint("");
681 }
682
683 TextEdit::gui_input(p_gui_input);
684
685 if (update_code_completion) {
686 _filter_code_completion_candidates_impl();
687 }
688 }
689
690 /* General overrides */
691 Control::CursorShape CodeEdit::get_cursor_shape(const Point2 &p_pos) const {
692 if (!symbol_lookup_word.is_empty()) {
693 return CURSOR_POINTING_HAND;
694 }
695
696 if (is_dragging_cursor()) {
697 return TextEdit::get_cursor_shape(p_pos);
698 }
699
700 if ((code_completion_active && code_completion_rect.has_point(p_pos)) || (!is_editable() && (!is_selecting_enabled() || get_line_count() == 0))) {
701 return CURSOR_ARROW;
702 }
703
704 if (code_completion_active && code_completion_scroll_rect.has_point(p_pos)) {
705 return CURSOR_ARROW;
706 }
707
708 Point2i pos = get_line_column_at_pos(p_pos, false);
709 int line = pos.y;
710 int col = pos.x;
711
712 if (line != -1 && is_line_folded(line)) {
713 int wrap_index = get_line_wrap_index_at_column(line, col);
714 if (wrap_index == get_line_wrap_count(line)) {
715 int eol_icon_width = theme_cache.folded_eol_icon->get_width();
716 int left_margin = get_total_gutter_width() + eol_icon_width + get_line_width(line, wrap_index) - get_h_scroll();
717 if (p_pos.x > left_margin && p_pos.x <= left_margin + eol_icon_width + 3) {
718 return CURSOR_POINTING_HAND;
719 }
720 }
721 }
722
723 return TextEdit::get_cursor_shape(p_pos);
724 }
725
726 void CodeEdit::_unhide_carets() {
727 // Unfold caret and selection origin.
728 for (int i = 0; i < get_caret_count(); i++) {
729 if (_is_line_hidden(get_caret_line(i))) {
730 unfold_line(get_caret_line(i));
731 }
732 if (has_selection(i) && _is_line_hidden(get_selection_origin_line(i))) {
733 unfold_line(get_selection_origin_line(i));
734 }
735 }
736 }
737
738 /* Text manipulation */
739
740 // Overridable actions
741 void CodeEdit::_handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) {
742 start_action(EditAction::ACTION_TYPING);
743 begin_multicaret_edit();
744 for (int i = 0; i < get_caret_count(); i++) {
745 if (p_caret != -1 && p_caret != i) {
746 continue;
747 }
748 if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
749 continue;
750 }
751
752 bool had_selection = has_selection(i);
753 String selection_text = (had_selection ? get_selected_text(i) : "");
754
755 if (had_selection) {
756 delete_selection(i);
757 }
758
759 // Remove the old character if in overtype mode and no selection.
760 if (is_overtype_mode_enabled() && !had_selection) {
761 // Make sure we don't try and remove empty space.
762 if (get_caret_column(i) < get_line(get_caret_line(i)).length()) {
763 remove_text(get_caret_line(i), get_caret_column(i), get_caret_line(i), get_caret_column(i) + 1);
764 }
765 }
766
767 const char32_t chr[2] = { (char32_t)p_unicode, 0 };
768
769 if (auto_brace_completion_enabled) {
770 int cl = get_caret_line(i);
771 int cc = get_caret_column(i);
772
773 if (had_selection) {
774 insert_text_at_caret(chr, i);
775
776 String close_key = get_auto_brace_completion_close_key(chr);
777 if (!close_key.is_empty()) {
778 insert_text_at_caret(selection_text + close_key, i);
779 set_caret_column(get_caret_column(i) - 1, i == 0, i);
780 }
781 } else {
782 int caret_move_offset = 1;
783
784 int post_brace_pair = cc < get_line(cl).length() ? _get_auto_brace_pair_close_at_pos(cl, cc) : -1;
785
786 if (has_string_delimiter(chr) && cc > 0 && !is_symbol(get_line(cl)[cc - 1]) && post_brace_pair == -1) {
787 insert_text_at_caret(chr, i);
788 } else if (cc < get_line(cl).length() && !is_symbol(get_line(cl)[cc])) {
789 insert_text_at_caret(chr, i);
790 } else if (post_brace_pair != -1 && auto_brace_completion_pairs[post_brace_pair].close_key[0] == chr[0]) {
791 caret_move_offset = auto_brace_completion_pairs[post_brace_pair].close_key.length();
792 } else if (is_in_comment(cl, cc) != -1 || (is_in_string(cl, cc) != -1 && has_string_delimiter(chr))) {
793 insert_text_at_caret(chr, i);
794 } else {
795 insert_text_at_caret(chr, i);
796
797 int pre_brace_pair = _get_auto_brace_pair_open_at_pos(cl, cc + 1);
798 if (pre_brace_pair != -1) {
799 insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
800 }
801 }
802 set_caret_column(cc + caret_move_offset, i == 0, i);
803 }
804 } else {
805 insert_text_at_caret(chr, i);
806 }
807 }
808 end_multicaret_edit();
809 end_action();
810 }
811
812 void CodeEdit::_backspace_internal(int p_caret) {
813 if (!is_editable()) {
814 return;
815 }
816
817 if (has_selection(p_caret)) {
818 delete_selection(p_caret);
819 return;
820 }
821
822 begin_complex_operation();
823 begin_multicaret_edit();
824 for (int i = 0; i < get_caret_count(); i++) {
825 if (p_caret != -1 && p_caret != i) {
826 continue;
827 }
828 if (p_caret == -1 && multicaret_edit_ignore_caret(i)) {
829 continue;
830 }
831
832 int to_line = get_caret_line(i);
833 int to_column = get_caret_column(i);
834
835 if (to_column == 0 && to_line == 0) {
836 continue;
837 }
838
839 if (to_line > 0 && to_column == 0 && _is_line_hidden(to_line - 1)) {
840 unfold_line(to_line - 1);
841 }
842
843 int from_line = to_column > 0 ? to_line : to_line - 1;
844 int from_column = 0;
845 if (to_column == 0) {
846 from_column = get_line(to_line - 1).length();
847 } else if (TextEdit::is_caret_mid_grapheme_enabled() || !TextEdit::is_backspace_deletes_composite_character_enabled()) {
848 from_column = to_column - 1;
849 } else {
850 from_column = TextEdit::get_previous_composite_character_column(to_line, to_column);
851 }
852
853 merge_gutters(from_line, to_line);
854
855 if (auto_brace_completion_enabled && to_column > 0) {
856 int idx = _get_auto_brace_pair_open_at_pos(to_line, to_column);
857 if (idx != -1) {
858 from_column = to_column - auto_brace_completion_pairs[idx].open_key.length();
859
860 if (_get_auto_brace_pair_close_at_pos(to_line, to_column) == idx) {
861 to_column += auto_brace_completion_pairs[idx].close_key.length();
862 }
863 }
864 }
865
866 // For space indentation we need to do a basic unindent if there are no chars to the left, acting the same way as tabs.
867 if (indent_using_spaces && to_column != 0) {
868 if (get_first_non_whitespace_column(to_line) >= to_column) {
869 from_column = to_column - _calculate_spaces_till_next_left_indent(to_column);
870 from_line = to_line;
871 }
872 }
873
874 remove_text(from_line, from_column, to_line, to_column);
875
876 set_caret_line(from_line, false, true, -1, i);
877 set_caret_column(from_column, i == 0, i);
878 }
879
880 end_multicaret_edit();
881 end_complex_operation();
882 }
883
884 void CodeEdit::_cut_internal(int p_caret) {
885 // Overridden to unfold lines.
886 _copy_internal(p_caret);
887
888 if (!is_editable()) {
889 return;
890 }
891
892 if (has_selection(p_caret)) {
893 delete_selection(p_caret);
894 return;
895 }
896 if (!is_empty_selection_clipboard_enabled()) {
897 return;
898 }
899 if (p_caret == -1) {
900 delete_lines();
901 } else {
902 unfold_line(get_caret_line(p_caret));
903 remove_line_at(get_caret_line(p_caret));
904 }
905 }
906
907 /* Indent management */
908 void CodeEdit::set_indent_size(const int p_size) {
909 ERR_FAIL_COND_MSG(p_size <= 0, "Indend size must be greater than 0.");
910 if (indent_size == p_size) {
911 return;
912 }
913
914 indent_size = p_size;
915 if (indent_using_spaces) {
916 indent_text = String(" ").repeat(p_size);
917 } else {
918 indent_text = "\t";
919 }
920 set_tab_size(p_size);
921 }
922
923 int CodeEdit::get_indent_size() const {
924 return indent_size;
925 }
926
927 void CodeEdit::set_indent_using_spaces(const bool p_use_spaces) {
928 indent_using_spaces = p_use_spaces;
929 if (indent_using_spaces) {
930 indent_text = String(" ").repeat(indent_size);
931 } else {
932 indent_text = "\t";
933 }
934 }
935
936 bool CodeEdit::is_indent_using_spaces() const {
937 return indent_using_spaces;
938 }
939
940 void CodeEdit::set_auto_indent_enabled(bool p_enabled) {
941 auto_indent = p_enabled;
942 }
943
944 bool CodeEdit::is_auto_indent_enabled() const {
945 return auto_indent;
946 }
947
948 void CodeEdit::set_auto_indent_prefixes(const TypedArray<String> &p_prefixes) {
949 auto_indent_prefixes.clear();
950 for (int i = 0; i < p_prefixes.size(); i++) {
951 const String prefix = p_prefixes[i];
952 auto_indent_prefixes.insert(prefix[0]);
953 }
954 }
955
956 TypedArray<String> CodeEdit::get_auto_indent_prefixes() const {
957 TypedArray<String> prefixes;
958 for (const char32_t &E : auto_indent_prefixes) {
959 prefixes.push_back(String::chr(E));
960 }
961 return prefixes;
962 }
963
964 void CodeEdit::do_indent() {
965 if (!is_editable()) {
966 return;
967 }
968
969 if (has_selection()) {
970 indent_lines();
971 return;
972 }
973
974 if (!indent_using_spaces) {
975 insert_text_at_caret("\t");
976 return;
977 }
978
979 begin_complex_operation();
980 begin_multicaret_edit();
981 for (int i = 0; i < get_caret_count(); i++) {
982 if (multicaret_edit_ignore_caret(i)) {
983 continue;
984 }
985 int spaces_to_add = _calculate_spaces_till_next_right_indent(get_caret_column(i));
986 if (spaces_to_add > 0) {
987 insert_text_at_caret(String(" ").repeat(spaces_to_add), i);
988 }
989 }
990 end_multicaret_edit();
991 end_complex_operation();
992 }
993
994 void CodeEdit::indent_lines() {
995 if (!is_editable()) {
996 return;
997 }
998
999 begin_complex_operation();
1000 begin_multicaret_edit();
1001
1002 Vector<Point2i> line_ranges = get_line_ranges_from_carets();
1003 for (Point2i line_range : line_ranges) {
1004 for (int i = line_range.x; i <= line_range.y; i++) {
1005 const String line_text = get_line(i);
1006 if (line_text.is_empty()) {
1007 // Ignore empty lines.
1008 continue;
1009 }
1010
1011 if (indent_using_spaces) {
1012 int spaces_to_add = _calculate_spaces_till_next_right_indent(get_first_non_whitespace_column(i));
1013 insert_text(String(" ").repeat(spaces_to_add), i, 0, false);
1014 } else {
1015 insert_text("\t", i, 0, false);
1016 }
1017 }
1018 }
1019
1020 end_multicaret_edit();
1021 end_complex_operation();
1022 }
1023
1024 void CodeEdit::unindent_lines() {
1025 if (!is_editable()) {
1026 return;
1027 }
1028
1029 begin_complex_operation();
1030 begin_multicaret_edit();
1031
1032 Vector<Point2i> line_ranges = get_line_ranges_from_carets();
1033 for (Point2i line_range : line_ranges) {
1034 for (int i = line_range.x; i <= line_range.y; i++) {
1035 const String line_text = get_line(i);
1036
1037 if (line_text.begins_with("\t")) {
1038 remove_text(i, 0, i, 1);
1039 } else if (line_text.begins_with(" ")) {
1040 // Remove only enough spaces to align text to nearest full multiple of indentation_size.
1041 int spaces_to_remove = _calculate_spaces_till_next_left_indent(get_first_non_whitespace_column(i));
1042 remove_text(i, 0, i, spaces_to_remove);
1043 }
1044 }
1045 }
1046
1047 end_multicaret_edit();
1048 end_complex_operation();
1049 }
1050
1051 void CodeEdit::convert_indent(int p_from_line, int p_to_line) {
1052 if (!is_editable()) {
1053 return;
1054 }
1055
1056 // Check line range.
1057 p_from_line = (p_from_line < 0) ? 0 : p_from_line;
1058 p_to_line = (p_to_line < 0) ? get_line_count() - 1 : p_to_line;
1059
1060 ERR_FAIL_COND(p_from_line >= get_line_count());
1061 ERR_FAIL_COND(p_to_line >= get_line_count());
1062 ERR_FAIL_COND(p_to_line < p_from_line);
1063
1064 // Check lines within range.
1065 const char32_t from_indent_char = indent_using_spaces ? '\t' : ' ';
1066 int size_diff = indent_using_spaces ? indent_size - 1 : -(indent_size - 1);
1067 bool changed_indentation = false;
1068 for (int i = p_from_line; i <= p_to_line; i++) {
1069 String line = get_line(i);
1070
1071 if (line.length() <= 0) {
1072 continue;
1073 }
1074
1075 if (is_in_string(i) != -1) {
1076 continue;
1077 }
1078
1079 // Check chars in the line.
1080 int j = 0;
1081 int space_count = 0;
1082 bool line_changed = false;
1083 while (j < line.length() && (line[j] == ' ' || line[j] == '\t')) {
1084 if (line[j] != from_indent_char) {
1085 space_count = 0;
1086 j++;
1087 continue;
1088 }
1089 space_count++;
1090
1091 if (!indent_using_spaces && space_count != indent_size) {
1092 j++;
1093 continue;
1094 }
1095
1096 line_changed = true;
1097 if (!changed_indentation) {
1098 begin_complex_operation();
1099 begin_multicaret_edit();
1100 changed_indentation = true;
1101 }
1102
1103 // Calculate new line.
1104 line = line.left(j + ((size_diff < 0) ? size_diff : 0)) + indent_text + line.substr(j + 1);
1105
1106 space_count = 0;
1107 j += size_diff;
1108 }
1109
1110 if (line_changed) {
1111 // Use set line to preserve carets visual position.
1112 set_line(i, line);
1113 }
1114 }
1115
1116 if (!changed_indentation) {
1117 return;
1118 }
1119
1120 merge_overlapping_carets();
1121 end_multicaret_edit();
1122 end_complex_operation();
1123 }
1124
1125 int CodeEdit::_calculate_spaces_till_next_left_indent(int p_column) const {
1126 int spaces_till_indent = p_column % indent_size;
1127 if (spaces_till_indent == 0) {
1128 spaces_till_indent = indent_size;
1129 }
1130 return spaces_till_indent;
1131 }
1132
1133 int CodeEdit::_calculate_spaces_till_next_right_indent(int p_column) const {
1134 return indent_size - p_column % indent_size;
1135 }
1136
1137 void CodeEdit::_new_line(bool p_split_current_line, bool p_above) {
1138 if (!is_editable()) {
1139 return;
1140 }
1141
1142 begin_complex_operation();
1143 begin_multicaret_edit();
1144
1145 for (int i = 0; i < get_caret_count(); i++) {
1146 if (multicaret_edit_ignore_caret(i)) {
1147 continue;
1148 }
1149 // When not splitting the line, we need to factor in indentation from the end of the current line.
1150 const int cc = p_split_current_line ? get_caret_column(i) : get_line(get_caret_line(i)).length();
1151 const int cl = get_caret_line(i);
1152
1153 const String line = get_line(cl);
1154
1155 String ins = "";
1156 if (!p_above) {
1157 ins = "\n";
1158 }
1159
1160 // Append current indentation.
1161 int space_count = 0;
1162 int line_col = 0;
1163 for (; line_col < cc; line_col++) {
1164 if (line[line_col] == '\t') {
1165 ins += indent_text;
1166 space_count = 0;
1167 continue;
1168 }
1169
1170 if (line[line_col] == ' ') {
1171 space_count++;
1172
1173 if (space_count == indent_size) {
1174 ins += indent_text;
1175 space_count = 0;
1176 }
1177 continue;
1178 }
1179 break;
1180 }
1181 if (p_above) {
1182 ins += "\n";
1183 }
1184
1185 if (is_line_folded(cl)) {
1186 unfold_line(cl);
1187 }
1188
1189 // Indent once again if the previous line needs it, ie ':'.
1190 // Then add an addition new line for any closing pairs aka '()'.
1191 // Skip this in comments or if we are going above.
1192 bool brace_indent = false;
1193 if (auto_indent && !p_above && cc > 0 && is_in_comment(cl) == -1) {
1194 bool should_indent = false;
1195 char32_t indent_char = ' ';
1196
1197 for (; line_col < cc; line_col++) {
1198 char32_t c = line[line_col];
1199 if (auto_indent_prefixes.has(c) && is_in_comment(cl, line_col) == -1) {
1200 should_indent = true;
1201 indent_char = c;
1202 continue;
1203 }
1204
1205 // Make sure this is the last char, trailing whitespace or comments are okay.
1206 // Increment column for comments because the delimiter (#) should be ignored.
1207 if (should_indent && (!is_whitespace(c) && is_in_comment(cl, line_col + 1) == -1)) {
1208 should_indent = false;
1209 }
1210 }
1211
1212 if (should_indent) {
1213 ins += indent_text;
1214
1215 String closing_pair = get_auto_brace_completion_close_key(String::chr(indent_char));
1216 if (!closing_pair.is_empty() && line.find(closing_pair, cc) == cc) {
1217 // No need to move the brace below if we are not taking the text with us.
1218 if (p_split_current_line) {
1219 brace_indent = true;
1220 ins += "\n" + ins.substr(indent_text.size(), ins.length() - 2);
1221 } else {
1222 brace_indent = false;
1223 ins = "\n" + ins.substr(indent_text.size(), ins.length() - 2);
1224 }
1225 }
1226 }
1227 }
1228
1229 if (p_split_current_line) {
1230 insert_text_at_caret(ins, i);
1231 } else {
1232 insert_text(ins, cl, p_above ? 0 : get_line(cl).length(), p_above, p_above);
1233 deselect(i);
1234 set_caret_line(p_above ? cl : cl + 1, false, true, -1, i);
1235 set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i);
1236 }
1237 if (brace_indent) {
1238 // Move to inner indented line.
1239 set_caret_line(get_caret_line(i) - 1, false, true, 0, i);
1240 set_caret_column(get_line(get_caret_line(i)).length(), i == 0, i);
1241 }
1242 }
1243
1244 end_multicaret_edit();
1245 end_complex_operation();
1246 }
1247
1248 /* Auto brace completion */
1249 void CodeEdit::set_auto_brace_completion_enabled(bool p_enabled) {
1250 auto_brace_completion_enabled = p_enabled;
1251 }
1252
1253 bool CodeEdit::is_auto_brace_completion_enabled() const {
1254 return auto_brace_completion_enabled;
1255 }
1256
1257 void CodeEdit::set_highlight_matching_braces_enabled(bool p_enabled) {
1258 highlight_matching_braces_enabled = p_enabled;
1259 queue_redraw();
1260 }
1261
1262 bool CodeEdit::is_highlight_matching_braces_enabled() const {
1263 return highlight_matching_braces_enabled;
1264 }
1265
1266 void CodeEdit::add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key) {
1267 ERR_FAIL_COND_MSG(p_open_key.is_empty(), "auto brace completion open key cannot be empty");
1268 ERR_FAIL_COND_MSG(p_close_key.is_empty(), "auto brace completion close key cannot be empty");
1269
1270 for (int i = 0; i < p_open_key.length(); i++) {
1271 ERR_FAIL_COND_MSG(!is_symbol(p_open_key[i]), "auto brace completion open key must be a symbol");
1272 }
1273 for (int i = 0; i < p_close_key.length(); i++) {
1274 ERR_FAIL_COND_MSG(!is_symbol(p_close_key[i]), "auto brace completion close key must be a symbol");
1275 }
1276
1277 int at = 0;
1278 for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
1279 ERR_FAIL_COND_MSG(auto_brace_completion_pairs[i].open_key == p_open_key, "auto brace completion open key '" + p_open_key + "' already exists.");
1280 if (p_open_key.length() < auto_brace_completion_pairs[i].open_key.length()) {
1281 at++;
1282 }
1283 }
1284
1285 BracePair brace_pair;
1286 brace_pair.open_key = p_open_key;
1287 brace_pair.close_key = p_close_key;
1288 auto_brace_completion_pairs.insert(at, brace_pair);
1289 }
1290
1291 void CodeEdit::set_auto_brace_completion_pairs(const Dictionary &p_auto_brace_completion_pairs) {
1292 auto_brace_completion_pairs.clear();
1293
1294 for (const KeyValue<Variant, Variant> &kv : p_auto_brace_completion_pairs) {
1295 add_auto_brace_completion_pair(kv.key, kv.value);
1296 }
1297 }
1298
1299 Dictionary CodeEdit::get_auto_brace_completion_pairs() const {
1300 Dictionary brace_pairs;
1301 for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
1302 brace_pairs[auto_brace_completion_pairs[i].open_key] = auto_brace_completion_pairs[i].close_key;
1303 }
1304 return brace_pairs;
1305 }
1306
1307 bool CodeEdit::has_auto_brace_completion_open_key(const String &p_open_key) const {
1308 for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
1309 if (auto_brace_completion_pairs[i].open_key == p_open_key) {
1310 return true;
1311 }
1312 }
1313 return false;
1314 }
1315
1316 bool CodeEdit::has_auto_brace_completion_close_key(const String &p_close_key) const {
1317 for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
1318 if (auto_brace_completion_pairs[i].close_key == p_close_key) {
1319 return true;
1320 }
1321 }
1322 return false;
1323 }
1324
1325 String CodeEdit::get_auto_brace_completion_close_key(const String &p_open_key) const {
1326 for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
1327 if (auto_brace_completion_pairs[i].open_key == p_open_key) {
1328 return auto_brace_completion_pairs[i].close_key;
1329 }
1330 }
1331 return String();
1332 }
1333
1334 /* Main Gutter */
1335 void CodeEdit::_update_draw_main_gutter() {
1336 set_gutter_draw(main_gutter, draw_breakpoints || draw_bookmarks || draw_executing_lines);
1337 }
1338
1339 void CodeEdit::set_draw_breakpoints_gutter(bool p_draw) {
1340 draw_breakpoints = p_draw;
1341 set_gutter_clickable(main_gutter, p_draw);
1342 _update_draw_main_gutter();
1343 }
1344
1345 bool CodeEdit::is_drawing_breakpoints_gutter() const {
1346 return draw_breakpoints;
1347 }
1348
1349 void CodeEdit::set_draw_bookmarks_gutter(bool p_draw) {
1350 draw_bookmarks = p_draw;
1351 _update_draw_main_gutter();
1352 }
1353
1354 bool CodeEdit::is_drawing_bookmarks_gutter() const {
1355 return draw_bookmarks;
1356 }
1357
1358 void CodeEdit::set_draw_executing_lines_gutter(bool p_draw) {
1359 draw_executing_lines = p_draw;
1360 _update_draw_main_gutter();
1361 }
1362
1363 bool CodeEdit::is_drawing_executing_lines_gutter() const {
1364 return draw_executing_lines;
1365 }
1366
1367 void CodeEdit::_main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
1368 bool hovering = get_hovered_gutter() == Vector2i(main_gutter, p_line);
1369 RID ci = get_text_canvas_item();
1370 if (draw_breakpoints && theme_cache.breakpoint_icon.is_valid()) {
1371 bool breakpointed = is_line_breakpointed(p_line);
1372 bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
1373
1374 if (breakpointed || (hovering && !is_dragging_cursor() && !shift_pressed)) {
1375 int padding = p_region.size.x / 6;
1376
1377 Color use_color = theme_cache.breakpoint_color;
1378 if (hovering && !shift_pressed) {
1379 use_color = breakpointed ? use_color.lightened(0.3) : use_color.darkened(0.5);
1380 }
1381 Rect2 icon_region = p_region;
1382 icon_region.position += Point2(padding, padding);
1383 icon_region.size -= Point2(padding, padding) * 2;
1384 theme_cache.breakpoint_icon->draw_rect(ci, icon_region, false, use_color);
1385 }
1386 }
1387
1388 if (draw_bookmarks && theme_cache.bookmark_icon.is_valid()) {
1389 bool bookmarked = is_line_bookmarked(p_line);
1390 bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
1391
1392 if (bookmarked || (hovering && !is_dragging_cursor() && shift_pressed)) {
1393 int horizontal_padding = p_region.size.x / 2;
1394 int vertical_padding = p_region.size.y / 4;
1395
1396 Color use_color = theme_cache.bookmark_color;
1397 if (hovering && shift_pressed) {
1398 use_color = bookmarked ? use_color.lightened(0.3) : use_color.darkened(0.5);
1399 }
1400 Rect2 icon_region = p_region;
1401 icon_region.position += Point2(horizontal_padding, 0);
1402 icon_region.size -= Point2(horizontal_padding * 1.1, vertical_padding);
1403 theme_cache.bookmark_icon->draw_rect(ci, icon_region, false, use_color);
1404 }
1405 }
1406
1407 if (draw_executing_lines && is_line_executing(p_line) && theme_cache.executing_line_icon.is_valid()) {
1408 int horizontal_padding = p_region.size.x / 10;
1409 int vertical_padding = p_region.size.y / 4;
1410
1411 Rect2 icon_region = p_region;
1412 icon_region.position += Point2(horizontal_padding, vertical_padding);
1413 icon_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
1414 theme_cache.executing_line_icon->draw_rect(ci, icon_region, false, theme_cache.executing_line_color);
1415 }
1416 }
1417
1418 // Breakpoints
1419 void CodeEdit::set_line_as_breakpoint(int p_line, bool p_breakpointed) {
1420 ERR_FAIL_INDEX(p_line, get_line_count());
1421
1422 int mask = get_line_gutter_metadata(p_line, main_gutter);
1423 set_line_gutter_metadata(p_line, main_gutter, p_breakpointed ? mask | MAIN_GUTTER_BREAKPOINT : mask & ~MAIN_GUTTER_BREAKPOINT);
1424 if (p_breakpointed) {
1425 breakpointed_lines[p_line] = true;
1426 } else if (breakpointed_lines.has(p_line)) {
1427 breakpointed_lines.erase(p_line);
1428 }
1429 emit_signal(SNAME("breakpoint_toggled"), p_line);
1430 queue_redraw();
1431 }
1432
1433 bool CodeEdit::is_line_breakpointed(int p_line) const {
1434 return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BREAKPOINT;
1435 }
1436
1437 void CodeEdit::clear_breakpointed_lines() {
1438 for (int i = 0; i < get_line_count(); i++) {
1439 if (is_line_breakpointed(i)) {
1440 set_line_as_breakpoint(i, false);
1441 }
1442 }
1443 }
1444
1445 PackedInt32Array CodeEdit::get_breakpointed_lines() const {
1446 PackedInt32Array ret;
1447 for (int i = 0; i < get_line_count(); i++) {
1448 if (is_line_breakpointed(i)) {
1449 ret.append(i);
1450 }
1451 }
1452 return ret;
1453 }
1454
1455 // Bookmarks
1456 void CodeEdit::set_line_as_bookmarked(int p_line, bool p_bookmarked) {
1457 int mask = get_line_gutter_metadata(p_line, main_gutter);
1458 set_line_gutter_metadata(p_line, main_gutter, p_bookmarked ? mask | MAIN_GUTTER_BOOKMARK : mask & ~MAIN_GUTTER_BOOKMARK);
1459 queue_redraw();
1460 }
1461
1462 bool CodeEdit::is_line_bookmarked(int p_line) const {
1463 return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_BOOKMARK;
1464 }
1465
1466 void CodeEdit::clear_bookmarked_lines() {
1467 for (int i = 0; i < get_line_count(); i++) {
1468 if (is_line_bookmarked(i)) {
1469 set_line_as_bookmarked(i, false);
1470 }
1471 }
1472 }
1473
1474 PackedInt32Array CodeEdit::get_bookmarked_lines() const {
1475 PackedInt32Array ret;
1476 for (int i = 0; i < get_line_count(); i++) {
1477 if (is_line_bookmarked(i)) {
1478 ret.append(i);
1479 }
1480 }
1481 return ret;
1482 }
1483
1484 // Executing lines
1485 void CodeEdit::set_line_as_executing(int p_line, bool p_executing) {
1486 int mask = get_line_gutter_metadata(p_line, main_gutter);
1487 set_line_gutter_metadata(p_line, main_gutter, p_executing ? mask | MAIN_GUTTER_EXECUTING : mask & ~MAIN_GUTTER_EXECUTING);
1488 queue_redraw();
1489 }
1490
1491 bool CodeEdit::is_line_executing(int p_line) const {
1492 return (int)get_line_gutter_metadata(p_line, main_gutter) & MAIN_GUTTER_EXECUTING;
1493 }
1494
1495 void CodeEdit::clear_executing_lines() {
1496 for (int i = 0; i < get_line_count(); i++) {
1497 if (is_line_executing(i)) {
1498 set_line_as_executing(i, false);
1499 }
1500 }
1501 }
1502
1503 PackedInt32Array CodeEdit::get_executing_lines() const {
1504 PackedInt32Array ret;
1505 for (int i = 0; i < get_line_count(); i++) {
1506 if (is_line_executing(i)) {
1507 ret.append(i);
1508 }
1509 }
1510 return ret;
1511 }
1512
1513 /* Line numbers */
1514 void CodeEdit::set_draw_line_numbers(bool p_draw) {
1515 set_gutter_draw(line_number_gutter, p_draw);
1516 }
1517
1518 bool CodeEdit::is_draw_line_numbers_enabled() const {
1519 return is_gutter_drawn(line_number_gutter);
1520 }
1521
1522 void CodeEdit::set_line_numbers_zero_padded(bool p_zero_padded) {
1523 String new_line_number_padding = p_zero_padded ? "0" : " ";
1524 if (line_number_padding == new_line_number_padding) {
1525 return;
1526 }
1527
1528 line_number_padding = new_line_number_padding;
1529 _clear_line_number_text_cache();
1530 queue_redraw();
1531 }
1532
1533 bool CodeEdit::is_line_numbers_zero_padded() const {
1534 return line_number_padding == "0";
1535 }
1536
1537 void CodeEdit::set_line_numbers_min_digits(int p_count) {
1538 if (line_numbers_min_digits == p_count) {
1539 return;
1540 }
1541 line_numbers_min_digits = p_count;
1542
1543 int digits = MAX(line_numbers_min_digits, std::log10(get_line_count()) + 1);
1544 if (digits == line_number_digits) {
1545 return;
1546 }
1547 line_number_digits = digits;
1548 _clear_line_number_text_cache();
1549 _update_line_number_gutter_width();
1550 queue_redraw();
1551 }
1552
1553 int CodeEdit::get_line_numbers_min_digits() const {
1554 return line_numbers_min_digits;
1555 }
1556
1557 void CodeEdit::_line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region) {
1558 if (!Rect2(Vector2(0, 0), get_size()).intersects(p_region)) {
1559 return;
1560 }
1561
1562 bool rtl = is_layout_rtl();
1563 HashMap<int, RID>::Iterator E = line_number_text_cache.find(p_line);
1564 RID text_rid;
1565 if (E) {
1566 text_rid = E->value;
1567 } else {
1568 const String &lang = _get_locale();
1569 String fc = String::num_int64(p_line + 1).lpad(line_number_digits, line_number_padding);
1570 if (is_localizing_numeral_system()) {
1571 fc = TranslationServer::get_singleton()->format_number(fc, lang);
1572 }
1573
1574 text_rid = TS->create_shaped_text();
1575 if (theme_cache.font.is_valid()) {
1576 TS->shaped_text_add_string(text_rid, fc, theme_cache.font->get_rids(), theme_cache.font_size, theme_cache.font->get_opentype_features(), lang);
1577 }
1578 line_number_text_cache.insert(p_line, text_rid);
1579 }
1580
1581 Size2 text_size = TS->shaped_text_get_size(text_rid);
1582 Point2 ofs = p_region.get_center() - text_size / 2;
1583 ofs.y += TS->shaped_text_get_ascent(text_rid);
1584
1585 if (rtl) {
1586 ofs.x = p_region.get_end().x - text_size.width;
1587 } else {
1588 ofs.x = p_region.position.x;
1589 }
1590
1591 Color number_color = get_line_gutter_item_color(p_line, line_number_gutter);
1592 if (number_color == Color(1, 1, 1)) {
1593 number_color = theme_cache.line_number_color;
1594 }
1595
1596 TS->shaped_text_draw(text_rid, get_text_canvas_item(), ofs, -1, -1, number_color);
1597 }
1598
1599 void CodeEdit::_clear_line_number_text_cache() {
1600 for (const KeyValue<int, RID> &KV : line_number_text_cache) {
1601 TS->free_rid(KV.value);
1602 }
1603 line_number_text_cache.clear();
1604 }
1605
1606 void CodeEdit::_update_line_number_gutter_width() {
1607 set_gutter_width(line_number_gutter, (line_number_digits + 1) * theme_cache.font->get_char_size('0', theme_cache.font_size).width);
1608 }
1609
1610 /* Fold Gutter */
1611 void CodeEdit::set_draw_fold_gutter(bool p_draw) {
1612 set_gutter_draw(fold_gutter, p_draw);
1613 }
1614
1615 bool CodeEdit::is_drawing_fold_gutter() const {
1616 return is_gutter_drawn(fold_gutter);
1617 }
1618
1619 void CodeEdit::_fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region) {
1620 if (!can_fold_line(p_line) && !is_line_folded(p_line)) {
1621 set_line_gutter_clickable(p_line, fold_gutter, false);
1622 return;
1623 }
1624 set_line_gutter_clickable(p_line, fold_gutter, true);
1625 RID ci = get_text_canvas_item();
1626
1627 int horizontal_padding = p_region.size.x / 10;
1628 int vertical_padding = p_region.size.y / 6;
1629
1630 p_region.position += Point2(horizontal_padding, vertical_padding);
1631 p_region.size -= Point2(horizontal_padding, vertical_padding) * 2;
1632
1633 bool can_fold = can_fold_line(p_line);
1634
1635 if (is_line_code_region_start(p_line)) {
1636 Color region_icon_color = theme_cache.folded_code_region_color;
1637 region_icon_color.a = MAX(region_icon_color.a, 0.4f);
1638 if (can_fold) {
1639 theme_cache.can_fold_code_region_icon->draw_rect(ci, p_region, false, region_icon_color);
1640 } else {
1641 theme_cache.folded_code_region_icon->draw_rect(ci, p_region, false, region_icon_color);
1642 }
1643 return;
1644 }
1645 if (can_fold) {
1646 theme_cache.can_fold_icon->draw_rect(ci, p_region, false, theme_cache.code_folding_color);
1647 return;
1648 }
1649 theme_cache.folded_icon->draw_rect(ci, p_region, false, theme_cache.code_folding_color);
1650 }
1651
1652 /* Line Folding */
1653 void CodeEdit::set_line_folding_enabled(bool p_enabled) {
1654 line_folding_enabled = p_enabled;
1655 _set_hiding_enabled(p_enabled);
1656 }
1657
1658 bool CodeEdit::is_line_folding_enabled() const {
1659 return line_folding_enabled;
1660 }
1661
1662 bool CodeEdit::can_fold_line(int p_line) const {
1663 ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
1664 if (!line_folding_enabled) {
1665 return false;
1666 }
1667
1668 if (p_line + 1 >= get_line_count() || get_line(p_line).strip_edges().is_empty()) {
1669 return false;
1670 }
1671
1672 if (_is_line_hidden(p_line) || is_line_folded(p_line)) {
1673 return false;
1674 }
1675
1676 // Check for code region.
1677 if (is_line_code_region_end(p_line)) {
1678 return false;
1679 }
1680 if (is_line_code_region_start(p_line)) {
1681 int region_level = 0;
1682 // Check if there is a valid end region tag.
1683 for (int next_line = p_line + 1; next_line < get_line_count(); next_line++) {
1684 if (is_line_code_region_end(next_line)) {
1685 region_level -= 1;
1686 if (region_level == -1) {
1687 return true;
1688 }
1689 }
1690 if (is_line_code_region_start(next_line)) {
1691 region_level += 1;
1692 }
1693 }
1694 return false;
1695 }
1696
1697 /* Check for full multiline line or block strings / comments. */
1698 int in_comment = is_in_comment(p_line);
1699 int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
1700 if (in_string != -1 || in_comment != -1) {
1701 if (get_delimiter_start_position(p_line, get_line(p_line).size() - 1).y != p_line) {
1702 return false;
1703 }
1704
1705 int delimiter_end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
1706 /* No end line, therefore we have a multiline region over the rest of the file. */
1707 if (delimiter_end_line == -1) {
1708 return true;
1709 }
1710 /* End line is the same therefore we have a block. */
1711 if (delimiter_end_line == p_line) {
1712 /* Check we are the start of the block. */
1713 if (p_line - 1 >= 0) {
1714 if ((in_string != -1 && is_in_string(p_line - 1) != -1) || (in_comment != -1 && is_in_comment(p_line - 1) != -1 && !is_line_code_region_start(p_line - 1) && !is_line_code_region_end(p_line - 1))) {
1715 return false;
1716 }
1717 }
1718 /* Check it continues for at least one line. */
1719 return ((in_string != -1 && is_in_string(p_line + 1) != -1) || (in_comment != -1 && is_in_comment(p_line + 1) != -1 && !is_line_code_region_start(p_line + 1) && !is_line_code_region_end(p_line + 1)));
1720 }
1721 return ((in_string != -1 && is_in_string(delimiter_end_line) != -1) || (in_comment != -1 && is_in_comment(delimiter_end_line) != -1));
1722 }
1723
1724 /* Otherwise check indent levels. */
1725 int start_indent = get_indent_level(p_line);
1726 for (int i = p_line + 1; i < get_line_count(); i++) {
1727 if (is_in_string(i) != -1 || is_in_comment(i) != -1 || get_line(i).strip_edges().is_empty()) {
1728 continue;
1729 }
1730 return (get_indent_level(i) > start_indent);
1731 }
1732 return false;
1733 }
1734
1735 bool CodeEdit::_fold_line(int p_line) {
1736 ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
1737 if (!is_line_folding_enabled() || !can_fold_line(p_line)) {
1738 return false;
1739 }
1740
1741 /* Find the last line to be hidden. */
1742 const int line_count = get_line_count() - 1;
1743 int end_line = line_count;
1744
1745 // Fold code region.
1746 if (is_line_code_region_start(p_line)) {
1747 int region_level = 0;
1748 for (int endregion_line = p_line + 1; endregion_line < get_line_count(); endregion_line++) {
1749 if (is_line_code_region_start(endregion_line)) {
1750 region_level += 1;
1751 }
1752 if (is_line_code_region_end(endregion_line)) {
1753 region_level -= 1;
1754 if (region_level == -1) {
1755 end_line = endregion_line;
1756 break;
1757 }
1758 }
1759 }
1760 set_line_background_color(p_line, theme_cache.folded_code_region_color);
1761 }
1762
1763 int in_comment = is_in_comment(p_line);
1764 int in_string = (in_comment == -1) ? is_in_string(p_line) : -1;
1765 if (!is_line_code_region_start(p_line)) {
1766 if (in_string != -1 || in_comment != -1) {
1767 end_line = get_delimiter_end_position(p_line, get_line(p_line).size() - 1).y;
1768 // End line is the same therefore we have a block of single line delimiters.
1769 if (end_line == p_line) {
1770 for (int i = p_line + 1; i <= line_count; i++) {
1771 if ((in_string != -1 && is_in_string(i) == -1) || (in_comment != -1 && is_in_comment(i) == -1)) {
1772 break;
1773 }
1774 if (in_comment != -1 && (is_line_code_region_start(i) || is_line_code_region_end(i))) {
1775 // A code region tag should split a comment block, ending it early.
1776 break;
1777 }
1778 end_line = i;
1779 }
1780 }
1781 } else {
1782 int start_indent = get_indent_level(p_line);
1783 for (int i = p_line + 1; i <= line_count; i++) {
1784 if (get_line(i).strip_edges().is_empty()) {
1785 continue;
1786 }
1787 if (get_indent_level(i) > start_indent) {
1788 end_line = i;
1789 continue;
1790 }
1791 if (is_in_string(i) == -1 && is_in_comment(i) == -1) {
1792 break;
1793 }
1794 }
1795 }
1796 }
1797
1798 for (int i = p_line + 1; i <= end_line; i++) {
1799 _set_line_as_hidden(i, true);
1800 }
1801
1802 // Collapse any carets in the hidden area.
1803 collapse_carets(p_line, get_line(p_line).length(), end_line, get_line(end_line).length(), true);
1804
1805 return true;
1806 }
1807
1808 bool CodeEdit::_unfold_line(int p_line) {
1809 ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
1810 if (!is_line_folded(p_line) && !_is_line_hidden(p_line)) {
1811 return false;
1812 }
1813
1814 int fold_start = p_line;
1815 for (; fold_start > 0; fold_start--) {
1816 if (is_line_folded(fold_start)) {
1817 break;
1818 }
1819 }
1820 fold_start = is_line_folded(fold_start) ? fold_start : p_line;
1821
1822 for (int i = fold_start + 1; i < get_line_count(); i++) {
1823 if (!_is_line_hidden(i)) {
1824 break;
1825 }
1826 _set_line_as_hidden(i, false);
1827 if (is_line_code_region_start(i - 1)) {
1828 set_line_background_color(i - 1, Color(0.0, 0.0, 0.0, 0.0));
1829 }
1830 }
1831 return true;
1832 }
1833
1834 void CodeEdit::fold_all_lines() {
1835 bool any_line_folded = false;
1836
1837 for (int i = 0; i < get_line_count(); i++) {
1838 any_line_folded |= _fold_line(i);
1839 }
1840
1841 if (any_line_folded) {
1842 emit_signal(SNAME("_fold_line_updated"));
1843 }
1844 }
1845
1846 void CodeEdit::fold_line(int p_line) {
1847 bool line_folded = _fold_line(p_line);
1848
1849 if (line_folded) {
1850 emit_signal(SNAME("_fold_line_updated"));
1851 }
1852 }
1853
1854 void CodeEdit::unfold_all_lines() {
1855 bool any_line_unfolded = false;
1856
1857 for (int i = 0; i < get_line_count(); i++) {
1858 any_line_unfolded |= _unfold_line(i);
1859 }
1860
1861 if (any_line_unfolded) {
1862 emit_signal(SNAME("_fold_line_updated"));
1863 }
1864 }
1865
1866 void CodeEdit::unfold_line(int p_line) {
1867 bool line_unfolded = _unfold_line(p_line);
1868
1869 if (line_unfolded) {
1870 emit_signal(SNAME("_fold_line_updated"));
1871 }
1872 }
1873
1874 void CodeEdit::toggle_foldable_line(int p_line) {
1875 ERR_FAIL_INDEX(p_line, get_line_count());
1876 if (is_line_folded(p_line)) {
1877 unfold_line(p_line);
1878 return;
1879 }
1880 fold_line(p_line);
1881 }
1882
1883 void CodeEdit::toggle_foldable_lines_at_carets() {
1884 begin_multicaret_edit();
1885 int previous_line = -1;
1886 Vector<int> sorted = get_sorted_carets();
1887 for (int caret_idx : sorted) {
1888 if (multicaret_edit_ignore_caret(caret_idx)) {
1889 continue;
1890 }
1891 int line_idx = get_caret_line(caret_idx);
1892 if (line_idx != previous_line) {
1893 toggle_foldable_line(line_idx);
1894 previous_line = line_idx;
1895 }
1896 }
1897 end_multicaret_edit();
1898 }
1899
1900 int CodeEdit::get_folded_line_header(int p_line) const {
1901 ERR_FAIL_INDEX_V(p_line, get_line_count(), 0);
1902 // Search for the first non hidden line.
1903 while (p_line > 0) {
1904 if (!_is_line_hidden(p_line)) {
1905 break;
1906 }
1907 p_line--;
1908 }
1909 return p_line;
1910 }
1911
1912 bool CodeEdit::is_line_folded(int p_line) const {
1913 ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
1914 return p_line + 1 < get_line_count() && !_is_line_hidden(p_line) && _is_line_hidden(p_line + 1);
1915 }
1916
1917 TypedArray<int> CodeEdit::get_folded_lines_bind() const {
1918 TypedArray<int> folded_lines;
1919 for (int i = 0; i < get_line_count(); i++) {
1920 if (is_line_folded(i)) {
1921 folded_lines.push_back(i);
1922 }
1923 }
1924 return folded_lines;
1925 }
1926
1927 PackedInt32Array CodeEdit::get_folded_lines() const {
1928 PackedInt32Array folded_lines;
1929 for (int i = 0; i < get_line_count(); i++) {
1930 if (is_line_folded(i)) {
1931 folded_lines.push_back(i);
1932 }
1933 }
1934 return folded_lines;
1935 }
1936
1937 /* Code region */
1938 void CodeEdit::create_code_region() {
1939 // Abort if there is no selected text.
1940 if (!has_selection()) {
1941 return;
1942 }
1943 // Check that region tag find a comment delimiter and is valid.
1944 if (code_region_start_string.is_empty()) {
1945 WARN_PRINT_ONCE("Cannot create code region without any one line comment delimiters");
1946 return;
1947 }
1948 String region_name = atr(ETR("New Code Region"));
1949
1950 begin_complex_operation();
1951 begin_multicaret_edit();
1952 Vector<Point2i> line_ranges = get_line_ranges_from_carets(true, false);
1953
1954 // Add start and end region tags.
1955 int line_offset = 0;
1956 for (Point2i line_range : line_ranges) {
1957 insert_text("\n" + code_region_end_string, line_range.y + line_offset, get_line(line_range.y + line_offset).length());
1958 insert_line_at(line_range.x + line_offset, code_region_start_string + " " + region_name);
1959 fold_line(line_range.x + line_offset);
1960 line_offset += 2;
1961 }
1962 int first_region_start = line_ranges[0].x;
1963
1964 // Select name of the first region to allow quick edit.
1965 remove_secondary_carets();
1966 int tag_length = code_region_start_string.length() + region_name.length() + 1;
1967 select(first_region_start, code_region_start_string.length() + 1, first_region_start, tag_length);
1968
1969 end_multicaret_edit();
1970 end_complex_operation();
1971 }
1972
1973 String CodeEdit::get_code_region_start_tag() const {
1974 return code_region_start_tag;
1975 }
1976
1977 String CodeEdit::get_code_region_end_tag() const {
1978 return code_region_end_tag;
1979 }
1980
1981 void CodeEdit::set_code_region_tags(const String &p_start, const String &p_end) {
1982 ERR_FAIL_COND_MSG(p_start == p_end, "Starting and ending region tags cannot be identical.");
1983 ERR_FAIL_COND_MSG(p_start.is_empty(), "Starting region tag cannot be empty.");
1984 ERR_FAIL_COND_MSG(p_end.is_empty(), "Ending region tag cannot be empty.");
1985 code_region_start_tag = p_start;
1986 code_region_end_tag = p_end;
1987 _update_code_region_tags();
1988 }
1989
1990 bool CodeEdit::is_line_code_region_start(int p_line) const {
1991 ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
1992 if (code_region_start_string.is_empty()) {
1993 return false;
1994 }
1995 if (is_in_string(p_line) != -1) {
1996 return false;
1997 }
1998 Vector<String> split = get_line(p_line).strip_edges().split_spaces(1);
1999 return split.size() > 0 && split[0] == code_region_start_string;
2000 }
2001
2002 bool CodeEdit::is_line_code_region_end(int p_line) const {
2003 ERR_FAIL_INDEX_V(p_line, get_line_count(), false);
2004 if (code_region_start_string.is_empty()) {
2005 return false;
2006 }
2007 if (is_in_string(p_line) != -1) {
2008 return false;
2009 }
2010 Vector<String> split = get_line(p_line).strip_edges().split_spaces(1);
2011 return split.size() > 0 && split[0] == code_region_end_string;
2012 }
2013
2014 /* Delimiters */
2015 // Strings
2016 void CodeEdit::add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
2017 _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_STRING);
2018 }
2019
2020 void CodeEdit::remove_string_delimiter(const String &p_start_key) {
2021 _remove_delimiter(p_start_key, TYPE_STRING);
2022 }
2023
2024 bool CodeEdit::has_string_delimiter(const String &p_start_key) const {
2025 return _has_delimiter(p_start_key, TYPE_STRING);
2026 }
2027
2028 void CodeEdit::set_string_delimiters(const TypedArray<String> &p_string_delimiters) {
2029 _set_delimiters(p_string_delimiters, TYPE_STRING);
2030 }
2031
2032 void CodeEdit::clear_string_delimiters() {
2033 _clear_delimiters(TYPE_STRING);
2034 }
2035
2036 TypedArray<String> CodeEdit::get_string_delimiters() const {
2037 return _get_delimiters(TYPE_STRING);
2038 }
2039
2040 int CodeEdit::is_in_string(int p_line, int p_column) const {
2041 return _is_in_delimiter(p_line, p_column, TYPE_STRING);
2042 }
2043
2044 // Comments
2045 void CodeEdit::add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only) {
2046 _add_delimiter(p_start_key, p_end_key, p_line_only, TYPE_COMMENT);
2047 }
2048
2049 void CodeEdit::remove_comment_delimiter(const String &p_start_key) {
2050 _remove_delimiter(p_start_key, TYPE_COMMENT);
2051 }
2052
2053 bool CodeEdit::has_comment_delimiter(const String &p_start_key) const {
2054 return _has_delimiter(p_start_key, TYPE_COMMENT);
2055 }
2056
2057 void CodeEdit::set_comment_delimiters(const TypedArray<String> &p_comment_delimiters) {
2058 _set_delimiters(p_comment_delimiters, TYPE_COMMENT);
2059 }
2060
2061 void CodeEdit::clear_comment_delimiters() {
2062 _clear_delimiters(TYPE_COMMENT);
2063 }
2064
2065 TypedArray<String> CodeEdit::get_comment_delimiters() const {
2066 return _get_delimiters(TYPE_COMMENT);
2067 }
2068
2069 int CodeEdit::is_in_comment(int p_line, int p_column) const {
2070 return _is_in_delimiter(p_line, p_column, TYPE_COMMENT);
2071 }
2072
2073 String CodeEdit::get_delimiter_start_key(int p_delimiter_idx) const {
2074 ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
2075 return delimiters[p_delimiter_idx].start_key;
2076 }
2077
2078 String CodeEdit::get_delimiter_end_key(int p_delimiter_idx) const {
2079 ERR_FAIL_INDEX_V(p_delimiter_idx, delimiters.size(), "");
2080 return delimiters[p_delimiter_idx].end_key;
2081 }
2082
2083 Point2 CodeEdit::get_delimiter_start_position(int p_line, int p_column) const {
2084 if (delimiters.is_empty()) {
2085 return Point2(-1, -1);
2086 }
2087 ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
2088 ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
2089
2090 Point2 start_position;
2091 start_position.y = -1;
2092 start_position.x = -1;
2093
2094 bool in_region = ((p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->get()) != -1;
2095
2096 /* Check the keys for this line. */
2097 for (const KeyValue<int, int> &E : delimiter_cache[p_line]) {
2098 if (E.key > p_column) {
2099 break;
2100 }
2101 in_region = E.value != -1;
2102 start_position.x = in_region ? E.key : -1;
2103 }
2104
2105 /* Region was found on this line and is not a multiline continuation. */
2106 int line_length = get_line(p_line).length();
2107 if (start_position.x != -1 && line_length > 0 && start_position.x != line_length + 1) {
2108 start_position.y = p_line;
2109 return start_position;
2110 }
2111
2112 /* Not in a region */
2113 if (!in_region) {
2114 return start_position;
2115 }
2116
2117 /* Region starts on a previous line */
2118 for (int i = p_line - 1; i >= 0; i--) {
2119 if (delimiter_cache[i].size() < 1) {
2120 continue;
2121 }
2122 start_position.y = i;
2123 start_position.x = delimiter_cache[i].back()->key();
2124
2125 /* Make sure it's not a multiline continuation. */
2126 line_length = get_line(i).length();
2127 if (line_length > 0 && start_position.x != line_length + 1) {
2128 break;
2129 }
2130 }
2131 return start_position;
2132 }
2133
2134 Point2 CodeEdit::get_delimiter_end_position(int p_line, int p_column) const {
2135 if (delimiters.is_empty()) {
2136 return Point2(-1, -1);
2137 }
2138 ERR_FAIL_INDEX_V(p_line, get_line_count(), Point2(-1, -1));
2139 ERR_FAIL_COND_V(p_column - 1 > get_line(p_line).size(), Point2(-1, -1));
2140
2141 Point2 end_position;
2142 end_position.y = -1;
2143 end_position.x = -1;
2144
2145 int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
2146
2147 /* Check the keys for this line. */
2148 for (const KeyValue<int, int> &E : delimiter_cache[p_line]) {
2149 end_position.x = (E.value == -1) ? E.key : -1;
2150 if (E.key > p_column) {
2151 break;
2152 }
2153 region = E.value;
2154 }
2155
2156 /* Region was found on this line and is not a multiline continuation. */
2157 if (region != -1 && end_position.x != -1 && (delimiters[region].line_only || end_position.x != get_line(p_line).length() + 1)) {
2158 end_position.y = p_line;
2159 return end_position;
2160 }
2161
2162 /* Not in a region */
2163 if (region == -1) {
2164 end_position.x = -1;
2165 return end_position;
2166 }
2167
2168 /* Region ends on a later line */
2169 for (int i = p_line + 1; i < get_line_count(); i++) {
2170 if (delimiter_cache[i].size() < 1 || delimiter_cache[i].front()->value() != -1) {
2171 continue;
2172 }
2173 end_position.x = delimiter_cache[i].front()->key();
2174
2175 /* Make sure it's not a multiline continuation. */
2176 if (get_line(i).length() > 0 && end_position.x != get_line(i).length() + 1) {
2177 end_position.y = i;
2178 break;
2179 }
2180 end_position.x = -1;
2181 }
2182 return end_position;
2183 }
2184
2185 /* Code hint */
2186 void CodeEdit::set_code_hint(const String &p_hint) {
2187 if (code_hint == p_hint) {
2188 return;
2189 }
2190 code_hint = p_hint;
2191 code_hint_xpos = -0xFFFF;
2192 queue_redraw();
2193 }
2194
2195 void CodeEdit::set_code_hint_draw_below(bool p_below) {
2196 if (code_hint_draw_below == p_below) {
2197 return;
2198 }
2199 code_hint_draw_below = p_below;
2200 queue_redraw();
2201 }
2202
2203 /* Code Completion */
2204 void CodeEdit::set_code_completion_enabled(bool p_enable) {
2205 code_completion_enabled = p_enable;
2206 }
2207
2208 bool CodeEdit::is_code_completion_enabled() const {
2209 return code_completion_enabled;
2210 }
2211
2212 void CodeEdit::set_code_completion_prefixes(const TypedArray<String> &p_prefixes) {
2213 code_completion_prefixes.clear();
2214 for (int i = 0; i < p_prefixes.size(); i++) {
2215 const String prefix = p_prefixes[i];
2216
2217 ERR_CONTINUE_MSG(prefix.is_empty(), "Code completion prefix cannot be empty.");
2218 code_completion_prefixes.insert(prefix[0]);
2219 }
2220 }
2221
2222 TypedArray<String> CodeEdit::get_code_completion_prefixes() const {
2223 TypedArray<String> prefixes;
2224 for (const char32_t &E : code_completion_prefixes) {
2225 prefixes.push_back(String::chr(E));
2226 }
2227 return prefixes;
2228 }
2229
2230 String CodeEdit::get_text_for_code_completion() const {
2231 StringBuilder completion_text;
2232 const int text_size = get_line_count();
2233 for (int i = 0; i < text_size; i++) {
2234 String line = get_line(i);
2235
2236 if (i == get_caret_line()) {
2237 completion_text += line.substr(0, get_caret_column());
2238 /* Not unicode, represents the caret. */
2239 completion_text += String::chr(0xFFFF);
2240 completion_text += line.substr(get_caret_column());
2241 } else {
2242 completion_text += line;
2243 }
2244
2245 if (i != text_size - 1) {
2246 completion_text += "\n";
2247 }
2248 }
2249 return completion_text.as_string();
2250 }
2251
2252 void CodeEdit::request_code_completion(bool p_force) {
2253 if (GDVIRTUAL_CALL(_request_code_completion, p_force)) {
2254 return;
2255 }
2256
2257 /* Don't re-query if all existing options are quoted types, eg path, signal. */
2258 bool ignored = code_completion_active && !code_completion_options.is_empty();
2259 if (ignored) {
2260 ScriptLanguage::CodeCompletionKind kind = ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT;
2261 const ScriptLanguage::CodeCompletionOption *previous_option = nullptr;
2262 for (int i = 0; i < code_completion_options.size(); i++) {
2263 const ScriptLanguage::CodeCompletionOption &current_option = code_completion_options[i];
2264 if (!previous_option) {
2265 previous_option = &current_option;
2266 kind = current_option.kind;
2267 }
2268 if (previous_option->kind != current_option.kind) {
2269 ignored = false;
2270 break;
2271 }
2272 }
2273 ignored = ignored && (kind == ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH || kind == ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH || kind == ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL);
2274 }
2275
2276 if (ignored) {
2277 return;
2278 }
2279
2280 if (p_force) {
2281 emit_signal(SNAME("code_completion_requested"));
2282 return;
2283 }
2284
2285 String line = get_line(get_caret_line());
2286 int ofs = CLAMP(get_caret_column(), 0, line.length());
2287
2288 if (ofs > 0 && (is_in_string(get_caret_line(), ofs) != -1 || !is_symbol(line[ofs - 1]) || code_completion_prefixes.has(line[ofs - 1]))) {
2289 emit_signal(SNAME("code_completion_requested"));
2290 } else if (ofs > 1 && line[ofs - 1] == ' ' && code_completion_prefixes.has(line[ofs - 2])) {
2291 emit_signal(SNAME("code_completion_requested"));
2292 }
2293 }
2294
2295 void CodeEdit::add_code_completion_option(CodeCompletionKind p_type, const String &p_display_text, const String &p_insert_text, const Color &p_text_color, const Ref<Resource> &p_icon, const Variant &p_value, int p_location) {
2296 ScriptLanguage::CodeCompletionOption completion_option;
2297 completion_option.kind = (ScriptLanguage::CodeCompletionKind)p_type;
2298 completion_option.display = p_display_text;
2299 completion_option.insert_text = p_insert_text;
2300 completion_option.font_color = p_text_color;
2301 completion_option.icon = p_icon;
2302 completion_option.default_value = p_value;
2303 completion_option.location = p_location;
2304 code_completion_option_submitted.push_back(completion_option);
2305 }
2306
2307 void CodeEdit::update_code_completion_options(bool p_forced) {
2308 code_completion_forced = p_forced;
2309 code_completion_option_sources = code_completion_option_submitted;
2310 code_completion_option_submitted.clear();
2311 _filter_code_completion_candidates_impl();
2312 }
2313
2314 TypedArray<Dictionary> CodeEdit::get_code_completion_options() const {
2315 if (!code_completion_active) {
2316 return TypedArray<Dictionary>();
2317 }
2318
2319 TypedArray<Dictionary> completion_options;
2320 completion_options.resize(code_completion_options.size());
2321 for (int i = 0; i < code_completion_options.size(); i++) {
2322 Dictionary option;
2323 option["kind"] = code_completion_options[i].kind;
2324 option["display_text"] = code_completion_options[i].display;
2325 option["insert_text"] = code_completion_options[i].insert_text;
2326 option["font_color"] = code_completion_options[i].font_color;
2327 option["icon"] = code_completion_options[i].icon;
2328 option["location"] = code_completion_options[i].location;
2329 option["default_value"] = code_completion_options[i].default_value;
2330 completion_options[i] = option;
2331 }
2332 return completion_options;
2333 }
2334
2335 Dictionary CodeEdit::get_code_completion_option(int p_index) const {
2336 if (!code_completion_active) {
2337 return Dictionary();
2338 }
2339 ERR_FAIL_INDEX_V(p_index, code_completion_options.size(), Dictionary());
2340
2341 Dictionary option;
2342 option["kind"] = code_completion_options[p_index].kind;
2343 option["display_text"] = code_completion_options[p_index].display;
2344 option["insert_text"] = code_completion_options[p_index].insert_text;
2345 option["font_color"] = code_completion_options[p_index].font_color;
2346 option["icon"] = code_completion_options[p_index].icon;
2347 option["location"] = code_completion_options[p_index].location;
2348 option["default_value"] = code_completion_options[p_index].default_value;
2349 return option;
2350 }
2351
2352 int CodeEdit::get_code_completion_selected_index() const {
2353 return (code_completion_active) ? code_completion_current_selected : -1;
2354 }
2355
2356 void CodeEdit::set_code_completion_selected_index(int p_index) {
2357 if (!code_completion_active) {
2358 return;
2359 }
2360 ERR_FAIL_INDEX(p_index, code_completion_options.size());
2361 code_completion_current_selected = p_index;
2362 code_completion_force_item_center = -1;
2363 code_completion_pan_offset = 0.0f;
2364 queue_redraw();
2365 }
2366
2367 void CodeEdit::confirm_code_completion(bool p_replace) {
2368 if (!is_editable() || !code_completion_active) {
2369 return;
2370 }
2371
2372 if (GDVIRTUAL_CALL(_confirm_code_completion, p_replace)) {
2373 return;
2374 }
2375
2376 char32_t caret_last_completion_char = 0;
2377 begin_complex_operation();
2378 begin_multicaret_edit();
2379
2380 for (int i = 0; i < get_caret_count(); i++) {
2381 if (multicaret_edit_ignore_caret(i)) {
2382 continue;
2383 }
2384 int caret_line = get_caret_line(i);
2385
2386 const String &insert_text = code_completion_options[code_completion_current_selected].insert_text;
2387 const String &display_text = code_completion_options[code_completion_current_selected].display;
2388
2389 if (p_replace) {
2390 // Find end of current section.
2391 const String line = get_line(caret_line);
2392 int caret_col = get_caret_column(i);
2393 int caret_remove_line = caret_line;
2394
2395 bool merge_text = true;
2396 int in_string = is_in_string(caret_line, caret_col);
2397 if (in_string != -1) {
2398 Point2 string_end = get_delimiter_end_position(caret_line, caret_col);
2399 if (string_end.x != -1) {
2400 merge_text = false;
2401 caret_remove_line = string_end.y;
2402 caret_col = string_end.x - 1;
2403 }
2404 }
2405
2406 if (merge_text) {
2407 for (; caret_col < line.length(); caret_col++) {
2408 if (is_symbol(line[caret_col])) {
2409 break;
2410 }
2411 }
2412 }
2413
2414 // Replace.
2415 remove_text(caret_line, get_caret_column(i) - code_completion_base.length(), caret_remove_line, caret_col);
2416 insert_text_at_caret(insert_text, i);
2417 } else {
2418 // Get first non-matching char.
2419 const String line = get_line(caret_line);
2420 int caret_col = get_caret_column(i);
2421 int matching_chars = code_completion_base.length();
2422 for (; matching_chars <= insert_text.length(); matching_chars++) {
2423 if (caret_col >= line.length() || line[caret_col] != insert_text[matching_chars]) {
2424 break;
2425 }
2426 caret_col++;
2427 }
2428
2429 // Remove base completion text.
2430 remove_text(caret_line, get_caret_column(i) - code_completion_base.length(), caret_line, get_caret_column(i));
2431
2432 // Merge with text.
2433 insert_text_at_caret(insert_text.substr(0, code_completion_base.length()), i);
2434 set_caret_column(caret_col, false, i);
2435 insert_text_at_caret(insert_text.substr(matching_chars), i);
2436 }
2437
2438 // Handle merging of symbols eg strings, brackets.
2439 caret_line = get_caret_line(i);
2440 const String line = get_line(caret_line);
2441 char32_t next_char = line[get_caret_column(i)];
2442 char32_t last_completion_char = insert_text[insert_text.length() - 1];
2443 if (i == 0) {
2444 caret_last_completion_char = last_completion_char;
2445 }
2446 char32_t last_completion_char_display = display_text[display_text.length() - 1];
2447
2448 bool last_char_matches = (last_completion_char == next_char || last_completion_char_display == next_char);
2449 int pre_brace_pair = get_caret_column(i) > 0 ? _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i)) : -1;
2450 int post_brace_pair = get_caret_column(i) < get_line(caret_line).length() ? _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i)) : -1;
2451
2452 // Strings do not nest like brackets, so ensure we don't add an additional closing pair.
2453 if (has_string_delimiter(String::chr(last_completion_char))) {
2454 if (post_brace_pair != -1 && last_char_matches) {
2455 remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
2456 }
2457 } else {
2458 if (pre_brace_pair != -1 && pre_brace_pair != post_brace_pair && last_char_matches) {
2459 remove_text(caret_line, get_caret_column(i), caret_line, get_caret_column(i) + 1);
2460 } else if (auto_brace_completion_enabled && pre_brace_pair != -1) {
2461 insert_text_at_caret(auto_brace_completion_pairs[pre_brace_pair].close_key, i);
2462 set_caret_column(get_caret_column(i) - auto_brace_completion_pairs[pre_brace_pair].close_key.length(), i == 0, i);
2463 }
2464 }
2465
2466 if (pre_brace_pair == -1 && post_brace_pair == -1 && get_caret_column(i) > 0 && get_caret_column(i) < get_line(caret_line).length()) {
2467 pre_brace_pair = _get_auto_brace_pair_open_at_pos(caret_line, get_caret_column(i) + 1);
2468 if (pre_brace_pair != -1 && pre_brace_pair == _get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) - 1)) {
2469 remove_text(caret_line, get_caret_column(i) - 2, caret_line, get_caret_column(i));
2470 if (_get_auto_brace_pair_close_at_pos(caret_line, get_caret_column(i) + 1) != pre_brace_pair) {
2471 set_caret_column(get_caret_column(i) + 1, i == 0, i);
2472 } else {
2473 set_caret_column(get_caret_column(i) + 2, i == 0, i);
2474 }
2475 }
2476 }
2477 }
2478
2479 end_multicaret_edit();
2480 end_complex_operation();
2481
2482 cancel_code_completion();
2483 if (code_completion_prefixes.has(caret_last_completion_char)) {
2484 request_code_completion();
2485 }
2486 }
2487
2488 void CodeEdit::cancel_code_completion() {
2489 if (!code_completion_active) {
2490 return;
2491 }
2492 code_completion_forced = false;
2493 code_completion_active = false;
2494 is_code_completion_drag_started = false;
2495 queue_redraw();
2496 }
2497
2498 /* Line length guidelines */
2499 void CodeEdit::set_line_length_guidelines(TypedArray<int> p_guideline_columns) {
2500 line_length_guideline_columns = p_guideline_columns;
2501 queue_redraw();
2502 }
2503
2504 TypedArray<int> CodeEdit::get_line_length_guidelines() const {
2505 return line_length_guideline_columns;
2506 }
2507
2508 /* Symbol lookup */
2509 void CodeEdit::set_symbol_lookup_on_click_enabled(bool p_enabled) {
2510 symbol_lookup_on_click_enabled = p_enabled;
2511 set_symbol_lookup_word_as_valid(false);
2512 }
2513
2514 bool CodeEdit::is_symbol_lookup_on_click_enabled() const {
2515 return symbol_lookup_on_click_enabled;
2516 }
2517
2518 String CodeEdit::get_text_for_symbol_lookup() const {
2519 Point2i mp = get_local_mouse_pos();
2520 Point2i pos = get_line_column_at_pos(mp, false, false);
2521 int line = pos.y;
2522 int col = pos.x;
2523
2524 if (line == -1) {
2525 return String();
2526 }
2527
2528 return get_text_with_cursor_char(line, col);
2529 }
2530
2531 String CodeEdit::get_text_with_cursor_char(int p_line, int p_column) const {
2532 const int text_size = get_line_count();
2533 StringBuilder result;
2534 for (int i = 0; i < text_size; i++) {
2535 String line_text = get_line(i);
2536 if (i == p_line && p_column >= 0 && p_column <= line_text.size()) {
2537 result += line_text.substr(0, p_column);
2538 /* Not unicode, represents the cursor. */
2539 result += String::chr(0xFFFF);
2540 result += line_text.substr(p_column);
2541 } else {
2542 result += line_text;
2543 }
2544
2545 if (i != text_size - 1) {
2546 result += "\n";
2547 }
2548 }
2549
2550 return result.as_string();
2551 }
2552
2553 String CodeEdit::get_lookup_word(int p_line, int p_column) const {
2554 if (p_line < 0 || p_column < 0) {
2555 return String();
2556 }
2557 if (is_in_string(p_line, p_column) != -1) {
2558 // Return the string in case it is a path.
2559 Point2 start_pos = get_delimiter_start_position(p_line, p_column);
2560 Point2 end_pos = get_delimiter_end_position(p_line, p_column);
2561 int start_line = start_pos.y;
2562 int start_column = start_pos.x;
2563 int end_line = end_pos.y;
2564 int end_column = end_pos.x;
2565 if (start_line == end_line && start_column >= 0 && end_column >= 0) {
2566 return get_line(start_line).substr(start_column, end_column - start_column - 1);
2567 }
2568 }
2569 return get_word(p_line, p_column);
2570 }
2571
2572 void CodeEdit::set_symbol_lookup_word_as_valid(bool p_valid) {
2573 symbol_lookup_word = p_valid ? symbol_lookup_new_word : "";
2574 symbol_lookup_new_word = "";
2575 if (lookup_symbol_word != symbol_lookup_word) {
2576 _set_symbol_lookup_word(symbol_lookup_word);
2577 }
2578 }
2579
2580 /* Symbol tooltip */
2581 void CodeEdit::set_symbol_tooltip_on_hover_enabled(bool p_enabled) {
2582 symbol_tooltip_on_hover_enabled = p_enabled;
2583 if (!p_enabled) {
2584 symbol_tooltip_timer->stop();
2585 }
2586 }
2587
2588 bool CodeEdit::is_symbol_tooltip_on_hover_enabled() const {
2589 return symbol_tooltip_on_hover_enabled;
2590 }
2591
2592 void CodeEdit::_on_symbol_tooltip_timer_timeout() {
2593 const int line = symbol_tooltip_pos.y;
2594 const int column = symbol_tooltip_pos.x;
2595 if (line >= 0 && column >= 0 && !symbol_tooltip_word.is_empty() && !Input::get_singleton()->is_anything_pressed()) {
2596 emit_signal(SNAME("symbol_hovered"), symbol_tooltip_word, line, column);
2597 }
2598 }
2599
2600 /* Text manipulation */
2601 void CodeEdit::move_lines_up() {
2602 begin_complex_operation();
2603 begin_multicaret_edit();
2604
2605 // Move lines up by swapping each line with the one above it.
2606 Vector<Point2i> line_ranges = get_line_ranges_from_carets();
2607 for (Point2i line_range : line_ranges) {
2608 if (line_range.x == 0) {
2609 continue;
2610 }
2611 unfold_line(line_range.x - 1);
2612 for (int line = line_range.x; line <= line_range.y; line++) {
2613 unfold_line(line);
2614 swap_lines(line - 1, line);
2615 }
2616 // Fix selection if the last one ends at column 0, since it wasn't moved.
2617 for (int i = 0; i < get_caret_count(); i++) {
2618 if (has_selection(i) && get_selection_to_column(i) == 0 && get_selection_to_line(i) == line_range.y + 1) {
2619 if (is_caret_after_selection_origin(i)) {
2620 set_caret_line(get_caret_line(i) - 1, false, true, -1, i);
2621 } else {
2622 set_selection_origin_line(get_selection_origin_line(i) - 1, true, -1, i);
2623 }
2624 break;
2625 }
2626 }
2627 }
2628 adjust_viewport_to_caret();
2629
2630 end_multicaret_edit();
2631 end_complex_operation();
2632 }
2633
2634 void CodeEdit::move_lines_down() {
2635 begin_complex_operation();
2636 begin_multicaret_edit();
2637
2638 // Move lines down by swapping each line with the one below it.
2639 Vector<Point2i> line_ranges = get_line_ranges_from_carets();
2640 // Reverse in case line ranges are adjacent, if the first ends at column 0.
2641 line_ranges.reverse();
2642 for (Point2i line_range : line_ranges) {
2643 if (line_range.y == get_line_count() - 1) {
2644 continue;
2645 }
2646 // Fix selection if the last one ends at column 0, since it won't be moved.
2647 bool selection_to_line_at_end = false;
2648 for (int i = 0; i < get_caret_count(); i++) {
2649 if (has_selection(i) && get_selection_to_column(i) == 0 && get_selection_to_line(i) == line_range.y + 1) {
2650 selection_to_line_at_end = get_selection_to_line(i) == get_line_count() - 1;
2651 if (selection_to_line_at_end) {
2652 break;
2653 }
2654 if (is_caret_after_selection_origin(i)) {
2655 set_caret_line(get_caret_line(i) + 1, false, true, -1, i);
2656 } else {
2657 set_selection_origin_line(get_selection_origin_line(i) + 1, true, -1, i);
2658 }
2659 break;
2660 }
2661 }
2662 if (selection_to_line_at_end) {
2663 continue;
2664 }
2665
2666 unfold_line(line_range.y + 1);
2667 for (int line = line_range.y; line >= line_range.x; line--) {
2668 unfold_line(line);
2669 swap_lines(line + 1, line);
2670 }
2671 }
2672 adjust_viewport_to_caret();
2673
2674 end_multicaret_edit();
2675 end_complex_operation();
2676 }
2677
2678 void CodeEdit::delete_lines() {
2679 begin_complex_operation();
2680 begin_multicaret_edit();
2681
2682 Vector<Point2i> line_ranges = get_line_ranges_from_carets();
2683 int line_offset = 0;
2684 for (Point2i line_range : line_ranges) {
2685 // Remove last line of range separately to preserve carets.
2686 unfold_line(line_range.y + line_offset);
2687 remove_line_at(line_range.y + line_offset);
2688 if (line_range.x != line_range.y) {
2689 remove_text(line_range.x + line_offset, 0, line_range.y + line_offset, 0);
2690 }
2691 line_offset += line_range.x - line_range.y - 1;
2692 }
2693
2694 // Deselect all.
2695 deselect();
2696
2697 end_multicaret_edit();
2698 end_complex_operation();
2699 }
2700
2701 void CodeEdit::duplicate_selection() {
2702 begin_complex_operation();
2703 begin_multicaret_edit();
2704
2705 // Duplicate lines from carets without selections first.
2706 for (int i = 0; i < get_caret_count(); i++) {
2707 if (multicaret_edit_ignore_caret(i)) {
2708 continue;
2709 }
2710 for (int l = get_selection_from_line(i); l <= get_selection_to_line(i); l++) {
2711 unfold_line(l);
2712 }
2713 if (has_selection(i)) {
2714 continue;
2715 }
2716
2717 String text_to_insert = get_line(get_caret_line(i)) + "\n";
2718 // Insert new text before the line, so the caret is on the second one.
2719 insert_text(text_to_insert, get_caret_line(i), 0);
2720 }
2721
2722 // Duplicate selections.
2723 for (int i = 0; i < get_caret_count(); i++) {
2724 if (multicaret_edit_ignore_caret(i)) {
2725 continue;
2726 }
2727 if (!has_selection(i)) {
2728 continue;
2729 }
2730
2731 // Insert new text before the selection, so the caret is on the second one.
2732 insert_text(get_selected_text(i), get_selection_from_line(i), get_selection_from_column(i));
2733 }
2734
2735 end_multicaret_edit();
2736 end_complex_operation();
2737 }
2738
2739 void CodeEdit::duplicate_lines() {
2740 begin_complex_operation();
2741 begin_multicaret_edit();
2742
2743 Vector<Point2i> line_ranges = get_line_ranges_from_carets(false, false);
2744 int line_offset = 0;
2745 for (Point2i line_range : line_ranges) {
2746 // The text that will be inserted. All lines in one string.
2747 String text_to_insert;
2748
2749 for (int i = line_range.x + line_offset; i <= line_range.y + line_offset; i++) {
2750 text_to_insert += get_line(i) + "\n";
2751 unfold_line(i);
2752 }
2753
2754 // Insert new text before the line.
2755 insert_text(text_to_insert, line_range.x + line_offset, 0);
2756 line_offset += line_range.y - line_range.x + 1;
2757 }
2758
2759 end_multicaret_edit();
2760 end_complex_operation();
2761 }
2762
2763 /* Visual */
2764 Color CodeEdit::_get_brace_mismatch_color() const {
2765 return theme_cache.brace_mismatch_color;
2766 }
2767
2768 Color CodeEdit::_get_code_folding_color() const {
2769 return theme_cache.code_folding_color;
2770 }
2771
2772 Ref<Texture2D> CodeEdit::_get_folded_eol_icon() const {
2773 return theme_cache.folded_eol_icon;
2774 }
2775
2776 void CodeEdit::_bind_methods() {
2777 /* Indent management */
2778 ClassDB::bind_method(D_METHOD("set_indent_size", "size"), &CodeEdit::set_indent_size);
2779 ClassDB::bind_method(D_METHOD("get_indent_size"), &CodeEdit::get_indent_size);
2780
2781 ClassDB::bind_method(D_METHOD("set_indent_using_spaces", "use_spaces"), &CodeEdit::set_indent_using_spaces);
2782 ClassDB::bind_method(D_METHOD("is_indent_using_spaces"), &CodeEdit::is_indent_using_spaces);
2783
2784 ClassDB::bind_method(D_METHOD("set_auto_indent_enabled", "enable"), &CodeEdit::set_auto_indent_enabled);
2785 ClassDB::bind_method(D_METHOD("is_auto_indent_enabled"), &CodeEdit::is_auto_indent_enabled);
2786
2787 ClassDB::bind_method(D_METHOD("set_auto_indent_prefixes", "prefixes"), &CodeEdit::set_auto_indent_prefixes);
2788 ClassDB::bind_method(D_METHOD("get_auto_indent_prefixes"), &CodeEdit::get_auto_indent_prefixes);
2789
2790 ClassDB::bind_method(D_METHOD("do_indent"), &CodeEdit::do_indent);
2791
2792 ClassDB::bind_method(D_METHOD("indent_lines"), &CodeEdit::indent_lines);
2793 ClassDB::bind_method(D_METHOD("unindent_lines"), &CodeEdit::unindent_lines);
2794
2795 ClassDB::bind_method(D_METHOD("convert_indent", "from_line", "to_line"), &CodeEdit::convert_indent, DEFVAL(-1), DEFVAL(-1));
2796
2797 /* Auto brace completion */
2798 ClassDB::bind_method(D_METHOD("set_auto_brace_completion_enabled", "enable"), &CodeEdit::set_auto_brace_completion_enabled);
2799 ClassDB::bind_method(D_METHOD("is_auto_brace_completion_enabled"), &CodeEdit::is_auto_brace_completion_enabled);
2800
2801 ClassDB::bind_method(D_METHOD("set_highlight_matching_braces_enabled", "enable"), &CodeEdit::set_highlight_matching_braces_enabled);
2802 ClassDB::bind_method(D_METHOD("is_highlight_matching_braces_enabled"), &CodeEdit::is_highlight_matching_braces_enabled);
2803
2804 ClassDB::bind_method(D_METHOD("add_auto_brace_completion_pair", "start_key", "end_key"), &CodeEdit::add_auto_brace_completion_pair);
2805 ClassDB::bind_method(D_METHOD("set_auto_brace_completion_pairs", "pairs"), &CodeEdit::set_auto_brace_completion_pairs);
2806 ClassDB::bind_method(D_METHOD("get_auto_brace_completion_pairs"), &CodeEdit::get_auto_brace_completion_pairs);
2807
2808 ClassDB::bind_method(D_METHOD("has_auto_brace_completion_open_key", "open_key"), &CodeEdit::has_auto_brace_completion_open_key);
2809 ClassDB::bind_method(D_METHOD("has_auto_brace_completion_close_key", "close_key"), &CodeEdit::has_auto_brace_completion_close_key);
2810
2811 ClassDB::bind_method(D_METHOD("get_auto_brace_completion_close_key", "open_key"), &CodeEdit::get_auto_brace_completion_close_key);
2812
2813 /* Main Gutter */
2814 ClassDB::bind_method(D_METHOD("set_draw_breakpoints_gutter", "enable"), &CodeEdit::set_draw_breakpoints_gutter);
2815 ClassDB::bind_method(D_METHOD("is_drawing_breakpoints_gutter"), &CodeEdit::is_drawing_breakpoints_gutter);
2816
2817 ClassDB::bind_method(D_METHOD("set_draw_bookmarks_gutter", "enable"), &CodeEdit::set_draw_bookmarks_gutter);
2818 ClassDB::bind_method(D_METHOD("is_drawing_bookmarks_gutter"), &CodeEdit::is_drawing_bookmarks_gutter);
2819
2820 ClassDB::bind_method(D_METHOD("set_draw_executing_lines_gutter", "enable"), &CodeEdit::set_draw_executing_lines_gutter);
2821 ClassDB::bind_method(D_METHOD("is_drawing_executing_lines_gutter"), &CodeEdit::is_drawing_executing_lines_gutter);
2822
2823 // Breakpoints
2824 ClassDB::bind_method(D_METHOD("set_line_as_breakpoint", "line", "breakpointed"), &CodeEdit::set_line_as_breakpoint);
2825 ClassDB::bind_method(D_METHOD("is_line_breakpointed", "line"), &CodeEdit::is_line_breakpointed);
2826 ClassDB::bind_method(D_METHOD("clear_breakpointed_lines"), &CodeEdit::clear_breakpointed_lines);
2827 ClassDB::bind_method(D_METHOD("get_breakpointed_lines"), &CodeEdit::get_breakpointed_lines);
2828
2829 // Bookmarks
2830 ClassDB::bind_method(D_METHOD("set_line_as_bookmarked", "line", "bookmarked"), &CodeEdit::set_line_as_bookmarked);
2831 ClassDB::bind_method(D_METHOD("is_line_bookmarked", "line"), &CodeEdit::is_line_bookmarked);
2832 ClassDB::bind_method(D_METHOD("clear_bookmarked_lines"), &CodeEdit::clear_bookmarked_lines);
2833 ClassDB::bind_method(D_METHOD("get_bookmarked_lines"), &CodeEdit::get_bookmarked_lines);
2834
2835 // Executing lines
2836 ClassDB::bind_method(D_METHOD("set_line_as_executing", "line", "executing"), &CodeEdit::set_line_as_executing);
2837 ClassDB::bind_method(D_METHOD("is_line_executing", "line"), &CodeEdit::is_line_executing);
2838 ClassDB::bind_method(D_METHOD("clear_executing_lines"), &CodeEdit::clear_executing_lines);
2839 ClassDB::bind_method(D_METHOD("get_executing_lines"), &CodeEdit::get_executing_lines);
2840
2841 /* Line numbers */
2842 ClassDB::bind_method(D_METHOD("set_draw_line_numbers", "enable"), &CodeEdit::set_draw_line_numbers);
2843 ClassDB::bind_method(D_METHOD("is_draw_line_numbers_enabled"), &CodeEdit::is_draw_line_numbers_enabled);
2844 ClassDB::bind_method(D_METHOD("set_line_numbers_zero_padded", "enable"), &CodeEdit::set_line_numbers_zero_padded);
2845 ClassDB::bind_method(D_METHOD("is_line_numbers_zero_padded"), &CodeEdit::is_line_numbers_zero_padded);
2846 ClassDB::bind_method(D_METHOD("set_line_numbers_min_digits", "count"), &CodeEdit::set_line_numbers_min_digits);
2847 ClassDB::bind_method(D_METHOD("get_line_numbers_min_digits"), &CodeEdit::get_line_numbers_min_digits);
2848
2849 /* Fold Gutter */
2850 ClassDB::bind_method(D_METHOD("set_draw_fold_gutter", "enable"), &CodeEdit::set_draw_fold_gutter);
2851 ClassDB::bind_method(D_METHOD("is_drawing_fold_gutter"), &CodeEdit::is_drawing_fold_gutter);
2852
2853 /* Line folding */
2854 ClassDB::bind_method(D_METHOD("set_line_folding_enabled", "enabled"), &CodeEdit::set_line_folding_enabled);
2855 ClassDB::bind_method(D_METHOD("is_line_folding_enabled"), &CodeEdit::is_line_folding_enabled);
2856
2857 ClassDB::bind_method(D_METHOD("can_fold_line", "line"), &CodeEdit::can_fold_line);
2858
2859 ClassDB::bind_method(D_METHOD("fold_line", "line"), &CodeEdit::fold_line);
2860 ClassDB::bind_method(D_METHOD("unfold_line", "line"), &CodeEdit::unfold_line);
2861 ClassDB::bind_method(D_METHOD("fold_all_lines"), &CodeEdit::fold_all_lines);
2862 ClassDB::bind_method(D_METHOD("unfold_all_lines"), &CodeEdit::unfold_all_lines);
2863 ClassDB::bind_method(D_METHOD("toggle_foldable_line", "line"), &CodeEdit::toggle_foldable_line);
2864 ClassDB::bind_method(D_METHOD("toggle_foldable_lines_at_carets"), &CodeEdit::toggle_foldable_lines_at_carets);
2865
2866 ClassDB::bind_method(D_METHOD("is_line_folded", "line"), &CodeEdit::is_line_folded);
2867 ClassDB::bind_method(D_METHOD("get_folded_lines"), &CodeEdit::get_folded_lines_bind);
2868
2869 /* Code region */
2870 ClassDB::bind_method(D_METHOD("create_code_region"), &CodeEdit::create_code_region);
2871 ClassDB::bind_method(D_METHOD("get_code_region_start_tag"), &CodeEdit::get_code_region_start_tag);
2872 ClassDB::bind_method(D_METHOD("get_code_region_end_tag"), &CodeEdit::get_code_region_end_tag);
2873 ClassDB::bind_method(D_METHOD("set_code_region_tags", "start", "end"), &CodeEdit::set_code_region_tags, DEFVAL("region"), DEFVAL("endregion"));
2874 ClassDB::bind_method(D_METHOD("is_line_code_region_start", "line"), &CodeEdit::is_line_code_region_start);
2875 ClassDB::bind_method(D_METHOD("is_line_code_region_end", "line"), &CodeEdit::is_line_code_region_end);
2876
2877 /* Delimiters */
2878 // Strings
2879 ClassDB::bind_method(D_METHOD("add_string_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_string_delimiter, DEFVAL(false));
2880 ClassDB::bind_method(D_METHOD("remove_string_delimiter", "start_key"), &CodeEdit::remove_string_delimiter);
2881 ClassDB::bind_method(D_METHOD("has_string_delimiter", "start_key"), &CodeEdit::has_string_delimiter);
2882
2883 ClassDB::bind_method(D_METHOD("set_string_delimiters", "string_delimiters"), &CodeEdit::set_string_delimiters);
2884 ClassDB::bind_method(D_METHOD("clear_string_delimiters"), &CodeEdit::clear_string_delimiters);
2885 ClassDB::bind_method(D_METHOD("get_string_delimiters"), &CodeEdit::get_string_delimiters);
2886
2887 ClassDB::bind_method(D_METHOD("is_in_string", "line", "column"), &CodeEdit::is_in_string, DEFVAL(-1));
2888
2889 // Comments
2890 ClassDB::bind_method(D_METHOD("add_comment_delimiter", "start_key", "end_key", "line_only"), &CodeEdit::add_comment_delimiter, DEFVAL(false));
2891 ClassDB::bind_method(D_METHOD("remove_comment_delimiter", "start_key"), &CodeEdit::remove_comment_delimiter);
2892 ClassDB::bind_method(D_METHOD("has_comment_delimiter", "start_key"), &CodeEdit::has_comment_delimiter);
2893
2894 ClassDB::bind_method(D_METHOD("set_comment_delimiters", "comment_delimiters"), &CodeEdit::set_comment_delimiters);
2895 ClassDB::bind_method(D_METHOD("clear_comment_delimiters"), &CodeEdit::clear_comment_delimiters);
2896 ClassDB::bind_method(D_METHOD("get_comment_delimiters"), &CodeEdit::get_comment_delimiters);
2897
2898 ClassDB::bind_method(D_METHOD("is_in_comment", "line", "column"), &CodeEdit::is_in_comment, DEFVAL(-1));
2899
2900 // Util
2901 ClassDB::bind_method(D_METHOD("get_delimiter_start_key", "delimiter_index"), &CodeEdit::get_delimiter_start_key);
2902 ClassDB::bind_method(D_METHOD("get_delimiter_end_key", "delimiter_index"), &CodeEdit::get_delimiter_end_key);
2903
2904 ClassDB::bind_method(D_METHOD("get_delimiter_start_position", "line", "column"), &CodeEdit::get_delimiter_start_position);
2905 ClassDB::bind_method(D_METHOD("get_delimiter_end_position", "line", "column"), &CodeEdit::get_delimiter_end_position);
2906
2907 /* Code hint */
2908 ClassDB::bind_method(D_METHOD("set_code_hint", "code_hint"), &CodeEdit::set_code_hint);
2909 ClassDB::bind_method(D_METHOD("set_code_hint_draw_below", "draw_below"), &CodeEdit::set_code_hint_draw_below);
2910
2911 /* Code Completion */
2912 BIND_ENUM_CONSTANT(KIND_CLASS);
2913 BIND_ENUM_CONSTANT(KIND_FUNCTION);
2914 BIND_ENUM_CONSTANT(KIND_SIGNAL);
2915 BIND_ENUM_CONSTANT(KIND_VARIABLE);
2916 BIND_ENUM_CONSTANT(KIND_MEMBER);
2917 BIND_ENUM_CONSTANT(KIND_ENUM);
2918 BIND_ENUM_CONSTANT(KIND_CONSTANT);
2919 BIND_ENUM_CONSTANT(KIND_NODE_PATH);
2920 BIND_ENUM_CONSTANT(KIND_FILE_PATH);
2921 BIND_ENUM_CONSTANT(KIND_PLAIN_TEXT);
2922
2923 BIND_ENUM_CONSTANT(LOCATION_LOCAL);
2924 BIND_ENUM_CONSTANT(LOCATION_PARENT_MASK);
2925 BIND_ENUM_CONSTANT(LOCATION_OTHER_USER_CODE)
2926 BIND_ENUM_CONSTANT(LOCATION_OTHER);
2927
2928 ClassDB::bind_method(D_METHOD("get_text_for_code_completion"), &CodeEdit::get_text_for_code_completion);
2929 ClassDB::bind_method(D_METHOD("request_code_completion", "force"), &CodeEdit::request_code_completion, DEFVAL(false));
2930 ClassDB::bind_method(D_METHOD("add_code_completion_option", "type", "display_text", "insert_text", "text_color", "icon", "value", "location"), &CodeEdit::add_code_completion_option, DEFVAL(Color(1, 1, 1)), DEFVAL(Ref<Resource>()), DEFVAL(Variant()), DEFVAL(LOCATION_OTHER));
2931 ClassDB::bind_method(D_METHOD("update_code_completion_options", "force"), &CodeEdit::update_code_completion_options);
2932 ClassDB::bind_method(D_METHOD("get_code_completion_options"), &CodeEdit::get_code_completion_options);
2933 ClassDB::bind_method(D_METHOD("get_code_completion_option", "index"), &CodeEdit::get_code_completion_option);
2934 ClassDB::bind_method(D_METHOD("get_code_completion_selected_index"), &CodeEdit::get_code_completion_selected_index);
2935 ClassDB::bind_method(D_METHOD("set_code_completion_selected_index", "index"), &CodeEdit::set_code_completion_selected_index);
2936
2937 ClassDB::bind_method(D_METHOD("confirm_code_completion", "replace"), &CodeEdit::confirm_code_completion, DEFVAL(false));
2938 ClassDB::bind_method(D_METHOD("cancel_code_completion"), &CodeEdit::cancel_code_completion);
2939
2940 ClassDB::bind_method(D_METHOD("set_code_completion_enabled", "enable"), &CodeEdit::set_code_completion_enabled);
2941 ClassDB::bind_method(D_METHOD("is_code_completion_enabled"), &CodeEdit::is_code_completion_enabled);
2942
2943 ClassDB::bind_method(D_METHOD("set_code_completion_prefixes", "prefixes"), &CodeEdit::set_code_completion_prefixes);
2944 ClassDB::bind_method(D_METHOD("get_code_completion_prefixes"), &CodeEdit::get_code_completion_prefixes);
2945
2946 // Overridable
2947
2948 GDVIRTUAL_BIND(_confirm_code_completion, "replace")
2949 GDVIRTUAL_BIND(_request_code_completion, "force")
2950 GDVIRTUAL_BIND(_filter_code_completion_candidates, "candidates")
2951
2952 /* Line length guidelines */
2953 ClassDB::bind_method(D_METHOD("set_line_length_guidelines", "guideline_columns"), &CodeEdit::set_line_length_guidelines);
2954 ClassDB::bind_method(D_METHOD("get_line_length_guidelines"), &CodeEdit::get_line_length_guidelines);
2955
2956 /* Symbol lookup */
2957 ClassDB::bind_method(D_METHOD("set_symbol_lookup_on_click_enabled", "enable"), &CodeEdit::set_symbol_lookup_on_click_enabled);
2958 ClassDB::bind_method(D_METHOD("is_symbol_lookup_on_click_enabled"), &CodeEdit::is_symbol_lookup_on_click_enabled);
2959
2960 ClassDB::bind_method(D_METHOD("get_text_for_symbol_lookup"), &CodeEdit::get_text_for_symbol_lookup);
2961 ClassDB::bind_method(D_METHOD("get_text_with_cursor_char", "line", "column"), &CodeEdit::get_text_with_cursor_char);
2962
2963 ClassDB::bind_method(D_METHOD("set_symbol_lookup_word_as_valid", "valid"), &CodeEdit::set_symbol_lookup_word_as_valid);
2964
2965 /* Symbol tooltip */
2966 ClassDB::bind_method(D_METHOD("set_symbol_tooltip_on_hover_enabled", "enable"), &CodeEdit::set_symbol_tooltip_on_hover_enabled);
2967 ClassDB::bind_method(D_METHOD("is_symbol_tooltip_on_hover_enabled"), &CodeEdit::is_symbol_tooltip_on_hover_enabled);
2968
2969 /* Text manipulation */
2970 ClassDB::bind_method(D_METHOD("move_lines_up"), &CodeEdit::move_lines_up);
2971 ClassDB::bind_method(D_METHOD("move_lines_down"), &CodeEdit::move_lines_down);
2972 ClassDB::bind_method(D_METHOD("delete_lines"), &CodeEdit::delete_lines);
2973 ClassDB::bind_method(D_METHOD("duplicate_selection"), &CodeEdit::duplicate_selection);
2974 ClassDB::bind_method(D_METHOD("duplicate_lines"), &CodeEdit::duplicate_lines);
2975
2976 /* Inspector */
2977 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "symbol_lookup_on_click"), "set_symbol_lookup_on_click_enabled", "is_symbol_lookup_on_click_enabled");
2978 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "symbol_tooltip_on_hover"), "set_symbol_tooltip_on_hover_enabled", "is_symbol_tooltip_on_hover_enabled");
2979 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "line_folding"), "set_line_folding_enabled", "is_line_folding_enabled");
2980
2981 ADD_PROPERTY(PropertyInfo(Variant::PACKED_INT32_ARRAY, "line_length_guidelines"), "set_line_length_guidelines", "get_line_length_guidelines");
2982
2983 ADD_GROUP("Gutters", "gutters_");
2984 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_breakpoints_gutter"), "set_draw_breakpoints_gutter", "is_drawing_breakpoints_gutter");
2985
2986 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_bookmarks"), "set_draw_bookmarks_gutter", "is_drawing_bookmarks_gutter");
2987
2988 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_executing_lines"), "set_draw_executing_lines_gutter", "is_drawing_executing_lines_gutter");
2989
2990 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_line_numbers"), "set_draw_line_numbers", "is_draw_line_numbers_enabled");
2991 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_zero_pad_line_numbers"), "set_line_numbers_zero_padded", "is_line_numbers_zero_padded");
2992 ADD_PROPERTY(PropertyInfo(Variant::INT, "gutters_line_numbers_min_digits", PROPERTY_HINT_RANGE, "1,5,1"), "set_line_numbers_min_digits", "get_line_numbers_min_digits");
2993
2994 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "gutters_draw_fold_gutter"), "set_draw_fold_gutter", "is_drawing_fold_gutter");
2995
2996 ADD_GROUP("Delimiters", "delimiter_");
2997 ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_strings"), "set_string_delimiters", "get_string_delimiters");
2998 ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "delimiter_comments"), "set_comment_delimiters", "get_comment_delimiters");
2999
3000 ADD_GROUP("Code Completion", "code_completion_");
3001 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "code_completion_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_code_completion_enabled", "is_code_completion_enabled");
3002 ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "code_completion_prefixes"), "set_code_completion_prefixes", "get_code_completion_prefixes");
3003
3004 ADD_GROUP("Indentation", "indent_");
3005 ADD_PROPERTY(PropertyInfo(Variant::INT, "indent_size"), "set_indent_size", "get_indent_size");
3006 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_use_spaces"), "set_indent_using_spaces", "is_indent_using_spaces");
3007 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "indent_automatic"), "set_auto_indent_enabled", "is_auto_indent_enabled");
3008 ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "indent_automatic_prefixes"), "set_auto_indent_prefixes", "get_auto_indent_prefixes");
3009
3010 ADD_GROUP("Auto Brace Completion", "auto_brace_completion_");
3011 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_enabled", PROPERTY_HINT_GROUP_ENABLE), "set_auto_brace_completion_enabled", "is_auto_brace_completion_enabled");
3012 ADD_PROPERTY(PropertyInfo(Variant::BOOL, "auto_brace_completion_highlight_matching"), "set_highlight_matching_braces_enabled", "is_highlight_matching_braces_enabled");
3013 ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "auto_brace_completion_pairs", PROPERTY_HINT_TYPE_STRING, "String;String"), "set_auto_brace_completion_pairs", "get_auto_brace_completion_pairs");
3014
3015 /* Signals */
3016 /* Gutters */
3017 ADD_SIGNAL(MethodInfo("breakpoint_toggled", PropertyInfo(Variant::INT, "line")));
3018
3019 /* Code Completion */
3020 ADD_SIGNAL(MethodInfo("code_completion_requested"));
3021
3022 /* Symbol lookup */
3023 ADD_SIGNAL(MethodInfo("symbol_lookup", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "column")));
3024 ADD_SIGNAL(MethodInfo("symbol_validate", PropertyInfo(Variant::STRING, "symbol")));
3025
3026 /* Symbol tooltip */
3027 ADD_SIGNAL(MethodInfo("symbol_hovered", PropertyInfo(Variant::STRING, "symbol"), PropertyInfo(Variant::INT, "line"), PropertyInfo(Variant::INT, "column")));
3028
3029 /* Theme items */
3030 /* Gutters */
3031 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, code_folding_color);
3032 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, folded_code_region_color);
3033 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, can_fold_icon, "can_fold");
3034 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, folded_icon, "folded");
3035 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, can_fold_code_region_icon, "can_fold_code_region");
3036 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, folded_code_region_icon, "folded_code_region");
3037 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CodeEdit, folded_eol_icon);
3038 BIND_THEME_ITEM(Theme::DATA_TYPE_ICON, CodeEdit, completion_color_bg);
3039
3040 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, breakpoint_color);
3041 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, breakpoint_icon, "breakpoint");
3042
3043 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, bookmark_color);
3044 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, bookmark_icon, "bookmark");
3045
3046 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, executing_line_color);
3047 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_ICON, CodeEdit, executing_line_icon, "executing_line");
3048
3049 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, line_number_color);
3050
3051 /* Code Completion */
3052 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, code_completion_style, "completion");
3053 BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_icon_separation, "h_separation", "ItemList");
3054
3055 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_max_width, "completion_max_width");
3056 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_max_lines, "completion_lines");
3057 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_CONSTANT, CodeEdit, code_completion_scroll_width, "completion_scroll_width");
3058 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_scroll_color, "completion_scroll_color");
3059 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_scroll_hovered_color, "completion_scroll_hovered_color");
3060 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_background_color, "completion_background_color");
3061 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_selected_color, "completion_selected_color");
3062 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_COLOR, CodeEdit, code_completion_existing_color, "completion_existing_color");
3063
3064 /* Code hint */
3065 BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_STYLEBOX, CodeEdit, code_hint_style, "panel", "TooltipPanel");
3066 BIND_THEME_ITEM_EXT(Theme::DATA_TYPE_COLOR, CodeEdit, code_hint_color, "font_color", "TooltipLabel");
3067
3068 /* Line length guideline */
3069 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, line_length_guideline_color);
3070
3071 /* Other visuals */
3072 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, style_normal, "normal");
3073 BIND_THEME_ITEM_CUSTOM(Theme::DATA_TYPE_STYLEBOX, CodeEdit, style_readonly, "read_only");
3074
3075 BIND_THEME_ITEM(Theme::DATA_TYPE_COLOR, CodeEdit, brace_mismatch_color);
3076
3077 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT, CodeEdit, font);
3078 BIND_THEME_ITEM(Theme::DATA_TYPE_FONT_SIZE, CodeEdit, font_size);
3079
3080 BIND_THEME_ITEM(Theme::DATA_TYPE_CONSTANT, CodeEdit, line_spacing);
3081 }
3082
3083 /* Auto brace completion */
3084 int CodeEdit::_get_auto_brace_pair_open_at_pos(int p_line, int p_col) {
3085 const String &line = get_line(p_line);
3086 int caret_col = MIN(p_col, line.length());
3087
3088 /* Should be fast enough, expecting low amount of pairs... */
3089 for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
3090 const String &open_key = auto_brace_completion_pairs[i].open_key;
3091 if (caret_col < open_key.length()) {
3092 continue;
3093 }
3094
3095 bool is_match = true;
3096 for (int j = 0; j < open_key.length(); j++) {
3097 if (line[(caret_col - 1) - j] != open_key[(open_key.length() - 1) - j]) {
3098 is_match = false;
3099 break;
3100 }
3101 }
3102
3103 if (is_match) {
3104 return i;
3105 }
3106 }
3107 return -1;
3108 }
3109
3110 int CodeEdit::_get_auto_brace_pair_close_at_pos(int p_line, int p_col) {
3111 const String &line = get_line(p_line);
3112
3113 /* Should be fast enough, expecting low amount of pairs... */
3114 for (int i = 0; i < auto_brace_completion_pairs.size(); i++) {
3115 if (p_col + auto_brace_completion_pairs[i].close_key.length() > line.length()) {
3116 continue;
3117 }
3118
3119 bool is_match = true;
3120 for (int j = 0; j < auto_brace_completion_pairs[i].close_key.length(); j++) {
3121 if (line[p_col + j] != auto_brace_completion_pairs[i].close_key[j]) {
3122 is_match = false;
3123 break;
3124 }
3125 }
3126
3127 if (is_match) {
3128 return i;
3129 }
3130 }
3131 return -1;
3132 }
3133
3134 /* Gutters */
3135 void CodeEdit::_gutter_clicked(int p_line, int p_gutter) {
3136 bool shift_pressed = Input::get_singleton()->is_key_pressed(Key::SHIFT);
3137
3138 if (p_gutter == main_gutter) {
3139 if (draw_breakpoints && !shift_pressed) {
3140 set_line_as_breakpoint(p_line, !is_line_breakpointed(p_line));
3141 } else if (draw_bookmarks && shift_pressed) {
3142 set_line_as_bookmarked(p_line, !is_line_bookmarked(p_line));
3143 }
3144 return;
3145 }
3146
3147 if (p_gutter == line_number_gutter) {
3148 remove_secondary_carets();
3149 set_selection_mode(TextEdit::SelectionMode::SELECTION_MODE_LINE);
3150 if (p_line == get_line_count() - 1) {
3151 select(p_line, 0, p_line, INT_MAX);
3152 } else {
3153 select(p_line, 0, p_line + 1, 0);
3154 }
3155 return;
3156 }
3157
3158 if (p_gutter == fold_gutter) {
3159 if (is_line_folded(p_line)) {
3160 unfold_line(p_line);
3161 } else if (can_fold_line(p_line)) {
3162 fold_line(p_line);
3163 }
3164 return;
3165 }
3166 }
3167
3168 void CodeEdit::_update_gutter_indexes() {
3169 for (int i = 0; i < get_gutter_count(); i++) {
3170 if (get_gutter_name(i) == "main_gutter") {
3171 main_gutter = i;
3172 continue;
3173 }
3174
3175 if (get_gutter_name(i) == "line_numbers") {
3176 line_number_gutter = i;
3177 continue;
3178 }
3179
3180 if (get_gutter_name(i) == "fold_gutter") {
3181 fold_gutter = i;
3182 continue;
3183 }
3184 }
3185 }
3186
3187 /* Code Region */
3188 void CodeEdit::_update_code_region_tags() {
3189 code_region_start_string = "";
3190 code_region_end_string = "";
3191
3192 if (code_region_start_tag.is_empty() || code_region_end_tag.is_empty()) {
3193 return;
3194 }
3195
3196 // A shorter delimiter has higher priority.
3197 for (int i = delimiters.size() - 1; i >= 0; i--) {
3198 if (delimiters[i].type != DelimiterType::TYPE_COMMENT) {
3199 continue;
3200 }
3201 if (delimiters[i].end_key.is_empty() && delimiters[i].line_only == true) {
3202 code_region_start_string = delimiters[i].start_key + code_region_start_tag;
3203 code_region_end_string = delimiters[i].start_key + code_region_end_tag;
3204 return;
3205 }
3206 }
3207 }
3208
3209 /* Delimiters */
3210 void CodeEdit::_update_delimiter_cache(int p_from_line, int p_to_line) {
3211 if (delimiters.is_empty()) {
3212 return;
3213 }
3214
3215 int line_count = get_line_count();
3216 if (p_to_line == -1) {
3217 p_to_line = line_count;
3218 }
3219
3220 int start_line = MIN(p_from_line, p_to_line);
3221 int end_line = MAX(p_from_line, p_to_line);
3222
3223 /* Make sure delimiter_cache has all the lines. */
3224 if (start_line != end_line) {
3225 if (p_to_line < p_from_line) {
3226 for (int i = end_line; i > start_line; i--) {
3227 delimiter_cache.remove_at(i);
3228 }
3229 } else {
3230 for (int i = start_line; i < end_line; i++) {
3231 delimiter_cache.insert(i, RBMap<int, int>());
3232 }
3233 }
3234 }
3235
3236 int in_region = -1;
3237 for (int i = start_line; i < MIN(end_line + 1, line_count); i++) {
3238 int current_end_region = (i < 0 || delimiter_cache[i].size() < 1) ? -1 : delimiter_cache[i].back()->value();
3239 in_region = (i <= 0 || delimiter_cache[i - 1].size() < 1) ? -1 : delimiter_cache[i - 1].back()->value();
3240
3241 const String &str = get_line(i);
3242 const int line_length = str.length();
3243 delimiter_cache.write[i].clear();
3244
3245 if (str.length() == 0) {
3246 if (in_region != -1) {
3247 delimiter_cache.write[i][0] = in_region;
3248 }
3249 if (i == end_line && current_end_region != in_region) {
3250 end_line++;
3251 end_line = MIN(end_line, line_count);
3252 }
3253 continue;
3254 }
3255
3256 int end_region = -1;
3257 for (int j = 0; j < line_length; j++) {
3258 int from = j;
3259 for (; from < line_length; from++) {
3260 if (str[from] == '\\') {
3261 from++;
3262 continue;
3263 }
3264 break;
3265 }
3266
3267 /* check if we are in entering a region */
3268 bool same_line = false;
3269 if (in_region == -1) {
3270 for (int d = 0; d < delimiters.size(); d++) {
3271 /* check there is enough room */
3272 int chars_left = line_length - from;
3273 int start_key_length = delimiters[d].start_key.length();
3274 int end_key_length = delimiters[d].end_key.length();
3275 if (chars_left < start_key_length) {
3276 continue;
3277 }
3278
3279 /* search the line */
3280 bool match = true;
3281 const char32_t *start_key = delimiters[d].start_key.get_data();
3282 for (int k = 0; k < start_key_length; k++) {
3283 if (start_key[k] != str[from + k]) {
3284 match = false;
3285 break;
3286 }
3287 }
3288 if (!match) {
3289 continue;
3290 }
3291 same_line = true;
3292 in_region = d;
3293 delimiter_cache.write[i][from + 1] = d;
3294 from += start_key_length;
3295
3296 /* check if it's the whole line */
3297 if (end_key_length == 0 || delimiters[d].line_only || from + end_key_length > line_length) {
3298 j = line_length;
3299 if (delimiters[d].line_only) {
3300 delimiter_cache.write[i][line_length + 1] = -1;
3301 } else {
3302 end_region = in_region;
3303 }
3304 }
3305 break;
3306 }
3307
3308 if (j == line_length || in_region == -1) {
3309 continue;
3310 }
3311 }
3312
3313 /* if we are in one find the end key */
3314 /* search the line */
3315 int region_end_index = -1;
3316 int end_key_length = delimiters[in_region].end_key.length();
3317 const char32_t *end_key = delimiters[in_region].end_key.get_data();
3318 for (; from < line_length; from++) {
3319 if (line_length - from < end_key_length) {
3320 break;
3321 }
3322
3323 if (!is_symbol(str[from])) {
3324 continue;
3325 }
3326
3327 if (str[from] == '\\') {
3328 from++;
3329 continue;
3330 }
3331
3332 region_end_index = from;
3333 for (int k = 0; k < end_key_length; k++) {
3334 if (end_key[k] != str[from + k]) {
3335 region_end_index = -1;
3336 break;
3337 }
3338 }
3339
3340 if (region_end_index != -1) {
3341 break;
3342 }
3343 }
3344
3345 j = from + (end_key_length - 1);
3346 end_region = (region_end_index == -1) ? in_region : -1;
3347 if (!same_line || region_end_index != -1) {
3348 delimiter_cache.write[i][j + 1] = end_region;
3349 }
3350 in_region = -1;
3351 }
3352
3353 if (i == end_line && current_end_region != end_region) {
3354 end_line++;
3355 end_line = MIN(end_line, line_count);
3356 }
3357 }
3358 }
3359
3360 int CodeEdit::_is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const {
3361 if (delimiters.is_empty() || p_line >= delimiter_cache.size()) {
3362 return -1;
3363 }
3364 ERR_FAIL_INDEX_V(p_line, get_line_count(), 0);
3365
3366 int region = (p_line <= 0 || delimiter_cache[p_line - 1].size() < 1) ? -1 : delimiter_cache[p_line - 1].back()->value();
3367 bool in_region = region != -1 && delimiters[region].type == p_type;
3368 for (RBMap<int, int>::Element *E = delimiter_cache[p_line].front(); E; E = E->next()) {
3369 /* If column is specified, loop until the key is larger then the column. */
3370 if (p_column != -1) {
3371 if (E->key() > p_column) {
3372 break;
3373 }
3374 in_region = E->value() != -1 && delimiters[E->value()].type == p_type;
3375 region = in_region ? E->value() : -1;
3376 continue;
3377 }
3378
3379 /* If no column, calculate if the entire line is a region */
3380 /* excluding whitespace. */
3381 const String line = get_line(p_line);
3382 if (!in_region) {
3383 if (E->value() == -1 || delimiters[E->value()].type != p_type) {
3384 break;
3385 }
3386
3387 region = E->value();
3388 in_region = true;
3389 for (int i = E->key() - 2; i >= 0; i--) {
3390 if (!is_whitespace(line[i])) {
3391 return -1;
3392 }
3393 }
3394 }
3395
3396 if (delimiters[region].line_only) {
3397 return region;
3398 }
3399
3400 int end_col = E->key();
3401 if (E->value() != -1) {
3402 if (!E->next()) {
3403 return region;
3404 }
3405 end_col = E->next()->key();
3406 }
3407
3408 for (int i = end_col; i < line.length(); i++) {
3409 if (!is_whitespace(line[i])) {
3410 return -1;
3411 }
3412 }
3413 return region;
3414 }
3415 return in_region ? region : -1;
3416 }
3417
3418 void CodeEdit::_add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type) {
3419 // If we are the editor allow "null" as a valid start key, otherwise users cannot add delimiters via the inspector.
3420 if (!(Engine::get_singleton()->is_editor_hint() && p_start_key == "null")) {
3421 ERR_FAIL_COND_MSG(p_start_key.is_empty(), "delimiter start key cannot be empty");
3422
3423 for (int i = 0; i < p_start_key.length(); i++) {
3424 ERR_FAIL_COND_MSG(!is_symbol(p_start_key[i]), "delimiter must start with a symbol");
3425 }
3426 }
3427
3428 if (p_end_key.length() > 0) {
3429 for (int i = 0; i < p_end_key.length(); i++) {
3430 ERR_FAIL_COND_MSG(!is_symbol(p_end_key[i]), "delimiter must end with a symbol");
3431 }
3432 }
3433
3434 int at = 0;
3435 for (int i = 0; i < delimiters.size(); i++) {
3436 ERR_FAIL_COND_MSG(delimiters[i].start_key == p_start_key, "delimiter with start key '" + p_start_key + "' already exists.");
3437 if (p_start_key.length() < delimiters[i].start_key.length()) {
3438 at++;
3439 } else {
3440 break;
3441 }
3442 }
3443
3444 Delimiter delimiter;
3445 delimiter.type = p_type;
3446 delimiter.start_key = p_start_key;
3447 delimiter.end_key = p_end_key;
3448 delimiter.line_only = p_line_only || p_end_key.is_empty();
3449 delimiters.insert(at, delimiter);
3450 if (!setting_delimiters) {
3451 delimiter_cache.clear();
3452 _update_delimiter_cache();
3453 }
3454 if (p_type == DelimiterType::TYPE_COMMENT) {
3455 _update_code_region_tags();
3456 }
3457 }
3458
3459 void CodeEdit::_remove_delimiter(const String &p_start_key, DelimiterType p_type) {
3460 for (int i = 0; i < delimiters.size(); i++) {
3461 if (delimiters[i].start_key != p_start_key) {
3462 continue;
3463 }
3464
3465 if (delimiters[i].type != p_type) {
3466 break;
3467 }
3468
3469 delimiters.remove_at(i);
3470 if (!setting_delimiters) {
3471 delimiter_cache.clear();
3472 _update_delimiter_cache();
3473 }
3474 if (p_type == DelimiterType::TYPE_COMMENT) {
3475 _update_code_region_tags();
3476 }
3477 break;
3478 }
3479 }
3480
3481 bool CodeEdit::_has_delimiter(const String &p_start_key, DelimiterType p_type) const {
3482 for (int i = 0; i < delimiters.size(); i++) {
3483 if (delimiters[i].start_key == p_start_key) {
3484 return delimiters[i].type == p_type;
3485 }
3486 }
3487 return false;
3488 }
3489
3490 void CodeEdit::_set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type) {
3491 setting_delimiters = true;
3492 _clear_delimiters(p_type);
3493
3494 for (int i = 0; i < p_delimiters.size(); i++) {
3495 String key = p_delimiters[i];
3496
3497 if (key.is_empty()) {
3498 continue;
3499 }
3500
3501 const String start_key = key.get_slicec(' ', 0);
3502 const String end_key = key.get_slice_count(" ") > 1 ? key.get_slicec(' ', 1) : String();
3503
3504 _add_delimiter(start_key, end_key, end_key.is_empty(), p_type);
3505 }
3506 setting_delimiters = false;
3507 _update_delimiter_cache();
3508 }
3509
3510 void CodeEdit::_clear_delimiters(DelimiterType p_type) {
3511 for (int i = delimiters.size() - 1; i >= 0; i--) {
3512 if (delimiters[i].type == p_type) {
3513 delimiters.remove_at(i);
3514 }
3515 }
3516 delimiter_cache.clear();
3517 if (!setting_delimiters) {
3518 _update_delimiter_cache();
3519 }
3520 if (p_type == DelimiterType::TYPE_COMMENT) {
3521 _update_code_region_tags();
3522 }
3523 }
3524
3525 TypedArray<String> CodeEdit::_get_delimiters(DelimiterType p_type) const {
3526 TypedArray<String> r_delimiters;
3527 for (int i = 0; i < delimiters.size(); i++) {
3528 if (delimiters[i].type != p_type) {
3529 continue;
3530 }
3531 r_delimiters.push_back(delimiters[i].start_key + (delimiters[i].end_key.is_empty() ? "" : " " + delimiters[i].end_key));
3532 }
3533 return r_delimiters;
3534 }
3535
3536 /* Code Completion */
3537 void CodeEdit::_update_scroll_selected_line(float p_mouse_y) {
3538 float percent = (float)(p_mouse_y - code_completion_scroll_rect.position.y) / code_completion_scroll_rect.size.height;
3539 percent = CLAMP(percent, 0.0f, 1.0f);
3540
3541 code_completion_current_selected = (int)(percent * (code_completion_options.size() - 1));
3542 code_completion_force_item_center = -1;
3543 code_completion_pan_offset = 0.0f;
3544 }
3545
3546 void CodeEdit::_filter_code_completion_candidates_impl() {
3547 int line_height = get_line_height();
3548
3549 if (GDVIRTUAL_IS_OVERRIDDEN(_filter_code_completion_candidates)) {
3550 Vector<ScriptLanguage::CodeCompletionOption> code_completion_options_new;
3551 code_completion_base = "";
3552
3553 /* Build options argument. */
3554 TypedArray<Dictionary> completion_options_sources;
3555 completion_options_sources.resize(code_completion_option_sources.size());
3556 int i = 0;
3557 for (const ScriptLanguage::CodeCompletionOption &E : code_completion_option_sources) {
3558 Dictionary option;
3559 option["kind"] = E.kind;
3560 option["display_text"] = E.display;
3561 option["insert_text"] = E.insert_text;
3562 option["font_color"] = E.font_color;
3563 option["icon"] = E.icon;
3564 option["default_value"] = E.default_value;
3565 option["location"] = E.location;
3566 completion_options_sources[i] = option;
3567 i++;
3568 }
3569
3570 TypedArray<Dictionary> completion_options;
3571
3572 GDVIRTUAL_CALL(_filter_code_completion_candidates, completion_options_sources, completion_options);
3573
3574 /* No options to complete, cancel. */
3575 if (completion_options.is_empty()) {
3576 cancel_code_completion();
3577 return;
3578 }
3579
3580 /* Convert back into options. */
3581 int max_width = 0;
3582 for (i = 0; i < completion_options.size(); i++) {
3583 ScriptLanguage::CodeCompletionOption option;
3584 option.kind = (ScriptLanguage::CodeCompletionKind)(int)completion_options[i].get("kind");
3585 option.display = completion_options[i].get("display_text");
3586 option.insert_text = completion_options[i].get("insert_text");
3587 option.font_color = completion_options[i].get("font_color");
3588 option.icon = completion_options[i].get("icon");
3589 option.location = completion_options[i].get("location");
3590 option.default_value = completion_options[i].get("default_value");
3591
3592 int offset = 0;
3593 if (option.default_value.get_type() == Variant::COLOR) {
3594 offset = line_height;
3595 }
3596
3597 if (theme_cache.font.is_valid()) {
3598 max_width = MAX(max_width, theme_cache.font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + offset);
3599 }
3600 code_completion_options_new.push_back(option);
3601 }
3602
3603 if (_should_reset_selected_option_for_new_options(code_completion_options_new)) {
3604 code_completion_current_selected = 0;
3605 code_completion_pan_offset = 0.0f;
3606 }
3607 code_completion_options = code_completion_options_new;
3608
3609 code_completion_longest_line = MIN(max_width, theme_cache.code_completion_max_width * theme_cache.font_size);
3610 code_completion_force_item_center = -1;
3611 code_completion_active = true;
3612 queue_redraw();
3613 return;
3614 }
3615
3616 const int caret_line = get_caret_line();
3617 const int caret_column = get_caret_column();
3618 const String line = get_line(caret_line);
3619 ERR_FAIL_INDEX_MSG(caret_column, line.length() + 1, "Caret column exceeds line length.");
3620
3621 if (caret_column > 0 && line[caret_column - 1] == '(' && !code_completion_forced) {
3622 cancel_code_completion();
3623 return;
3624 }
3625
3626 /* Get string status, are we in one or at the close. */
3627 int in_string = is_in_string(caret_line, caret_column);
3628 int first_quote_col = -1;
3629 if (in_string != -1) {
3630 Point2 string_start_pos = get_delimiter_start_position(caret_line, caret_column);
3631 first_quote_col = (string_start_pos.y == caret_line) ? string_start_pos.x : -1;
3632 } else if (caret_column > 0) {
3633 if (is_in_string(caret_line, caret_column - 1) != -1) {
3634 first_quote_col = caret_column - 1;
3635 }
3636 }
3637
3638 int cofs = caret_column;
3639 String string_to_complete;
3640 bool prev_is_word = false;
3641
3642 /* Cancel if we are at the close of a string. */
3643 if (caret_column > 0 && in_string == -1 && first_quote_col == cofs - 1) {
3644 cancel_code_completion();
3645 return;
3646 /* In a string, therefore we are trying to complete the string text. */
3647 } else if (in_string != -1 && first_quote_col != -1) {
3648 int key_length = delimiters[in_string].start_key.length();
3649 string_to_complete = line.substr(first_quote_col - key_length, (cofs - first_quote_col) + key_length);
3650 /* If we have a space, previous word might be a keyword. eg "func |". */
3651 } else if (cofs > 0 && line[cofs - 1] == ' ') {
3652 int ofs = cofs - 1;
3653 while (ofs > 0 && line[ofs] == ' ') {
3654 ofs--;
3655 }
3656 prev_is_word = !is_symbol(line[ofs]);
3657 /* Otherwise get current word and set cofs to the start. */
3658 } else {
3659 int start_cofs = cofs;
3660 while (cofs > 0 && line[cofs - 1] > 32 && (line[cofs - 1] == '/' || !is_symbol(line[cofs - 1]))) {
3661 cofs--;
3662 }
3663 string_to_complete = line.substr(cofs, start_cofs - cofs);
3664 }
3665
3666 /* If all else fails, check for a prefix. */
3667 /* Single space between caret and prefix is okay. */
3668 bool prev_is_prefix = false;
3669 if (cofs > 0 && code_completion_prefixes.has(line[cofs - 1])) {
3670 prev_is_prefix = true;
3671 } else if (cofs > 1 && line[cofs - 1] == ' ' && code_completion_prefixes.has(line[cofs - 2])) {
3672 prev_is_prefix = true;
3673 }
3674
3675 if (!prev_is_word && string_to_complete.is_empty() && (cofs == 0 || !prev_is_prefix)) {
3676 cancel_code_completion();
3677 return;
3678 }
3679
3680 /* Filter Options. */
3681 /* For now handle only traditional quoted strings. */
3682 bool single_quote = in_string != -1 && first_quote_col > 0 && delimiters[in_string].start_key == "'";
3683
3684 Vector<ScriptLanguage::CodeCompletionOption> code_completion_options_new;
3685 code_completion_base = string_to_complete;
3686
3687 /* Don't autocomplete setting numerical values. */
3688 if (code_completion_base.is_numeric()) {
3689 cancel_code_completion();
3690 return;
3691 }
3692
3693 int max_width = 0;
3694 String string_to_complete_lower = string_to_complete.to_lower();
3695
3696 for (ScriptLanguage::CodeCompletionOption &option : code_completion_option_sources) {
3697 option.matches.clear();
3698 option.matches_dirty = true;
3699 if (single_quote && option.display.is_quoted()) {
3700 option.display = option.display.unquote().quote("'");
3701 }
3702
3703 int offset = option.default_value.get_type() == Variant::COLOR ? line_height : 0;
3704
3705 if (in_string != -1) {
3706 // The completion string may have a literal behind it, which should be removed before re-quoting.
3707 String literal;
3708 if (option.insert_text.substr(1).is_quoted()) {
3709 literal = option.display.left(1);
3710 option.display = option.display.substr(1);
3711 option.insert_text = option.insert_text.substr(1);
3712 }
3713 String quote = single_quote ? "'" : "\"";
3714 option.display = literal + (option.display.unquote().quote(quote));
3715 option.insert_text = literal + (option.insert_text.unquote().quote(quote));
3716 }
3717
3718 if (option.display.length() == 0) {
3719 continue;
3720 }
3721
3722 if (string_to_complete.length() == 0) {
3723 option.get_option_characteristics(string_to_complete);
3724 code_completion_options_new.push_back(option);
3725
3726 if (theme_cache.font.is_valid()) {
3727 max_width = MAX(max_width, theme_cache.font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + offset);
3728 }
3729 continue;
3730 }
3731
3732 String target_lower = option.display.to_lower();
3733 int long_option = target_lower.size() > 50;
3734 const char32_t *string_to_complete_char_lower = &string_to_complete_lower[0];
3735 const char32_t *target_char_lower = &target_lower[0];
3736
3737 Vector<Vector<Pair<int, int>>> all_possible_subsequence_matches;
3738 for (int i = 0; *target_char_lower; i++, target_char_lower++) {
3739 if (*target_char_lower == *string_to_complete_char_lower) {
3740 all_possible_subsequence_matches.push_back({ { i, 1 } });
3741 }
3742 }
3743 string_to_complete_char_lower++;
3744
3745 for (int i = 1; *string_to_complete_char_lower && (all_possible_subsequence_matches.size() > 0); i++, string_to_complete_char_lower++) {
3746 // find all occurrences of ssq_lower to avoid looking everywhere each time
3747 Vector<int> all_occurrences;
3748 if (long_option) {
3749 all_occurrences.push_back(target_lower.find_char(*string_to_complete_char_lower));
3750 } else {
3751 for (int j = i; j < target_lower.length(); j++) {
3752 if (target_lower[j] == *string_to_complete_char_lower) {
3753 all_occurrences.push_back(j);
3754 }
3755 }
3756 }
3757 Vector<Vector<Pair<int, int>>> next_subsequence_matches;
3758 for (Vector<Pair<int, int>> &subsequence_match : all_possible_subsequence_matches) {
3759 Pair<int, int> match_last_segment = subsequence_match[subsequence_match.size() - 1];
3760 int next_index = match_last_segment.first + match_last_segment.second;
3761 // get the last index from current sequence
3762 // and look for next char starting from that index
3763 if (target_lower[next_index] == *string_to_complete_char_lower) {
3764 Vector<Pair<int, int>> new_match = subsequence_match;
3765 new_match.write[new_match.size() - 1].second++;
3766 next_subsequence_matches.push_back(new_match);
3767 if (long_option) {
3768 continue;
3769 }
3770 }
3771 for (int index : all_occurrences) {
3772 if (index > next_index) {
3773 Vector<Pair<int, int>> new_match = subsequence_match;
3774 new_match.push_back({ index, 1 });
3775 next_subsequence_matches.push_back(new_match);
3776 }
3777 }
3778 }
3779 all_possible_subsequence_matches = next_subsequence_matches;
3780 }
3781 // go through all possible matches to get the best one as defined by CodeCompletionOptionCompare
3782 if (all_possible_subsequence_matches.size() > 0) {
3783 option.matches = all_possible_subsequence_matches[0];
3784 option.matches_dirty = true;
3785 option.get_option_characteristics(string_to_complete);
3786 all_possible_subsequence_matches = all_possible_subsequence_matches.slice(1);
3787 if (all_possible_subsequence_matches.size() > 0) {
3788 CodeCompletionOptionCompare compare;
3789 ScriptLanguage::CodeCompletionOption compared_option = option;
3790 compared_option.clear_characteristics();
3791 for (Vector<Pair<int, int>> &matches : all_possible_subsequence_matches) {
3792 compared_option.matches = matches;
3793 compared_option.matches_dirty = true;
3794 compared_option.get_option_characteristics(string_to_complete);
3795 if (compare(compared_option, option)) {
3796 option = compared_option;
3797 compared_option.clear_characteristics();
3798 }
3799 }
3800 }
3801
3802 code_completion_options_new.push_back(option);
3803 if (theme_cache.font.is_valid()) {
3804 max_width = MAX(max_width, theme_cache.font->get_string_size(option.display, HORIZONTAL_ALIGNMENT_LEFT, -1, theme_cache.font_size).width + offset);
3805 }
3806 }
3807 }
3808
3809 /* No options to complete, cancel. */
3810 if (code_completion_options_new.is_empty()) {
3811 cancel_code_completion();
3812 return;
3813 }
3814
3815 /* A perfect match, stop completion. */
3816 if (code_completion_options_new.size() == 1 && string_to_complete == code_completion_options_new[0].display) {
3817 cancel_code_completion();
3818 return;
3819 }
3820
3821 code_completion_options_new.sort_custom<CodeCompletionOptionCompare>();
3822 if (_should_reset_selected_option_for_new_options(code_completion_options_new)) {
3823 code_completion_current_selected = 0;
3824 code_completion_pan_offset = 0.0f;
3825 }
3826 code_completion_options = code_completion_options_new;
3827
3828 code_completion_longest_line = MIN(max_width, theme_cache.code_completion_max_width * theme_cache.font_size);
3829 code_completion_force_item_center = -1;
3830 code_completion_active = true;
3831 queue_redraw();
3832 }
3833
3834 // Assumes both the new_options and the code_completion_options are sorted.
3835 bool CodeEdit::_should_reset_selected_option_for_new_options(const Vector<ScriptLanguage::CodeCompletionOption> &p_new_options) {
3836 if (code_completion_current_selected >= p_new_options.size()) {
3837 return true;
3838 }
3839
3840 for (int i = 0; i < code_completion_options.size() && i < p_new_options.size(); i++) {
3841 if (i > code_completion_current_selected) {
3842 return false;
3843 }
3844 if (code_completion_options[i].display != p_new_options[i].display) {
3845 return true;
3846 }
3847 }
3848 return false;
3849 }
3850
3851 void CodeEdit::_lines_edited_from(int p_from_line, int p_to_line) {
3852 _update_delimiter_cache(p_from_line, p_to_line);
3853
3854 if (p_from_line == p_to_line) {
3855 return;
3856 }
3857
3858 lines_edited_changed += p_to_line - p_from_line;
3859 lines_edited_from = (lines_edited_from == -1) ? MIN(p_from_line, p_to_line) : MIN(lines_edited_from, MIN(p_from_line, p_to_line));
3860 lines_edited_to = (lines_edited_to == -1) ? MAX(p_from_line, p_to_line) : MAX(lines_edited_from, MAX(p_from_line, p_to_line));
3861 }
3862
3863 void CodeEdit::_text_set() {
3864 lines_edited_from = 0;
3865 lines_edited_to = 9999;
3866 _text_changed();
3867 }
3868
3869 void CodeEdit::_text_changed() {
3870 if (lines_edited_from == -1) {
3871 return;
3872 }
3873
3874 int lc = get_line_count();
3875 int new_line_number_digits = MAX(line_numbers_min_digits, std::log10(lc) + 1);
3876 if (line_number_digits != new_line_number_digits) {
3877 _clear_line_number_text_cache();
3878 }
3879 line_number_digits = new_line_number_digits;
3880 _update_line_number_gutter_width();
3881
3882 List<int> breakpoints;
3883 for (const KeyValue<int, bool> &E : breakpointed_lines) {
3884 breakpoints.push_back(E.key);
3885 }
3886 for (const int &line : breakpoints) {
3887 if (line < lines_edited_from || (line < lc && is_line_breakpointed(line))) {
3888 continue;
3889 }
3890
3891 breakpointed_lines.erase(line);
3892 emit_signal(SNAME("breakpoint_toggled"), line);
3893
3894 int next_line = line + lines_edited_changed;
3895 if (next_line > -1 && next_line < lc && is_line_breakpointed(next_line)) {
3896 emit_signal(SNAME("breakpoint_toggled"), next_line);
3897 breakpointed_lines[next_line] = true;
3898 continue;
3899 }
3900 }
3901
3902 lines_edited_from = -1;
3903 lines_edited_to = -1;
3904 lines_edited_changed = 0;
3905 }
3906
3907 CodeEdit::CodeEdit() {
3908 /* Indent management */
3909 auto_indent_prefixes.insert(':');
3910 auto_indent_prefixes.insert('{');
3911 auto_indent_prefixes.insert('[');
3912 auto_indent_prefixes.insert('(');
3913
3914 /* Auto brace completion */
3915 add_auto_brace_completion_pair("(", ")");
3916 add_auto_brace_completion_pair("{", "}");
3917 add_auto_brace_completion_pair("[", "]");
3918 add_auto_brace_completion_pair("\"", "\"");
3919 add_auto_brace_completion_pair("\'", "\'");
3920
3921 /* Delimiter tracking */
3922 add_string_delimiter("\"", "\"", false);
3923 add_string_delimiter("\'", "\'", false);
3924
3925 /* Text Direction */
3926 set_layout_direction(LAYOUT_DIRECTION_LTR);
3927 set_text_direction(TEXT_DIRECTION_LTR);
3928
3929 /* Gutters */
3930 int gutter_idx = 0;
3931
3932 /* Main Gutter */
3933 add_gutter();
3934 set_gutter_name(gutter_idx, "main_gutter");
3935 set_gutter_draw(gutter_idx, false);
3936 set_gutter_overwritable(gutter_idx, true);
3937 set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
3938 set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_main_gutter_draw_callback));
3939 gutter_idx++;
3940
3941 /* Line numbers */
3942 add_gutter();
3943 set_gutter_name(gutter_idx, "line_numbers");
3944 set_gutter_draw(gutter_idx, false);
3945 set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
3946 set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_line_number_draw_callback));
3947 gutter_idx++;
3948
3949 /* Fold Gutter */
3950 add_gutter();
3951 set_gutter_name(gutter_idx, "fold_gutter");
3952 set_gutter_draw(gutter_idx, false);
3953 set_gutter_type(gutter_idx, GUTTER_TYPE_CUSTOM);
3954 set_gutter_custom_draw(gutter_idx, callable_mp(this, &CodeEdit::_fold_gutter_draw_callback));
3955 gutter_idx++;
3956
3957 /* Symbol tooltip */
3958 symbol_tooltip_timer = memnew(Timer);
3959 symbol_tooltip_timer->set_wait_time(0.5); // See `_apply_project_settings()`.
3960 symbol_tooltip_timer->set_one_shot(true);
3961 symbol_tooltip_timer->connect("timeout", callable_mp(this, &CodeEdit::_on_symbol_tooltip_timer_timeout));
3962 add_child(symbol_tooltip_timer, false, INTERNAL_MODE_FRONT);
3963
3964 /* Fold Lines Private signal */
3965 add_user_signal(MethodInfo("_fold_line_updated"));
3966
3967 connect("lines_edited_from", callable_mp(this, &CodeEdit::_lines_edited_from));
3968 connect("text_set", callable_mp(this, &CodeEdit::_text_set));
3969 connect(SceneStringName(text_changed), callable_mp(this, &CodeEdit::_text_changed));
3970
3971 connect("gutter_clicked", callable_mp(this, &CodeEdit::_gutter_clicked));
3972 connect("gutter_added", callable_mp(this, &CodeEdit::_update_gutter_indexes));
3973 connect("gutter_removed", callable_mp(this, &CodeEdit::_update_gutter_indexes));
3974 _update_gutter_indexes();
3975 }
3976
3977 CodeEdit::~CodeEdit() {
3978 _clear_line_number_text_cache();
3979 }
3980
3981 // Return true if l should come before r
3982 bool CodeCompletionOptionCompare::operator()(const ScriptLanguage::CodeCompletionOption &l, const ScriptLanguage::CodeCompletionOption &r) const {
3983 TypedArray<int> lcharac = l.get_option_cached_characteristics();
3984 TypedArray<int> rcharac = r.get_option_cached_characteristics();
3985
3986 if (lcharac != rcharac) {
3987 return lcharac < rcharac;
3988 }
3989
3990 // to get here they need to have the same size so we can take the size of whichever we want
3991 for (int i = 0; i < l.matches.size(); ++i) {
3992 if (l.matches[i].first != r.matches[i].first) {
3993 return l.matches[i].first < r.matches[i].first;
3994 }
3995 if (l.matches[i].second != r.matches[i].second) {
3996 return l.matches[i].second > r.matches[i].second;
3997 }
3998 }
3999 return l.display.naturalnocasecmp_to(r.display) < 0;
4000 }