From 24a048a9f85bd26b4cc8f2a362788bea1d77c4c7 Mon Sep 17 00:00:00 2001 From: Tyler Hallada Date: Mon, 16 Nov 2026 22:10:24 -0500 Subject: [PATCH] A column's panes stand in the order of their sources By where their parents stand in the column before, then by where in the parent each was opened from (src, measured by the page and stored with the view), then in tree order. Panes put in an order by hand keep it; a pane never placed by hand goes just after the one before it in its own order. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab --- src/ui/forks.ts | 50 ++++++++++++++++++++++++++++++++++++--------- tests/forks.test.ts | 43 +++++++++++++++++++++++++++++++++++++- 2 files changed, 82 insertions(+), 11 deletions(-) diff --git a/src/ui/forks.ts b/src/ui/forks.ts index 346eaa2..4cfc2e3 100644 --- a/src/ui/forks.ts +++ b/src/ui/forks.ts @@ -38,6 +38,8 @@ export interface Tree { rank?: number; /** Its branch's colour, 0 to HUES − 1; none on the first tree's root trail. Kept for the pane's life. */ hue?: number; + /** Where in its parent pane it was opened from: the source's place down that pane's text (set by the page once it is drawn). */ + src?: number; } /** The trees, the first starting at the top node; `at` is the one the focused branch is in. */ @@ -151,19 +153,37 @@ export function all(f: Forest): Placed[] { } /** - * The panes of each column, top to bottom: in the order the reader has put them in (by rank), then - * those never placed by hand, in tree order (tree by tree, depth first, earlier forks first). + * The panes of each column, top to bottom. Their own order: by where their parents stand in the + * column before (an orphan's root after those, in tree order), then by where in the parent each was + * opened from (its source's place down the parent's text), then in tree order. So a pane opened + * from higher up stands higher, and curves cross as little as they can. Panes the reader has put in + * an order by hand (ranked, by dragging) keep it; a pane never placed by hand goes just after the + * pane before it in its own order (or first, if none is before it). */ export function columns(f: Forest): Map[]> { - const out = new Map[]>(); - for (const x of all(f)) { - const c = out.get(x.col); + const byCol = new Map[]>(); + const tree = new Map, number>(); + all(f).forEach((x, i) => { + tree.set(x, i); + const c = byCol.get(x.col); if (c) c.push(x); - else out.set(x.col, [x]); - } - for (const c of out.values()) { - const at = new Map(c.map((x, i) => [x, i])); - c.sort((a, b) => (a.node.rank ?? Infinity) - (b.node.rank ?? Infinity) || at.get(a)! - at.get(b)!); + else byCol.set(x.col, [x]); + }); + const out = new Map[]>(); + /** Each pane's place in its column, as columns are ordered left to right. */ + const place = new Map(); + for (const col of [...byCol.keys()].sort((a, b) => a - b)) { + const c = byCol.get(col)!; + const parentAt = (x: Placed) => (x.parent ? (place.get(x.parent.id) ?? Infinity) : Infinity); + const natural = c.slice().sort((a, b) => parentAt(a) - parentAt(b) || (a.node.src ?? Infinity) - (b.node.src ?? Infinity) || tree.get(a)! - tree.get(b)!); + const order = natural.filter((x) => x.node.rank !== undefined).sort((a, b) => a.node.rank! - b.node.rank! || tree.get(a)! - tree.get(b)!); + natural.forEach((x, i) => { + if (x.node.rank !== undefined) return; + const before = i > 0 ? order.indexOf(natural[i - 1]) : -1; + order.splice(before + 1, 0, x); + }); + order.forEach((x, i) => place.set(x.node.id, i)); + out.set(col, order); } return out; } @@ -304,6 +324,13 @@ export function close(f: Forest, id: number, active: number): { fore return { forest: focus(g, still), active: still }; } +/** Record where panes were opened from in their parents (by id), as the page measures it. */ +export function setSources(f: Forest, srcs: Map): Forest { + let g = f; + for (const [id, v] of srcs) g = updateIn(g, id, (n) => (n.src === v ? n : { ...n, src: v })); + return g; +} + /** Pin or unpin a pane. */ export const pin = (f: Forest, id: number, on: boolean): Forest => updateIn(f, id, (n) => (!!n.pinned === on ? n : { ...n, pinned: on || undefined })); @@ -403,6 +430,7 @@ export interface Saved { col?: number; rank?: number; hue?: number; + src?: number; } export function save(t: Tree, ref: (r: R) => O): Saved { @@ -413,6 +441,7 @@ export function save(t: Tree, ref: (r: R) => O): Saved { if (t.col) out.col = t.col; if (t.rank !== undefined) out.rank = t.rank; if (t.hue !== undefined) out.hue = t.hue; + if (t.src !== undefined) out.src = t.src; return out; } @@ -427,6 +456,7 @@ export function load(o: unknown, ok: (r: unknown) => r is R): Tree | if (typeof s.col === "number" && s.col >= 1) t.col = Math.floor(s.col); if (typeof s.rank === "number" && Number.isFinite(s.rank)) t.rank = s.rank; if (typeof s.hue === "number" && Number.isInteger(s.hue) && s.hue >= 0 && s.hue < HUES) t.hue = s.hue; + if (typeof s.src === "number" && Number.isFinite(s.src)) t.src = s.src; return t; } diff --git a/tests/forks.test.ts b/tests/forks.test.ts index 1f31a4b..eb05f4d 100644 --- a/tests/forks.test.ts +++ b/tests/forks.test.ts @@ -1,7 +1,7 @@ /** The panes view's forest of forks (src/ui/forks.ts): each operation, and any sequence of them leaving a well-formed forest with one focused branch and one active pane. */ import fc from "fast-check"; import { describe, expect, it } from "vitest"; -import { HUES, all, besideInColumn, close, columns, find, focus, focused, load, open, path, pin, plain, prune, reorder, restore, save, valid, withBranch, type Forest, type Tree } from "../src/ui/forks"; +import { HUES, all, besideInColumn, close, columns, find, focus, focused, load, open, path, pin, plain, prune, reorder, restore, save, setSources, valid, withBranch, type Forest, type Tree } from "../src/ui/forks"; type F = Forest; type T = Tree; @@ -375,3 +375,44 @@ describe("branch colours", () => { expect(load({ ref: "a", hue: 1.5 }, isStr)!.hue).toBeUndefined(); }); }); + +describe("column order", () => { + const col = (f: F, c: number) => columns(f).get(c)!.map((x) => x.node.ref); + it("a column's panes stand by their parents' places, then by where in the parent each was opened from", () => { + let f: F = plain(["lib"]); + f = open(f, 0, "octopus", true, same); + f = open(focus(f, f.trees[0].id), 0, "expand", true, same); + expect(col(f, 1)).toEqual(["octopus", "expand"]); // not measured yet: in tree order + f = setSources(f, new Map([[idOf(f, "octopus"), 40], [idOf(f, "expand"), 10]])); + expect(col(f, 1)).toEqual(["expand", "octopus"]); + // Their children follow their parents' order, then their own sources. + f = open(focus(f, idOf(f, "octopus")), 1, "o1", true, same); + f = open(focus(f, idOf(f, "expand")), 1, "e2", true, same); + f = open(focus(f, idOf(f, "expand")), 1, "e1", true, same); + f = setSources(f, new Map([[idOf(f, "o1"), 5], [idOf(f, "e2"), 90], [idOf(f, "e1"), 20]])); + expect(col(f, 2)).toEqual(["e1", "e2", "o1"]); + // Stored with the view. + const isStr = (r: unknown): r is string => typeof r === "string"; + const back = restore(JSON.parse(JSON.stringify({ forest: f.trees.map((t) => save(t, (r) => r)), at: f.at, hues: f.hues })), isStr); + expect(col(back, 2)).toEqual(["e1", "e2", "o1"]); + }); + + it("an order set by hand stays; a pane never placed by hand goes just after the one before it in its own order", () => { + let f: F = plain(["lib"]); + for (const [r, y] of [["a", 10], ["b", 20], ["c", 30]] as const) { + f = open(focus(f, f.trees[0].id), 0, r, true, same); + f = setSources(f, new Map([[idOf(f, r), y]])); + } + expect(col(f, 1)).toEqual(["a", "b", "c"]); + f = reorder(f, idOf(f, "c"), idOf(f, "a")); + expect(col(f, 1)).toEqual(["c", "a", "b"]); + // A new one opened from between a and b: just after a, where the reader put a. + f = open(focus(f, f.trees[0].id), 0, "ab", true, same); + f = setSources(f, new Map([[idOf(f, "ab"), 15]])); + expect(col(f, 1)).toEqual(["c", "a", "ab", "b"]); + // One from above all: first. + f = open(focus(f, f.trees[0].id), 0, "top", true, same); + f = setSources(f, new Map([[idOf(f, "top"), 1]])); + expect(col(f, 1)).toEqual(["top", "c", "a", "ab", "b"]); + }); +});