A1: tab management: preview tabs, pinning, close actions, reopen closed tab, split into two groups
- Pure tab model (components/editor/tabs/model.ts) with Vitest coverage. - Editor store keeps one buffer sync per buffer and a group layout; a buffer in both groups shares text and undo. Bulk closes ask once with a list. - Tab context menu, tabs.* UI commands (COMMAND_IDS) in a Window menu group, no default shortcuts; Show Local History appears once history.show exists. - Single click in the tree, Find and Go-to opens preview tabs. - Layout stored through layout.set and restored on project open. - Public API for other areas: tabOfPath, tabOf, reloadBuffer, openFile. - Docs: ui-parity, gaps (G2), architecture. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
02922eb89d
commit
7f309dbc4e
28 changed files with 3003 additions and 211 deletions
262
ui/tests/editor/tabModel.test.ts
Normal file
262
ui/tests/editor/tabModel.test.ts
Normal file
|
|
@ -0,0 +1,262 @@
|
|||
/**
|
||||
* Tests for the pure tab layout (C10 A1, `components/editor/tabs/model.ts`):
|
||||
* preview replacement, pinning, bulk close targets, splitting, moving and
|
||||
* joining groups, the stored layout and the reopen stack.
|
||||
*/
|
||||
import { describe, expect, it } from "vitest";
|
||||
import {
|
||||
activeBuffer,
|
||||
closeTargets,
|
||||
emptyLayout,
|
||||
groupsOf,
|
||||
keepOpen,
|
||||
MAX_CLOSED,
|
||||
moveToOtherGroup,
|
||||
parseStored,
|
||||
placeTab,
|
||||
pushClosed,
|
||||
removeBuffer,
|
||||
removeTab,
|
||||
setPinned,
|
||||
split,
|
||||
toStored,
|
||||
unsplit,
|
||||
type ClosedTab,
|
||||
type TabLayout,
|
||||
} from "../../src/components/editor/tabs/model";
|
||||
|
||||
/**
|
||||
* A one-group layout with permanent tabs for the ids, the last one active.
|
||||
*
|
||||
* @param ids - buffer ids in order.
|
||||
*/
|
||||
function withTabs(...ids: number[]): TabLayout {
|
||||
let layout = emptyLayout();
|
||||
for (const id of ids) {
|
||||
layout = placeTab(layout, 0, id, { preview: false, activate: true }).layout;
|
||||
}
|
||||
return layout;
|
||||
}
|
||||
|
||||
/**
|
||||
* The buffer ids of a group in order.
|
||||
*
|
||||
* @param layout - the layout.
|
||||
* @param group - group index.
|
||||
*/
|
||||
function ids(layout: TabLayout, group = 0): number[] {
|
||||
return layout.groups[group]?.tabs.map((t) => t.id) ?? [];
|
||||
}
|
||||
|
||||
describe("placing tabs", () => {
|
||||
it("inserts after the active tab and activates", () => {
|
||||
let layout = withTabs(1, 2, 3);
|
||||
layout = placeTab(layout, 0, 1, { preview: false, activate: true }).layout;
|
||||
layout = placeTab(layout, 0, 4, { preview: false, activate: true }).layout;
|
||||
expect(ids(layout)).toEqual([1, 4, 2, 3]);
|
||||
expect(activeBuffer(layout)).toBe(4);
|
||||
});
|
||||
|
||||
it("replaces the one preview tab in place and reports it", () => {
|
||||
let layout = withTabs(1, 2);
|
||||
layout = placeTab(layout, 0, 1, { preview: false, activate: true }).layout;
|
||||
let result = placeTab(layout, 0, 5, { preview: true, activate: true });
|
||||
expect(result.replaced).toBeNull();
|
||||
layout = placeTab(result.layout, 0, 2, { preview: false, activate: true }).layout;
|
||||
result = placeTab(layout, 0, 6, { preview: true, activate: true });
|
||||
expect(result.replaced).toBe(5);
|
||||
expect(ids(result.layout)).toEqual([1, 6, 2]);
|
||||
expect(result.layout.groups[0]?.tabs.filter((t) => t.preview).map((t) => t.id)).toEqual([6]);
|
||||
});
|
||||
|
||||
it("opening a preview for good, or keeping it open, makes it permanent", () => {
|
||||
let layout = placeTab(emptyLayout(), 0, 1, { preview: true, activate: true }).layout;
|
||||
expect(placeTab(layout, 0, 1, { preview: true, activate: true }).layout.groups[0]?.tabs[0]?.preview).toBe(true);
|
||||
expect(placeTab(layout, 0, 1, { preview: false, activate: true }).layout.groups[0]?.tabs[0]?.preview).toBe(false);
|
||||
layout = keepOpen(layout, 1);
|
||||
expect(placeTab(layout, 0, 2, { preview: true, activate: true }).replaced).toBeNull();
|
||||
});
|
||||
|
||||
it("never puts an unpinned tab before a pinned one", () => {
|
||||
let layout = withTabs(1, 2);
|
||||
layout = setPinned(layout, 0, 2, true);
|
||||
expect(ids(layout)).toEqual([2, 1]);
|
||||
layout = placeTab(layout, 0, 3, { preview: false, activate: true, index: 0 }).layout;
|
||||
expect(ids(layout)).toEqual([2, 3, 1]);
|
||||
});
|
||||
|
||||
it("leaves the input layout alone", () => {
|
||||
const layout = withTabs(1);
|
||||
placeTab(layout, 0, 2, { preview: false, activate: true });
|
||||
expect(ids(layout)).toEqual([1]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("pinning", () => {
|
||||
it("moves a pinned tab to the end of the pinned block and back", () => {
|
||||
let layout = withTabs(1, 2, 3);
|
||||
layout = setPinned(layout, 0, 3, true);
|
||||
layout = setPinned(layout, 0, 2, true);
|
||||
expect(ids(layout)).toEqual([3, 2, 1]);
|
||||
layout = setPinned(layout, 0, 3, false);
|
||||
expect(ids(layout)).toEqual([2, 3, 1]);
|
||||
});
|
||||
|
||||
it("keeps a pinned preview open", () => {
|
||||
const layout = setPinned(placeTab(emptyLayout(), 0, 1, { preview: true, activate: true }).layout, 0, 1, true);
|
||||
expect(layout.groups[0]?.tabs[0]).toEqual({ id: 1, pinned: true, preview: false });
|
||||
});
|
||||
});
|
||||
|
||||
describe("close targets", () => {
|
||||
const layout = setPinned(withTabs(1, 2, 3, 4, 5), 0, 1, true);
|
||||
const group = layout.groups[0];
|
||||
const dirty = (id: number): boolean => id === 4;
|
||||
|
||||
it("leave pinned tabs out of every bulk close", () => {
|
||||
expect(group && closeTargets(group, 3, "others", dirty)).toEqual([2, 4, 5]);
|
||||
expect(group && closeTargets(group, 3, "all", dirty)).toEqual([2, 3, 4, 5]);
|
||||
expect(group && closeTargets(group, 3, "left", dirty)).toEqual([2]);
|
||||
expect(group && closeTargets(group, 3, "right", dirty)).toEqual([4, 5]);
|
||||
expect(group && closeTargets(group, null, "unmodified", dirty)).toEqual([2, 3, 5]);
|
||||
});
|
||||
|
||||
it("close nothing left or right of a tab the group lacks", () => {
|
||||
expect(group && closeTargets(group, 99, "left", dirty)).toEqual([]);
|
||||
expect(group && closeTargets(group, 99, "right", dirty)).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("removing tabs", () => {
|
||||
it("activates the right neighbour, else the left one", () => {
|
||||
let layout = withTabs(1, 2, 3);
|
||||
layout = placeTab(layout, 0, 2, { preview: false, activate: true }).layout;
|
||||
layout = removeTab(layout, 0, 2);
|
||||
expect(activeBuffer(layout)).toBe(3);
|
||||
layout = removeTab(layout, 0, 3);
|
||||
expect(activeBuffer(layout)).toBe(1);
|
||||
layout = removeTab(layout, 0, 1);
|
||||
expect(activeBuffer(layout)).toBeNull();
|
||||
expect(layout.groups).toHaveLength(1);
|
||||
});
|
||||
});
|
||||
|
||||
describe("groups", () => {
|
||||
it("split shows the active tab in a new focused group, at most two groups", () => {
|
||||
let layout = split(withTabs(1, 2), "right");
|
||||
expect(layout.groups).toHaveLength(2);
|
||||
expect(layout.focused).toBe(1);
|
||||
expect(ids(layout, 1)).toEqual([2]);
|
||||
expect(groupsOf(layout, 2)).toEqual([0, 1]);
|
||||
layout = placeTab(layout, 1, 3, { preview: false, activate: true }).layout;
|
||||
layout = split(layout, "down");
|
||||
expect(layout.groups).toHaveLength(2);
|
||||
expect(layout.orientation).toBe("down");
|
||||
expect(layout.focused).toBe(0);
|
||||
expect(ids(layout, 0)).toEqual([1, 2, 3]);
|
||||
});
|
||||
|
||||
it("an emptied group goes away and the focus stays valid", () => {
|
||||
let layout = split(withTabs(1, 2), "right");
|
||||
layout = removeTab(layout, 1, 2);
|
||||
expect(layout.groups).toHaveLength(1);
|
||||
expect(layout.focused).toBe(0);
|
||||
expect(ids(layout)).toEqual([1, 2]);
|
||||
});
|
||||
|
||||
it("closing a buffer removes it from both groups", () => {
|
||||
const layout = removeBuffer(split(withTabs(1, 2), "right"), 2);
|
||||
expect(layout.groups).toHaveLength(1);
|
||||
expect(ids(layout)).toEqual([1]);
|
||||
});
|
||||
|
||||
it("moving a tab splits when needed and takes it out of its group", () => {
|
||||
let layout = moveToOtherGroup(withTabs(1, 2), 0, 2);
|
||||
expect(ids(layout, 0)).toEqual([1]);
|
||||
expect(ids(layout, 1)).toEqual([2]);
|
||||
expect(layout.focused).toBe(1);
|
||||
layout = moveToOtherGroup(layout, 1, 2);
|
||||
expect(layout.groups).toHaveLength(1);
|
||||
expect(ids(layout)).toEqual([1, 2]);
|
||||
expect(moveToOtherGroup(withTabs(1), 0, 1)).toEqual(withTabs(1));
|
||||
});
|
||||
|
||||
it("unsplit joins the other group's tabs into the focused one", () => {
|
||||
let layout = split(withTabs(1, 2), "right");
|
||||
layout = placeTab(layout, 1, 3, { preview: true, activate: true }).layout;
|
||||
layout = unsplit(layout);
|
||||
expect(layout.groups).toHaveLength(1);
|
||||
expect(ids(layout)).toEqual([2, 3, 1]);
|
||||
expect(layout.groups[0]?.tabs.map((t) => t.preview)).toEqual([false, true, false]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("stored layout", () => {
|
||||
it("round-trips groups, pins, previews and the active tabs by path", () => {
|
||||
let layout = split(setPinned(withTabs(1, 2), 0, 2, true), "down");
|
||||
layout = placeTab(layout, 1, 3, { preview: true, activate: true }).layout;
|
||||
const paths: Record<number, string> = { 1: "a.php", 2: "b.php", 3: "c.php" };
|
||||
const stored = toStored(layout, (id) => paths[id]);
|
||||
expect(stored).toEqual({
|
||||
version: 1,
|
||||
focused: 1,
|
||||
orientation: "down",
|
||||
groups: [
|
||||
{ tabs: [{ path: "b.php", pinned: true, preview: false }, { path: "a.php", pinned: false, preview: false }], active: "b.php" },
|
||||
{ tabs: [{ path: "b.php", pinned: false, preview: false }, { path: "c.php", pinned: false, preview: true }], active: "c.php" },
|
||||
],
|
||||
});
|
||||
expect(parseStored(JSON.parse(JSON.stringify(stored)))).toEqual(stored);
|
||||
});
|
||||
|
||||
it("reads untrusted values defensively", () => {
|
||||
for (const bad of [null, 1, "x", [], {}, { version: 2, groups: [] }, { version: 1, groups: "x" }]) {
|
||||
expect(parseStored(bad)).toBeNull();
|
||||
}
|
||||
const parsed = parseStored({
|
||||
version: 1,
|
||||
focused: 7,
|
||||
orientation: "diagonal",
|
||||
groups: [
|
||||
{ tabs: [{ path: "a" }, { path: "a" }, { path: "" }, 3, null, { path: "p", pinned: true, preview: true }], active: "nope" },
|
||||
null,
|
||||
{ tabs: [] },
|
||||
{ tabs: [{ path: "c" }] },
|
||||
],
|
||||
});
|
||||
expect(parsed).toEqual({
|
||||
version: 1,
|
||||
focused: 0,
|
||||
orientation: "right",
|
||||
groups: [{ tabs: [{ path: "p", pinned: true, preview: false }, { path: "a", pinned: false, preview: false }], active: "p" }],
|
||||
});
|
||||
});
|
||||
|
||||
it("caps the number of stored tabs", () => {
|
||||
const tabs = Array.from({ length: 500 }, (_, i) => ({ path: `f${String(i)}.php` }));
|
||||
expect(parseStored({ version: 1, groups: [{ tabs }] })?.groups[0]?.tabs).toHaveLength(200);
|
||||
});
|
||||
});
|
||||
|
||||
describe("reopen stack", () => {
|
||||
/**
|
||||
* A closed tab entry.
|
||||
*
|
||||
* @param path - its path.
|
||||
*/
|
||||
function entry(path: string): ClosedTab {
|
||||
return { path, group: 0, index: 0, pinned: false, anchor: 0, head: 0, top: null };
|
||||
}
|
||||
|
||||
it("keeps one entry per path, newest last, and caps its size", () => {
|
||||
let stack = pushClosed([], entry("a"));
|
||||
stack = pushClosed(stack, entry("b"));
|
||||
stack = pushClosed(stack, entry("a"));
|
||||
expect(stack.map((e) => e.path)).toEqual(["b", "a"]);
|
||||
for (let i = 0; i < 50; i++) {
|
||||
stack = pushClosed(stack, entry(String(i)));
|
||||
}
|
||||
expect(stack).toHaveLength(MAX_CLOSED);
|
||||
expect(stack[stack.length - 1]?.path).toBe("49");
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue