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