Debian Codegraph @

scene/gui/code_edit.h

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