Skip to content
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
64 changes: 64 additions & 0 deletions .ai/contexts/changes-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -859,6 +859,70 @@ The split uses `createSplitter` (`public/splitter.js`, shared with the panel's s

Switching rows is an exit like Back, the tab toggle and the panel's close button: it asks `confirmDiscardChangesEdits` when the buffer is dirty and returns without touching anything if the answer is no. Clicking the row that is already open is not a switch and asks nothing. The row cap (`MAX_CHANGES_ROWS`) and the idle refresh are unchanged by the layout — a refresh rebuilds the list while the editor keeps its instance and its DOM node, which the editor-host MutationObserver test pins with the list now rebuilding alongside it.

### List or tree

The list has two views, switched by one `.icon-btn` in the list toolbar
(`#changes-list-view-btn`) and persisted under `localStorage.changesListView`
(`CHANGES_LIST_VIEW_KEY`), the same shape as the editor's
`changesDiffMode` toggle: the icon shows the current view, `data-view` carries
it, the tooltip names the other. `list` is the default and is what every
earlier release rendered; a stored `tree` is honoured at load, anything else
falls back to `list`. Ported from upstream `c380d187` (doctly/switchboard#98,
Vincenzo Favara), whose tree builder lived in `project-git-view.js`; the fork
has no Git tab, so the builder moved into `public/file-panel.js` next to the
rows it feeds, and the default stayed `list` (upstream later flipped to tree).

`appendChangesRows(sessionId, tab, files, subSessionId)` is the single place
both the session's own rows and each subagent group's rows go through, so the
two render alike in either view. In `list` it is the flat loop it replaced. In
`tree`, `buildChangesTree` splits each path on `/` (git's separator on every
platform; the fork's rows carry repo-relative git paths) into folder nodes,
`sortChangesTreeLevel` puts folders before files, sorts each level with a
numeric, case-insensitive `localeCompare`, and compacts a folder whose only
child is a folder into one row named `a/b` (VS Code's compact folders), and
`changesTreeRows` flattens that into display rows, skipping the children of a
collapsed folder. A leaf is one status record, not one path: git reports two
records at one path after `git rm --cached f` (a staged deletion and an
untracked file), the flat list shows both, and the tree keeps both under the
folder, in git's order, each with its own click target (the leaf key carries
the record's index). A file row in tree mode shows the node's own name, carries
`.tree` and `--depth` for its indent, keeps `data-path` as the full repo path
(so the selection, the click handler, the untracked-count writeback and every
`[data-path=…]` selector are unchanged), and takes the full path — with the
rename arrow — as its `title`. The row cap applies before the tree is built:
the tree is made from the first `MAX_CHANGES_ROWS` files, and the
`+N more files not shown` note follows as before.

A folder row is a `<button class="changes-folder-row">` with `data-folder`
(the folder's repo path, compact name included), `data-subagent` for a group's
folder, `aria-expanded`, a chevron, the name and a count of the changed files
under it. Collapsed state is per tab, in `tab.collapsedFolders`, a `Set` keyed
by `changesFolderKey(subSessionId, folderPath)` — the group id and the path
joined by `\0`, which no git path can contain — so the parent's `src` and a
group's `src` fold independently, and a fold survives a refresh (the tab is
the same) but not a reopened tab. The keys are real folder paths, never the
compacted display name: a compacted row stands for every folder in its chain
(`changesFolderChain`, `src` and `src/nested` for a row named `src/nested`) and
is collapsed when any of them is in the set, so a fold made on `src` holds when
a refresh leaves `src/nested` as its only child and the row compacts, and a
fold made on `nested` holds when `src` compacts onto it. Folding adds the
deepest folder of the row; unfolding removes the whole chain, so no stale fold
reappears once the chain splits again. Clicking a folder bumps `tab.listRevision`
and goes through `renderPanel`, the same path as the untracked-count
writeback: the list is rebuilt, the editor is reused (the reuse test's
`editors.length` pins that), and focus is put back on the new copy of the
folder row. `updateChangesListSelection` highlights a collapsed folder whose
path is a prefix of the selected file's, on the same `(subSessionId, path)`
identity the file rows use, so the open file is never invisible in the list.

`changesListView` is part of the list's reuse signature, next to
`tab.listRevision`: switching views rebuilds the current tab's list without a
status request, and a tab snapshotted in the other view (`tab.listView`, see
"The list and the editor") is rebuilt rather than restored when it is shown
again. `test/dom-file-panel-changes.test.js` ("List/tree toggle") pins the
default, the toggle, the persistence, the compaction, the sort, the fold, the
focus, the stand-in highlight, the refresh and the per-group keys.

### Look and feel

The panel draws only from the app's existing tokens and palette. Every colour in its CSS is a `:root` token or a value `public/style.css` already uses elsewhere; none is its own. The app has one theme, dark, so there are no light variants to keep in step.
Expand Down
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ What changes for you in each release of Switchboard. How to write an entry: [doc

## Unreleased

### New
- The Changes list can be viewed as a tree: a toolbar button switches between the flat list (the default) and files grouped under collapsible folders. The choice is remembered. (#828)

### Changed
- Removed the per-project Work Files sidebar tab. Open session files from Touched or terminal path links; files in `.work-files/` stay on disk. (#1018)

Expand Down
8 changes: 8 additions & 0 deletions docs/changes-view.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ Changes diff, which compares an unstaged file with the index.
branch (at most 8 agents with changes, 100 rows each; the panel counts the agents it leaves out). Those rows open as read-only
diffs. A subagent in the session's directory, one whose worktree is gone, and
one with nothing changed add nothing. Local sessions only.
- The button left of **Refresh** switches between **List**, the default (one
row per file with its full path, in git's order), and **Tree** (files
grouped under their folders, folders first, sorted by name). A folder row
shows how many changed files it holds; clicking it folds or unfolds it, and
a folded folder that holds the open file is highlighted in its place. A
folder with a single subfolder is shown as one row (`src/app`). The choice is
remembered (`localStorage.changesListView`); the folds last while the tab is
open. The icon shows the view you are in and the tooltip names the other.
- **Refresh** reloads the list.

Clicking a row opens the file under the list, which stays visible with the row
Expand Down
190 changes: 181 additions & 9 deletions public/file-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ let changesDiffHostEl = null;
let changesDiffPreviewEl = null;
let changesPreviewTab = null;
let changesListSplitterEl = null;
let changesListViewBtn = null;

// Row ceiling for the Changes list — see .ai/contexts/changes-view.md ("Untracked files")
const MAX_CHANGES_ROWS = 500;
Expand Down Expand Up @@ -82,6 +83,13 @@ const CHANGES_STATE_NAMES = { M: 'Modified', A: 'Added', D: 'Deleted', R: 'Renam
const CHANGES_DIFF_MODE_KEY = 'changesDiffMode';
const CHANGES_DIFF_MODES = ['side-by-side', 'inline', 'plain'];
const CHANGES_DIFF_MODE_LABELS = { 'side-by-side': 'Side-by-side diff', inline: 'Inline diff', plain: 'Plain editor, no diff' };
// see .ai/contexts/changes-view.md ("List or tree")
const CHANGES_LIST_VIEW_KEY = 'changesListView';
const CHANGES_LIST_VIEWS = ['list', 'tree'];
const CHANGES_LIST_VIEW_LABELS = { list: 'List', tree: 'Tree' };
let changesListView = CHANGES_LIST_VIEWS.includes(localStorage.getItem(CHANGES_LIST_VIEW_KEY))
? localStorage.getItem(CHANGES_LIST_VIEW_KEY)
: 'list';
// see .ai/contexts/changes-view.md ("The list and the editor")
let changesDiffMode = CHANGES_DIFF_MODES.includes(localStorage.getItem(CHANGES_DIFF_MODE_KEY))
? localStorage.getItem(CHANGES_DIFF_MODE_KEY)
Expand All @@ -98,6 +106,10 @@ const FP_ICONS = {
inline: FP_STROKE_ICON('<rect x="3" y="4" width="18" height="16" rx="2"/><path d="M3 12h18"/>'),
plain: FP_STROKE_ICON('<path d="M4 6h16"/><path d="M4 12h16"/><path d="M4 18h10"/>'),
formatted: FP_STROKE_ICON('<path d="M2 12s3.5-7 10-7 10 7 10 7-3.5 7-10 7S2 12 2 12z"/><circle cx="12" cy="12" r="3"/>'),
list: FP_STROKE_ICON('<path d="M4 6h16"/><path d="M4 12h10"/><path d="M4 18h7"/>'),
tree: FP_STROKE_ICON('<path d="M21 12h-8"/><path d="M21 6H8"/><path d="M21 18h-8"/><path d="M3 6v4c0 1.1.9 2 2 2h3"/><path d="M3 10v6c0 1.1.9 2 2 2h3"/>'),
chevronRight: FP_STROKE_ICON('<path d="m9 6 6 6-6 6"/>'),
chevronDown: FP_STROKE_ICON('<path d="m6 9 6 6 6-6"/>'),
};
// ── Initialization ──────────────────────────────────────────────────

Expand Down Expand Up @@ -341,6 +353,13 @@ function initFilePanel() {
const changesControls = document.createElement('div');
changesControls.className = 'viewer-toolbar-controls';

changesListViewBtn = document.createElement('button');
changesListViewBtn.className = 'icon-btn';
changesListViewBtn.id = 'changes-list-view-btn';
changesListViewBtn.addEventListener('click', handleChangesListViewToggle);
changesControls.appendChild(changesListViewBtn);
updateChangesListViewButton();

const changesRefreshBtn = document.createElement('button');
changesRefreshBtn.className = 'icon-btn';
changesRefreshBtn.id = 'changes-refresh-btn';
Expand Down Expand Up @@ -1691,7 +1710,7 @@ function renderChangesContent(sessionId, tab) {
}

function renderChangesList(sessionId, tab) {
const signature = [tab.data, tab.loading, tab.error, tab.listRevision];
const signature = [tab.data, tab.loading, tab.error, tab.listRevision, changesListView];
const branchInfoEl = document.getElementById('changes-branch-info');
if (reusePanelList(changesListEl, changesSummaryEl, tab, signature, branchInfoEl)) {
updateChangesListSelection(tab);
Expand Down Expand Up @@ -1742,9 +1761,7 @@ function renderChangesList(sessionId, tab) {

changesListEl.innerHTML = '';
const shown = files.length > MAX_CHANGES_ROWS ? files.slice(0, MAX_CHANGES_ROWS) : files;
for (const file of shown) {
changesListEl.appendChild(buildChangesFileRow(sessionId, tab, file));
}
appendChangesRows(sessionId, tab, shown, null);
if (shown.length < files.length) {
const more = document.createElement('div');
more.className = 'changes-more-note';
Expand All @@ -1768,6 +1785,140 @@ function updateChangesListSelection(tab) {
row.classList.toggle('selected', !!selected && row.dataset.path === selected.path
&& (row.dataset.subagent || null) === (selected.subSessionId || null));
}
for (const row of changesListEl.querySelectorAll('.changes-folder-row')) {
row.classList.toggle('selected', !!selected && row.getAttribute('aria-expanded') === 'false'
&& selected.path.startsWith(row.dataset.folder + '/')
&& (row.dataset.subagent || null) === (selected.subSessionId || null));
}
}

// see .ai/contexts/changes-view.md ("List or tree")
function appendChangesRows(sessionId, tab, files, subSessionId) {
if (changesListView !== 'tree') {
for (const file of files) changesListEl.appendChild(buildChangesFileRow(sessionId, tab, file, subSessionId));
return;
}
if (!(tab.collapsedFolders instanceof Set)) tab.collapsedFolders = new Set();
for (const row of changesTreeRows(buildChangesTree(files), tab.collapsedFolders, subSessionId)) {
if (row.kind === 'folder') changesListEl.appendChild(buildChangesFolderRow(sessionId, tab, row, subSessionId));
else changesListEl.appendChild(buildChangesFileRow(sessionId, tab, row.file, subSessionId, { depth: row.depth, name: row.name }));
}
}

function compareChangesNames(a, b) {
return String(a).localeCompare(String(b), undefined, { numeric: true, sensitivity: 'base' });
}

function sortChangesTreeLevel(nodes) {
const sorted = nodes.map((node) => {
if (node.kind !== 'folder') return node;
let name = node.name;
let children = sortChangesTreeLevel(node.children);
while (children.length === 1 && children[0].kind === 'folder') {
name = `${name}/${children[0].name}`;
children = children[0].children;
}
return { kind: 'folder', name, children };
});
sorted.sort((a, b) => {
if (a.kind !== b.kind) return a.kind === 'folder' ? -1 : 1;
return compareChangesNames(a.name, b.name);
});
return sorted;
}

function buildChangesTree(files) {
const root = new Map();
files.forEach((file, index) => {
const parts = String(file.path || '').split('/').filter(Boolean);
let level = root;
for (let i = 0; i < parts.length - 1; i++) {
let node = level.get(parts[i]);
if (!node || node.kind !== 'folder') {
node = { kind: 'folder', name: parts[i], children: new Map() };
level.set(parts[i], node);
}
level = node.children;
}
const name = parts[parts.length - 1] || file.path;
level.set(`\0${name}\0${index}`, { kind: 'file', name, file });
});
const toArray = (map) => [...map.values()].map(node => node.kind === 'folder' ? { ...node, children: toArray(node.children) } : node);
return sortChangesTreeLevel(toArray(root));
}

function countChangesTreeFiles(node) {
let count = 0;
for (const child of node.children) count += child.kind === 'file' ? 1 : countChangesTreeFiles(child);
return count;
}

function changesFolderKey(subSessionId, folderPath) {
return `${subSessionId || ''}\0${folderPath}`;
}

// The folders a compacted row stands for, outermost first — see .ai/contexts/changes-view.md ("List or tree")
function changesFolderChain(parentPath, name) {
const chain = [];
let folderPath = parentPath;
for (const part of String(name).split('/')) {
folderPath = folderPath ? `${folderPath}/${part}` : part;
chain.push(folderPath);
}
return chain;
}

function changesTreeRows(tree, collapsed, subSessionId, parentPath = '', depth = 0) {
const rows = [];
for (const node of tree) {
if (node.kind === 'folder') {
const chain = changesFolderChain(parentPath, node.name);
const folderPath = chain[chain.length - 1];
const isCollapsed = chain.some(folder => collapsed.has(changesFolderKey(subSessionId, folder)));
rows.push({ kind: 'folder', depth, name: node.name, folderPath, chain, count: countChangesTreeFiles(node), collapsed: isCollapsed });
if (!isCollapsed) rows.push(...changesTreeRows(node.children, collapsed, subSessionId, folderPath, depth + 1));
} else {
rows.push({ kind: 'file', depth, name: node.name, file: node.file });
}
}
return rows;
}

function buildChangesFolderRow(sessionId, tab, row, subSessionId) {
const button = document.createElement('button');
button.type = 'button';
button.className = 'changes-folder-row';
button.dataset.folder = row.folderPath;
if (subSessionId) button.dataset.subagent = subSessionId;
button.style.setProperty('--depth', String(row.depth));
button.setAttribute('aria-expanded', row.collapsed ? 'false' : 'true');
button.title = row.folderPath;

const chevron = document.createElement('span');
chevron.className = 'changes-folder-chevron';
chevron.innerHTML = row.collapsed ? FP_ICONS.chevronRight : FP_ICONS.chevronDown;
button.appendChild(chevron);

const name = document.createElement('span');
name.className = 'changes-folder-name';
name.textContent = row.name;
button.appendChild(name);

const count = document.createElement('span');
count.className = 'changes-folder-count';
count.textContent = String(row.count);
button.appendChild(count);

button.addEventListener('click', () => {
if (row.collapsed) for (const folder of row.chain) tab.collapsedFolders.delete(changesFolderKey(subSessionId, folder));
else tab.collapsedFolders.add(changesFolderKey(subSessionId, row.folderPath));
tab.listRevision = (tab.listRevision || 0) + 1;
renderPanel(sessionId);
const again = [...changesListEl.querySelectorAll('.changes-folder-row')]
.find(el => el.dataset.folder === row.folderPath && (el.dataset.subagent || null) === (subSessionId || null));
if (again) again.focus();
});
return button;
}

// see .ai/contexts/changes-view.md ("Subagent worktrees")
Expand All @@ -1781,9 +1932,7 @@ function appendSubagentChangesGroup(sessionId, tab, group) {
changesListEl.appendChild(header);

const shown = group.files.length > MAX_SUBAGENT_GROUP_ROWS ? group.files.slice(0, MAX_SUBAGENT_GROUP_ROWS) : group.files;
for (const file of shown) {
changesListEl.appendChild(buildChangesFileRow(sessionId, tab, file, group.sessionId));
}
appendChangesRows(sessionId, tab, shown, group.sessionId);
if (shown.length < group.files.length) {
const more = document.createElement('div');
more.className = 'changes-more-note';
Expand All @@ -1807,11 +1956,17 @@ function describeUncounted(uncounted) {
return ` (${uncounted} file${uncounted === 1 ? '' : 's'} not counted)`;
}

function buildChangesFileRow(sessionId, tab, file, subSessionId = null) {
function buildChangesFileRow(sessionId, tab, file, subSessionId = null, treeNode = null) {
const row = document.createElement('div');
row.className = 'changes-file-row';
row.dataset.path = file.path;
if (subSessionId) row.dataset.subagent = subSessionId;
const fullLabel = (file.renamed && file.origPath) ? `${file.origPath} → ${file.path}` : file.path;
if (treeNode) {
row.classList.add('tree');
row.style.setProperty('--depth', String(treeNode.depth));
row.title = fullLabel;
}

const state = document.createElement('span');
state.className = 'changes-file-state changes-state-' + (file.state || '?').toLowerCase();
Expand All @@ -1821,7 +1976,7 @@ function buildChangesFileRow(sessionId, tab, file, subSessionId = null) {

const pathEl = document.createElement('span');
pathEl.className = 'changes-file-path';
pathEl.textContent = (file.renamed && file.origPath) ? `${file.origPath} → ${file.path}` : file.path;
pathEl.textContent = treeNode ? treeNode.name : fullLabel;
row.appendChild(pathEl);

const counts = document.createElement('span');
Expand Down Expand Up @@ -2279,6 +2434,23 @@ function isChangesBufferDirty(tab) {
return content != null && content !== tab.savedContent;
}

function updateChangesListViewButton() {
if (!changesListViewBtn) return;
const next = CHANGES_LIST_VIEWS[(CHANGES_LIST_VIEWS.indexOf(changesListView) + 1) % CHANGES_LIST_VIEWS.length];
changesListViewBtn.dataset.view = changesListView;
changesListViewBtn.innerHTML = FP_ICONS[changesListView];
changesListViewBtn.title = `${CHANGES_LIST_VIEW_LABELS[changesListView]} — click for ${CHANGES_LIST_VIEW_LABELS[next].toLowerCase()} view`;
changesListViewBtn.setAttribute('aria-label', changesListViewBtn.title);
}

function handleChangesListViewToggle() {
const next = (CHANGES_LIST_VIEWS.indexOf(changesListView) + 1) % CHANGES_LIST_VIEWS.length;
changesListView = CHANGES_LIST_VIEWS[next];
try { localStorage.setItem(CHANGES_LIST_VIEW_KEY, changesListView); } catch {}
updateChangesListViewButton();
if (currentPanelSessionId) renderPanel(currentPanelSessionId);
}

function handleChangesDiffModeToggle() {
const next = (CHANGES_DIFF_MODES.indexOf(changesDiffMode) + 1) % CHANGES_DIFF_MODES.length;
changesDiffMode = CHANGES_DIFF_MODES[next];
Expand Down
Loading
Loading