Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/gui/code_edit.h
9903 views
1
/**************************************************************************/
2
/* code_edit.h */
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
#pragma once
32
33
#include "core/object/script_language.h"
34
#include "scene/gui/text_edit.h"
35
36
class CodeEdit : public TextEdit {
37
GDCLASS(CodeEdit, TextEdit)
38
39
public:
40
// Keep enums in sync with:
41
// core/object/script_language.h - ScriptLanguage::CodeCompletionKind
42
enum CodeCompletionKind {
43
KIND_CLASS,
44
KIND_FUNCTION,
45
KIND_SIGNAL,
46
KIND_VARIABLE,
47
KIND_MEMBER,
48
KIND_ENUM,
49
KIND_CONSTANT,
50
KIND_NODE_PATH,
51
KIND_FILE_PATH,
52
KIND_PLAIN_TEXT,
53
};
54
55
// core/object/script_language.h - ScriptLanguage::CodeCompletionLocation
56
enum CodeCompletionLocation {
57
LOCATION_LOCAL = 0,
58
LOCATION_PARENT_MASK = 1 << 8,
59
LOCATION_OTHER_USER_CODE = 1 << 9,
60
LOCATION_OTHER = 1 << 10,
61
};
62
63
private:
64
/* Indent management */
65
int indent_size = 4;
66
String indent_text = "\t";
67
68
bool auto_indent = false;
69
HashSet<char32_t> auto_indent_prefixes;
70
71
bool indent_using_spaces = false;
72
int _calculate_spaces_till_next_left_indent(int p_column) const;
73
int _calculate_spaces_till_next_right_indent(int p_column) const;
74
75
void _new_line(bool p_split_current_line = true, bool p_above = false);
76
77
/* Auto brace completion */
78
bool auto_brace_completion_enabled = false;
79
80
/* BracePair open_key must be uniquie and ordered by length. */
81
struct BracePair {
82
String open_key = "";
83
String close_key = "";
84
};
85
Vector<BracePair> auto_brace_completion_pairs;
86
87
int _get_auto_brace_pair_open_at_pos(int p_line, int p_col);
88
int _get_auto_brace_pair_close_at_pos(int p_line, int p_col);
89
90
/* Main Gutter */
91
enum MainGutterType {
92
MAIN_GUTTER_BREAKPOINT = 0x01,
93
MAIN_GUTTER_BOOKMARK = 0x02,
94
MAIN_GUTTER_EXECUTING = 0x04
95
};
96
97
int main_gutter = -1;
98
void _update_draw_main_gutter();
99
void _main_gutter_draw_callback(int p_line, int p_gutter, const Rect2 &p_region);
100
101
// breakpoints
102
HashMap<int, bool> breakpointed_lines;
103
bool draw_breakpoints = false;
104
105
// bookmarks
106
bool draw_bookmarks = false;
107
108
// executing lines
109
bool draw_executing_lines = false;
110
111
/* Line numbers */
112
int line_number_gutter = -1;
113
int line_number_digits = 1;
114
String line_number_padding = " ";
115
HashMap<int, RID> line_number_text_cache;
116
void _clear_line_number_text_cache();
117
void _update_line_number_gutter_width();
118
void _line_number_draw_callback(int p_line, int p_gutter, const Rect2 &p_region);
119
120
/* Fold Gutter */
121
int fold_gutter = -1;
122
bool draw_fold_gutter = false;
123
void _fold_gutter_draw_callback(int p_line, int p_gutter, Rect2 p_region);
124
125
void _gutter_clicked(int p_line, int p_gutter);
126
void _update_gutter_indexes();
127
128
/* Line Folding */
129
bool line_folding_enabled = false;
130
String code_region_start_string;
131
String code_region_end_string;
132
String code_region_start_tag = "region";
133
String code_region_end_tag = "endregion";
134
void _update_code_region_tags();
135
bool _fold_line(int p_line);
136
bool _unfold_line(int p_line);
137
138
/* Delimiters */
139
enum DelimiterType {
140
TYPE_STRING,
141
TYPE_COMMENT,
142
};
143
144
struct Delimiter {
145
DelimiterType type;
146
String start_key = "";
147
String end_key = "";
148
bool line_only = true;
149
};
150
bool setting_delimiters = false;
151
Vector<Delimiter> delimiters;
152
/*
153
* Vector entry per line, contains a Map of column numbers to delimiter index, -1 marks the end of a region.
154
* e.g the following text will be stored as so:
155
*
156
* 0: nothing here
157
* 1:
158
* 2: # test
159
* 3: "test" text "multiline
160
* 4:
161
* 5: test
162
* 6: string"
163
*
164
* Vector [
165
* 0 = []
166
* 1 = []
167
* 2 = [
168
* 1 = 1
169
* 6 = -1
170
* ]
171
* 3 = [
172
* 1 = 0
173
* 6 = -1
174
* 13 = 0
175
* ]
176
* 4 = [
177
* 0 = 0
178
* ]
179
* 5 = [
180
* 5 = 0
181
* ]
182
* 6 = [
183
* 7 = -1
184
* ]
185
* ]
186
*/
187
Vector<RBMap<int, int>> delimiter_cache;
188
189
void _update_delimiter_cache(int p_from_line = 0, int p_to_line = -1);
190
int _is_in_delimiter(int p_line, int p_column, DelimiterType p_type) const;
191
192
void _add_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only, DelimiterType p_type);
193
void _remove_delimiter(const String &p_start_key, DelimiterType p_type);
194
bool _has_delimiter(const String &p_start_key, DelimiterType p_type) const;
195
196
void _set_delimiters(const TypedArray<String> &p_delimiters, DelimiterType p_type);
197
void _clear_delimiters(DelimiterType p_type);
198
TypedArray<String> _get_delimiters(DelimiterType p_type) const;
199
200
/* Code Hint */
201
String code_hint = "";
202
203
bool code_hint_draw_below = true;
204
int code_hint_xpos = -0xFFFF;
205
206
/* Code Completion */
207
bool code_completion_enabled = false;
208
bool code_completion_forced = false;
209
210
bool code_completion_active = false;
211
bool is_code_completion_scroll_hovered = false;
212
bool is_code_completion_scroll_pressed = false;
213
bool is_code_completion_drag_started = false;
214
Vector<ScriptLanguage::CodeCompletionOption> code_completion_options;
215
int code_completion_line_ofs = 0;
216
int code_completion_current_selected = 0;
217
int code_completion_force_item_center = -1;
218
int code_completion_longest_line = 0;
219
Rect2i code_completion_rect;
220
Rect2i code_completion_scroll_rect;
221
float code_completion_pan_offset = 0.0f;
222
223
HashSet<char32_t> code_completion_prefixes;
224
List<ScriptLanguage::CodeCompletionOption> code_completion_option_submitted;
225
List<ScriptLanguage::CodeCompletionOption> code_completion_option_sources;
226
String code_completion_base;
227
228
void _update_scroll_selected_line(float p_mouse_y);
229
void _filter_code_completion_candidates_impl();
230
bool _should_reset_selected_option_for_new_options(const Vector<ScriptLanguage::CodeCompletionOption> &p_new_options);
231
232
/* Line length guidelines */
233
TypedArray<int> line_length_guideline_columns;
234
235
/* Symbol lookup */
236
bool symbol_lookup_on_click_enabled = false;
237
Point2i symbol_lookup_pos; // Column and line.
238
String symbol_lookup_new_word;
239
String symbol_lookup_word;
240
241
/* Symbol tooltip */
242
bool symbol_tooltip_on_hover_enabled = false;
243
Point2i symbol_tooltip_pos; // Column and line.
244
String symbol_tooltip_word;
245
Timer *symbol_tooltip_timer = nullptr;
246
void _on_symbol_tooltip_timer_timeout();
247
248
/* Visual */
249
struct ThemeCache {
250
/* Gutters */
251
Color code_folding_color = Color(1, 1, 1);
252
Color folded_code_region_color = Color(1, 1, 1);
253
Ref<Texture2D> can_fold_icon;
254
Ref<Texture2D> folded_icon;
255
Ref<Texture2D> can_fold_code_region_icon;
256
Ref<Texture2D> folded_code_region_icon;
257
Ref<Texture2D> folded_eol_icon;
258
Ref<Texture2D> completion_color_bg;
259
260
Color breakpoint_color = Color(1, 1, 1);
261
Ref<Texture2D> breakpoint_icon;
262
263
Color bookmark_color = Color(1, 1, 1);
264
Ref<Texture2D> bookmark_icon;
265
266
Color executing_line_color = Color(1, 1, 1);
267
Ref<Texture2D> executing_line_icon;
268
269
Color line_number_color = Color(1, 1, 1);
270
271
/* Code Completion */
272
Ref<StyleBox> code_completion_style;
273
int code_completion_icon_separation = 0;
274
275
int code_completion_max_width = 0;
276
int code_completion_max_lines = 7;
277
int code_completion_scroll_width = 0;
278
Color code_completion_scroll_color = Color(0, 0, 0, 0);
279
Color code_completion_scroll_hovered_color = Color(0, 0, 0, 0);
280
Color code_completion_background_color = Color(0, 0, 0, 0);
281
Color code_completion_selected_color = Color(0, 0, 0, 0);
282
Color code_completion_existing_color = Color(0, 0, 0, 0);
283
284
/* Code hint */
285
Ref<StyleBox> code_hint_style;
286
Color code_hint_color;
287
288
/* Line length guideline */
289
Color line_length_guideline_color;
290
291
/* Other visuals */
292
Ref<StyleBox> style_normal;
293
294
Color brace_mismatch_color;
295
296
Ref<Font> font;
297
int font_size = 16;
298
int line_spacing = 1;
299
} theme_cache;
300
301
virtual Color _get_brace_mismatch_color() const override;
302
virtual Color _get_code_folding_color() const override;
303
virtual Ref<Texture2D> _get_folded_eol_icon() const override;
304
305
/* Callbacks */
306
int lines_edited_changed = 0;
307
int lines_edited_from = -1;
308
int lines_edited_to = -1;
309
310
void _lines_edited_from(int p_from_line, int p_to_line);
311
void _text_set();
312
void _text_changed();
313
314
void _apply_project_settings();
315
316
protected:
317
void _notification(int p_what);
318
static void _bind_methods();
319
320
#ifndef DISABLE_DEPRECATED
321
String _get_text_for_symbol_lookup_bind_compat_73196();
322
void _add_code_completion_option_compat_84906(CodeCompletionKind p_type, const String &p_display_text, const String &p_insert_text, const Color &p_text_color = Color(1, 1, 1), const Ref<Resource> &p_icon = Ref<Resource>(), const Variant &p_value = Variant::NIL, int p_location = LOCATION_OTHER);
323
static void _bind_compatibility_methods();
324
#endif
325
326
virtual void _unhide_carets() override;
327
328
virtual void _draw_guidelines() override;
329
330
/* Text manipulation */
331
332
// Overridable actions
333
virtual void _handle_unicode_input_internal(const uint32_t p_unicode, int p_caret) override;
334
virtual void _backspace_internal(int p_caret) override;
335
virtual void _cut_internal(int p_caret) override;
336
337
GDVIRTUAL1(_confirm_code_completion, bool)
338
GDVIRTUAL1(_request_code_completion, bool)
339
GDVIRTUAL1RC(TypedArray<Dictionary>, _filter_code_completion_candidates, TypedArray<Dictionary>)
340
341
public:
342
/* General overrides */
343
virtual void gui_input(const Ref<InputEvent> &p_gui_input) override;
344
virtual CursorShape get_cursor_shape(const Point2 &p_pos = Point2i()) const override;
345
346
/* Indent management */
347
void set_indent_size(const int p_size);
348
int get_indent_size() const;
349
350
void set_indent_using_spaces(const bool p_use_spaces);
351
bool is_indent_using_spaces() const;
352
353
void set_auto_indent_enabled(bool p_enabled);
354
bool is_auto_indent_enabled() const;
355
356
void set_auto_indent_prefixes(const TypedArray<String> &p_prefixes);
357
TypedArray<String> get_auto_indent_prefixes() const;
358
359
void do_indent();
360
361
void indent_lines();
362
void unindent_lines();
363
364
void convert_indent(int p_from_line = -1, int p_to_line = -1);
365
366
/* Auto brace completion */
367
void set_auto_brace_completion_enabled(bool p_enabled);
368
bool is_auto_brace_completion_enabled() const;
369
370
void set_highlight_matching_braces_enabled(bool p_enabled);
371
bool is_highlight_matching_braces_enabled() const;
372
373
void add_auto_brace_completion_pair(const String &p_open_key, const String &p_close_key);
374
void set_auto_brace_completion_pairs(const Dictionary &p_auto_brace_completion_pairs);
375
Dictionary get_auto_brace_completion_pairs() const;
376
377
bool has_auto_brace_completion_open_key(const String &p_open_key) const;
378
bool has_auto_brace_completion_close_key(const String &p_close_key) const;
379
380
String get_auto_brace_completion_close_key(const String &p_open_key) const;
381
382
/* Main Gutter */
383
void set_draw_breakpoints_gutter(bool p_draw);
384
bool is_drawing_breakpoints_gutter() const;
385
386
void set_draw_bookmarks_gutter(bool p_draw);
387
bool is_drawing_bookmarks_gutter() const;
388
389
void set_draw_executing_lines_gutter(bool p_draw);
390
bool is_drawing_executing_lines_gutter() const;
391
392
// breakpoints
393
void set_line_as_breakpoint(int p_line, bool p_breakpointed);
394
bool is_line_breakpointed(int p_line) const;
395
void clear_breakpointed_lines();
396
PackedInt32Array get_breakpointed_lines() const;
397
398
// bookmarks
399
void set_line_as_bookmarked(int p_line, bool p_bookmarked);
400
bool is_line_bookmarked(int p_line) const;
401
void clear_bookmarked_lines();
402
PackedInt32Array get_bookmarked_lines() const;
403
404
// executing lines
405
void set_line_as_executing(int p_line, bool p_executing);
406
bool is_line_executing(int p_line) const;
407
void clear_executing_lines();
408
PackedInt32Array get_executing_lines() const;
409
410
/* Line numbers */
411
void set_draw_line_numbers(bool p_draw);
412
bool is_draw_line_numbers_enabled() const;
413
void set_line_numbers_zero_padded(bool p_zero_padded);
414
bool is_line_numbers_zero_padded() const;
415
416
/* Fold gutter */
417
void set_draw_fold_gutter(bool p_draw);
418
bool is_drawing_fold_gutter() const;
419
420
/* Line Folding */
421
void set_line_folding_enabled(bool p_enabled);
422
bool is_line_folding_enabled() const;
423
424
bool can_fold_line(int p_line) const;
425
426
void fold_line(int p_line);
427
void unfold_line(int p_line);
428
void fold_all_lines();
429
void unfold_all_lines();
430
void toggle_foldable_line(int p_line);
431
void toggle_foldable_lines_at_carets();
432
433
int get_folded_line_header(int p_line) const;
434
bool is_line_folded(int p_line) const;
435
TypedArray<int> get_folded_lines() const;
436
437
/* Code region */
438
void create_code_region();
439
String get_code_region_start_tag() const;
440
String get_code_region_end_tag() const;
441
void set_code_region_tags(const String &p_start = "region", const String &p_end = "endregion");
442
bool is_line_code_region_start(int p_line) const;
443
bool is_line_code_region_end(int p_line) const;
444
445
/* Delimiters */
446
void add_string_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false);
447
void remove_string_delimiter(const String &p_start_key);
448
bool has_string_delimiter(const String &p_start_key) const;
449
450
void set_string_delimiters(const TypedArray<String> &p_string_delimiters);
451
void clear_string_delimiters();
452
TypedArray<String> get_string_delimiters() const;
453
454
int is_in_string(int p_line, int p_column = -1) const;
455
456
void add_comment_delimiter(const String &p_start_key, const String &p_end_key, bool p_line_only = false);
457
void remove_comment_delimiter(const String &p_start_key);
458
bool has_comment_delimiter(const String &p_start_key) const;
459
460
void set_comment_delimiters(const TypedArray<String> &p_comment_delimiters);
461
void clear_comment_delimiters();
462
TypedArray<String> get_comment_delimiters() const;
463
464
int is_in_comment(int p_line, int p_column = -1) const;
465
466
String get_delimiter_start_key(int p_delimiter_idx) const;
467
String get_delimiter_end_key(int p_delimiter_idx) const;
468
469
Point2 get_delimiter_start_position(int p_line, int p_column) const;
470
Point2 get_delimiter_end_position(int p_line, int p_column) const;
471
472
/* Code hint */
473
void set_code_hint(const String &p_hint);
474
void set_code_hint_draw_below(bool p_below);
475
476
/* Code Completion */
477
void set_code_completion_enabled(bool p_enable);
478
bool is_code_completion_enabled() const;
479
480
void set_code_completion_prefixes(const TypedArray<String> &p_prefixes);
481
TypedArray<String> get_code_completion_prefixes() const;
482
483
String get_text_for_code_completion() const;
484
485
void request_code_completion(bool p_force = false);
486
487
void add_code_completion_option(CodeCompletionKind p_type, const String &p_display_text, const String &p_insert_text, const Color &p_text_color = Color(1, 1, 1), const Ref<Resource> &p_icon = Ref<Resource>(), const Variant &p_value = Variant(), int p_location = LOCATION_OTHER);
488
void update_code_completion_options(bool p_forced = false);
489
490
TypedArray<Dictionary> get_code_completion_options() const;
491
Dictionary get_code_completion_option(int p_index) const;
492
493
int get_code_completion_selected_index() const;
494
void set_code_completion_selected_index(int p_index);
495
496
void confirm_code_completion(bool p_replace = false);
497
void cancel_code_completion();
498
499
/* Line length guidelines */
500
void set_line_length_guidelines(TypedArray<int> p_guideline_columns);
501
TypedArray<int> get_line_length_guidelines() const;
502
503
/* Symbol lookup */
504
void set_symbol_lookup_on_click_enabled(bool p_enabled);
505
bool is_symbol_lookup_on_click_enabled() const;
506
507
String get_text_for_symbol_lookup() const;
508
String get_text_with_cursor_char(int p_line, int p_column) const;
509
String get_lookup_word(int p_line, int p_column) const;
510
511
void set_symbol_lookup_word_as_valid(bool p_valid);
512
513
/* Symbol tooltip */
514
void set_symbol_tooltip_on_hover_enabled(bool p_enabled);
515
bool is_symbol_tooltip_on_hover_enabled() const;
516
517
/* Text manipulation */
518
void move_lines_up();
519
void move_lines_down();
520
void delete_lines();
521
void duplicate_selection();
522
void duplicate_lines();
523
524
CodeEdit();
525
~CodeEdit();
526
};
527
528
VARIANT_ENUM_CAST(CodeEdit::CodeCompletionKind);
529
VARIANT_ENUM_CAST(CodeEdit::CodeCompletionLocation);
530
531
// The custom comparer which will sort completion options.
532
struct CodeCompletionOptionCompare {
533
_FORCE_INLINE_ bool operator()(const ScriptLanguage::CodeCompletionOption &l, const ScriptLanguage::CodeCompletionOption &r) const;
534
};
535
536