-
-
Notifications
You must be signed in to change notification settings - Fork 26.4k
Expand file tree
/
Copy pathscript_editor_base.cpp
More file actions
970 lines (842 loc) · 35.8 KB
/
Copy pathscript_editor_base.cpp
File metadata and controls
970 lines (842 loc) · 35.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
/**************************************************************************/
/* script_editor_base.cpp */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
#include "script_editor_base.h"
#include "core/io/json.h"
#include "core/object/callable_mp.h"
#include "core/object/class_db.h"
#include "editor/editor_node.h"
#include "editor/script/find_in_files.h"
#include "editor/script/script_editor_navigation_marker.h"
#include "editor/script/script_editor_plugin.h"
#include "editor/script/syntax_highlighters.h"
#include "editor/settings/editor_settings.h"
#include "scene/gui/menu_button.h"
#include "scene/gui/rich_text_label.h"
#include "scene/gui/split_container.h"
#include "scene/resources/shader.h"
#include "scene/resources/shader_include.h"
#include "servers/display/display_server.h"
void ScriptEditorBase::_bind_methods() {
ADD_SIGNAL(MethodInfo("name_changed"));
// First use in TextEditorBase.
ADD_SIGNAL(MethodInfo("_validation_updated"));
ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text")));
ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &ScriptEditorBase::add_syntax_highlighter);
ClassDB::bind_method(D_METHOD("get_base_editor"), &ScriptEditorBase::get_base_editor);
// First use in ScriptTextEditor.
ADD_SIGNAL(MethodInfo("_request_save_new_history", PropertyInfo(Variant::DICTIONARY, "state")));
ADD_SIGNAL(MethodInfo("_request_save_previous_state", PropertyInfo(Variant::DICTIONARY, "state")));
ADD_SIGNAL(MethodInfo("request_help", PropertyInfo(Variant::STRING, "topic")));
ADD_SIGNAL(MethodInfo("request_open_script_at_line", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::INT, "line")));
ADD_SIGNAL(MethodInfo("go_to_help", PropertyInfo(Variant::STRING, "what")));
ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text")));
ADD_SIGNAL(MethodInfo("go_to_method", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::STRING, "method")));
#ifndef DISABLE_DEPRECATED
ADD_SIGNAL(MethodInfo("request_save_history"));
ADD_SIGNAL(MethodInfo("request_save_previous_state", PropertyInfo(Variant::DICTIONARY, "state")));
ADD_SIGNAL(MethodInfo("edited_script_changed"));
#endif
}
String ScriptEditorBase::get_document_name() const {
String name;
name = edited_res->get_path().get_file();
if (name.is_empty()) {
// This appears for newly created built-in text_files before saving the scene.
name = TTR("[unsaved]");
} else if (edited_res->is_built_in()) {
const String &text_file_name = edited_res->get_name();
if (!text_file_name.is_empty()) {
// If the built-in text_file has a custom resource name defined,
// display the built-in text_file name as follows: `ResourceName (scene_file.tscn)`
name = vformat("%s (%s)", text_file_name, name.get_slice("::", 0));
}
}
return name;
}
Ref<Texture2D> ScriptEditorBase::get_theme_icon() {
return EditorNode::get_singleton()->get_object_icon(edited_res.ptr());
}
void ScriptEditorBase::tag_saved_version() {
edited_file_data.last_modified_time = FileAccess::get_modified_time(edited_file_data.path);
}
//// TextEditorBase
TextEditorBase *TextEditorBase::EditMenus::_get_active_editor() {
if (se) {
return Object::cast_to<TextEditorBase>(se->get_current_editor());
}
return nullptr;
}
void TextEditorBase::EditMenus::_edit_option(int p_op) {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
script_text_editor->_edit_option(p_op);
}
void TextEditorBase::EditMenus::_prepare_edit_menu() {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
const CodeEdit *tx = script_text_editor->code_editor->get_text_editor();
PopupMenu *popup = edit_menu->get_popup();
popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());
popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());
}
void TextEditorBase::EditMenus::_update_highlighter_menu() {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
Ref<EditorSyntaxHighlighter> current_highlighter = script_text_editor->get_code_editor()->get_text_editor()->get_syntax_highlighter();
highlighter_menu->clear();
for (const Ref<EditorSyntaxHighlighter> &highlighter : script_text_editor->highlighters) {
highlighter_menu->add_radio_check_item(highlighter->_get_name());
highlighter_menu->set_item_checked(-1, highlighter == current_highlighter);
}
}
void TextEditorBase::EditMenus::_change_syntax_highlighter(int p_idx) {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
ERR_FAIL_INDEX(p_idx, (int)script_text_editor->highlighters.size());
script_text_editor->set_syntax_highlighter(script_text_editor->highlighters[p_idx]);
}
void TextEditorBase::EditMenus::_update_bookmark_list() {
bookmarks_menu->clear();
bookmarks_menu->reset_size();
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);
bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);
TextEditorBase *script_text_editor = _get_active_editor();
if (script_text_editor == nullptr) {
return;
}
PackedInt32Array bookmark_list = script_text_editor->code_editor->get_text_editor()->get_bookmarked_lines();
if (bookmark_list.is_empty()) {
return;
}
bookmarks_menu->add_separator();
for (int32_t bookmark : bookmark_list) {
// Strip edges to remove spaces or tabs.
// Also replace any tabs by spaces, since we can't print tabs in the menu.
String line = script_text_editor->code_editor->get_text_editor()->get_line(bookmark).replace("\t", " ").strip_edges();
// Limit the size of the line if too big.
if (line.length() > 50) {
line = line.substr(0, 50);
}
bookmarks_menu->add_item(String::num_int64(bookmark + 1) + " - `" + line + "`");
bookmarks_menu->set_item_metadata(-1, bookmark);
}
}
void TextEditorBase::EditMenus::_bookmark_item_pressed(int p_idx) {
TextEditorBase *script_text_editor = _get_active_editor();
ERR_FAIL_NULL(script_text_editor);
if (p_idx < 4) { // Any item before the separator.
script_text_editor->_edit_option(bookmarks_menu->get_item_id(p_idx));
} else {
ScriptEditorNavigationMarker::get_singleton()->locate_begin();
script_text_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));
ScriptEditorNavigationMarker::get_singleton()->locate_end();
}
}
TextEditorBase::EditMenus::EditMenus(ScriptEditor *p_se) {
se = p_se;
edit_menu = memnew(MenuButton);
edit_menu->set_flat(false);
edit_menu->set_theme_type_variation("FlatMenuButton");
edit_menu->set_text(TTRC("Edit"));
edit_menu->set_switch_on_hover(true);
add_child(edit_menu);
edit_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_prepare_edit_menu));
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);
edit_menu->get_popup()->add_separator();
{
edit_menu_line = memnew(PopupMenu);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/join_lines"), EDIT_JOIN_LINES);
edit_menu_line->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Line"), edit_menu_line);
}
{
edit_menu_fold = memnew(PopupMenu);
edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);
edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);
edit_menu_fold->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Folding"), edit_menu_fold);
}
edit_menu->get_popup()->add_separator();
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_final_newlines"), EDIT_TRIM_FINAL_NEWLINES);
{
edit_menu_convert_indent = memnew(PopupMenu);
edit_menu_convert_indent->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);
edit_menu_convert_indent->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);
edit_menu_convert_indent->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Indentation"), edit_menu_convert_indent);
}
edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_separator();
{
edit_menu_convert = memnew(PopupMenu);
edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/capitalize"), EDIT_CAPITALIZE);
edit_menu_convert->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
edit_menu->get_popup()->add_submenu_node_item(TTRC("Convert Case"), edit_menu_convert);
}
highlighter_menu = memnew(PopupMenu);
edit_menu->get_popup()->add_submenu_node_item(TTRC("Syntax Highlighter"), highlighter_menu);
highlighter_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_highlighter_menu));
highlighter_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_change_syntax_highlighter));
search_menu = memnew(MenuButton);
search_menu->set_flat(false);
search_menu->set_theme_type_variation("FlatMenuButton");
search_menu->set_text(TTRC("Search"));
search_menu->set_switch_on_hover(true);
add_child(search_menu);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);
search_menu->get_popup()->add_separator();
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("editor/find_in_files"), SEARCH_IN_FILES);
search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("editor/replace_in_files"), REPLACE_IN_FILES);
search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
goto_menu = memnew(MenuButton);
goto_menu->set_flat(false);
goto_menu->set_theme_type_variation("FlatMenuButton");
goto_menu->set_text(TTRC("Go To"));
goto_menu->set_switch_on_hover(true);
add_child(goto_menu);
goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);
goto_menu->get_popup()->add_separator();
bookmarks_menu = memnew(PopupMenu);
goto_menu->get_popup()->add_submenu_node_item(TTRC("Bookmarks"), bookmarks_menu);
bookmarks_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_bookmark_list));
bookmarks_menu->connect("index_pressed", callable_mp(this, &EditMenus::_bookmark_item_pressed));
goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));
// Update immediately for shortcuts.
_update_bookmark_list();
}
void TextEditorBase::_make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position, bool p_show) {
context_menu->clear();
if (DisplayServer::get_singleton()->has_feature(DisplayServerEnums::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {
context_menu->add_item(TTRC("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);
context_menu->add_separator();
}
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);
if (p_selection) {
context_menu->add_separator();
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);
}
if (p_foldable) {
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);
}
if (p_show) {
_show_context_menu(p_position);
}
}
void TextEditorBase::_show_context_menu(const Vector2 &p_position) {
const CodeEdit *tx = code_editor->get_text_editor();
context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());
context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());
context_menu->set_position(get_screen_position() + p_position);
context_menu->reset_size();
context_menu->popup();
}
void TextEditorBase::_text_edit_gui_input(const Ref<InputEvent> &p_ev) {
Ref<InputEventMouseButton> mb = p_ev;
if (mb.is_valid()) {
if (mb->get_button_index() == MouseButton::RIGHT) {
CodeEdit *tx = code_editor->get_text_editor();
tx->apply_ime();
Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());
int row = pos.y;
int col = pos.x;
tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));
if (tx->is_move_caret_on_right_click_enabled()) {
tx->remove_secondary_carets();
if (tx->has_selection()) {
int from_line = tx->get_selection_from_line();
int to_line = tx->get_selection_to_line();
int from_column = tx->get_selection_from_column();
int to_column = tx->get_selection_to_column();
if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {
// Right click is outside the selected text.
tx->deselect();
}
}
if (!tx->has_selection()) {
tx->set_caret_line(row, true, false, -1);
tx->set_caret_column(col);
}
}
if (!mb->is_pressed()) {
bool can_fold = tx->can_fold_line(row);
bool is_folded = tx->is_line_folded(row);
_make_context_menu(tx->has_selection(), can_fold || is_folded, get_local_mouse_position());
}
}
}
Ref<InputEventKey> k = p_ev;
if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {
CodeEdit *tx = code_editor->get_text_editor();
int line = tx->get_caret_line();
tx->adjust_viewport_to_caret();
bool can_fold = tx->can_fold_line(line);
bool is_folded = tx->is_line_folded(line);
_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)));
context_menu->grab_focus();
}
}
bool TextEditorBase::_edit_option(int p_op) {
CodeEdit *tx = code_editor->get_text_editor();
tx->apply_ime();
switch (p_op) {
case EDIT_UNDO:
case EDIT_REDO:
case EDIT_CUT:
case EDIT_COPY:
case EDIT_PASTE:
case EDIT_SELECT_ALL:
case EDIT_TRIM_TRAILING_WHITESAPCE:
case EDIT_TRIM_FINAL_NEWLINES:
case EDIT_CONVERT_INDENT_TO_SPACES:
case EDIT_CONVERT_INDENT_TO_TABS:
case EDIT_MOVE_LINE_UP:
case EDIT_MOVE_LINE_DOWN:
case EDIT_INDENT:
case EDIT_UNINDENT:
case EDIT_DELETE_LINE:
case EDIT_JOIN_LINES:
case EDIT_DUPLICATE_SELECTION:
case EDIT_DUPLICATE_LINES:
case EDIT_TO_UPPERCASE:
case EDIT_TO_LOWERCASE:
case EDIT_CAPITALIZE:
case EDIT_TOGGLE_FOLD_LINE:
case EDIT_FOLD_ALL_LINES:
case EDIT_TOGGLE_WORD_WRAP:
case EDIT_UNFOLD_ALL_LINES:
case EDIT_EMOJI_AND_SYMBOL:
case SEARCH_FIND_NEXT:
case SEARCH_FIND_PREV:
case BOOKMARK_TOGGLE:
case BOOKMARK_GOTO_NEXT:
case BOOKMARK_GOTO_PREV:
case BOOKMARK_REMOVE_ALL: {
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
}
}
switch (p_op) {
case EDIT_UNDO: {
tx->undo();
} break;
case EDIT_REDO: {
tx->redo();
} break;
case EDIT_CUT: {
tx->cut();
} break;
case EDIT_COPY: {
tx->copy();
} break;
case EDIT_PASTE: {
tx->paste();
} break;
case EDIT_SELECT_ALL: {
tx->select_all();
} break;
case EDIT_MOVE_LINE_UP: {
tx->move_lines_up();
} break;
case EDIT_MOVE_LINE_DOWN: {
tx->move_lines_down();
} break;
case EDIT_INDENT: {
tx->indent_lines();
} break;
case EDIT_UNINDENT: {
tx->unindent_lines();
} break;
case EDIT_DELETE_LINE: {
tx->delete_lines();
} break;
case EDIT_JOIN_LINES: {
tx->join_lines();
} break;
case EDIT_DUPLICATE_SELECTION: {
tx->duplicate_selection();
} break;
case EDIT_DUPLICATE_LINES: {
tx->duplicate_lines();
} break;
case EDIT_TRIM_TRAILING_WHITESAPCE: {
trim_trailing_whitespace();
} break;
case EDIT_TRIM_FINAL_NEWLINES: {
trim_final_newlines();
} break;
case EDIT_CONVERT_INDENT_TO_SPACES: {
code_editor->set_indent_using_spaces(true);
convert_indent();
} break;
case EDIT_CONVERT_INDENT_TO_TABS: {
code_editor->set_indent_using_spaces(false);
convert_indent();
} break;
case EDIT_TO_UPPERCASE: {
_convert_case(CodeTextEditor::UPPER);
} break;
case EDIT_TO_LOWERCASE: {
_convert_case(CodeTextEditor::LOWER);
} break;
case EDIT_CAPITALIZE: {
_convert_case(CodeTextEditor::CAPITALIZE);
} break;
case EDIT_TOGGLE_WORD_WRAP: {
TextEdit::LineWrappingMode wrap = tx->get_line_wrapping_mode();
tx->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);
} break;
case SEARCH_FIND: {
code_editor->get_find_replace_bar()->popup_search();
} break;
case SEARCH_FIND_NEXT: {
code_editor->get_find_replace_bar()->search_next();
} break;
case SEARCH_FIND_PREV: {
code_editor->get_find_replace_bar()->search_prev();
} break;
case SEARCH_REPLACE: {
code_editor->get_find_replace_bar()->popup_replace();
} break;
case SEARCH_IN_FILES:
case REPLACE_IN_FILES: {
String selected_text = tx->get_selected_text();
FindInFiles::get_singleton()->open_dock(selected_text, p_op == REPLACE_IN_FILES);
} break;
case SEARCH_GOTO_LINE: {
goto_line_popup->popup_find_line(code_editor);
} break;
case BOOKMARK_TOGGLE: {
code_editor->toggle_bookmark();
} break;
case BOOKMARK_GOTO_NEXT: {
ScriptEditorNavigationMarker::get_singleton()->locate_begin();
code_editor->goto_next_bookmark();
ScriptEditorNavigationMarker::get_singleton()->locate_end();
} break;
case BOOKMARK_GOTO_PREV: {
ScriptEditorNavigationMarker::get_singleton()->locate_begin();
code_editor->goto_prev_bookmark();
ScriptEditorNavigationMarker::get_singleton()->locate_end();
} break;
case BOOKMARK_REMOVE_ALL: {
code_editor->remove_all_bookmarks();
} break;
case EDIT_EMOJI_AND_SYMBOL: {
tx->show_emoji_and_symbol_picker();
} break;
case EDIT_TOGGLE_FOLD_LINE: {
tx->toggle_foldable_lines_at_carets();
} break;
case EDIT_FOLD_ALL_LINES: {
tx->fold_all_lines();
} break;
case EDIT_UNFOLD_ALL_LINES: {
tx->unfold_all_lines();
} break;
default: {
return false;
}
}
return true;
}
void TextEditorBase::_load_theme_settings() {
code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();
}
void TextEditorBase::_validate_script() {
emit_signal(SNAME("_validation_updated"));
}
void TextEditorBase::_saved_update() {
if (was_unsaved != is_unsaved()) {
was_unsaved = is_unsaved();
emit_signal(SNAME("name_changed"));
}
}
void TextEditorBase::_emit_request_save_new_history() {
Dictionary state = get_edit_state();
state["ensure_caret_visible"] = true;
previous_history_line = state["row"];
emit_signal(SNAME("_request_save_new_history"), state);
}
void TextEditorBase::_emit_request_save_previous_state() {
Dictionary state = get_edit_state();
state["ensure_caret_visible"] = true;
previous_history_line = state["row"];
emit_signal(SNAME("_request_save_previous_state"), state);
}
void TextEditorBase::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
ERR_FAIL_COND(p_highlighter.is_null());
highlighters.push_back(p_highlighter);
}
void TextEditorBase::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {
ERR_FAIL_COND(p_highlighter.is_null());
CodeEdit *te = code_editor->get_text_editor();
p_highlighter->_set_edited_resource(edited_res);
te->set_syntax_highlighter(p_highlighter);
}
void TextEditorBase::set_edited_resource(const Ref<Resource> &p_res) {
ERR_FAIL_COND(edited_res.is_valid());
ERR_FAIL_COND(p_res.is_null());
edited_res = p_res;
Ref<TextFile> text_file = edited_res;
String text;
if (text_file.is_valid()) {
text = text_file->get_text();
}
Ref<JSON> json_file = edited_res;
if (json_file.is_valid()) {
text = json_file->get_parsed_text();
}
code_editor->get_text_editor()->set_text(text);
code_editor->get_text_editor()->clear_undo_history();
code_editor->get_text_editor()->tag_saved_version();
code_editor->update_line_and_column();
}
bool TextEditorBase::is_unsaved() {
return code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() || edited_res->get_path().is_empty(); // In memory.
}
void TextEditorBase::tag_saved_version() {
code_editor->get_text_editor()->tag_saved_version();
ScriptEditorBase::tag_saved_version();
_saved_update();
}
void TextEditorBase::reload_text() {
ERR_FAIL_COND(edited_res.is_null());
CodeEdit *te = code_editor->get_text_editor();
int column = te->get_caret_column();
int row = te->get_caret_line();
int h = te->get_h_scroll();
int v = te->get_v_scroll();
Ref<TextFile> text_file = edited_res;
if (text_file.is_valid()) {
te->set_text(text_file->get_text());
}
Ref<JSON> json_file = edited_res;
if (json_file.is_valid()) {
te->set_text(json_file->get_parsed_text());
}
Ref<Script> script = edited_res;
if (script.is_valid()) {
te->set_text(script->get_source_code());
}
Ref<ShaderInclude> shader_inc = edited_res;
if (shader_inc.is_valid()) {
te->set_text(shader_inc->get_code());
}
Ref<Shader> shader = edited_res;
if (shader.is_valid()) {
te->set_text(shader->get_code());
}
te->set_caret_line(row);
te->set_caret_column(column);
te->set_h_scroll(h);
te->set_v_scroll(v);
te->tag_saved_version();
_saved_update();
code_editor->update_line_and_column();
if (editor_enabled) {
_validate_script();
}
}
void TextEditorBase::enable_editor() {
if (editor_enabled) {
return;
}
editor_enabled = true;
_load_theme_settings();
_validate_script();
}
void TextEditorBase::set_tooltip_request_func(const Callable &p_toolip_callback) {
Variant args[1] = { this };
const Variant *argp[] = { &args[0] };
code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));
}
void TextEditorBase::set_edit_state(const Variant &p_state, bool p_grab_focus) {
code_editor->set_edit_state(p_state);
Dictionary state = p_state;
if (state.has("row")) {
previous_history_line = state["row"];
}
if (state.has("syntax_highlighter")) {
for (const Ref<EditorSyntaxHighlighter> &highlighter : highlighters) {
if (highlighter->_get_name() == String(state["syntax_highlighter"])) {
set_syntax_highlighter(highlighter);
break;
}
}
}
if (p_grab_focus) {
ensure_focus();
}
}
TextEditorBase::TextEditorBase() {
code_editor = memnew(CodeTextEditor);
code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);
code_editor->show_toggle_files_button();
code_editor->get_text_editor()->set_context_menu_enabled(false);
code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &TextEditorBase::_text_edit_gui_input));
code_editor->get_text_editor()->connect(SceneStringName(text_changed), callable_mp(this, &TextEditorBase::_saved_update));
code_editor->connect("validate_script", callable_mp(this, &TextEditorBase::_validate_script));
code_editor->connect("load_theme_settings", callable_mp(this, &TextEditorBase::_load_theme_settings));
code_editor->connect("show_goto_popup", callable_mp(this, &TextEditorBase::_edit_option).bind(SEARCH_GOTO_LINE));
code_editor->connect("_request_save_new_history", callable_mp(this, &TextEditorBase::_emit_request_save_new_history));
context_menu = memnew(PopupMenu);
context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextEditorBase::_edit_option));
add_child(context_menu);
goto_line_popup = memnew(GotoLinePopup);
add_child(goto_line_popup);
Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;
plain_highlighter.instantiate();
add_syntax_highlighter(plain_highlighter);
Ref<EditorStandardSyntaxHighlighter> highlighter;
highlighter.instantiate();
add_syntax_highlighter(highlighter);
set_syntax_highlighter(plain_highlighter);
update_settings();
}
TextEditorBase::~TextEditorBase() {
highlighters.clear();
}
//// CodeEditorBase
void CodeEditorBase::EditMenusCEB::_update_breakpoint_list() {
breakpoints_menu->clear();
breakpoints_menu->reset_size();
TextEditorBase *text_editor_base = _get_active_editor();
if (text_editor_base == nullptr) {
return;
}
PackedInt32Array breakpoint_list = text_editor_base->get_code_editor()->get_text_editor()->get_breakpointed_lines();
if (breakpoint_list.is_empty()) {
return;
}
breakpoints_menu->add_separator();
for (int i = 0; i < breakpoint_list.size(); i++) {
// Strip edges to remove spaces or tabs.
// Also replace any tabs by spaces, since we can't print tabs in the menu.
String line = text_editor_base->get_code_editor()->get_text_editor()->get_line(breakpoint_list[i]).replace("\t", " ").strip_edges();
// Limit the size of the line if too big.
if (line.length() > 50) {
line = line.substr(0, 50);
}
breakpoints_menu->add_item(String::num_int64(breakpoint_list[i] + 1) + " - `" + line + "`");
breakpoints_menu->set_item_metadata(-1, breakpoint_list[i]);
}
}
void CodeEditorBase::EditMenusCEB::_breakpoint_item_pressed(int p_idx) {
TextEditorBase *text_editor_base = _get_active_editor();
ERR_FAIL_NULL(text_editor_base);
if (p_idx < 4) { // Any item before the separator.
_edit_option(breakpoints_menu->get_item_id(p_idx));
} else {
text_editor_base->goto_line_centered(breakpoints_menu->get_item_metadata(p_idx));
}
}
void CodeEditorBase::EditMenusCEB::_notification(int p_what) {
switch (p_what) {
case NOTIFICATION_POSTINITIALIZE: {
// Update immediately for shortcuts.
_update_breakpoint_list();
}
}
}
CodeEditorBase::EditMenusCEB::EditMenusCEB(ScriptEditor *p_se, String p_breakpoint_menu_name) : EditMenus(p_se) {
edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);
_popup_move_item(EDIT_TRIM_TRAILING_WHITESAPCE, edit_menu->get_popup(), false);
edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
breakpoints_menu = memnew(PopupMenu);
goto_menu->get_popup()->add_submenu_node_item(TTRC(p_breakpoint_menu_name), breakpoints_menu);
breakpoints_menu->connect("about_to_popup", callable_mp(this, &EditMenusCEB::_update_breakpoint_list));
breakpoints_menu->connect("index_pressed", callable_mp(this, &EditMenusCEB::_breakpoint_item_pressed));
}
void CodeEditorBase::set_breakpoint(int p_line, bool p_enabled) {
code_editor->get_text_editor()->set_line_as_breakpoint(p_line, p_enabled);
}
void CodeEditorBase::_show_warnings_panel(bool p_show) {
warnings_panel->set_visible(p_show);
}
bool CodeEditorBase::_warning_clicked(const Variant &p_line) {
if (p_line.get_type() == Variant::INT) {
goto_line_centered(p_line.operator int64_t());
return true;
}
return false;
}
void CodeEditorBase::_make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position, bool p_show) {
TextEditorBase::_make_context_menu(p_selection, p_foldable, p_position, false);
context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);
_popup_move_item(EDIT_UNINDENT, context_menu);
if (p_show) {
_show_context_menu(p_position);
}
}
void CodeEditorBase::_code_complete_scripts(void *p_ud, const String &p_code, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_force) {
CodeEditorBase *ste = (CodeEditorBase *)p_ud;
ste->_code_complete_script(p_code, r_options, r_force);
}
bool CodeEditorBase::_edit_option(int p_option) {
CodeEdit *tx = code_editor->get_text_editor();
tx->apply_ime();
switch (p_option) {
case DEBUG_TOGGLE_BREAKPOINT:
case DEBUG_REMOVE_ALL_BREAKPOINTS:
case DEBUG_GOTO_NEXT_BREAKPOINT:
case DEBUG_GOTO_PREV_BREAKPOINT: {
callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);
}
}
switch (p_option) {
case DEBUG_TOGGLE_BREAKPOINT: {
Vector<int> sorted_carets = tx->get_sorted_carets();
int last_line = -1;
for (const int &c : sorted_carets) {
int from = tx->get_selection_from_line(c);
from += from == last_line ? 1 : 0;
int to = tx->get_selection_to_line(c);
if (to < from) {
continue;
}
// Check first if there's any lines with breakpoints in the selection.
bool selection_has_breakpoints = false;
for (int line = from; line <= to; line++) {
if (tx->is_line_breakpointed(line)) {
selection_has_breakpoints = true;
break;
}
}
// Set breakpoint on caret or remove all bookmarks from the selection.
if (!selection_has_breakpoints) {
if (tx->get_caret_line(c) != last_line) {
tx->set_line_as_breakpoint(tx->get_caret_line(c), true);
}
} else {
for (int line = from; line <= to; line++) {
tx->set_line_as_breakpoint(line, false);
}
}
last_line = to;
}
} break;
case DEBUG_GOTO_NEXT_BREAKPOINT: {
PackedInt32Array bpoints = tx->get_breakpointed_lines();
if (bpoints.is_empty()) {
return true;
}
int current_line = tx->get_caret_line();
int bpoint_idx = 0;
if (current_line < (int)bpoints[bpoints.size() - 1]) {
while (bpoint_idx < bpoints.size() && bpoints[bpoint_idx] <= current_line) {
bpoint_idx++;
}
}
ScriptEditorNavigationMarker::get_singleton()->locate_begin();
goto_line_centered(bpoints[bpoint_idx]);
ScriptEditorNavigationMarker::get_singleton()->locate_end();
} break;
case DEBUG_GOTO_PREV_BREAKPOINT: {
PackedInt32Array bpoints = tx->get_breakpointed_lines();
if (bpoints.is_empty()) {
return true;
}
int current_line = tx->get_caret_line();
int bpoint_idx = bpoints.size() - 1;
if (current_line > (int)bpoints[0]) {
while (bpoint_idx >= 0 && bpoints[bpoint_idx] >= current_line) {
bpoint_idx--;
}
}
ScriptEditorNavigationMarker::get_singleton()->locate_begin();
goto_line_centered(bpoints[bpoint_idx]);
ScriptEditorNavigationMarker::get_singleton()->locate_end();
} break;
case DEBUG_REMOVE_ALL_BREAKPOINTS: {
for (int line : tx->get_breakpointed_lines()) {
tx->set_line_as_breakpoint(line, false);
}
} break;
default: {
return TextEditorBase::_edit_option(p_option);
}
}
return true;
}
CodeEditorBase::CodeEditorBase() {
code_editor->set_code_complete_func(_code_complete_scripts, this);
code_editor->connect("show_warnings_panel", callable_mp(this, &CodeEditorBase::_show_warnings_panel));
code_editor->get_text_editor()->set_symbol_lookup_on_click_enabled(true);
code_editor->get_text_editor()->connect("breakpoint_toggled", callable_mp(this, &CodeEditorBase::_breakpoint_toggled));
warnings_panel = memnew(RichTextLabel);
warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));
warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);
warnings_panel->set_meta_underline(true);
warnings_panel->set_selection_enabled(true);
warnings_panel->set_context_menu_enabled(true);
warnings_panel->set_focus_mode(FOCUS_CLICK);
warnings_panel->hide();
warnings_panel->connect("meta_clicked", callable_mp(this, &CodeEditorBase::_warning_clicked));
editor_box = memnew(VSplitContainer);
add_child(editor_box);
editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);
editor_box->set_v_size_flags(SIZE_EXPAND_FILL);
}