|
1 | 1 | use asyncgit::StatusItem;
|
2 | 2 | use std::{
|
3 |
| - collections::{BTreeSet, BinaryHeap}, |
| 3 | + collections::BTreeSet, |
4 | 4 | convert::TryFrom,
|
5 | 5 | ops::{Index, IndexMut},
|
6 | 6 | path::Path,
|
@@ -122,23 +122,25 @@ impl FileTreeItems {
|
122 | 122 | list: &[StatusItem],
|
123 | 123 | collapsed: &BTreeSet<&String>,
|
124 | 124 | ) -> Self {
|
125 |
| - let mut nodes = BinaryHeap::with_capacity(list.len()); |
| 125 | + let mut nodes = Vec::with_capacity(list.len()); |
126 | 126 | let mut paths_added = BTreeSet::new();
|
127 | 127 |
|
128 | 128 | for e in list {
|
129 |
| - let item_path = Path::new(&e.path); |
130 |
| - |
131 |
| - FileTreeItems::push_dirs( |
132 |
| - item_path, |
133 |
| - &mut nodes, |
134 |
| - &mut paths_added, |
135 |
| - &collapsed, |
136 |
| - ); |
| 129 | + { |
| 130 | + let item_path = Path::new(&e.path); |
| 131 | + |
| 132 | + FileTreeItems::push_dirs( |
| 133 | + item_path, |
| 134 | + &mut nodes, |
| 135 | + &mut paths_added, |
| 136 | + &collapsed, |
| 137 | + ); |
| 138 | + } |
137 | 139 |
|
138 |
| - nodes.push(FileTreeItem::new_file(e)); |
| 140 | + nodes.push(FileTreeItem::new_file(&e)); |
139 | 141 | }
|
140 | 142 |
|
141 |
| - Self(nodes.into_sorted_vec()) |
| 143 | + Self(nodes) |
142 | 144 | }
|
143 | 145 |
|
144 | 146 | ///
|
@@ -173,11 +175,15 @@ impl FileTreeItems {
|
173 | 175 |
|
174 | 176 | fn push_dirs<'a>(
|
175 | 177 | item_path: &'a Path,
|
176 |
| - nodes: &mut BinaryHeap<FileTreeItem>, |
| 178 | + nodes: &mut Vec<FileTreeItem>, |
177 | 179 | paths_added: &mut BTreeSet<&'a Path>,
|
178 | 180 | collapsed: &BTreeSet<&String>,
|
179 | 181 | ) {
|
180 |
| - for c in item_path.ancestors().skip(1) { |
| 182 | + let mut ancestors = |
| 183 | + { item_path.ancestors().skip(1).collect::<Vec<_>>() }; |
| 184 | + ancestors.reverse(); |
| 185 | + |
| 186 | + for c in &ancestors { |
181 | 187 | if c.parent().is_some() {
|
182 | 188 | let path_string = String::from(c.to_str().unwrap());
|
183 | 189 | if !paths_added.contains(c) {
|
@@ -290,6 +296,24 @@ mod tests {
|
290 | 296 | assert_eq!(res.next(), Some((2, "file.txt")));
|
291 | 297 | }
|
292 | 298 |
|
| 299 | + #[test] |
| 300 | + fn test_indent_folder_file_name() { |
| 301 | + let items = string_vec_to_status(&[ |
| 302 | + "a/b", // |
| 303 | + "a.txt", // |
| 304 | + ]); |
| 305 | + |
| 306 | + let list = FileTreeItems::new(&items, &BTreeSet::new()); |
| 307 | + let mut res = list |
| 308 | + .0 |
| 309 | + .iter() |
| 310 | + .map(|i| (i.info.indent, i.info.path.as_str())); |
| 311 | + |
| 312 | + assert_eq!(res.next(), Some((0, "a"))); |
| 313 | + assert_eq!(res.next(), Some((1, "b"))); |
| 314 | + assert_eq!(res.next(), Some((0, "a.txt"))); |
| 315 | + } |
| 316 | + |
293 | 317 | #[test]
|
294 | 318 | fn test_folder_dup() {
|
295 | 319 | let items = string_vec_to_status(&[
|
|
0 commit comments