A column's own order: panes put in order by hand, kept with the forest

A column lists its panes in tree order until the reader drags one: then its
order is its own (a rank on each pane, stored with the tree), panes opened into
it later come after, and a pane opened in place of another takes its place.
[ and ] go by that order.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014yB6q4BHMwptTX3YFtkRab
This commit is contained in:
thalladaandClaude Opus 5.5 committed 2026-11-16 01:40:24 -05:00
1 parent 1337d73bac
commit 7a2df56440
2 files changed
+78 -9

No files matched your search

+43 -6
View File
@@ -26,6 +26,8 @@ export interface Tree<R, S = unknown> {
pinned?: boolean;
/** On the root of an orphan: the column it stays in. */
col?: number;
/** Its place in its column, once the panes there have been put in an order by hand (dragged); unranked panes follow, in tree order. */
rank?: number;
}
/** The trees, the first starting at the top node; `at` is the one the focused branch is in. */
@@ -116,6 +118,37 @@ export function all<R, S>(f: Forest<R, S>): Placed<R, S>[] {
return out;
}
/**
* 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).
*/
export function columns<R, S>(f: Forest<R, S>): Map<number, Placed<R, S>[]> {
const out = new Map<number, Placed<R, S>[]>();
for (const x of all(f)) {
const c = out.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)!);
}
return out;
}
/** Move a pane within its column, to just above another pane there (or to the bottom): the column's order is then set by hand. */
export function reorder<R, S>(f: Forest<R, S>, id: number, before: number | null): Forest<R, S> {
const me = all(f).find((x) => x.node.id === id);
if (!me || id === before) return f;
const col = columns(f).get(me.col)!.map((x) => x.node.id);
const rest = col.filter((x) => x !== id);
const at = before === null ? rest.length : rest.indexOf(before);
if (at < 0) return f;
rest.splice(at, 0, id);
if (rest.every((x, i) => x === col[i]) && col.every((x) => (all(f).find((y) => y.node.id === x)!.node.rank ?? -1) >= 0)) return f;
return rest.reduce((g, x, i) => updateIn(g, x, (n) => (n.rank === i ? n : { ...n, rank: i })), f);
}
/** Change the node with this id, copying the path to it. */
export function update<R, S>(t: Tree<R, S>, id: number, f: (n: Tree<R, S>) => Tree<R, S>): Tree<R, S> {
if (t.id === id) return f(t);
@@ -198,7 +231,8 @@ export function open<R, S>(f: Forest<R, S>, i: number, ref: R, fork: boolean, sa
if (fork || n.kids[n.pick].pinned) return { ...n, kids: [...n.kids, leaf(ref)], pick: n.kids.length };
orphans.push(...survivors(n.kids[n.pick], colOf(t) + i + 1, true));
const kids = n.kids.slice();
kids[n.pick] = leaf(ref);
// In its place, in its column too.
kids[n.pick] = n.kids[n.pick].rank === undefined ? leaf(ref) : { ...leaf<R, S>(ref), rank: n.kids[n.pick].rank };
return { ...n, kids };
});
return replace(f, f.at, next, orphans);
@@ -239,11 +273,11 @@ export const pin = <R, S>(f: Forest<R, S>, id: number, on: boolean): Forest<R, S
/** The pane above (−1) or below (+1) this one in its column, or null at the top or bottom. */
export function besideInColumn<R, S>(f: Forest<R, S>, id: number, delta: number): number | null {
const placed = all(f);
const me = placed.find((x) => x.node.id === id);
if (!me) return null;
const col = placed.filter((x) => x.col === me.col);
return col[col.indexOf(me) + delta]?.node.id ?? null;
for (const col of columns(f).values()) {
const i = col.findIndex((x) => x.node.id === id);
if (i >= 0) return col[i + delta]?.node.id ?? null;
}
return null;
}
/** Drop every pane whose ref fails `keep` (with everything right of it but pinned panes, which stay as orphans). Unchanged forests are returned as they are. */
@@ -328,6 +362,7 @@ export interface Saved<R> {
pick?: number;
pin?: 1;
col?: number;
rank?: number;
}
export function save<R, S, O>(t: Tree<R, S>, ref: (r: R) => O): Saved<O> {
@@ -336,6 +371,7 @@ export function save<R, S, O>(t: Tree<R, S>, ref: (r: R) => O): Saved<O> {
if (t.pick) out.pick = t.pick;
if (t.pinned) out.pin = 1;
if (t.col) out.col = t.col;
if (t.rank !== undefined) out.rank = t.rank;
return out;
}
@@ -348,6 +384,7 @@ export function load<R, S>(o: unknown, ok: (r: unknown) => r is R): Tree<R, S> |
const t: Tree<R, S> = { ref: s.ref as R, kids, pick, id: ++ids };
if (s.pin) t.pinned = true;
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;
return t;
}
+35 -3
View File
@@ -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 { all, besideInColumn, close, find, focus, focused, load, open, path, pin, plain, prune, restore, save, valid, withBranch, type Forest, type Tree } from "../src/ui/forks";
import { 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";
type F = Forest<string>;
type T = Tree<string>;
@@ -121,6 +121,30 @@ describe("forks", () => {
expect(focus(g, idOf(f, "b1"))).toBe(g);
});
it("a column's panes can be put in an order by hand, across forks; new panes come after; a replaced pane keeps its place; the order is stored", () => {
let f: F = plain(["a", "b", "b1"]);
f = open(f, 1, "b2", true, same);
f = open(f, 0, "c", true, same);
f = open(f, 1, "c1", false, same);
const col2 = (g: F) => columns(g).get(2)!.map((x) => x.node.ref);
expect(col2(f)).toEqual(["b1", "b2", "c1"]);
f = reorder(f, idOf(f, "c1"), idOf(f, "b1"));
expect(col2(f)).toEqual(["c1", "b1", "b2"]);
expect(besideInColumn(f, idOf(f, "b1"), -1)).toBe(idOf(f, "c1"));
f = reorder(f, idOf(f, "c1"), null);
expect(col2(f)).toEqual(["b1", "b2", "c1"]);
f = reorder(f, idOf(f, "b2"), idOf(f, "b1"));
expect(col2(f)).toEqual(["b2", "b1", "c1"]);
// A new pane in that column goes to the bottom; a pane opened in place of another takes its place.
f = open(focus(f, idOf(f, "c")), 1, "c2", true, same);
expect(col2(f)).toEqual(["b2", "b1", "c1", "c2"]);
f = open(focus(f, idOf(f, "b1")), 1, "bx", false, same);
expect(col2(f)).toEqual(["b2", "bx", "c1", "c2"]);
const isStr = (r: unknown): r is string => typeof r === "string";
const back = restore<string, unknown>(JSON.parse(JSON.stringify({ forest: f.trees.map((t) => save(t, (r) => r)), at: f.at })), isStr);
expect(col2(back)).toEqual(["b2", "bx", "c1", "c2"]);
});
it("drops a deleted node's pane wherever it is, with what is right of it but pinned panes", () => {
let f: F = plain(["a", "b", "c"]);
f = open(f, 0, "x", true, same);
@@ -169,9 +193,9 @@ describe("forks", () => {
expect(restore<string, unknown>({ forest: [{ ref: "o", col: 2 }, { ref: "a" }], at: 7 }, isStr).trees.map((t) => t.ref)).toEqual(["a", "o"]);
});
it("any sequence of open, fork, close, pin, unpin and focus leaves a valid forest with exactly one active pane, and closing removes no pinned pane but the one closed", () => {
it("any sequence of open, fork, close, pin, unpin, focus and reorder leaves a valid forest with exactly one active pane, and closing removes no pinned pane but the one closed", () => {
const refs = ["a", "b", "c", "d", "e"];
const op = fc.record({ kind: fc.integer({ min: 0, max: 8 }), i: fc.nat(40), r: fc.integer({ min: 0, max: refs.length - 1 }), d: fc.boolean() });
const op = fc.record({ kind: fc.integer({ min: 0, max: 9 }), i: fc.nat(40), r: fc.integer({ min: 0, max: refs.length - 1 }), d: fc.boolean() });
fc.assert(
fc.property(fc.array(op, { maxLength: 60 }), (ops) => {
let f: F = plain(["root"]);
@@ -215,6 +239,14 @@ describe("forks", () => {
f = withBranch(f, ["root", ...refs.slice(0, o.i % 4)], same);
active = f.trees[0].id;
break;
case 9: {
const col = [...columns(f).values()].find((c) => c.some((x) => x.node.id === any.id))!;
const before = col[o.r % col.length].node.id;
const was = all(f).length;
f = reorder(f, any.id, o.d ? before : null);
if (all(f).length !== was) return false;
break;
}
}
if (!valid(f, same)) return false;
// One branch, from the focused tree's root to a pane with nothing right of it, each step a picked child.