Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/gui/code_editor.h
9903 views
1
/**************************************************************************/
2
/* code_editor.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 "scene/gui/box_container.h"
34
#include "scene/gui/code_edit.h"
35
#include "scene/gui/dialogs.h"
36
37
class Button;
38
class CheckBox;
39
class CodeTextEditor;
40
class Label;
41
class LineEdit;
42
class MenuButton;
43
class RichTextLabel;
44
class Timer;
45
46
class GotoLinePopup : public PopupPanel {
47
GDCLASS(GotoLinePopup, PopupPanel);
48
49
Variant original_state;
50
LineEdit *line_input = nullptr;
51
CodeTextEditor *text_editor = nullptr;
52
53
void _goto_line();
54
void _submit();
55
56
protected:
57
void _notification(int p_what);
58
virtual void _input_from_window(const Ref<InputEvent> &p_event) override;
59
60
public:
61
void popup_find_line(CodeTextEditor *p_text_editor);
62
63
GotoLinePopup();
64
};
65
66
class FindReplaceBar : public HBoxContainer {
67
GDCLASS(FindReplaceBar, HBoxContainer);
68
69
enum SearchMode {
70
SEARCH_CURRENT,
71
SEARCH_NEXT,
72
SEARCH_PREV,
73
};
74
75
Button *toggle_replace_button = nullptr;
76
LineEdit *search_text = nullptr;
77
Label *matches_label = nullptr;
78
Button *find_prev = nullptr;
79
Button *find_next = nullptr;
80
CheckBox *case_sensitive = nullptr;
81
CheckBox *whole_words = nullptr;
82
Button *hide_button = nullptr;
83
84
LineEdit *replace_text = nullptr;
85
Button *replace = nullptr;
86
Button *replace_all = nullptr;
87
CheckBox *selection_only = nullptr;
88
89
HBoxContainer *hbc_button_replace = nullptr;
90
HBoxContainer *hbc_option_replace = nullptr;
91
92
CodeTextEditor *base_text_editor = nullptr;
93
CodeEdit *text_editor = nullptr;
94
95
uint32_t flags = 0;
96
97
int result_line = 0;
98
int result_col = 0;
99
int results_count = -1;
100
int results_count_to_current = -1;
101
102
bool replace_all_mode = false;
103
bool preserve_cursor = false;
104
105
virtual void input(const Ref<InputEvent> &p_event) override;
106
107
void _get_search_from(int &r_line, int &r_col, SearchMode p_search_mode);
108
void _update_results_count();
109
void _update_matches_display();
110
111
void _show_search(bool p_with_replace, bool p_show_only);
112
void _hide_bar();
113
void _update_toggle_replace_button(bool p_replace_visible);
114
115
void _editor_text_changed();
116
void _search_options_changed(bool p_pressed);
117
void _search_text_changed(const String &p_text);
118
void _search_text_submitted(const String &p_text);
119
void _replace_text_submitted(const String &p_text);
120
void _toggle_replace_pressed();
121
122
protected:
123
void _notification(int p_what);
124
125
void _update_flags(bool p_direction_backwards);
126
127
bool _search(uint32_t p_flags, int p_from_line, int p_from_col);
128
129
void _replace();
130
void _replace_all();
131
132
static void _bind_methods();
133
134
public:
135
String get_search_text() const;
136
String get_replace_text() const;
137
138
bool is_case_sensitive() const;
139
bool is_whole_words() const;
140
bool is_selection_only() const;
141
142
void set_text_edit(CodeTextEditor *p_text_editor);
143
144
void popup_search(bool p_show_only = false);
145
void popup_replace();
146
147
bool search_current();
148
bool search_prev();
149
bool search_next();
150
151
bool needs_to_count_results = true;
152
bool line_col_changed_for_result = false;
153
154
FindReplaceBar();
155
};
156
157
typedef void (*CodeTextEditorCodeCompleteFunc)(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced);
158
159
class CodeTextEditor : public VBoxContainer {
160
GDCLASS(CodeTextEditor, VBoxContainer);
161
162
CodeEdit *text_editor = nullptr;
163
FindReplaceBar *find_replace_bar = nullptr;
164
HBoxContainer *status_bar = nullptr;
165
166
Button *toggle_files_button = nullptr;
167
Control *toggle_files_list = nullptr;
168
Button *error_button = nullptr;
169
Button *warning_button = nullptr;
170
171
MenuButton *zoom_button = nullptr;
172
Label *line_and_col_txt = nullptr;
173
Label *indentation_txt = nullptr;
174
175
Label *info = nullptr;
176
Timer *idle = nullptr;
177
float idle_time = 0.0f;
178
float idle_time_with_errors = 0.0f;
179
bool code_complete_enabled = true;
180
Timer *code_complete_timer = nullptr;
181
int code_complete_timer_line = 0;
182
183
float zoom_factor = 1.0f;
184
185
RichTextLabel *error = nullptr;
186
int error_line;
187
int error_column;
188
189
bool preview_navigation_change = false;
190
Dictionary previous_state;
191
192
void _update_text_editor_theme();
193
void _update_font_ligatures();
194
void _complete_request();
195
Ref<Texture2D> _get_completion_icon(const ScriptLanguage::CodeCompletionOption &p_option);
196
197
virtual void input(const Ref<InputEvent> &event) override;
198
void _text_editor_gui_input(const Ref<InputEvent> &p_event);
199
200
Color completion_font_color;
201
Color completion_string_color;
202
Color completion_string_name_color;
203
Color completion_node_path_color;
204
Color completion_comment_color;
205
Color completion_doc_comment_color;
206
CodeTextEditorCodeCompleteFunc code_complete_func;
207
void *code_complete_ud = nullptr;
208
209
void _zoom_in();
210
void _zoom_out();
211
void _zoom_to(float p_zoom_factor);
212
213
void _update_error_content_height();
214
215
void _error_button_pressed();
216
void _warning_button_pressed();
217
void _set_show_errors_panel(bool p_show);
218
void _set_show_warnings_panel(bool p_show);
219
void _error_pressed(const Ref<InputEvent> &p_event);
220
221
void _zoom_popup_id_pressed(int p_idx);
222
223
void _toggle_files_pressed();
224
225
protected:
226
virtual void _load_theme_settings() {}
227
virtual void _validate_script() {}
228
virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options) {}
229
230
void _text_changed_idle_timeout();
231
void _code_complete_timer_timeout();
232
void _text_changed();
233
void _line_col_changed();
234
void _notification(int);
235
static void _bind_methods();
236
237
bool is_warnings_panel_opened = false;
238
bool is_errors_panel_opened = false;
239
240
public:
241
void trim_trailing_whitespace();
242
void trim_final_newlines();
243
void insert_final_newline();
244
245
enum CaseStyle {
246
UPPER,
247
LOWER,
248
CAPITALIZE,
249
};
250
void convert_case(CaseStyle p_case);
251
252
void set_indent_using_spaces(bool p_use_spaces);
253
254
/// Toggle inline comment on currently selected lines, or on current line if nothing is selected,
255
/// by adding or removing comment delimiter
256
void toggle_inline_comment(const String &delimiter);
257
258
void goto_line(int p_line, int p_column = 0);
259
void goto_line_selection(int p_line, int p_begin, int p_end);
260
void goto_line_centered(int p_line, int p_column = 0);
261
void set_executing_line(int p_line);
262
void clear_executing_line();
263
264
Variant get_edit_state();
265
void set_edit_state(const Variant &p_state);
266
Variant get_navigation_state();
267
Variant get_previous_state();
268
void store_previous_state();
269
270
bool is_previewing_navigation_change() const;
271
void set_preview_navigation_change(bool p_preview);
272
273
void set_error_count(int p_error_count);
274
void set_warning_count(int p_warning_count);
275
276
void update_editor_settings();
277
void set_error(const String &p_error);
278
void set_error_pos(int p_line, int p_column);
279
Point2i get_error_pos() const;
280
void update_line_and_column() { _line_col_changed(); }
281
CodeEdit *get_text_editor() { return text_editor; }
282
FindReplaceBar *get_find_replace_bar() { return find_replace_bar; }
283
void set_find_replace_bar(FindReplaceBar *p_bar);
284
void remove_find_replace_bar();
285
virtual void apply_code() {}
286
virtual void goto_error();
287
288
void toggle_bookmark();
289
void goto_next_bookmark();
290
void goto_prev_bookmark();
291
void remove_all_bookmarks();
292
293
void set_zoom_factor(float p_zoom_factor);
294
float get_zoom_factor();
295
296
void set_code_complete_func(CodeTextEditorCodeCompleteFunc p_code_complete_func, void *p_ud);
297
298
void validate_script();
299
300
void set_toggle_list_control(Control *p_control);
301
void show_toggle_files_button();
302
void update_toggle_files_button();
303
304
CodeTextEditor();
305
};
306
307