From 4dd84847eb5304d75478ff9b3fab8e209af74308 Mon Sep 17 00:00:00 2001 From: Relintai Date: Sat, 18 Feb 2023 13:22:43 +0100 Subject: [PATCH] Moved ScriptEditor to it's own file. --- editor/connections_dialog.cpp | 2 +- editor/editor_data.cpp | 2 +- editor/editor_export.cpp | 2 +- editor/editor_help.cpp | 2 +- editor/editor_node.cpp | 2 +- editor/editor_plugin.cpp | 2 +- editor/editor_resource_picker.cpp | 3 +- editor/editor_run.cpp | 2 +- editor/plugins/canvas_item_editor_plugin.cpp | 2 +- editor/plugins/spatial_editor_plugin.cpp | 2 +- editor/scene_tree_dock.cpp | 2 +- editor/scene_tree_editor.cpp | 3 +- editor/settings_config_dialog.cpp | 2 +- main/main.cpp | 5 + modules/code_editor/SCsub | 1 + modules/code_editor/code_editor.cpp | 2 + modules/code_editor/register_types.cpp | 1 + modules/code_editor/script_editor.cpp | 3509 ++++++++++++++++++ modules/code_editor/script_editor.h | 427 +++ modules/code_editor/script_editor_plugin.cpp | 3410 +---------------- modules/code_editor/script_editor_plugin.h | 392 +- modules/code_editor/script_text_editor.cpp | 2 + modules/code_editor/script_text_editor.h | 10 +- modules/code_editor/text_editor.cpp | 2 + modules/code_editor/text_editor.h | 2 +- modules/cscript/register_types.cpp | 4 + modules/gdscript/register_types.cpp | 4 + 27 files changed, 3983 insertions(+), 3816 deletions(-) create mode 100644 modules/code_editor/script_editor.cpp create mode 100644 modules/code_editor/script_editor.h diff --git a/editor/connections_dialog.cpp b/editor/connections_dialog.cpp index 83824a8ea..556b9dc4b 100644 --- a/editor/connections_dialog.cpp +++ b/editor/connections_dialog.cpp @@ -75,7 +75,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif static Node *_find_first_script(Node *p_root, Node *p_node) { diff --git a/editor/editor_data.cpp b/editor/editor_data.cpp index 67a0218b8..b0a974759 100644 --- a/editor/editor_data.cpp +++ b/editor/editor_data.cpp @@ -46,7 +46,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif class Resource; diff --git a/editor/editor_export.cpp b/editor/editor_export.cpp index 45c533062..49d66ca20 100644 --- a/editor/editor_export.cpp +++ b/editor/editor_export.cpp @@ -60,7 +60,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif static int _get_pad(int p_alignment, int p_n) { diff --git a/editor/editor_help.cpp b/editor/editor_help.cpp index 4c4b65a6d..1c18c91ab 100644 --- a/editor/editor_help.cpp +++ b/editor/editor_help.cpp @@ -66,7 +66,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif #define CONTRIBUTE_URL vformat("%s/community/contributing/updating_the_class_reference.html", VERSION_DOCS_URL) diff --git a/editor/editor_node.cpp b/editor/editor_node.cpp index 4f09a6d68..c728db343 100644 --- a/editor/editor_node.cpp +++ b/editor/editor_node.cpp @@ -193,7 +193,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #include "modules/code_editor/script_text_editor.h" #include "modules/code_editor/text_editor.h" #endif diff --git a/editor/editor_plugin.cpp b/editor/editor_plugin.cpp index ae9a0a609..f53173e1d 100644 --- a/editor/editor_plugin.cpp +++ b/editor/editor_plugin.cpp @@ -77,7 +77,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif class ConfigFile; diff --git a/editor/editor_resource_picker.cpp b/editor/editor_resource_picker.cpp index 427b4aa0c..f94e12529 100644 --- a/editor/editor_resource_picker.cpp +++ b/editor/editor_resource_picker.cpp @@ -72,7 +72,8 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" +#include "modules/code_editor/script_editor_base.h" #endif HashMap> EditorResourcePicker::allowed_types_cache; diff --git a/editor/editor_run.cpp b/editor/editor_run.cpp index c9edaa2ed..121a17e11 100644 --- a/editor/editor_run.cpp +++ b/editor/editor_run.cpp @@ -45,7 +45,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif EditorRun::Status EditorRun::get_status() const { diff --git a/editor/plugins/canvas_item_editor_plugin.cpp b/editor/plugins/canvas_item_editor_plugin.cpp index 38bda7a4a..37899961b 100644 --- a/editor/plugins/canvas_item_editor_plugin.cpp +++ b/editor/plugins/canvas_item_editor_plugin.cpp @@ -103,7 +103,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif // Min and Max are power of two in order to play nicely with successive increment. diff --git a/editor/plugins/spatial_editor_plugin.cpp b/editor/plugins/spatial_editor_plugin.cpp index 46bc2a279..3b03e4e6c 100644 --- a/editor/plugins/spatial_editor_plugin.cpp +++ b/editor/plugins/spatial_editor_plugin.cpp @@ -102,7 +102,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif #define DISTANCE_DEFAULT 4 diff --git a/editor/scene_tree_dock.cpp b/editor/scene_tree_dock.cpp index ea934ca8a..5f88af47c 100644 --- a/editor/scene_tree_dock.cpp +++ b/editor/scene_tree_dock.cpp @@ -99,7 +99,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif void SceneTreeDock::_nodes_drag_begin() { diff --git a/editor/scene_tree_editor.cpp b/editor/scene_tree_editor.cpp index 51d2e1564..6591520f2 100644 --- a/editor/scene_tree_editor.cpp +++ b/editor/scene_tree_editor.cpp @@ -67,7 +67,8 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" +#include "modules/code_editor/script_editor_base.h" #endif Node *SceneTreeEditor::get_scene_node() { diff --git a/editor/settings_config_dialog.cpp b/editor/settings_config_dialog.cpp index 471a85950..007694e92 100644 --- a/editor/settings_config_dialog.cpp +++ b/editor/settings_config_dialog.cpp @@ -70,7 +70,7 @@ #include "modules/modules_enabled.gen.h" #ifdef MODULE_CODE_EDITOR_ENABLED -#include "modules/code_editor/script_editor_plugin.h" +#include "modules/code_editor/script_editor.h" #endif void EditorSettingsDialog::ok_pressed() { diff --git a/main/main.cpp b/main/main.cpp index 831f9234a..962765fae 100644 --- a/main/main.cpp +++ b/main/main.cpp @@ -84,6 +84,11 @@ #endif #endif +#include "modules/modules_enabled.gen.h" +#ifdef MODULE_CODE_EDITOR_ENABLED +#include "modules/code_editor/script_editor.h" +#endif + /* Static members */ // Singletons diff --git a/modules/code_editor/SCsub b/modules/code_editor/SCsub index e89403978..1fbde99cf 100644 --- a/modules/code_editor/SCsub +++ b/modules/code_editor/SCsub @@ -12,6 +12,7 @@ sources = [ "text_editor.cpp", "script_editor_quick_open.cpp", "script_editor_base.cpp", + "script_editor.cpp", ] env_mlp.add_source_files(env.modules_sources, sources) diff --git a/modules/code_editor/code_editor.cpp b/modules/code_editor/code_editor.cpp index a3c1d18c5..1a32b2de3 100644 --- a/modules/code_editor/code_editor.cpp +++ b/modules/code_editor/code_editor.cpp @@ -65,6 +65,8 @@ #include "scene/resources/font.h" #include "scene/resources/texture.h" +#include "script_editor.h" + void GotoLineDialog::popup_find_line(TextEdit *p_edit) { text_editor = p_edit; diff --git a/modules/code_editor/register_types.cpp b/modules/code_editor/register_types.cpp index 86992976f..2d25c2b58 100644 --- a/modules/code_editor/register_types.cpp +++ b/modules/code_editor/register_types.cpp @@ -6,6 +6,7 @@ #include "script_text_editor.h" #include "text_editor.h" #include "script_editor_base.h" +#include "script_editor.h" void register_code_editor_types(ModuleRegistrationLevel p_level) { if (p_level == MODULE_REGISTRATION_LEVEL_SCENE) { diff --git a/modules/code_editor/script_editor.cpp b/modules/code_editor/script_editor.cpp new file mode 100644 index 000000000..ecaf91a92 --- /dev/null +++ b/modules/code_editor/script_editor.cpp @@ -0,0 +1,3509 @@ + +/*************************************************************************/ +/* 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