From 7b3e1a5bde17eb058eba1ab70000b4474fa0aa8f Mon Sep 17 00:00:00 2001 From: Micky Date: Mon, 15 Jan 2024 00:48:06 +0100 Subject: [PATCH] Optimize SceneTree's `change_scene_to_file` autocompletion --- modules/gdscript/gdscript_editor.cpp | 18 ++++++++-- scene/main/scene_tree.cpp | 49 ++++++---------------------- 2 files changed, 26 insertions(+), 41 deletions(-) diff --git a/modules/gdscript/gdscript_editor.cpp b/modules/gdscript/gdscript_editor.cpp index babd2c177210..12ff22c878a3 100644 --- a/modules/gdscript/gdscript_editor.cpp +++ b/modules/gdscript/gdscript_editor.cpp @@ -828,17 +828,21 @@ static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_functio return arghint; } -static void _get_directory_contents(EditorFileSystemDirectory *p_dir, HashMap &r_list) { +static void _get_directory_contents(EditorFileSystemDirectory *p_dir, HashMap &r_list, const StringName &p_required_type = StringName()) { const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\""; + const bool requires_type = p_required_type; for (int i = 0; i < p_dir->get_file_count(); i++) { + if (requires_type && !ClassDB::is_parent_class(p_dir->get_file_type(i), p_required_type)) { + continue; + } ScriptLanguage::CodeCompletionOption option(p_dir->get_file_path(i), ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH); option.insert_text = option.display.quote(quote_style); r_list.insert(option.display, option); } for (int i = 0; i < p_dir->get_subdir_count(); i++) { - _get_directory_contents(p_dir->get_subdir(i), r_list); + _get_directory_contents(p_dir->get_subdir(i), r_list, p_required_type); } } @@ -2803,6 +2807,16 @@ static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, c r_result.insert(option.display, option); } } + if (EDITOR_GET("text_editor/completion/complete_file_paths")) { + if (p_argidx == 0 && p_method == SNAME("change_scene_to_file") && ClassDB::is_parent_class(class_name, SNAME("SceneTree"))) { + HashMap list; + _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), list, SNAME("PackedScene")); + for (const KeyValue &key_value_pair : list) { + ScriptLanguage::CodeCompletionOption option = key_value_pair.value; + r_result.insert(option.display, option); + } + } + } base_type.kind = GDScriptParser::DataType::UNRESOLVED; } break; diff --git a/scene/main/scene_tree.cpp b/scene/main/scene_tree.cpp index ac6ff30de01c..cb16f425b56b 100644 --- a/scene/main/scene_tree.cpp +++ b/scene/main/scene_tree.cpp @@ -1693,45 +1693,16 @@ void SceneTree::add_idle_callback(IdleCallback p_callback) { #ifdef TOOLS_ENABLED void SceneTree::get_argument_options(const StringName &p_function, int p_idx, List *r_options) const { const String pf = p_function; - if (pf == "change_scene_to_file") { - Ref dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES); - List directories; - directories.push_back(dir_access->get_current_dir()); - - while (!directories.is_empty()) { - dir_access->change_dir(directories.back()->get()); - directories.pop_back(); - - dir_access->list_dir_begin(); - String filename = dir_access->get_next(); - - while (!filename.is_empty()) { - if (filename == "." || filename == "..") { - filename = dir_access->get_next(); - continue; - } - - if (dir_access->dir_exists(filename)) { - directories.push_back(dir_access->get_current_dir().path_join(filename)); - } else if (filename.ends_with(".tscn") || filename.ends_with(".scn")) { - r_options->push_back("\"" + dir_access->get_current_dir().path_join(filename) + "\""); - } - - filename = dir_access->get_next(); - } - } - } else { - bool add_options = false; - if (p_idx == 0) { - add_options = pf == "get_nodes_in_group" || pf == "has_group" || pf == "get_first_node_in_group" || pf == "set_group" || pf == "notify_group" || pf == "call_group" || pf == "add_to_group"; - } else if (p_idx == 1) { - add_options = pf == "set_group_flags" || pf == "call_group_flags" || pf == "notify_group_flags"; - } - if (add_options) { - HashMap global_groups = ProjectSettings::get_singleton()->get_global_groups_list(); - for (const KeyValue &E : global_groups) { - r_options->push_back(E.key.operator String().quote()); - } + bool add_options = false; + if (p_idx == 0) { + add_options = pf == "get_nodes_in_group" || pf == "has_group" || pf == "get_first_node_in_group" || pf == "set_group" || pf == "notify_group" || pf == "call_group" || pf == "add_to_group"; + } else if (p_idx == 1) { + add_options = pf == "set_group_flags" || pf == "call_group_flags" || pf == "notify_group_flags"; + } + if (add_options) { + HashMap global_groups = ProjectSettings::get_singleton()->get_global_groups_list(); + for (const KeyValue &E : global_groups) { + r_options->push_back(E.key.operator String().quote()); } } MainLoop::get_argument_options(p_function, p_idx, r_options);