Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/editor/script/script_editor_base.cpp
21022 views
1
/**************************************************************************/
2
/* script_editor_base.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 "script_editor_base.h"
32
33
#include "core/io/json.h"
34
#include "editor/editor_node.h"
35
#include "editor/script/script_editor_plugin.h"
36
#include "editor/script/syntax_highlighters.h"
37
#include "editor/settings/editor_settings.h"
38
#include "scene/gui/menu_button.h"
39
#include "scene/gui/rich_text_label.h"
40
#include "scene/gui/split_container.h"
41
42
void ScriptEditorBase::_bind_methods() {
43
ADD_SIGNAL(MethodInfo("name_changed"));
44
45
// First use in TextEditorBase.
46
ADD_SIGNAL(MethodInfo("edited_script_changed"));
47
ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text")));
48
ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &ScriptEditorBase::add_syntax_highlighter);
49
ClassDB::bind_method(D_METHOD("get_base_editor"), &ScriptEditorBase::get_base_editor);
50
51
// First use in ScriptTextEditor.
52
ADD_SIGNAL(MethodInfo("request_save_history"));
53
ADD_SIGNAL(MethodInfo("request_help", PropertyInfo(Variant::STRING, "topic")));
54
ADD_SIGNAL(MethodInfo("request_open_script_at_line", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::INT, "line")));
55
ADD_SIGNAL(MethodInfo("go_to_help", PropertyInfo(Variant::STRING, "what")));
56
ADD_SIGNAL(MethodInfo("request_save_previous_state", PropertyInfo(Variant::DICTIONARY, "state")));
57
ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text")));
58
ADD_SIGNAL(MethodInfo("go_to_method", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::STRING, "method")));
59
}
60
61
String ScriptEditorBase::get_name() {
62
String name;
63
64
name = edited_res->get_path().get_file();
65
if (name.is_empty()) {
66
// This appears for newly created built-in text_files before saving the scene.
67
name = TTR("[unsaved]");
68
} else if (edited_res->is_built_in()) {
69
const String &text_file_name = edited_res->get_name();
70
if (!text_file_name.is_empty()) {
71
// If the built-in text_file has a custom resource name defined,
72
// display the built-in text_file name as follows: `ResourceName (scene_file.tscn)`
73
name = vformat("%s (%s)", text_file_name, name.get_slice("::", 0));
74
}
75
}
76
77
if (is_unsaved()) {
78
name += "(*)";
79
}
80
81
return name;
82
}
83
84
Ref<Texture2D> ScriptEditorBase::get_theme_icon() {
85
return EditorNode::get_singleton()->get_object_icon(edited_res.ptr());
86
}
87
88
void ScriptEditorBase::tag_saved_version() {
89
edited_file_data.last_modified_time = FileAccess::get_modified_time(edited_file_data.path);
90
}
91
92
//// TextEditorBase
93
94
TextEditorBase *TextEditorBase::EditMenus::_get_active_editor() {
95
return Object::cast_to<TextEditorBase>(ScriptEditor::get_singleton()->get_current_editor());
96
}
97
98
void TextEditorBase::EditMenus::_edit_option(int p_op) {
99
TextEditorBase *script_text_editor = _get_active_editor();
100
ERR_FAIL_NULL(script_text_editor);
101
script_text_editor->_edit_option(p_op);
102
}
103
104
void TextEditorBase::EditMenus::_prepare_edit_menu() {
105
TextEditorBase *script_text_editor = _get_active_editor();
106
ERR_FAIL_NULL(script_text_editor);
107
const CodeEdit *tx = script_text_editor->code_editor->get_text_editor();
108
PopupMenu *popup = edit_menu->get_popup();
109
popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
110
popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
111
}
112
113
void TextEditorBase::EditMenus::_update_highlighter_menu() {
114
TextEditorBase *script_text_editor = _get_active_editor();
115
ERR_FAIL_NULL(script_text_editor);
116
117
Ref<EditorSyntaxHighlighter> current_highlighter = script_text_editor->get_code_editor()->get_text_editor()->get_syntax_highlighter();
118
highlighter_menu->clear();
119
for (const Ref<EditorSyntaxHighlighter> &highlighter : script_text_editor->highlighters) {
120
highlighter_menu->add_radio_check_item(highlighter->_get_name());
121
highlighter_menu->set_item_checked(-1, highlighter == current_highlighter);
122
}
123
}
124
125
void TextEditorBase::EditMenus::_change_syntax_highlighter(int p_idx) {
126
TextEditorBase *script_text_editor = _get_active_editor();
127
ERR_FAIL_NULL(script_text_editor);
128
ERR_FAIL_INDEX(p_idx, (int)script_text_editor->highlighters.size());
129
script_text_editor->set_syntax_highlighter(script_text_editor->highlighters[p_idx]);
130
}
131
132
void TextEditorBase::EditMenus::_update_bookmark_list() {
133
TextEditorBase *script_text_editor = _get_active_editor();
134
ERR_FAIL_NULL(script_text_editor);
135
bookmarks_menu->clear();
136
bookmarks_menu->reset_size();
137
138
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
139
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
140
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
141
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
142
143
PackedInt32Array bookmark_list = script_text_editor->code_editor->get_text_editor()->get_bookmarked_lines();
144
if (bookmark_list.is_empty()) {
145
return;
146
}
147
148
bookmarks_menu->add_separator();
149
150
for (int32_t bookmark : bookmark_list) {
151
// Strip edges to remove spaces or tabs.
152
// Also replace any tabs by spaces, since we can't print tabs in the menu.
153
String line = script_text_editor->code_editor->get_text_editor()->get_line(bookmark).replace("\t", " ").strip_edges();
154
155
// Limit the size of the line if too big.
156
if (line.length() > 50) {
157
line = line.substr(0, 50);
158
}
159
160
bookmarks_menu->add_item(String::num_int64(bookmark + 1) + " - `" + line + "`");
161
bookmarks_menu->set_item_metadata(-1, bookmark);
162
}
163
}
164
165
void TextEditorBase::EditMenus::_bookmark_item_pressed(int p_idx) {
166
TextEditorBase *script_text_editor = _get_active_editor();
167
ERR_FAIL_NULL(script_text_editor);
168
if (p_idx < 4) { // Any item before the separator.
169
script_text_editor->_edit_option(bookmarks_menu->get_item_id(p_idx));
170
} else {
171
script_text_editor->code_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));
172
}
173
}
174
175
TextEditorBase::EditMenus::EditMenus() {
176
edit_menu = memnew(MenuButton);
177
edit_menu->set_flat(false);
178
edit_menu->set_theme_type_variation("FlatMenuButton");
179
edit_menu->set_text(TTRC("Edit"));
180
edit_menu->set_switch_on_hover(true);
181
182
add_child(edit_menu);
183
edit_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_prepare_edit_menu));
184
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
185
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
186
edit_menu->get_popup()->add_separator();
187
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
188
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
189
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
190
edit_menu->get_popup()->add_separator();
191
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
192
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
193
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);
194
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);
195
edit_menu->get_popup()->add_separator();
196
{
197
edit_menu_line = memnew(PopupMenu);
198
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
199
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
200
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
201
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
202
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
203
edit_menu_line->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
204
edit_menu->get_popup()->add_submenu_node_item(TTRC("Line"), edit_menu_line);
205
}
206
{
207
edit_menu_fold = memnew(PopupMenu);
208
edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
209
edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
210
edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
211
edit_menu_fold->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
212
edit_menu->get_popup()->add_submenu_node_item(TTRC("Folding"), edit_menu_fold);
213
}
214
edit_menu->get_popup()->add_separator();
215
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
216
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_final_newlines"), EDIT_TRIM_FINAL_NEWLINES);
217
{
218
edit_menu_convert_indent = memnew(PopupMenu);
219
edit_menu_convert_indent->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
220
edit_menu_convert_indent->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
221
edit_menu_convert_indent->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
222
edit_menu->get_popup()->add_submenu_node_item(TTRC("Indentation"), edit_menu_convert_indent);
223
}
224
edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
225
edit_menu->get_popup()->add_separator();
226
{
227
edit_menu_convert = memnew(PopupMenu);
228
edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
229
edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
230
edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/capitalize"), EDIT_CAPITALIZE);
231
edit_menu_convert->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
232
edit_menu->get_popup()->add_submenu_node_item(TTRC("Convert Case"), edit_menu_convert);
233
}
234
highlighter_menu = memnew(PopupMenu);
235
edit_menu->get_popup()->add_submenu_node_item(TTRC("Syntax Highlighter"), highlighter_menu);
236
237
highlighter_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_highlighter_menu));
238
highlighter_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_change_syntax_highlighter));
239
240
search_menu = memnew(MenuButton);
241
search_menu->set_flat(false);
242
search_menu->set_theme_type_variation("FlatMenuButton");
243
search_menu->set_text(TTRC("Search"));
244
search_menu->set_switch_on_hover(true);
245
246
add_child(search_menu);
247
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
248
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
249
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
250
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
251
search_menu->get_popup()->add_separator();
252
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("editor/find_in_files"), SEARCH_IN_FILES);
253
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);
254
search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
255
256
goto_menu = memnew(MenuButton);
257
goto_menu->set_flat(false);
258
goto_menu->set_theme_type_variation("FlatMenuButton");
259
goto_menu->set_text(TTRC("Go To"));
260
goto_menu->set_switch_on_hover(true);
261
add_child(goto_menu);
262
goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
263
goto_menu->get_popup()->add_separator();
264
265
bookmarks_menu = memnew(PopupMenu);
266
goto_menu->get_popup()->add_submenu_node_item(TTRC("Bookmarks"), bookmarks_menu);
267
bookmarks_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_bookmark_list));
268
bookmarks_menu->connect("index_pressed", callable_mp(this, &EditMenus::_bookmark_item_pressed));
269
270
goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
271
}
272
273
void TextEditorBase::_make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position, bool p_show) {
274
context_menu->clear();
275
if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
276
context_menu->add_item(TTRC("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);
277
context_menu->add_separator();
278
}
279
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
280
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
281
context_menu->add_separator();
282
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
283
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
284
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
285
context_menu->add_separator();
286
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
287
context_menu->add_separator();
288
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
289
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
290
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
291
292
if (p_selection) {
293
context_menu->add_separator();
294
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
295
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
296
}
297
298
if (p_foldable) {
299
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
300
}
301
302
if (p_show) {
303
_show_context_menu(p_position);
304
}
305
}
306
307
void TextEditorBase::_show_context_menu(const Vector2 &p_position) {
308
const CodeEdit *tx = code_editor->get_text_editor();
309
context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
310
context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
311
312
context_menu->set_position(get_screen_position() + p_position);
313
context_menu->reset_size();
314
context_menu->popup();
315
}
316
317
void TextEditorBase::_text_edit_gui_input(const Ref<InputEvent> &p_ev) {
318
Ref<InputEventMouseButton> mb = p_ev;
319
320
if (mb.is_valid()) {
321
if (mb->get_button_index() == MouseButton::RIGHT) {
322
CodeEdit *tx = code_editor->get_text_editor();
323
324
tx->apply_ime();
325
326
Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
327
int row = pos.y;
328
int col = pos.x;
329
330
tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
331
332
if (tx->is_move_caret_on_right_click_enabled()) {
333
tx->remove_secondary_carets();
334
if (tx->has_selection()) {
335
int from_line = tx->get_selection_from_line();
336
int to_line = tx->get_selection_to_line();
337
int from_column = tx->get_selection_from_column();
338
int to_column = tx->get_selection_to_column();
339
340
if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
341
// Right click is outside the selected text.
342
tx->deselect();
343
}
344
}
345
if (!tx->has_selection()) {
346
tx->set_caret_line(row, true, false, -1);
347
tx->set_caret_column(col);
348
}
349
}
350
351
if (!mb->is_pressed()) {
352
bool can_fold = tx->can_fold_line(row);
353
bool is_folded = tx->is_line_folded(row);
354
_make_context_menu(tx->has_selection(), can_fold || is_folded, get_local_mouse_position());
355
}
356
}
357
}
358
359
Ref<InputEventKey> k = p_ev;
360
if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {
361
CodeEdit *tx = code_editor->get_text_editor();
362
int line = tx->get_caret_line();
363
tx->adjust_viewport_to_caret();
364
bool can_fold = tx->can_fold_line(line);
365
bool is_folded = tx->is_line_folded(line);
366
_make_context_menu(tx->has_selection(0), can_fold || is_folded, (get_global_transform().inverse() * tx->get_global_transform()).xform(tx->get_caret_draw_pos(0)));
367
context_menu->grab_focus();
368
}
369
}
370
371
bool TextEditorBase::_edit_option(int p_op) {
372
CodeEdit *tx = code_editor->get_text_editor();
373
tx->apply_ime();
374
375
switch (p_op) {
376
case EDIT_UNDO: {
377
tx->undo();
378
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
379
} break;
380
case EDIT_REDO: {
381
tx->redo();
382
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
383
} break;
384
case EDIT_CUT: {
385
tx->cut();
386
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
387
} break;
388
case EDIT_COPY: {
389
tx->copy();
390
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
391
} break;
392
case EDIT_PASTE: {
393
tx->paste();
394
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
395
} break;
396
case EDIT_SELECT_ALL: {
397
tx->select_all();
398
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
399
} break;
400
case EDIT_MOVE_LINE_UP: {
401
tx->move_lines_up();
402
} break;
403
case EDIT_MOVE_LINE_DOWN: {
404
tx->move_lines_down();
405
} break;
406
case EDIT_INDENT: {
407
tx->indent_lines();
408
} break;
409
case EDIT_UNINDENT: {
410
tx->unindent_lines();
411
} break;
412
case EDIT_DELETE_LINE: {
413
tx->delete_lines();
414
} break;
415
case EDIT_DUPLICATE_SELECTION: {
416
tx->duplicate_selection();
417
} break;
418
case EDIT_DUPLICATE_LINES: {
419
tx->duplicate_lines();
420
} break;
421
case EDIT_TRIM_TRAILING_WHITESAPCE: {
422
trim_trailing_whitespace();
423
} break;
424
case EDIT_TRIM_FINAL_NEWLINES: {
425
trim_final_newlines();
426
} break;
427
case EDIT_CONVERT_INDENT_TO_SPACES: {
428
code_editor->set_indent_using_spaces(true);
429
convert_indent();
430
} break;
431
case EDIT_CONVERT_INDENT_TO_TABS: {
432
code_editor->set_indent_using_spaces(false);
433
convert_indent();
434
} break;
435
case EDIT_TO_UPPERCASE: {
436
_convert_case(CodeTextEditor::UPPER);
437
} break;
438
case EDIT_TO_LOWERCASE: {
439
_convert_case(CodeTextEditor::LOWER);
440
} break;
441
case EDIT_CAPITALIZE: {
442
_convert_case(CodeTextEditor::CAPITALIZE);
443
} break;
444
case EDIT_TOGGLE_WORD_WRAP: {
445
TextEdit::LineWrappingMode wrap = tx->get_line_wrapping_mode();
446
tx->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);
447
} break;
448
case SEARCH_FIND: {
449
code_editor->get_find_replace_bar()->popup_search();
450
} break;
451
case SEARCH_FIND_NEXT: {
452
code_editor->get_find_replace_bar()->search_next();
453
} break;
454
case SEARCH_FIND_PREV: {
455
code_editor->get_find_replace_bar()->search_prev();
456
} break;
457
case SEARCH_REPLACE: {
458
code_editor->get_find_replace_bar()->popup_replace();
459
} break;
460
case SEARCH_IN_FILES: {
461
String selected_text = tx->get_selected_text();
462
463
// Yep, because it doesn't make sense to instance this dialog for every single script open...
464
// So this will be delegated to the ScriptEditor.
465
emit_signal(SNAME("search_in_files_requested"), selected_text);
466
} break;
467
case REPLACE_IN_FILES: {
468
String selected_text = tx->get_selected_text();
469
emit_signal(SNAME("replace_in_files_requested"), selected_text);
470
} break;
471
case SEARCH_GOTO_LINE: {
472
goto_line_popup->popup_find_line(code_editor);
473
} break;
474
case BOOKMARK_TOGGLE: {
475
code_editor->toggle_bookmark();
476
} break;
477
case BOOKMARK_GOTO_NEXT: {
478
code_editor->goto_next_bookmark();
479
} break;
480
case BOOKMARK_GOTO_PREV: {
481
code_editor->goto_prev_bookmark();
482
} break;
483
case BOOKMARK_REMOVE_ALL: {
484
code_editor->remove_all_bookmarks();
485
} break;
486
case EDIT_EMOJI_AND_SYMBOL: {
487
tx->show_emoji_and_symbol_picker();
488
} break;
489
case EDIT_TOGGLE_FOLD_LINE: {
490
tx->toggle_foldable_lines_at_carets();
491
} break;
492
case EDIT_FOLD_ALL_LINES: {
493
tx->fold_all_lines();
494
} break;
495
case EDIT_UNFOLD_ALL_LINES: {
496
tx->unfold_all_lines();
497
} break;
498
default: {
499
return false;
500
}
501
}
502
return true;
503
}
504
505
void TextEditorBase::_load_theme_settings() {
506
code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
507
}
508
509
void TextEditorBase::_validate_script() {
510
emit_signal(SNAME("name_changed"));
511
emit_signal(SNAME("edited_script_changed"));
512
}
513
514
void TextEditorBase::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
515
ERR_FAIL_COND(p_highlighter.is_null());
516
517
highlighters.push_back(p_highlighter);
518
}
519
520
void TextEditorBase::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
521
ERR_FAIL_COND(p_highlighter.is_null());
522
523
CodeEdit *te = code_editor->get_text_editor();
524
p_highlighter->_set_edited_resource(edited_res);
525
te->set_syntax_highlighter(p_highlighter);
526
}
527
528
void TextEditorBase::set_edited_resource(const Ref<Resource> &p_res) {
529
ERR_FAIL_COND(edited_res.is_valid());
530
ERR_FAIL_COND(p_res.is_null());
531
532
edited_res = p_res;
533
534
Ref<TextFile> text_file = edited_res;
535
String text;
536
if (text_file.is_valid()) {
537
text = text_file->get_text();
538
}
539
540
Ref<JSON> json_file = edited_res;
541
if (json_file.is_valid()) {
542
text = json_file->get_parsed_text();
543
}
544
545
code_editor->get_text_editor()->set_text(text);
546
code_editor->get_text_editor()->clear_undo_history();
547
code_editor->get_text_editor()->tag_saved_version();
548
549
emit_signal(SNAME("name_changed"));
550
code_editor->update_line_and_column();
551
}
552
553
bool TextEditorBase::is_unsaved() {
554
return code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() || edited_res->get_path().is_empty(); // In memory.
555
}
556
557
void TextEditorBase::tag_saved_version() {
558
code_editor->get_text_editor()->tag_saved_version();
559
ScriptEditorBase::tag_saved_version();
560
}
561
562
void TextEditorBase::reload_text() {
563
ERR_FAIL_COND(edited_res.is_null());
564
565
CodeEdit *te = code_editor->get_text_editor();
566
int column = te->get_caret_column();
567
int row = te->get_caret_line();
568
int h = te->get_h_scroll();
569
int v = te->get_v_scroll();
570
571
Ref<TextFile> text_file = edited_res;
572
if (text_file.is_valid()) {
573
te->set_text(text_file->get_text());
574
}
575
576
Ref<JSON> json_file = edited_res;
577
if (json_file.is_valid()) {
578
te->set_text(json_file->get_parsed_text());
579
}
580
581
Ref<Script> script = edited_res;
582
if (script.is_valid()) {
583
te->set_text(script->get_source_code());
584
}
585
586
te->set_caret_line(row);
587
te->set_caret_column(column);
588
te->set_h_scroll(h);
589
te->set_v_scroll(v);
590
591
te->tag_saved_version();
592
593
code_editor->update_line_and_column();
594
if (editor_enabled) {
595
_validate_script();
596
}
597
}
598
599
void TextEditorBase::enable_editor() {
600
if (editor_enabled) {
601
return;
602
}
603
604
editor_enabled = true;
605
606
_load_theme_settings();
607
608
_validate_script();
609
}
610
611
void TextEditorBase::set_tooltip_request_func(const Callable &p_toolip_callback) {
612
Variant args[1] = { this };
613
const Variant *argp[] = { &args[0] };
614
code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
615
}
616
617
void TextEditorBase::set_edit_state(const Variant &p_state) {
618
code_editor->set_edit_state(p_state);
619
620
Dictionary state = p_state;
621
if (state.has("syntax_highlighter")) {
622
for (const Ref<EditorSyntaxHighlighter> &highlighter : highlighters) {
623
if (highlighter->_get_name() == String(state["syntax_highlighter"])) {
624
set_syntax_highlighter(highlighter);
625
break;
626
}
627
}
628
}
629
630
ensure_focus();
631
}
632
633
TextEditorBase::TextEditorBase() {
634
code_editor = memnew(CodeTextEditor);
635
code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
636
code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
637
code_editor->show_toggle_files_button();
638
code_editor->get_text_editor()->set_context_menu_enabled(false);
639
code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &TextEditorBase::_text_edit_gui_input));
640
code_editor->connect("validate_script", callable_mp(this, &TextEditorBase::_validate_script));
641
code_editor->connect("load_theme_settings", callable_mp(this, &TextEditorBase::_load_theme_settings));
642
643
context_menu = memnew(PopupMenu);
644
context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextEditorBase::_edit_option));
645
add_child(context_menu);
646
647
edit_hb = memnew(HBoxContainer);
648
649
goto_line_popup = memnew(GotoLinePopup);
650
add_child(goto_line_popup);
651
652
Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
653
plain_highlighter.instantiate();
654
add_syntax_highlighter(plain_highlighter);
655
656
Ref<EditorStandardSyntaxHighlighter> highlighter;
657
highlighter.instantiate();
658
add_syntax_highlighter(highlighter);
659
set_syntax_highlighter(plain_highlighter);
660
661
update_settings();
662
}
663
664
TextEditorBase::~TextEditorBase() {
665
highlighters.clear();
666
}
667
668
//// CodeEditorBase
669
670
bool CodeEditorBase::_warning_clicked(const Variant &p_line) {
671
if (p_line.get_type() == Variant::INT) {
672
goto_line_centered(p_line.operator int64_t());
673
return true;
674
}
675
return false;
676
}
677
678
CodeEditorBase::EditMenusCEB::EditMenusCEB() {
679
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
680
_popup_move_item(EDIT_TRIM_TRAILING_WHITESAPCE, edit_menu->get_popup(), false);
681
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
682
}
683
684
CodeEditorBase::CodeEditorBase() {
685
warnings_panel = memnew(RichTextLabel);
686
warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
687
warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
688
warnings_panel->set_meta_underline(true);
689
warnings_panel->set_selection_enabled(true);
690
warnings_panel->set_context_menu_enabled(true);
691
warnings_panel->set_focus_mode(FOCUS_CLICK);
692
warnings_panel->hide();
693
warnings_panel->connect("meta_clicked", callable_mp(this, &CodeEditorBase::_warning_clicked));
694
695
editor_box = memnew(VSplitContainer);
696
add_child(editor_box);
697
editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
698
editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
699
editor_box->add_child(code_editor);
700
editor_box->add_child(warnings_panel);
701
}
702
703