Skip to content

Commit

Permalink
Fixes tree compaction for branches
Browse files Browse the repository at this point in the history
  • Loading branch information
eamodio committed Nov 15, 2019
1 parent 46af83f commit 7797103
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 9 deletions.
16 changes: 11 additions & 5 deletions src/system/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ export namespace Arrays {
values: T[],
splitPath: (i: T) => string[],
joinPath: (...paths: string[]) => string,
compact: boolean = false
compact: boolean = false,
canCompact?: (i: T) => boolean
): HierarchicalItem<T> {
const seed = {
name: '',
Expand Down Expand Up @@ -139,7 +140,7 @@ export namespace Arrays {
}, seed);

if (compact) {
hierarchy = compactHierarchy(hierarchy, joinPath, true);
hierarchy = compactHierarchy(hierarchy, joinPath, true, canCompact);
}

return hierarchy;
Expand All @@ -148,22 +149,27 @@ export namespace Arrays {
export function compactHierarchy<T>(
root: HierarchicalItem<T>,
joinPath: (...paths: string[]) => string,
isRoot: boolean = true
isRoot: boolean = true,
canCompact?: (i: T) => boolean
): HierarchicalItem<T> {
if (root.children === undefined) return root;

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

if (!isRoot && children.length === 1) {
const child = children[0];
if (child.value === undefined) {
if (
root.value === undefined &&
(child.value === undefined || canCompact === undefined || canCompact(child.value))
) {
root.name = joinPath(root.name, child.name);
root.relativePath = child.relativePath;
root.children = child.children;
root.descendants = child.descendants;
root.value = child.value;
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/views/nodes/branchNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export class BranchNode extends ViewRefNode<RepositoriesView> implements Pageabl
return BranchNode.getId(this.branch.repoPath, this.branch.name, this.root);
}

compacted: boolean = false;

get current(): boolean {
return this.branch.current;
}
Expand All @@ -48,7 +50,7 @@ export class BranchNode extends ViewRefNode<RepositoriesView> implements Pageabl
const branchName = this.branch.getName();
if (this.view.config.branches.layout === ViewBranchesLayout.List) return branchName;

return (this.root && this.current) || this.branch.detached || this.branch.starred
return this.compacted || this.root || this.current || this.branch.detached || this.branch.starred
? branchName
: this.branch.getBasename();
}
Expand All @@ -58,7 +60,7 @@ export class BranchNode extends ViewRefNode<RepositoriesView> implements Pageabl
}

get treeHierarchy(): string[] {
return this.branch.current || this.branch.detached || this.branch.starred
return this.root || this.current || this.branch.detached || this.branch.starred
? [this.branch.name]
: this.branch.getName().split('/');
}
Expand Down
6 changes: 5 additions & 1 deletion src/views/nodes/branchesNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ export class BranchesNode extends ViewNode<RepositoriesView> {
branchNodes,
n => n.treeHierarchy,
(...paths) => paths.join('/'),
this.view.config.files.compact
this.view.config.files.compact,
b => {
b.compacted = true;
return true;
}
);

const root = new BranchOrTagFolderNode(
Expand Down
6 changes: 5 additions & 1 deletion src/views/nodes/remoteNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,11 @@ export class RemoteNode extends ViewNode<RepositoriesView> {
branchNodes,
n => n.treeHierarchy,
(...paths) => paths.join('/'),
this.view.config.files.compact
this.view.config.files.compact,
b => {
b.compacted = true;
return true;
}
);

const root = new BranchOrTagFolderNode(
Expand Down

0 comments on commit 7797103

Please sign in to comment.