/**************************************************************************/ /* script_editor_plugin.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_plugin.h" #include "core/config/project_settings.h" #include "core/input/input.h" #include "core/io/config_file.h" #include "core/io/file_access.h" #include "core/io/json.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/object/callable_mp.h" #include "core/object/class_db.h" #include "core/os/keyboard.h" #include "core/os/os.h" #include "core/string/fuzzy_search.h" #include "core/variant/dictionary.h" #include "core/version.h" #include "editor/debugger/editor_debugger_node.h" #include "editor/debugger/script_editor_debugger.h" #include "editor/doc/editor_help.h" #include "editor/doc/editor_help_search.h" #include "editor/docks/filesystem_dock.h" #include "editor/docks/inspector_dock.h" #include "editor/docks/signals_dock.h" #include "editor/editor_interface.h" #include "editor/editor_main_screen.h" #include "editor/editor_node.h" #include "editor/editor_string_names.h" #include "editor/file_system/editor_paths.h" #include "editor/gui/code_editor.h" #include "editor/gui/editor_file_dialog.h" #include "editor/gui/editor_toaster.h" #include "editor/gui/filter_line_edit.h" #include "editor/gui/window_wrapper.h" #include "editor/inspector/editor_context_menu_plugin.h" #include "editor/run/editor_run_bar.h" #include "editor/scene/editor_scene_tabs.h" #include "editor/script/find_in_files.h" #include "editor/script/script_editor_navigation_marker.h" #include "editor/script/script_text_editor.h" #include "editor/script/syntax_highlighters.h" #include "editor/script/text_editor.h" #include "editor/settings/editor_command_palette.h" #include "editor/settings/editor_settings.h" #include "editor/shader/shader_editor_plugin.h" #include "editor/shader/text_shader_editor.h" #include "editor/themes/editor_scale.h" #include "editor/themes/editor_theme_manager.h" #include "scene/gui/separator.h" #include "scene/gui/tab_container.h" #include "scene/gui/texture_rect.h" #include "scene/main/node.h" #include "scene/main/scene_tree.h" #include "scene/main/window.h" #include "servers/display/display_server.h" void ScriptEditorQuickOpen::popup_dialog(const Vector &p_functions, bool p_dontclear) { popup_centered_ratio(0.6); if (p_dontclear) { search_box->select_all(); } else { search_box->clear(); } search_box->grab_focus(); functions = p_functions; _update_search(); } void ScriptEditorQuickOpen::_text_changed(const String &p_newtext) { _update_search(); } void ScriptEditorQuickOpen::_update_search() { search_options->clear(); TreeItem *root = search_options->create_item(); for (int i = 0; i < functions.size(); i++) { String file = functions[i]; if ((search_box->get_text().is_empty() || file.containsn(search_box->get_text()))) { TreeItem *ti = search_options->create_item(root); ti->set_text(0, file); if (root->get_first_child() == ti) { ti->select(0); } } } get_ok_button()->set_disabled(root->get_first_child() == nullptr); } void ScriptEditorQuickOpen::_confirmed() { TreeItem *ti = search_options->get_selected(); if (!ti) { return; } int line = ti->get_text(0).get_slicec(':', 1).to_int(); emit_signal(SNAME("goto_line"), line - 1); hide(); } void ScriptEditorQuickOpen::_notification(int p_what) { switch (p_what) { case NOTIFICATION_ENTER_TREE: { connect(SceneStringName(confirmed), callable_mp(this, &ScriptEditorQuickOpen::_confirmed)); } break; case NOTIFICATION_EXIT_TREE: { disconnect(SceneStringName(confirmed), callable_mp(this, &ScriptEditorQuickOpen::_confirmed)); } break; } } void ScriptEditorQuickOpen::_bind_methods() { ADD_SIGNAL(MethodInfo("goto_line", PropertyInfo(Variant::INT, "line"))); } ScriptEditorQuickOpen::ScriptEditorQuickOpen() { set_ok_button_text(TTRC("Open")); get_ok_button()->set_disabled(true); set_hide_on_ok(false); VBoxContainer *vbc = memnew(VBoxContainer); add_child(vbc); search_box = memnew(FilterLineEdit); vbc->add_margin_child(TTRC("Search:"), search_box); search_box->connect(SceneStringName(text_changed), callable_mp(this, &ScriptEditorQuickOpen::_text_changed)); register_text_enter(search_box); search_options = memnew(Tree); search_box->set_forward_control(search_options); search_options->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); search_options->set_hide_root(true); search_options->set_hide_folding(true); search_options->add_theme_constant_override("draw_guides", 1); vbc->add_margin_child(TTRC("Matches:"), search_options, true); search_options->connect("item_activated", callable_mp(this, &ScriptEditorQuickOpen::_confirmed)); } ///////////////////////////////// void DocumentOutline::_notification(int p_what) { switch (p_what) { case NOTIFICATION_THEME_CHANGED: { sort_button->set_button_icon(get_editor_theme_icon(SNAME("Sort"))); update_visibility(); } break; } } void DocumentOutline::_item_list_selected(int p_idx) { Control *active_editor = ScriptEditor::get_singleton()->get_active_editor(); int line = item_list->get_item_metadata(p_idx); if (TextEditorBase *teb = Object::cast_to(active_editor)) { teb->goto_line_centered(line); } else if (EditorHelp *eh = Object::cast_to(active_editor)) { eh->scroll_to_section(line); } } void DocumentOutline::_toggle_sort(bool p_alphabetic_sort) { EditorSettings::get_singleton()->set("text_editor/script_list/sort_members_outline_alphabetically", p_alphabetic_sort); update_outline(); } void DocumentOutline::update_editor_settings() { members_overview_enabled = EDITOR_GET("text_editor/script_list/show_members_overview"); help_overview_enabled = EDITOR_GET("text_editor/help/show_help_index"); update_visibility(); } void DocumentOutline::update_outline() { Control *active_editor = ScriptEditor::get_singleton()->get_active_editor(); item_list->clear(); if (CodeEditorBase *ceb = Object::cast_to(active_editor)) { PackedStringArray functions = ceb->get_functions(); if (EDITOR_GET("text_editor/script_list/sort_members_outline_alphabetically")) { functions.sort(); } const String &filter_text = filter->get_text(); if (filter_text.is_empty()) { for (const String &function_name : functions) { item_list->add_item(function_name.get_slicec(':', 0)); item_list->set_item_metadata(-1, function_name.get_slicec(':', 1).to_int() - 1); } } else { PackedStringArray search_names; for (const String &function_name : functions) { search_names.append(function_name.get_slicec(':', 0)); } FuzzySearch fuzzy; fuzzy.set_case_sensitive(false); Vector> results = fuzzy.search_all(filter_text, search_names); for (const Ref &res : results) { String name = functions[res->get_original_index()].get_slicec(':', 0); int line = functions[res->get_original_index()].get_slicec(':', 1).to_int() - 1; item_list->add_item(name); item_list->set_item_metadata(-1, line); } } } else if (EditorHelp *eh = Object::cast_to(active_editor)) { const Vector> sections = eh->get_sections(); for (const Pair §ion : sections) { item_list->add_item(section.first); item_list->set_item_metadata(-1, section.second); } } } void DocumentOutline::update_visibility() { Control *active_editor = ScriptEditor::get_singleton()->get_active_editor(); ScriptEditorBase *seb = Object::cast_to(active_editor); EditorHelp *eh = Object::cast_to(active_editor); bool members_overview_visible = members_overview_enabled && seb && seb->show_members_overview(); bool help_overview_visible = help_overview_enabled && eh; buttons_hbox->set_visible(members_overview_visible); set_visible(members_overview_visible || help_overview_visible); bool use_monospace_font = members_overview_visible && EDITOR_GET("interface/theme/use_monospace_font_for_editor_symbols"); if (use_monospace_font) { Ref monospace_font = get_theme_font(SNAME("source"), EditorStringName(EditorFonts)); if (item_list->get_theme_font(SceneStringName(font)) != monospace_font) { item_list->add_theme_font_override(SceneStringName(font), monospace_font); } } else if (item_list->has_theme_font_override(SceneStringName(font))) { item_list->remove_theme_font_override(SceneStringName(font)); } } DocumentOutline::DocumentOutline() { buttons_hbox = memnew(HBoxContainer); filter = memnew(FilterLineEdit); filter->set_placeholder(TTRC("Filter Methods")); filter->set_accessibility_name(TTRC("Filter Methods")); filter->set_clear_button_enabled(true); filter->set_h_size_flags(SIZE_EXPAND_FILL); filter->connect(SceneStringName(text_changed), callable_mp(this, &DocumentOutline::update_outline).unbind(1)); buttons_hbox->add_child(filter); sort_button = memnew(Button); sort_button->set_theme_type_variation(SceneStringName(FlatButton)); sort_button->set_tooltip_text(TTRC("Toggle alphabetical sorting of the method list.")); sort_button->set_toggle_mode(true); sort_button->set_pressed(EDITOR_GET("text_editor/script_list/sort_members_outline_alphabetically")); sort_button->connect(SceneStringName(toggled), callable_mp(this, &DocumentOutline::_toggle_sort)); buttons_hbox->add_child(sort_button); add_child(buttons_hbox); item_list = memnew(ItemList); filter->set_forward_control(item_list); item_list->set_auto_translate_mode(AUTO_TRANSLATE_MODE_DISABLED); item_list->set_theme_type_variation("ItemListSecondary"); item_list->set_allow_reselect(true); item_list->set_allow_rmb_select(true); item_list->set_custom_minimum_size(Size2(0, 60) * EDSCALE); item_list->set_v_size_flags(SIZE_EXPAND_FILL); item_list->connect(SceneStringName(item_selected), callable_mp(this, &DocumentOutline::_item_list_selected)); add_child(item_list); } ///////////////////////////////// ScriptEditor *ScriptEditor::script_editor = nullptr; /*** SCRIPT EDITOR ******/ String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *p_se) { if (EDITOR_GET("text_editor/behavior/documentation/enable_tooltips")) { return String(); } // NOTE: See also `ScriptTextEditor::_show_symbol_tooltip()` for documentation tooltips enabled. String debug_value = EditorDebuggerNode::get_singleton()->get_var_value(p_text); if (!debug_value.is_empty()) { constexpr int DISPLAY_LIMIT = 1024; if (debug_value.size() > DISPLAY_LIMIT) { debug_value = debug_value.left(DISPLAY_LIMIT) + "... " + TTR("(truncated)"); } debug_value = TTR("Current value: ") + debug_value; } return debug_value; } void ScriptEditor::_script_created(Ref