Skip to content

Commit 113621d

Browse files
committed
Don't rebuild tree when navigating to path
1 parent 47bc374 commit 113621d

File tree

2 files changed

+49
-4
lines changed

2 files changed

+49
-4
lines changed

editor/filesystem_dock.cpp

+48-4
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ void FileSystemDock::_create_tree(TreeItem *p_parent, EditorFileSystemDirectory
245245
}
246246
subdirectory_item->set_selectable(0, true);
247247
subdirectory_item->set_metadata(0, lpath);
248+
folder_map[lpath] = subdirectory_item;
249+
248250
if (!p_select_in_favorites && (current_path == lpath || ((display_mode != DISPLAY_MODE_TREE_ONLY) && current_path.get_base_dir() == lpath))) {
249251
subdirectory_item->select(0);
250252
// Keep select an item when re-created a tree
@@ -371,6 +373,7 @@ void FileSystemDock::_update_tree(const Vector<String> &p_uncollapsed_paths, boo
371373
tree_update_id++;
372374
updating_tree = true;
373375
TreeItem *root = tree->create_item();
376+
folder_map.clear();
374377

375378
// Handles the favorites.
376379
favorites_item = tree->create_item(root);
@@ -698,19 +701,21 @@ void FileSystemDock::_set_current_path_line_edit_text(const String &p_path) {
698701
}
699702

700703
void FileSystemDock::_navigate_to_path(const String &p_path, bool p_select_in_favorites) {
704+
bool is_directory = false;
701705
if (p_path == "Favorites") {
702706
current_path = p_path;
703707
} else {
704708
String target_path = p_path;
705709
// If the path is a file, do not only go to the directory in the tree, also select the file in the file list.
706710
if (target_path.ends_with("/")) {
707-
target_path = target_path.substr(0, target_path.length() - 1);
711+
target_path = target_path.trim_suffix("/");
708712
}
709713
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
710714
if (da->file_exists(p_path)) {
711715
current_path = target_path;
712716
} else if (da->dir_exists(p_path)) {
713717
current_path = target_path + "/";
718+
is_directory = true;
714719
} else {
715720
ERR_FAIL_MSG(vformat("Cannot navigate to '%s' as it has not been found in the file system!", p_path));
716721
}
@@ -719,17 +724,56 @@ void FileSystemDock::_navigate_to_path(const String &p_path, bool p_select_in_fa
719724
_set_current_path_line_edit_text(current_path);
720725
_push_to_history();
721726

722-
_update_tree(get_uncollapsed_paths(), false, p_select_in_favorites, true);
727+
const String file_name = is_directory ? p_path.trim_suffix("/").get_file() + "/" : p_path.get_file();
728+
bool found = false;
729+
730+
TreeItem **base_dir_ptr;
731+
{
732+
const String base_dir = current_path.get_base_dir();
733+
if (base_dir == "res://") {
734+
base_dir_ptr = folder_map.getptr(base_dir);
735+
} else if (is_directory) {
736+
base_dir_ptr = folder_map.getptr(base_dir.get_base_dir() + "/");
737+
} else {
738+
base_dir_ptr = folder_map.getptr(base_dir + "/");
739+
}
740+
}
741+
742+
if (base_dir_ptr) {
743+
TreeItem *directory = *base_dir_ptr;
744+
{
745+
TreeItem *entry = directory->get_first_child();
746+
while (entry) {
747+
if (entry->get_metadata(0).operator String().ends_with(file_name)) {
748+
tree->deselect_all();
749+
entry->select(0);
750+
found = true;
751+
break;
752+
}
753+
entry = entry->get_next();
754+
}
755+
}
756+
757+
while (directory) {
758+
directory->set_collapsed(false);
759+
directory = directory->get_parent();
760+
}
761+
}
762+
763+
if (!found) {
764+
return;
765+
}
766+
767+
tree->ensure_cursor_is_visible();
723768
if (display_mode != DISPLAY_MODE_TREE_ONLY) {
724769
_update_file_list(false);
725770

726771
// Reset the scroll for a directory.
727-
if (p_path.ends_with("/")) {
772+
if (is_directory) {
728773
files->get_v_scroll_bar()->set_value(0);
729774
}
730775
}
731776

732-
String file_name = p_path.get_file();
733777
if (!file_name.is_empty()) {
734778
for (int i = 0; i < files->get_item_count(); i++) {
735779
if (files->get_item_text(i) == file_name) {

editor/filesystem_dock.h

+1
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ class FileSystemDock : public VBoxContainer {
133133
CONVERT_BASE_ID = 1000,
134134
};
135135

136+
HashMap<String, TreeItem *> folder_map;
136137
HashMap<String, Color> folder_colors;
137138
Dictionary assigned_folder_colors;
138139

0 commit comments

Comments
 (0)