From 7797103321db952fd8fe131f2f381102f0d8cd16 Mon Sep 17 00:00:00 2001 From: Eric Amodio Date: Fri, 15 Nov 2019 02:33:31 -0500 Subject: [PATCH] Fixes tree compaction for branches --- src/system/array.ts | 16 +++++++++++----- src/views/nodes/branchNode.ts | 6 ++++-- src/views/nodes/branchesNode.ts | 6 +++++- src/views/nodes/remoteNode.ts | 6 +++++- 4 files changed, 25 insertions(+), 9 deletions(-) diff --git a/src/system/array.ts b/src/system/array.ts index fdfb21b..1d83e34 100644 --- a/src/system/array.ts +++ b/src/system/array.ts @@ -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 { const seed = { name: '', @@ -139,7 +140,7 @@ export namespace Arrays { }, seed); if (compact) { - hierarchy = compactHierarchy(hierarchy, joinPath, true); + hierarchy = compactHierarchy(hierarchy, joinPath, true, canCompact); } return hierarchy; @@ -148,22 +149,27 @@ export namespace Arrays { export function compactHierarchy( root: HierarchicalItem, joinPath: (...paths: string[]) => string, - isRoot: boolean = true + isRoot: boolean = true, + canCompact?: (i: T) => boolean ): HierarchicalItem { 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; } } diff --git a/src/views/nodes/branchNode.ts b/src/views/nodes/branchNode.ts index a980169..b073e65 100644 --- a/src/views/nodes/branchNode.ts +++ b/src/views/nodes/branchNode.ts @@ -40,6 +40,8 @@ export class BranchNode extends ViewRefNode implements Pageabl return BranchNode.getId(this.branch.repoPath, this.branch.name, this.root); } + compacted: boolean = false; + get current(): boolean { return this.branch.current; } @@ -48,7 +50,7 @@ export class BranchNode extends ViewRefNode 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(); } @@ -58,7 +60,7 @@ export class BranchNode extends ViewRefNode 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('/'); } diff --git a/src/views/nodes/branchesNode.ts b/src/views/nodes/branchesNode.ts index bcfa287..0ab2013 100644 --- a/src/views/nodes/branchesNode.ts +++ b/src/views/nodes/branchesNode.ts @@ -43,7 +43,11 @@ export class BranchesNode extends ViewNode { 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( diff --git a/src/views/nodes/remoteNode.ts b/src/views/nodes/remoteNode.ts index a836a97..8a2ffda 100644 --- a/src/views/nodes/remoteNode.ts +++ b/src/views/nodes/remoteNode.ts @@ -51,7 +51,11 @@ export class RemoteNode extends ViewNode { 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(