Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/script/script_editor_base.h
20854 views
1
/**************************************************************************/
2
/* script_editor_base.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 "editor/gui/code_editor.h"
34
35
class EditorSyntaxHighlighter;
36
class MenuButton;
37
class VSplitContainer;
38
39
class ScriptEditorBase : public Control {
40
GDCLASS(ScriptEditorBase, Control);
41
42
protected:
43
Ref<Resource> edited_res;
44
45
static void _bind_methods();
46
47
public:
48
struct EditedFileData {
49
String path;
50
uint64_t last_modified_time = -1;
51
} edited_file_data;
52
53
virtual String get_name();
54
virtual Ref<Texture2D> get_theme_icon();
55
56
virtual void set_toggle_list_control(Control *p_toggle_list_control) = 0;
57
virtual void update_toggle_files_button() = 0;
58
59
virtual bool show_members_overview() { return false; }
60
61
virtual void set_edited_resource(const Ref<Resource> &p_res) = 0;
62
virtual Ref<Resource> get_edited_resource() const { return edited_res; }
63
64
virtual void apply_code() = 0;
65
virtual void validate_script() = 0;
66
virtual bool is_unsaved() = 0;
67
virtual void tag_saved_version();
68
69
virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {}
70
virtual Control *get_base_editor() const { return nullptr; }
71
};
72
73
typedef ScriptEditorBase *(*CreateScriptEditorFunc)(const Ref<Resource> &p_resource);
74
75
class TextEditorBase : public ScriptEditorBase {
76
GDCLASS(TextEditorBase, ScriptEditorBase);
77
78
void _post_init();
79
80
protected:
81
enum {
82
EDIT_UNDO,
83
EDIT_REDO,
84
EDIT_CUT,
85
EDIT_COPY,
86
EDIT_PASTE,
87
EDIT_SELECT_ALL,
88
EDIT_TRIM_TRAILING_WHITESAPCE,
89
EDIT_TRIM_FINAL_NEWLINES,
90
EDIT_CONVERT_INDENT_TO_SPACES,
91
EDIT_CONVERT_INDENT_TO_TABS,
92
EDIT_MOVE_LINE_UP,
93
EDIT_MOVE_LINE_DOWN,
94
EDIT_INDENT,
95
EDIT_UNINDENT,
96
EDIT_DELETE_LINE,
97
EDIT_DUPLICATE_SELECTION,
98
EDIT_DUPLICATE_LINES,
99
EDIT_TO_UPPERCASE,
100
EDIT_TO_LOWERCASE,
101
EDIT_CAPITALIZE,
102
EDIT_TOGGLE_FOLD_LINE,
103
EDIT_FOLD_ALL_LINES,
104
EDIT_TOGGLE_WORD_WRAP,
105
EDIT_UNFOLD_ALL_LINES,
106
EDIT_EMOJI_AND_SYMBOL,
107
108
SEARCH_FIND,
109
SEARCH_FIND_NEXT,
110
SEARCH_FIND_PREV,
111
SEARCH_REPLACE,
112
SEARCH_IN_FILES,
113
SEARCH_GOTO_LINE,
114
115
REPLACE_IN_FILES,
116
117
BOOKMARK_TOGGLE,
118
BOOKMARK_GOTO_NEXT,
119
BOOKMARK_GOTO_PREV,
120
BOOKMARK_REMOVE_ALL,
121
122
BASE_ENUM_COUNT,
123
};
124
125
class EditMenus : public HBoxContainer {
126
GDCLASS(EditMenus, HBoxContainer);
127
128
protected:
129
MenuButton *edit_menu = nullptr;
130
MenuButton *search_menu = nullptr;
131
MenuButton *goto_menu = nullptr;
132
PopupMenu *bookmarks_menu = nullptr;
133
PopupMenu *highlighter_menu = nullptr;
134
135
PopupMenu *edit_menu_line = nullptr;
136
PopupMenu *edit_menu_fold = nullptr;
137
PopupMenu *edit_menu_convert_indent = nullptr;
138
PopupMenu *edit_menu_convert = nullptr;
139
140
TextEditorBase *_get_active_editor();
141
void _edit_option(int p_op);
142
void _prepare_edit_menu();
143
void _update_highlighter_menu();
144
void _change_syntax_highlighter(int p_idx);
145
void _update_bookmark_list();
146
void _bookmark_item_pressed(int p_idx);
147
148
public:
149
EditMenus();
150
};
151
152
static void _popup_move_item(int p_target_id, PopupMenu *r_popup, bool p_move_after = true, int p_idx = -1) {
153
int target_idx = r_popup->get_item_index(p_target_id) + p_move_after;
154
if (target_idx >= 0 && target_idx < r_popup->get_item_count()) {
155
r_popup->set_item_index(p_idx, target_idx);
156
}
157
}
158
159
static inline EditMenus *edit_menus = nullptr;
160
161
bool editor_enabled = false;
162
CodeTextEditor *code_editor = nullptr;
163
HBoxContainer *edit_hb = nullptr;
164
165
GotoLinePopup *goto_line_popup = nullptr;
166
167
LocalVector<Ref<EditorSyntaxHighlighter>> highlighters;
168
169
PopupMenu *context_menu = nullptr;
170
MenuButton *search_menu = nullptr;
171
172
void _make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position = Vector2(0, 0), bool p_show = true);
173
void _show_context_menu(const Vector2 &p_position);
174
175
virtual void _text_edit_gui_input(const Ref<InputEvent> &p_ev);
176
virtual bool _edit_option(int p_op);
177
178
virtual void _load_theme_settings();
179
virtual void _validate_script();
180
181
void _convert_case(CodeTextEditor::CaseStyle p_case) { code_editor->convert_case(p_case); }
182
183
public:
184
virtual void add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) override;
185
virtual void set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter);
186
187
virtual void set_edited_resource(const Ref<Resource> &p_res) override;
188
189
virtual bool is_unsaved() override;
190
virtual void tag_saved_version() override;
191
192
virtual void reload_text();
193
virtual void enable_editor();
194
195
virtual Control *get_edit_menu() = 0;
196
197
virtual Control *get_base_editor() const override { return code_editor->get_text_editor(); }
198
virtual CodeTextEditor *get_code_editor() const { return code_editor; }
199
200
virtual void set_tooltip_request_func(const Callable &p_toolip_callback);
201
202
virtual void ensure_focus() { code_editor->get_text_editor()->grab_focus(); }
203
virtual void convert_indent() { code_editor->get_text_editor()->convert_indent(); }
204
205
virtual void trim_trailing_whitespace() { code_editor->trim_trailing_whitespace(); }
206
virtual void trim_final_newlines() { code_editor->trim_final_newlines(); }
207
virtual void insert_final_newline() { code_editor->insert_final_newline(); }
208
209
virtual void goto_line(int p_line, int p_column = 0) { code_editor->goto_line(p_line, p_column); }
210
virtual void goto_line_selection(int p_line, int p_begin, int p_end) { code_editor->goto_line_selection(p_line, p_begin, p_end); }
211
virtual void goto_line_centered(int p_line, int p_column = 0) { code_editor->goto_line_centered(p_line, p_column); }
212
virtual void set_executing_line(int p_line) { code_editor->set_executing_line(p_line); }
213
virtual void clear_executing_line() { code_editor->clear_executing_line(); }
214
215
virtual Variant get_edit_state() { return code_editor->get_edit_state(); }
216
virtual void set_edit_state(const Variant &p_state);
217
virtual Variant get_navigation_state() { return code_editor->get_navigation_state(); }
218
219
virtual void update_settings() { code_editor->update_editor_settings(); }
220
virtual void set_find_replace_bar(FindReplaceBar *p_bar) { code_editor->set_find_replace_bar(p_bar); }
221
222
virtual void validate_script() override { code_editor->validate_script(); }
223
224
virtual void set_toggle_list_control(Control *p_toggle_list_control) override {
225
code_editor->set_toggle_list_control(p_toggle_list_control);
226
}
227
virtual void update_toggle_files_button() override { code_editor->update_toggle_files_button(); }
228
229
TextEditorBase();
230
~TextEditorBase();
231
};
232
233
class CodeEditorBase : public TextEditorBase {
234
GDCLASS(CodeEditorBase, TextEditorBase);
235
236
protected:
237
enum {
238
EDIT_COMPLETE = BASE_ENUM_COUNT,
239
EDIT_TOGGLE_COMMENT,
240
241
CODE_ENUM_COUNT,
242
};
243
244
class EditMenusCEB : public EditMenus {
245
GDCLASS(EditMenusCEB, EditMenus);
246
247
public:
248
EditMenusCEB();
249
};
250
251
VSplitContainer *editor_box = nullptr;
252
RichTextLabel *warnings_panel = nullptr;
253
254
virtual void _code_complete_script(const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) = 0;
255
virtual bool _warning_clicked(const Variant &p_line);
256
257
public:
258
virtual bool show_members_overview() override { return true; }
259
260
virtual Vector<String> get_functions() { return Vector<String>(); }
261
262
virtual PackedInt32Array get_breakpoints() { return PackedInt32Array(); }
263
virtual void set_breakpoint(int p_line, bool p_enabled) {}
264
virtual void clear_breakpoints() {}
265
266
CodeEditorBase();
267
};
268
269