Skip to content

Commit a79a4b5

Browse files
committed
Fix FileSystem navigation when using Split Mode
1 parent b9437c3 commit a79a4b5

File tree

1 file changed

+35
-61
lines changed

1 file changed

+35
-61
lines changed

editor/filesystem_dock.cpp

Lines changed: 35 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -705,88 +705,62 @@ void FileSystemDock::_set_current_path_line_edit_text(const String &p_path) {
705705
}
706706

707707
void FileSystemDock::_navigate_to_path(const String &p_path, bool p_select_in_favorites) {
708+
String target_path = p_path;
708709
bool is_directory = false;
709-
if (p_path == "Favorites") {
710-
current_path = p_path;
711-
} else {
712-
String target_path = p_path;
713-
// If the path is a file, do not only go to the directory in the tree, also select the file in the file list.
714-
if (target_path.ends_with("/")) {
715-
target_path = target_path.trim_suffix("/");
716-
}
710+
711+
if (p_path.is_empty()) {
712+
target_path = "res://";
713+
is_directory = true;
714+
} else if (p_path != "Favorites") {
717715
Ref<DirAccess> da = DirAccess::create(DirAccess::ACCESS_RESOURCES);
718-
if (da->file_exists(p_path)) {
719-
current_path = target_path;
720-
} else if (da->dir_exists(p_path)) {
721-
current_path = target_path + "/";
716+
if (da->dir_exists(p_path)) {
722717
is_directory = true;
723-
} else {
718+
if (!p_path.ends_with("/")) {
719+
target_path += "/";
720+
}
721+
} else if (!da->file_exists(p_path)) {
724722
ERR_FAIL_MSG(vformat("Cannot navigate to '%s' as it has not been found in the file system!", p_path));
725723
}
726724
}
727725

726+
current_path = target_path;
728727
_set_current_path_line_edit_text(current_path);
729728
_push_to_history();
730729

731-
const String file_name = is_directory ? p_path.trim_suffix("/").get_file() + "/" : p_path.get_file();
732-
bool found = false;
733-
734-
TreeItem **base_dir_ptr;
735-
{
736-
const String base_dir = current_path.get_base_dir();
737-
if (base_dir == "res://") {
738-
base_dir_ptr = folder_map.getptr(base_dir);
739-
} else if (is_directory) {
740-
base_dir_ptr = folder_map.getptr(base_dir.get_base_dir() + "/");
741-
} else {
742-
base_dir_ptr = folder_map.getptr(base_dir + "/");
743-
}
730+
String base_dir_path = target_path.get_base_dir();
731+
if (base_dir_path != "res://") {
732+
base_dir_path += "/";
744733
}
745734

746-
if (base_dir_ptr) {
747-
TreeItem *directory = *base_dir_ptr;
748-
{
749-
TreeItem *entry = directory->get_first_child();
750-
while (entry) {
751-
if (entry->get_metadata(0).operator String().ends_with(file_name)) {
752-
tree->deselect_all();
753-
entry->select(0);
754-
found = true;
755-
break;
756-
}
757-
entry = entry->get_next();
758-
}
759-
}
760-
761-
while (directory) {
762-
directory->set_collapsed(false);
763-
directory = directory->get_parent();
764-
}
765-
}
766-
767-
if (!found) {
735+
TreeItem **directory_ptr = folder_map.getptr(base_dir_path);
736+
if (!directory_ptr) {
768737
return;
769738
}
770739

771-
tree->ensure_cursor_is_visible();
772-
if (display_mode != DISPLAY_MODE_TREE_ONLY) {
773-
_update_file_list(false);
774-
775-
// Reset the scroll for a directory.
776-
if (is_directory) {
777-
files->get_v_scroll_bar()->set_value(0);
778-
}
740+
// Unfold all folders along the path.
741+
TreeItem *ti = *directory_ptr;
742+
while (ti) {
743+
ti->set_collapsed(false);
744+
ti = ti->get_parent();
779745
}
780746

781-
if (!file_name.is_empty()) {
782-
for (int i = 0; i < files->get_item_count(); i++) {
783-
if (files->get_item_text(i) == file_name) {
784-
files->select(i, true);
785-
files->ensure_current_is_visible();
747+
// Select the file or directory in the tree.
748+
tree->deselect_all();
749+
if (display_mode == DISPLAY_MODE_TREE_ONLY) {
750+
const String file_name = is_directory ? target_path.trim_suffix("/").get_file() + "/" : target_path.get_file();
751+
TreeItem *item = is_directory ? *directory_ptr : (*directory_ptr)->get_first_child();
752+
while (item) {
753+
if (item->get_metadata(0).operator String().ends_with(file_name)) {
754+
item->select(0);
786755
break;
787756
}
757+
item = item->get_next();
788758
}
759+
} else {
760+
(*directory_ptr)->select(0);
761+
_update_file_list(false);
789762
}
763+
tree->ensure_cursor_is_visible();
790764
}
791765

792766
bool FileSystemDock::_update_filtered_items(TreeItem *p_tree_item) {

0 commit comments

Comments
 (0)