Path: blob/master/editor/script/script_editor_base.cpp
21022 views
/**************************************************************************/1/* script_editor_base.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "script_editor_base.h"3132#include "core/io/json.h"33#include "editor/editor_node.h"34#include "editor/script/script_editor_plugin.h"35#include "editor/script/syntax_highlighters.h"36#include "editor/settings/editor_settings.h"37#include "scene/gui/menu_button.h"38#include "scene/gui/rich_text_label.h"39#include "scene/gui/split_container.h"4041void ScriptEditorBase::_bind_methods() {42ADD_SIGNAL(MethodInfo("name_changed"));4344// First use in TextEditorBase.45ADD_SIGNAL(MethodInfo("edited_script_changed"));46ADD_SIGNAL(MethodInfo("search_in_files_requested", PropertyInfo(Variant::STRING, "text")));47ClassDB::bind_method(D_METHOD("add_syntax_highlighter", "highlighter"), &ScriptEditorBase::add_syntax_highlighter);48ClassDB::bind_method(D_METHOD("get_base_editor"), &ScriptEditorBase::get_base_editor);4950// First use in ScriptTextEditor.51ADD_SIGNAL(MethodInfo("request_save_history"));52ADD_SIGNAL(MethodInfo("request_help", PropertyInfo(Variant::STRING, "topic")));53ADD_SIGNAL(MethodInfo("request_open_script_at_line", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::INT, "line")));54ADD_SIGNAL(MethodInfo("go_to_help", PropertyInfo(Variant::STRING, "what")));55ADD_SIGNAL(MethodInfo("request_save_previous_state", PropertyInfo(Variant::DICTIONARY, "state")));56ADD_SIGNAL(MethodInfo("replace_in_files_requested", PropertyInfo(Variant::STRING, "text")));57ADD_SIGNAL(MethodInfo("go_to_method", PropertyInfo(Variant::OBJECT, "script"), PropertyInfo(Variant::STRING, "method")));58}5960String ScriptEditorBase::get_name() {61String name;6263name = edited_res->get_path().get_file();64if (name.is_empty()) {65// This appears for newly created built-in text_files before saving the scene.66name = TTR("[unsaved]");67} else if (edited_res->is_built_in()) {68const String &text_file_name = edited_res->get_name();69if (!text_file_name.is_empty()) {70// If the built-in text_file has a custom resource name defined,71// display the built-in text_file name as follows: `ResourceName (scene_file.tscn)`72name = vformat("%s (%s)", text_file_name, name.get_slice("::", 0));73}74}7576if (is_unsaved()) {77name += "(*)";78}7980return name;81}8283Ref<Texture2D> ScriptEditorBase::get_theme_icon() {84return EditorNode::get_singleton()->get_object_icon(edited_res.ptr());85}8687void ScriptEditorBase::tag_saved_version() {88edited_file_data.last_modified_time = FileAccess::get_modified_time(edited_file_data.path);89}9091//// TextEditorBase9293TextEditorBase *TextEditorBase::EditMenus::_get_active_editor() {94return Object::cast_to<TextEditorBase>(ScriptEditor::get_singleton()->get_current_editor());95}9697void TextEditorBase::EditMenus::_edit_option(int p_op) {98TextEditorBase *script_text_editor = _get_active_editor();99ERR_FAIL_NULL(script_text_editor);100script_text_editor->_edit_option(p_op);101}102103void TextEditorBase::EditMenus::_prepare_edit_menu() {104TextEditorBase *script_text_editor = _get_active_editor();105ERR_FAIL_NULL(script_text_editor);106const CodeEdit *tx = script_text_editor->code_editor->get_text_editor();107PopupMenu *popup = edit_menu->get_popup();108popup->set_item_disabled(popup->get_item_index(EDIT_UNDO), !tx->has_undo());109popup->set_item_disabled(popup->get_item_index(EDIT_REDO), !tx->has_redo());110}111112void TextEditorBase::EditMenus::_update_highlighter_menu() {113TextEditorBase *script_text_editor = _get_active_editor();114ERR_FAIL_NULL(script_text_editor);115116Ref<EditorSyntaxHighlighter> current_highlighter = script_text_editor->get_code_editor()->get_text_editor()->get_syntax_highlighter();117highlighter_menu->clear();118for (const Ref<EditorSyntaxHighlighter> &highlighter : script_text_editor->highlighters) {119highlighter_menu->add_radio_check_item(highlighter->_get_name());120highlighter_menu->set_item_checked(-1, highlighter == current_highlighter);121}122}123124void TextEditorBase::EditMenus::_change_syntax_highlighter(int p_idx) {125TextEditorBase *script_text_editor = _get_active_editor();126ERR_FAIL_NULL(script_text_editor);127ERR_FAIL_INDEX(p_idx, (int)script_text_editor->highlighters.size());128script_text_editor->set_syntax_highlighter(script_text_editor->highlighters[p_idx]);129}130131void TextEditorBase::EditMenus::_update_bookmark_list() {132TextEditorBase *script_text_editor = _get_active_editor();133ERR_FAIL_NULL(script_text_editor);134bookmarks_menu->clear();135bookmarks_menu->reset_size();136137bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);138bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/remove_all_bookmarks"), BOOKMARK_REMOVE_ALL);139bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_next_bookmark"), BOOKMARK_GOTO_NEXT);140bookmarks_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_previous_bookmark"), BOOKMARK_GOTO_PREV);141142PackedInt32Array bookmark_list = script_text_editor->code_editor->get_text_editor()->get_bookmarked_lines();143if (bookmark_list.is_empty()) {144return;145}146147bookmarks_menu->add_separator();148149for (int32_t bookmark : bookmark_list) {150// Strip edges to remove spaces or tabs.151// Also replace any tabs by spaces, since we can't print tabs in the menu.152String line = script_text_editor->code_editor->get_text_editor()->get_line(bookmark).replace("\t", " ").strip_edges();153154// Limit the size of the line if too big.155if (line.length() > 50) {156line = line.substr(0, 50);157}158159bookmarks_menu->add_item(String::num_int64(bookmark + 1) + " - `" + line + "`");160bookmarks_menu->set_item_metadata(-1, bookmark);161}162}163164void TextEditorBase::EditMenus::_bookmark_item_pressed(int p_idx) {165TextEditorBase *script_text_editor = _get_active_editor();166ERR_FAIL_NULL(script_text_editor);167if (p_idx < 4) { // Any item before the separator.168script_text_editor->_edit_option(bookmarks_menu->get_item_id(p_idx));169} else {170script_text_editor->code_editor->goto_line_centered(bookmarks_menu->get_item_metadata(p_idx));171}172}173174TextEditorBase::EditMenus::EditMenus() {175edit_menu = memnew(MenuButton);176edit_menu->set_flat(false);177edit_menu->set_theme_type_variation("FlatMenuButton");178edit_menu->set_text(TTRC("Edit"));179edit_menu->set_switch_on_hover(true);180181add_child(edit_menu);182edit_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_prepare_edit_menu));183edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);184edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);185edit_menu->get_popup()->add_separator();186edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);187edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);188edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);189edit_menu->get_popup()->add_separator();190edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);191edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_selection"), EDIT_DUPLICATE_SELECTION);192edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/duplicate_lines"), EDIT_DUPLICATE_LINES);193edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_word_wrap"), EDIT_TOGGLE_WORD_WRAP);194edit_menu->get_popup()->add_separator();195{196edit_menu_line = memnew(PopupMenu);197edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_up"), EDIT_MOVE_LINE_UP);198edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/move_down"), EDIT_MOVE_LINE_DOWN);199edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);200edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);201edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/delete_line"), EDIT_DELETE_LINE);202edit_menu_line->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));203edit_menu->get_popup()->add_submenu_node_item(TTRC("Line"), edit_menu_line);204}205{206edit_menu_fold = memnew(PopupMenu);207edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);208edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/fold_all_lines"), EDIT_FOLD_ALL_LINES);209edit_menu_fold->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unfold_all_lines"), EDIT_UNFOLD_ALL_LINES);210edit_menu_fold->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));211edit_menu->get_popup()->add_submenu_node_item(TTRC("Folding"), edit_menu_fold);212}213edit_menu->get_popup()->add_separator();214edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_trailing_whitespace"), EDIT_TRIM_TRAILING_WHITESAPCE);215edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/trim_final_newlines"), EDIT_TRIM_FINAL_NEWLINES);216{217edit_menu_convert_indent = memnew(PopupMenu);218edit_menu_convert_indent->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_spaces"), EDIT_CONVERT_INDENT_TO_SPACES);219edit_menu_convert_indent->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_indent_to_tabs"), EDIT_CONVERT_INDENT_TO_TABS);220edit_menu_convert_indent->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));221edit_menu->get_popup()->add_submenu_node_item(TTRC("Indentation"), edit_menu_convert_indent);222}223edit_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));224edit_menu->get_popup()->add_separator();225{226edit_menu_convert = memnew(PopupMenu);227edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);228edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);229edit_menu_convert->add_shortcut(ED_GET_SHORTCUT("script_text_editor/capitalize"), EDIT_CAPITALIZE);230edit_menu_convert->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));231edit_menu->get_popup()->add_submenu_node_item(TTRC("Convert Case"), edit_menu_convert);232}233highlighter_menu = memnew(PopupMenu);234edit_menu->get_popup()->add_submenu_node_item(TTRC("Syntax Highlighter"), highlighter_menu);235236highlighter_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_highlighter_menu));237highlighter_menu->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_change_syntax_highlighter));238239search_menu = memnew(MenuButton);240search_menu->set_flat(false);241search_menu->set_theme_type_variation("FlatMenuButton");242search_menu->set_text(TTRC("Search"));243search_menu->set_switch_on_hover(true);244245add_child(search_menu);246search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find"), SEARCH_FIND);247search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_next"), SEARCH_FIND_NEXT);248search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/find_previous"), SEARCH_FIND_PREV);249search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace"), SEARCH_REPLACE);250search_menu->get_popup()->add_separator();251search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("editor/find_in_files"), SEARCH_IN_FILES);252search_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/replace_in_files"), REPLACE_IN_FILES);253search_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));254255goto_menu = memnew(MenuButton);256goto_menu->set_flat(false);257goto_menu->set_theme_type_variation("FlatMenuButton");258goto_menu->set_text(TTRC("Go To"));259goto_menu->set_switch_on_hover(true);260add_child(goto_menu);261goto_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("script_text_editor/goto_line"), SEARCH_GOTO_LINE);262goto_menu->get_popup()->add_separator();263264bookmarks_menu = memnew(PopupMenu);265goto_menu->get_popup()->add_submenu_node_item(TTRC("Bookmarks"), bookmarks_menu);266bookmarks_menu->connect("about_to_popup", callable_mp(this, &EditMenus::_update_bookmark_list));267bookmarks_menu->connect("index_pressed", callable_mp(this, &EditMenus::_bookmark_item_pressed));268269goto_menu->get_popup()->connect(SceneStringName(id_pressed), callable_mp(this, &EditMenus::_edit_option));270}271272void TextEditorBase::_make_context_menu(bool p_selection, bool p_foldable, const Vector2 &p_position, bool p_show) {273context_menu->clear();274if (DisplayServer::get_singleton()->has_feature(DisplayServer::FEATURE_EMOJI_AND_SYMBOL_PICKER)) {275context_menu->add_item(TTRC("Emoji & Symbols"), EDIT_EMOJI_AND_SYMBOL);276context_menu->add_separator();277}278context_menu->add_shortcut(ED_GET_SHORTCUT("ui_undo"), EDIT_UNDO);279context_menu->add_shortcut(ED_GET_SHORTCUT("ui_redo"), EDIT_REDO);280context_menu->add_separator();281context_menu->add_shortcut(ED_GET_SHORTCUT("ui_cut"), EDIT_CUT);282context_menu->add_shortcut(ED_GET_SHORTCUT("ui_copy"), EDIT_COPY);283context_menu->add_shortcut(ED_GET_SHORTCUT("ui_paste"), EDIT_PASTE);284context_menu->add_separator();285context_menu->add_shortcut(ED_GET_SHORTCUT("ui_text_select_all"), EDIT_SELECT_ALL);286context_menu->add_separator();287context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/indent"), EDIT_INDENT);288context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/unindent"), EDIT_UNINDENT);289context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_bookmark"), BOOKMARK_TOGGLE);290291if (p_selection) {292context_menu->add_separator();293context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_uppercase"), EDIT_TO_UPPERCASE);294context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/convert_to_lowercase"), EDIT_TO_LOWERCASE);295}296297if (p_foldable) {298context_menu->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_fold_line"), EDIT_TOGGLE_FOLD_LINE);299}300301if (p_show) {302_show_context_menu(p_position);303}304}305306void TextEditorBase::_show_context_menu(const Vector2 &p_position) {307const CodeEdit *tx = code_editor->get_text_editor();308context_menu->set_item_disabled(context_menu->get_item_index(EDIT_UNDO), !tx->has_undo());309context_menu->set_item_disabled(context_menu->get_item_index(EDIT_REDO), !tx->has_redo());310311context_menu->set_position(get_screen_position() + p_position);312context_menu->reset_size();313context_menu->popup();314}315316void TextEditorBase::_text_edit_gui_input(const Ref<InputEvent> &p_ev) {317Ref<InputEventMouseButton> mb = p_ev;318319if (mb.is_valid()) {320if (mb->get_button_index() == MouseButton::RIGHT) {321CodeEdit *tx = code_editor->get_text_editor();322323tx->apply_ime();324325Point2i pos = tx->get_line_column_at_pos(mb->get_global_position() - tx->get_global_position());326int row = pos.y;327int col = pos.x;328329tx->set_move_caret_on_right_click_enabled(EDITOR_GET("text_editor/behavior/navigation/move_caret_on_right_click"));330331if (tx->is_move_caret_on_right_click_enabled()) {332tx->remove_secondary_carets();333if (tx->has_selection()) {334int from_line = tx->get_selection_from_line();335int to_line = tx->get_selection_to_line();336int from_column = tx->get_selection_from_column();337int to_column = tx->get_selection_to_column();338339if (row < from_line || row > to_line || (row == from_line && col < from_column) || (row == to_line && col > to_column)) {340// Right click is outside the selected text.341tx->deselect();342}343}344if (!tx->has_selection()) {345tx->set_caret_line(row, true, false, -1);346tx->set_caret_column(col);347}348}349350if (!mb->is_pressed()) {351bool can_fold = tx->can_fold_line(row);352bool is_folded = tx->is_line_folded(row);353_make_context_menu(tx->has_selection(), can_fold || is_folded, get_local_mouse_position());354}355}356}357358Ref<InputEventKey> k = p_ev;359if (k.is_valid() && k->is_pressed() && k->is_action("ui_menu", true)) {360CodeEdit *tx = code_editor->get_text_editor();361int line = tx->get_caret_line();362tx->adjust_viewport_to_caret();363bool can_fold = tx->can_fold_line(line);364bool is_folded = tx->is_line_folded(line);365_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)));366context_menu->grab_focus();367}368}369370bool TextEditorBase::_edit_option(int p_op) {371CodeEdit *tx = code_editor->get_text_editor();372tx->apply_ime();373374switch (p_op) {375case EDIT_UNDO: {376tx->undo();377callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);378} break;379case EDIT_REDO: {380tx->redo();381callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);382} break;383case EDIT_CUT: {384tx->cut();385callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);386} break;387case EDIT_COPY: {388tx->copy();389callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);390} break;391case EDIT_PASTE: {392tx->paste();393callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);394} break;395case EDIT_SELECT_ALL: {396tx->select_all();397callable_mp((Control *)tx, &Control::grab_focus).call_deferred(false);398} break;399case EDIT_MOVE_LINE_UP: {400tx->move_lines_up();401} break;402case EDIT_MOVE_LINE_DOWN: {403tx->move_lines_down();404} break;405case EDIT_INDENT: {406tx->indent_lines();407} break;408case EDIT_UNINDENT: {409tx->unindent_lines();410} break;411case EDIT_DELETE_LINE: {412tx->delete_lines();413} break;414case EDIT_DUPLICATE_SELECTION: {415tx->duplicate_selection();416} break;417case EDIT_DUPLICATE_LINES: {418tx->duplicate_lines();419} break;420case EDIT_TRIM_TRAILING_WHITESAPCE: {421trim_trailing_whitespace();422} break;423case EDIT_TRIM_FINAL_NEWLINES: {424trim_final_newlines();425} break;426case EDIT_CONVERT_INDENT_TO_SPACES: {427code_editor->set_indent_using_spaces(true);428convert_indent();429} break;430case EDIT_CONVERT_INDENT_TO_TABS: {431code_editor->set_indent_using_spaces(false);432convert_indent();433} break;434case EDIT_TO_UPPERCASE: {435_convert_case(CodeTextEditor::UPPER);436} break;437case EDIT_TO_LOWERCASE: {438_convert_case(CodeTextEditor::LOWER);439} break;440case EDIT_CAPITALIZE: {441_convert_case(CodeTextEditor::CAPITALIZE);442} break;443case EDIT_TOGGLE_WORD_WRAP: {444TextEdit::LineWrappingMode wrap = tx->get_line_wrapping_mode();445tx->set_line_wrapping_mode(wrap == TextEdit::LINE_WRAPPING_BOUNDARY ? TextEdit::LINE_WRAPPING_NONE : TextEdit::LINE_WRAPPING_BOUNDARY);446} break;447case SEARCH_FIND: {448code_editor->get_find_replace_bar()->popup_search();449} break;450case SEARCH_FIND_NEXT: {451code_editor->get_find_replace_bar()->search_next();452} break;453case SEARCH_FIND_PREV: {454code_editor->get_find_replace_bar()->search_prev();455} break;456case SEARCH_REPLACE: {457code_editor->get_find_replace_bar()->popup_replace();458} break;459case SEARCH_IN_FILES: {460String selected_text = tx->get_selected_text();461462// Yep, because it doesn't make sense to instance this dialog for every single script open...463// So this will be delegated to the ScriptEditor.464emit_signal(SNAME("search_in_files_requested"), selected_text);465} break;466case REPLACE_IN_FILES: {467String selected_text = tx->get_selected_text();468emit_signal(SNAME("replace_in_files_requested"), selected_text);469} break;470case SEARCH_GOTO_LINE: {471goto_line_popup->popup_find_line(code_editor);472} break;473case BOOKMARK_TOGGLE: {474code_editor->toggle_bookmark();475} break;476case BOOKMARK_GOTO_NEXT: {477code_editor->goto_next_bookmark();478} break;479case BOOKMARK_GOTO_PREV: {480code_editor->goto_prev_bookmark();481} break;482case BOOKMARK_REMOVE_ALL: {483code_editor->remove_all_bookmarks();484} break;485case EDIT_EMOJI_AND_SYMBOL: {486tx->show_emoji_and_symbol_picker();487} break;488case EDIT_TOGGLE_FOLD_LINE: {489tx->toggle_foldable_lines_at_carets();490} break;491case EDIT_FOLD_ALL_LINES: {492tx->fold_all_lines();493} break;494case EDIT_UNFOLD_ALL_LINES: {495tx->unfold_all_lines();496} break;497default: {498return false;499}500}501return true;502}503504void TextEditorBase::_load_theme_settings() {505code_editor->get_text_editor()->get_syntax_highlighter()->update_cache();506}507508void TextEditorBase::_validate_script() {509emit_signal(SNAME("name_changed"));510emit_signal(SNAME("edited_script_changed"));511}512513void TextEditorBase::add_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {514ERR_FAIL_COND(p_highlighter.is_null());515516highlighters.push_back(p_highlighter);517}518519void TextEditorBase::set_syntax_highlighter(Ref<EditorSyntaxHighlighter> p_highlighter) {520ERR_FAIL_COND(p_highlighter.is_null());521522CodeEdit *te = code_editor->get_text_editor();523p_highlighter->_set_edited_resource(edited_res);524te->set_syntax_highlighter(p_highlighter);525}526527void TextEditorBase::set_edited_resource(const Ref<Resource> &p_res) {528ERR_FAIL_COND(edited_res.is_valid());529ERR_FAIL_COND(p_res.is_null());530531edited_res = p_res;532533Ref<TextFile> text_file = edited_res;534String text;535if (text_file.is_valid()) {536text = text_file->get_text();537}538539Ref<JSON> json_file = edited_res;540if (json_file.is_valid()) {541text = json_file->get_parsed_text();542}543544code_editor->get_text_editor()->set_text(text);545code_editor->get_text_editor()->clear_undo_history();546code_editor->get_text_editor()->tag_saved_version();547548emit_signal(SNAME("name_changed"));549code_editor->update_line_and_column();550}551552bool TextEditorBase::is_unsaved() {553return code_editor->get_text_editor()->get_version() != code_editor->get_text_editor()->get_saved_version() || edited_res->get_path().is_empty(); // In memory.554}555556void TextEditorBase::tag_saved_version() {557code_editor->get_text_editor()->tag_saved_version();558ScriptEditorBase::tag_saved_version();559}560561void TextEditorBase::reload_text() {562ERR_FAIL_COND(edited_res.is_null());563564CodeEdit *te = code_editor->get_text_editor();565int column = te->get_caret_column();566int row = te->get_caret_line();567int h = te->get_h_scroll();568int v = te->get_v_scroll();569570Ref<TextFile> text_file = edited_res;571if (text_file.is_valid()) {572te->set_text(text_file->get_text());573}574575Ref<JSON> json_file = edited_res;576if (json_file.is_valid()) {577te->set_text(json_file->get_parsed_text());578}579580Ref<Script> script = edited_res;581if (script.is_valid()) {582te->set_text(script->get_source_code());583}584585te->set_caret_line(row);586te->set_caret_column(column);587te->set_h_scroll(h);588te->set_v_scroll(v);589590te->tag_saved_version();591592code_editor->update_line_and_column();593if (editor_enabled) {594_validate_script();595}596}597598void TextEditorBase::enable_editor() {599if (editor_enabled) {600return;601}602603editor_enabled = true;604605_load_theme_settings();606607_validate_script();608}609610void TextEditorBase::set_tooltip_request_func(const Callable &p_toolip_callback) {611Variant args[1] = { this };612const Variant *argp[] = { &args[0] };613code_editor->get_text_editor()->set_tooltip_request_func(p_toolip_callback.bindp(argp, 1));614}615616void TextEditorBase::set_edit_state(const Variant &p_state) {617code_editor->set_edit_state(p_state);618619Dictionary state = p_state;620if (state.has("syntax_highlighter")) {621for (const Ref<EditorSyntaxHighlighter> &highlighter : highlighters) {622if (highlighter->_get_name() == String(state["syntax_highlighter"])) {623set_syntax_highlighter(highlighter);624break;625}626}627}628629ensure_focus();630}631632TextEditorBase::TextEditorBase() {633code_editor = memnew(CodeTextEditor);634code_editor->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);635code_editor->set_v_size_flags(Control::SIZE_EXPAND_FILL);636code_editor->show_toggle_files_button();637code_editor->get_text_editor()->set_context_menu_enabled(false);638code_editor->get_text_editor()->connect(SceneStringName(gui_input), callable_mp(this, &TextEditorBase::_text_edit_gui_input));639code_editor->connect("validate_script", callable_mp(this, &TextEditorBase::_validate_script));640code_editor->connect("load_theme_settings", callable_mp(this, &TextEditorBase::_load_theme_settings));641642context_menu = memnew(PopupMenu);643context_menu->connect(SceneStringName(id_pressed), callable_mp(this, &TextEditorBase::_edit_option));644add_child(context_menu);645646edit_hb = memnew(HBoxContainer);647648goto_line_popup = memnew(GotoLinePopup);649add_child(goto_line_popup);650651Ref<EditorPlainTextSyntaxHighlighter> plain_highlighter;652plain_highlighter.instantiate();653add_syntax_highlighter(plain_highlighter);654655Ref<EditorStandardSyntaxHighlighter> highlighter;656highlighter.instantiate();657add_syntax_highlighter(highlighter);658set_syntax_highlighter(plain_highlighter);659660update_settings();661}662663TextEditorBase::~TextEditorBase() {664highlighters.clear();665}666667//// CodeEditorBase668669bool CodeEditorBase::_warning_clicked(const Variant &p_line) {670if (p_line.get_type() == Variant::INT) {671goto_line_centered(p_line.operator int64_t());672return true;673}674return false;675}676677CodeEditorBase::EditMenusCEB::EditMenusCEB() {678edit_menu->get_popup()->add_shortcut(ED_GET_SHORTCUT("ui_text_completion_query"), EDIT_COMPLETE);679_popup_move_item(EDIT_TRIM_TRAILING_WHITESAPCE, edit_menu->get_popup(), false);680edit_menu_line->add_shortcut(ED_GET_SHORTCUT("script_text_editor/toggle_comment"), EDIT_TOGGLE_COMMENT);681}682683CodeEditorBase::CodeEditorBase() {684warnings_panel = memnew(RichTextLabel);685warnings_panel->set_custom_minimum_size(Size2(0, 100 * EDSCALE));686warnings_panel->set_h_size_flags(SIZE_EXPAND_FILL);687warnings_panel->set_meta_underline(true);688warnings_panel->set_selection_enabled(true);689warnings_panel->set_context_menu_enabled(true);690warnings_panel->set_focus_mode(FOCUS_CLICK);691warnings_panel->hide();692warnings_panel->connect("meta_clicked", callable_mp(this, &CodeEditorBase::_warning_clicked));693694editor_box = memnew(VSplitContainer);695add_child(editor_box);696editor_box->set_anchors_and_offsets_preset(Control::PRESET_FULL_RECT);697editor_box->set_v_size_flags(SIZE_EXPAND_FILL);698editor_box->add_child(code_editor);699editor_box->add_child(warnings_panel);700}701702703