Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve folder coloring #107

Merged
merged 1 commit into from
Jun 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion web/src/App.tsx

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions web/src/Explorer/FolderState.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ function it (
fileTree.pushNode(newNode(node))
}
if (input.squash) fileTree.squash()
fileTree.order()
const evs: Message[] = []
const folderState = FolderState.fromFileTree(fileTree)
folderState.registerListener('test', (m) => evs.push(m))
Expand Down
6 changes: 2 additions & 4 deletions web/src/Explorer/FolderState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,13 @@ export class FolderState<F> {

static fromFileTree<T extends ColoredFileLeaf> (tree: FileTree<T>): FolderState<T> {
const folderState = new FolderState<T>()
const folderKeys = [...tree.subTrees.keys()].sort()
for (const folder of folderKeys) {
for (const folder of tree.subTrees.keys()) {
const childFolderState = FolderState.fromFileTree(tree.subTrees.get(folder)!)
childFolderState.parent = folderState
folderState.folders.set(folder, childFolderState)
}

const fileKeys = [...tree.leafs.keys()].sort()
for (const file of fileKeys) {
for (const file of tree.leafs.keys()) {
folderState.files.set(file, tree.leafs.get(file)!)
}
const { h, s, v } = tree.__data?.__color ?? { h: 0, s: 0, v: 1 }
Expand Down
39 changes: 20 additions & 19 deletions web/src/FileTree.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,48 @@ describe("FileTree", () => {
{
render: `\
__dep_tree_root__
a
b
c
d
e.ts -> 4
foo
bar
a.ts -> 0
b.ts -> 1
baz
c.ts -> 2
d.ts -> 3
a
b
c
d
e.ts -> 4
f.ts -> 5`,
parents: [
['a', 'b', 'c', 'd'],
['foo', 'bar'],
['foo', 'bar'],
['foo', 'baz'],
['foo'],
['a', 'b', 'c', 'd'],
[]
],
parentStats: [
{ kind: 'tree', depth: 4, total: 1, index: 0 },
{ kind: 'tree', depth: 2, total: 2, index: 0 },
{ kind: 'tree', depth: 2, total: 2, index: 0 },
{ kind: 'tree', depth: 2, total: 2, index: 1 },
{ kind: 'tree', depth: 1, total: 2, index: 0 },
{ kind: 'tree', depth: 4, total: 1, index: 0 },
{ kind: 'tree', depth: 1, total: 2, index: 1 },
{ kind: 'tree', depth: 0, total: 1, index: 0 },
],
leafs: [0, 1, 2, 3, 4, 5]
leafs: [4, 0, 1, 2, 3, 5]
}
)

it(
'squash',
{
nodes: [
['a', 'b', 'c', 'd', 'e.ts'],
['foo', 'bar', 'a.ts'],
['foo', 'bar', 'b.ts'],
['foo', 'baz', 'c.ts'],
['foo', 'd.ts'],
['a', 'b', 'c', 'd', 'e.ts'],
['f.ts'],
['foo', 'bar', 'a', 'b', 'g.ts']
],
Expand All @@ -68,37 +68,37 @@ __dep_tree_root__
{
render: `\
__dep_tree_root__
a/b/c/d
e.ts -> 0
foo
bar
a/b
g.ts -> 6
a.ts -> 0
b.ts -> 1
a.ts -> 1
b.ts -> 2
baz
c.ts -> 2
d.ts -> 3
a/b/c/d
e.ts -> 4
c.ts -> 3
d.ts -> 4
f.ts -> 5`,
parents: [
['a/b/c/d'],
['foo', 'bar', 'a/b'],
['foo', 'bar'],
['foo', 'bar'],
['foo', 'baz'],
['foo'],
['a/b/c/d'],
[],
],
parentStats: [
{ kind: 'tree', depth: 1, total: 2, index: 0 },
{ kind: 'tree', depth: 3, total: 1, index: 0 },
{ kind: 'tree', depth: 2, total: 2, index: 0 },
{ kind: 'tree', depth: 2, total: 2, index: 0 },
{ kind: 'tree', depth: 2, total: 2, index: 1 },
{ kind: 'tree', depth: 1, total: 2, index: 0 },
{ kind: 'tree', depth: 1, total: 2, index: 1 },
{ kind: 'tree', depth: 0, total: 1, index: 0 },
],
leafs: [6, 0, 1, 2, 3, 4, 5]
leafs: [0, 6, 1, 2, 3, 4, 5]
}
)
})
Expand All @@ -120,6 +120,7 @@ function it (
fileTree.pushNode(newNode(node))
}
if (input.squash) fileTree.squash()
fileTree.order()

// ensure tree integrity
ensureChildrenParents(fileTree)
Expand Down
29 changes: 28 additions & 1 deletion web/src/FileTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@ export class FileTree<T = object> {
}
}

/**
* A Map in JS will be iterated in the order of insertion. This function
* orders the internal Maps alphabetically.
*/
order() {
const subTrees = [...this.subTrees.entries()]
this.subTrees.clear()
subTrees.sort(([a], [b]) => a > b ? 1 : -1)
let i = 0
for (const [k, v] of subTrees) {
v.__index = i
v.order()
this.subTrees.set(k, v)
i++
}

const leafs = [...this.leafs.entries()]
this.leafs.clear()
leafs.sort(([a], [b]) => a > b ? 1 : -1)
i = 0
for (const [k, v] of leafs) {
v.__index = i
this.leafs.set(k, v)
i++
}
}

/**
* Retrieves the parent {@link FileTree} of a leaf node.
*/
Expand Down Expand Up @@ -148,7 +175,7 @@ export class FileTree<T = object> {
* @param node
*/
static stats<T> (node: FileLeaf<T> | FileTree<T>): NodeStats {
let depth = 0 // starts with -1 because there's always going to be at least 1 parent.
let depth = 0
let parent = node.__parent
while (parent !== undefined) {
depth++
Expand Down
1 change: 1 addition & 0 deletions web/src/XGraph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export function buildXGraph (graph: Graph) {
const fileTree = FileTree.root<XNode>()
graph.nodes.forEach(node => fileTree.pushNode(node))
fileTree.squash()
fileTree.order()
color(fileTree)

// Check if there are more than one groups in all the nodes.
Expand Down
Loading