Skip to content

Commit d48fdd2

Browse files
author
Stephan Dilly
committed
fix folder indenting when a file starting exactly like the folder exists next to it
1 parent 6e9836b commit d48fdd2

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed

asyncgit/src/sync/status.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::sync::utils;
44
use git2::{Status, StatusOptions, StatusShow};
55
use scopetime::scope_time;
6+
use std::path::Path;
67

78
///
89
#[derive(Copy, Clone, Hash, PartialEq, Debug)]
@@ -100,5 +101,9 @@ pub fn get_status(
100101
});
101102
}
102103

104+
res.sort_by(|a, b| {
105+
Path::new(a.path.as_str()).cmp(Path::new(b.path.as_str()))
106+
});
107+
103108
res
104109
}

src/components/filetree.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use asyncgit::StatusItem;
22
use std::{
3-
collections::{BTreeSet, BinaryHeap},
3+
collections::BTreeSet,
44
convert::TryFrom,
55
ops::{Index, IndexMut},
66
path::Path,
@@ -122,23 +122,25 @@ impl FileTreeItems {
122122
list: &[StatusItem],
123123
collapsed: &BTreeSet<&String>,
124124
) -> Self {
125-
let mut nodes = BinaryHeap::with_capacity(list.len());
125+
let mut nodes = Vec::with_capacity(list.len());
126126
let mut paths_added = BTreeSet::new();
127127

128128
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+
}
137139

138-
nodes.push(FileTreeItem::new_file(e));
140+
nodes.push(FileTreeItem::new_file(&e));
139141
}
140142

141-
Self(nodes.into_sorted_vec())
143+
Self(nodes)
142144
}
143145

144146
///
@@ -173,11 +175,15 @@ impl FileTreeItems {
173175

174176
fn push_dirs<'a>(
175177
item_path: &'a Path,
176-
nodes: &mut BinaryHeap<FileTreeItem>,
178+
nodes: &mut Vec<FileTreeItem>,
177179
paths_added: &mut BTreeSet<&'a Path>,
178180
collapsed: &BTreeSet<&String>,
179181
) {
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 {
181187
if c.parent().is_some() {
182188
let path_string = String::from(c.to_str().unwrap());
183189
if !paths_added.contains(c) {
@@ -290,6 +296,24 @@ mod tests {
290296
assert_eq!(res.next(), Some((2, "file.txt")));
291297
}
292298

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+
293317
#[test]
294318
fn test_folder_dup() {
295319
let items = string_vec_to_status(&[

src/components/statustree.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,7 @@ mod tests {
340340

341341
assert_eq!(res.selection, Some(0));
342342

343-
res.update(&string_vec_to_status(&["b", "a"]));
343+
res.update(&string_vec_to_status(&["a", "b"]));
344344

345345
assert_eq!(res.selection, Some(1));
346346
}

0 commit comments

Comments
 (0)