/*************************************************************************/ /* script_editor_plugin.cpp */ /*************************************************************************/ /* This file is part of: */ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ /* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ /* Copyright (c) 2014-2022 Godot Engine contributors (cf. AUTHORS.md). */ /* */ /* 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.h" #include "core/config/project_settings.h" #include "core/containers/pair.h" #include "core/containers/rb_map.h" #include "core/error/error_macros.h" #include "core/input/input.h" #include "core/input/input_event.h" #include "core/io/config_file.h" #include "core/io/resource_loader.h" #include "core/io/resource_saver.h" #include "core/math/color.h" #include "core/math/math_funcs.h" #include "core/math/transform_2d.h" #include "core/object/class_db.h" #include "core/object/undo_redo.h" #include "core/os/file_access.h" #include "core/os/keyboard.h" #include "core/os/main_loop.h" #include "core/os/memory.h" #include "core/os/os.h" #include "core/string/string_name.h" #include "core/variant/dictionary.h" #include "core/version_generated.gen.h" #include "editor/editor_data.h" #include "editor/editor_file_dialog.h" #include "editor/editor_file_system.h" #include "editor/editor_help.h" #include "editor/editor_help_search.h" #include "editor/editor_node.h" #include "editor/editor_run_script.h" #include "editor/editor_scale.h" #include "editor/editor_settings.h" #include "editor/filesystem_dock.h" #include "editor/find_in_files.h" #include "editor/inspector_dock.h" #include "editor/node_dock.h" #include "editor/script_create_dialog.h" #include "editor/script_editor_debugger.h" #include "scene/2d/canvas_item.h" #include "scene/gui/button.h" #include "scene/gui/control.h" #include "scene/gui/item_list.h" #include "scene/gui/label.h" #include "scene/gui/line_edit.h" #include "scene/gui/menu_button.h" #include "scene/gui/popup_menu.h" #include "scene/gui/separator.h" #include "scene/gui/split_container.h" #include "scene/gui/tab_container.h" #include "scene/gui/text_edit.h" #include "scene/gui/texture_rect.h" #include "scene/gui/tool_button.h" #include "scene/gui/tree.h" #include "scene/main/node.h" #include "scene/main/scene_tree.h" #include "scene/main/timer.h" #include "scene/main/viewport.h" #include "scene/resources/text_file.h" #include "scene/resources/texture.h" #include "scene/scene_string_names.h" #include "script_editor_quick_open.h" #include "script_text_editor.h" #include "text_editor.h" #include "modules/modules_enabled.gen.h" #ifdef MODULE_SHADER_EDITOR_ENABLED #include "shader_editor/shader_editor_plugin.h" #endif static bool _is_built_in_script(Script *p_script) { String path = p_script->get_path(); return path.find("::") != -1; } class EditorScriptCodeCompletionCache : public ScriptCodeCompletionCache { struct Cache { uint64_t time_loaded; RES cache; Cache() { time_loaded = 0; } }; RBMap cached; public: uint64_t max_time_cache; int max_cache_size; void cleanup() { List::Element *> to_clean; RBMap::Element *I = cached.front(); while (I) { if ((OS::get_singleton()->get_ticks_msec() - I->get().time_loaded) > max_time_cache) { to_clean.push_back(I); } I = I->next(); } while (to_clean.front()) { cached.erase(to_clean.front()->get()); to_clean.pop_front(); } } virtual RES get_cached_resource(const String &p_path) { RBMap::Element *E = cached.find(p_path); if (!E) { Cache c; c.cache = ResourceLoader::load(p_path); E = cached.insert(p_path, c); } E->get().time_loaded = OS::get_singleton()->get_ticks_msec(); if (cached.size() > max_cache_size) { uint64_t older; RBMap::Element *O = cached.front(); older = O->get().time_loaded; RBMap::Element *I = O; while (I) { if (I->get().time_loaded < older) { older = I->get().time_loaded; O = I; } I = I->next(); } if (O != E) { //should never happen.. cached.erase(O); } } return E->get().cache; } EditorScriptCodeCompletionCache() { max_cache_size = 128; max_time_cache = 5 * 60 * 1000; //minutes, five } virtual ~EditorScriptCodeCompletionCache() {} }; ///////////////////////////////// ScriptEditor *ScriptEditor::script_editor = nullptr; /*** SCRIPT EDITOR ******/ String ScriptEditor::_get_debug_tooltip(const String &p_text, Node *_se) { String val = debugger->get_var_value(p_text); if (val != String()) { return p_text + ": " + val; } else { return String(); } } void ScriptEditor::_breaked(bool p_breaked, bool p_can_debug) { if (bool(EditorSettings::get_singleton()->get("text_editor/external/use_external_editor"))) { return; } debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_NEXT), !(p_breaked && p_can_debug)); debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_STEP), !(p_breaked && p_can_debug)); debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_BREAK), p_breaked); debug_menu->get_popup()->set_item_disabled(debug_menu->get_popup()->get_item_index(DEBUG_CONTINUE), !p_breaked); for (int i = 0; i < tab_container->get_child_count(); i++) { ScriptEditorBase *se = Object::cast_to(tab_container->get_child(i)); if (!se) { continue; } se->set_debugger_active(p_breaked); } } void ScriptEditor::_show_debugger(bool p_show) { } void ScriptEditor::_script_created(Ref