From d48fdd2774ca9463667b3366241577efa1ee4a7b Mon Sep 17 00:00:00 2001 From: Stephan Dilly Date: Tue, 28 Apr 2020 17:55:08 +0200 Subject: [PATCH] fix folder indenting when a file starting exactly like the folder exists next to it --- asyncgit/src/sync/status.rs | 5 ++++ src/components/filetree.rs | 50 ++++++++++++++++++++++++++---------- src/components/statustree.rs | 2 +- 3 files changed, 43 insertions(+), 14 deletions(-) diff --git a/asyncgit/src/sync/status.rs b/asyncgit/src/sync/status.rs index af44ddad..a3cfb547 100644 --- a/asyncgit/src/sync/status.rs +++ b/asyncgit/src/sync/status.rs @@ -3,6 +3,7 @@ use crate::sync::utils; use git2::{Status, StatusOptions, StatusShow}; use scopetime::scope_time; +use std::path::Path; /// #[derive(Copy, Clone, Hash, PartialEq, Debug)] @@ -100,5 +101,9 @@ pub fn get_status( }); } + res.sort_by(|a, b| { + Path::new(a.path.as_str()).cmp(Path::new(b.path.as_str())) + }); + res } diff --git a/src/components/filetree.rs b/src/components/filetree.rs index 1a873be4..46923f2d 100644 --- a/src/components/filetree.rs +++ b/src/components/filetree.rs @@ -1,6 +1,6 @@ use asyncgit::StatusItem; use std::{ - collections::{BTreeSet, BinaryHeap}, + collections::BTreeSet, convert::TryFrom, ops::{Index, IndexMut}, path::Path, @@ -122,23 +122,25 @@ impl FileTreeItems { list: &[StatusItem], collapsed: &BTreeSet<&String>, ) -> Self { - let mut nodes = BinaryHeap::with_capacity(list.len()); + let mut nodes = Vec::with_capacity(list.len()); let mut paths_added = BTreeSet::new(); for e in list { - let item_path = Path::new(&e.path); + { + let item_path = Path::new(&e.path); - FileTreeItems::push_dirs( - item_path, - &mut nodes, - &mut paths_added, - &collapsed, - ); + FileTreeItems::push_dirs( + item_path, + &mut nodes, + &mut paths_added, + &collapsed, + ); + } - nodes.push(FileTreeItem::new_file(e)); + nodes.push(FileTreeItem::new_file(&e)); } - Self(nodes.into_sorted_vec()) + Self(nodes) } /// @@ -173,11 +175,15 @@ impl FileTreeItems { fn push_dirs<'a>( item_path: &'a Path, - nodes: &mut BinaryHeap, + nodes: &mut Vec, paths_added: &mut BTreeSet<&'a Path>, collapsed: &BTreeSet<&String>, ) { - for c in item_path.ancestors().skip(1) { + let mut ancestors = + { item_path.ancestors().skip(1).collect::>() }; + ancestors.reverse(); + + for c in &ancestors { if c.parent().is_some() { let path_string = String::from(c.to_str().unwrap()); if !paths_added.contains(c) { @@ -290,6 +296,24 @@ mod tests { assert_eq!(res.next(), Some((2, "file.txt"))); } + #[test] + fn test_indent_folder_file_name() { + let items = string_vec_to_status(&[ + "a/b", // + "a.txt", // + ]); + + let list = FileTreeItems::new(&items, &BTreeSet::new()); + let mut res = list + .0 + .iter() + .map(|i| (i.info.indent, i.info.path.as_str())); + + assert_eq!(res.next(), Some((0, "a"))); + assert_eq!(res.next(), Some((1, "b"))); + assert_eq!(res.next(), Some((0, "a.txt"))); + } + #[test] fn test_folder_dup() { let items = string_vec_to_status(&[ diff --git a/src/components/statustree.rs b/src/components/statustree.rs index 8ae4410a..c657a426 100644 --- a/src/components/statustree.rs +++ b/src/components/statustree.rs @@ -340,7 +340,7 @@ mod tests { assert_eq!(res.selection, Some(0)); - res.update(&string_vec_to_status(&["b", "a"])); + res.update(&string_vec_to_status(&["a", "b"])); assert_eq!(res.selection, Some(1)); }