Skip to content

Commit 2358518

Browse files
committed
Fixes hierarchical compaction
- Previously given `test` & `test/foo`, `test` would disappear
1 parent 184c8d3 commit 2358518

File tree

3 files changed

+54
-33
lines changed

3 files changed

+54
-33
lines changed

src/system/array.ts

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,7 @@ export function makeHierarchical<T>(
9595
compact: boolean = false,
9696
canCompact?: (i: T) => boolean,
9797
): HierarchicalItem<T> {
98-
const seed = {
99-
name: '',
100-
relativePath: '',
101-
children: new Map(),
102-
descendants: [],
103-
};
98+
const seed = { name: '', relativePath: '', children: new Map(), descendants: [] };
10499

105100
let hierarchy = values.reduce((root: HierarchicalItem<T>, value) => {
106101
let folder = root;
@@ -109,12 +104,9 @@ export function makeHierarchical<T>(
109104
for (const folderName of splitPath(value)) {
110105
relativePath = joinPath(relativePath, folderName);
111106

112-
if (folder.children === undefined) {
113-
folder.children = new Map();
114-
}
115-
107+
folder.children ??= new Map();
116108
let f = folder.children.get(folderName);
117-
if (f === undefined) {
109+
if (f == null) {
118110
f = {
119111
name: folderName,
120112
relativePath: relativePath,
@@ -125,9 +117,7 @@ export function makeHierarchical<T>(
125117
folder.children.set(folderName, f);
126118
}
127119

128-
if (folder.descendants === undefined) {
129-
folder.descendants = [];
130-
}
120+
folder.descendants ??= [];
131121
folder.descendants.push(value);
132122
folder = f;
133123
}
@@ -150,21 +140,28 @@ export function compactHierarchy<T>(
150140
isRoot: boolean = true,
151141
canCompact?: (i: T) => boolean,
152142
): HierarchicalItem<T> {
153-
if (root.children === undefined) return root;
143+
if (root.children == null) return root;
154144

155145
const children = [...root.children.values()];
156146
for (const child of children) {
157147
compactHierarchy(child, joinPath, false, canCompact);
158148
}
159149

160-
if (!isRoot && children.length === 1) {
150+
if (!isRoot && root.value == null && children.length === 1) {
161151
const child = children[0];
162-
if (child.value === undefined || canCompact?.(child.value)) {
152+
if (child.value == null || canCompact?.(child.value)) {
163153
root.name = joinPath(root.name, child.name);
164154
root.relativePath = child.relativePath;
165155
root.children = child.children;
166156
root.descendants = child.descendants;
167157
root.value = child.value;
158+
159+
// Update the parent pointer for the new children if they exist
160+
if (root.children != null) {
161+
for (const grandChild of root.children.values()) {
162+
grandChild.parent = root;
163+
}
164+
}
168165
}
169166
}
170167

src/views/nodes/branchOrTagFolderNode.ts

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from 'vscode';
22
import { GitUri } from '../../git/gitUri';
33
import type { HierarchicalItem } from '../../system/array';
4+
import { first } from '../../system/iterable';
45
import type { View } from '../viewBase';
56
import { ContextValues, getViewNodeId, ViewNode } from './abstract/viewNode';
67
import type { BranchNode } from './branchNode';
@@ -32,12 +33,29 @@ export class BranchOrTagFolderNode extends ViewNode<'branch-tag-folder'> {
3233
}
3334

3435
getChildren(): ViewNode[] {
35-
if (this.root.descendants === undefined || this.root.children === undefined) return [];
36+
if (this.root.descendants == null || this.root.children == null) return [];
3637

3738
const children: (BranchOrTagFolderNode | BranchNode | TagNode | WorktreeNode)[] = [];
3839

3940
for (const folder of this.root.children.values()) {
40-
if (folder.value === undefined) {
41+
if (folder.value != null) {
42+
// Make sure to set the parent
43+
folder.value.parent = this.folderName ? this : this.parent;
44+
children.push(folder.value);
45+
}
46+
47+
if (!folder.children?.size) continue;
48+
if (folder.children.size === 1) {
49+
const child = first(folder.children.values());
50+
if (child?.value != null) {
51+
// Make sure to set the parent
52+
child.value.parent = this.folderName ? this : this.parent;
53+
if ('compacted' in child.value && typeof child.value.compacted === 'boolean') {
54+
child.value.compacted = true;
55+
}
56+
children.push(child.value);
57+
}
58+
} else {
4159
// If the folder contains the current branch or an active worktree, expand it by default
4260
const expand = folder.descendants?.some(
4361
n =>
@@ -56,12 +74,7 @@ export class BranchOrTagFolderNode extends ViewNode<'branch-tag-folder'> {
5674
expand,
5775
),
5876
);
59-
continue;
6077
}
61-
62-
// Make sure to set the parent
63-
folder.value.parent = this.folderName ? this : this.parent;
64-
children.push(folder.value);
6578
}
6679

6780
return children;

src/views/nodes/folderNode.ts

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { ThemeIcon, TreeItem, TreeItemCollapsibleState } from 'vscode';
22
import type { ViewFilesLayout, ViewsFilesConfig } from '../../config';
33
import { GitUri } from '../../git/gitUri';
44
import type { HierarchicalItem } from '../../system/array';
5+
import { first } from '../../system/iterable';
56
import { sortCompare } from '../../system/string';
67
import type { StashesView } from '../stashesView';
78
import type { ViewsWithCommits } from '../viewBase';
@@ -44,22 +45,38 @@ export class FolderNode extends ViewNode<'folder', ViewsWithCommits | StashesVie
4445
}
4546

4647
getChildren(): (FolderNode | FileNode)[] {
47-
if (this.root.descendants === undefined || this.root.children === undefined) return [];
48+
if (this.root.descendants == null || this.root.children == null) return [];
4849

4950
let children: (FolderNode | FileNode)[];
5051

5152
const nesting = FolderNode.getFileNesting(
5253
this.view.config.files,
5354
this.root.descendants,
54-
this.relativePath === undefined,
55+
this.relativePath == null,
5556
);
5657
if (nesting === 'list') {
5758
this.root.descendants.forEach(n => (n.relativePath = this.root.relativePath));
5859
children = this.root.descendants;
5960
} else {
6061
children = [];
6162
for (const folder of this.root.children.values()) {
62-
if (folder.value === undefined) {
63+
if (folder.value != null) {
64+
// Make sure to set the parent
65+
folder.value.parent = this.folderName ? this : this.parent;
66+
folder.value.relativePath = this.root.relativePath;
67+
children.push(folder.value);
68+
}
69+
70+
if (!folder.children?.size) continue;
71+
if (folder.children.size === 1) {
72+
const child = first(folder.children.values());
73+
if (child?.value != null) {
74+
// Make sure to set the parent
75+
child.value.parent = this.folderName ? this : this.parent;
76+
child.value.relativePath = this.root.relativePath;
77+
children.push(child.value);
78+
}
79+
} else {
6380
children.push(
6481
new FolderNode(
6582
this.view,
@@ -71,13 +88,7 @@ export class FolderNode extends ViewNode<'folder', ViewsWithCommits | StashesVie
7188
this.containsWorkingFiles,
7289
),
7390
);
74-
continue;
7591
}
76-
77-
// Make sure to set the parent
78-
folder.value.parent = this.folderName ? this : this.parent;
79-
folder.value.relativePath = this.root.relativePath;
80-
children.push(folder.value);
8192
}
8293
}
8394

0 commit comments

Comments
 (0)