Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize SceneTree's change_scene_to_file autocompletion #87197

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions modules/gdscript/gdscript_editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, ScriptLanguage::CodeCompletionOption> &r_list) {
static void _get_directory_contents(EditorFileSystemDirectory *p_dir, HashMap<String, ScriptLanguage::CodeCompletionOption> &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);
}
}

Expand Down Expand Up @@ -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<String, ScriptLanguage::CodeCompletionOption> list;
_get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), list, SNAME("PackedScene"));
for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &key_value_pair : list) {
ScriptLanguage::CodeCompletionOption option = key_value_pair.value;
r_result.insert(option.display, option);
}
}
}

base_type.kind = GDScriptParser::DataType::UNRESOLVED;
} break;
Expand Down
49 changes: 10 additions & 39 deletions scene/main/scene_tree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> *r_options) const {
const String pf = p_function;
if (pf == "change_scene_to_file") {
Ref<DirAccess> dir_access = DirAccess::create(DirAccess::ACCESS_RESOURCES);
List<String> 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<StringName, String> global_groups = ProjectSettings::get_singleton()->get_global_groups_list();
for (const KeyValue<StringName, String> &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<StringName, String> global_groups = ProjectSettings::get_singleton()->get_global_groups_list();
for (const KeyValue<StringName, String> &E : global_groups) {
r_options->push_back(E.key.operator String().quote());
}
}
MainLoop::get_argument_options(p_function, p_idx, r_options);
Expand Down
Loading