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