M4.2: Changes tool window with staging and commit

A Changes tab in the bottom panel (ui/src/components/vcs/changes/): the
project's changed files from git.status in Staged, Changes and Untracked
groups, re-read on start, project open, gitChanged and vcs.refresh
(serialised). A checkbox per file and per group stages or unstages through
git.stage / git.unstage (a staged rename passes both paths); Space stages,
Enter or a double-click runs vcs.showDiff when provided, else opens the
file. A message box with Amend and Commit (also Mod+Enter) runs flushDirty()
first, cancels when a save fails, then git.commit and toasts the short hash.
All git calls go through runWaiting.

UI commands vcs.openChanges (Mod+Alt+G), vcs.commit (Mod+K, focuses the
message box) and vcs.refresh in a new Git menu group; gitChanged added to
EngineEventPayload. 19 Vitest tests; palette, menu and catalog tests follow
the new commands. Docs: architecture, gaps G8, Git parity row 4% (est.),
ui/tooling checklists, roadmap M4 row, ROADMAP depth ~5.4% and log, README.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-25 17:17:39 +01:00
parent 8701f50d37
commit f67f6b9f7c
18 changed files with 1792 additions and 23 deletions

View file

@ -0,0 +1,632 @@
<script setup lang="ts">
/**
* The Changes tool window (M4.2), shown in the bottom panel: the project's
* changed files from git in the groups Staged, Changes and Untracked, and
* a commit message box.
*
* - **Status line:** the branch (or the short hash when detached) and the
* counts, or why there is nothing to show: git missing or too old (with
* a hint to the setting `git.executable`), no repository, no changes.
* A refresh button runs `vcs.refresh`.
* - **Groups:** a header with a checkbox that stages (Changes, Untracked)
* or unstages (Staged) the whole group, then the files. Each file has a
* checkbox doing the same for itself, its name coloured by its state
* (`--lt-vcs-*`), its directory and its state. Up/Down, Page Up/Down,
* Home and End move the selection, Space stages or unstages it, Enter or
* a double-click opens it.
* - **Opening:** through the UI command `vcs.showDiff {path, staged}` when
* something provides it (the diff viewer, M4.3); until then the file
* opens in the editor (`file.open`), unless it was deleted.
* - **Commit box:** the message, an Amend checkbox and Commit. Commit
* saves dirty buffers first (`flushDirty`); `Mod+Enter` in the box
* commits too.
* - State lives in the Changes store (`changes.ts`), so the status and the
* message survive hiding the window. Icons are Lucide's; colours are
* tokens.
*/
import { RefreshCw } from "@lucide/vue";
import { computed, nextTick, onMounted, ref, watch } from "vue";
import { useCommandsStore } from "../../../commands/store";
import { isMac } from "../../../commands/keymap";
import { t } from "../../../i18n";
import { run } from "../../../ipc";
import { baseName } from "../../../stores/editor";
import { useNotificationsStore } from "../../../stores/notifications";
import { SHORT_HASH, useChangesStore, type ChangeRow, type FileState, type GroupId } from "./changes";
const changes = useChangesStore();
const commands = useCommandsStore();
const notifications = useNotificationsStore();
/** The list element. */
const list = ref<HTMLElement | null>(null);
/** The commit message box. */
const messageBox = ref<HTMLTextAreaElement | null>(null);
/** Rows moved by Page Up and Page Down. */
const PAGE = 10;
/**
* The title of a group.
*
* @param group - the group.
*/
function groupTitle(group: GroupId): string {
const titles: Record<GroupId, () => string> = {
staged: () => t("vcs.changes.group.staged"),
changes: () => t("vcs.changes.group.changes"),
untracked: () => t("vcs.changes.group.untracked"),
};
return titles[group]();
}
/**
* The name of a state, for people.
*
* @param state - the state.
*/
function stateLabel(state: FileState): string {
const labels: Record<FileState, () => string> = {
unmodified: () => t("vcs.state.unmodified"),
modified: () => t("vcs.state.modified"),
added: () => t("vcs.state.added"),
deleted: () => t("vcs.state.deleted"),
renamed: () => t("vcs.state.renamed"),
copied: () => t("vcs.state.copied"),
untracked: () => t("vcs.state.untracked"),
ignored: () => t("vcs.state.ignored"),
conflicted: () => t("vcs.state.conflicted"),
};
return labels[state]();
}
/**
* The colour class suffix of a state (`changes__name--<suffix>`, which
* uses the token `--lt-vcs-<suffix>`).
*
* @param state - the state.
*/
function stateColor(state: FileState): string {
switch (state) {
case "added":
case "copied":
return "added";
case "deleted":
return "deleted";
case "untracked":
return "untracked";
case "ignored":
return "ignored";
case "conflicted":
return "conflicted";
default:
return "modified";
}
}
/** The line above the list. */
const statusLine = computed(() => {
const s = changes.status;
switch (changes.phase) {
case "idle":
return changes.loadError ?? t("vcs.changes.noProject");
case "loading":
return t("vcs.changes.loading");
case "unavailable":
return t("vcs.changes.unavailable", { reason: s?.error ?? "" });
case "noRepo":
return s?.error ? t("vcs.changes.repoError", { reason: s.error }) : t("vcs.changes.noRepo");
default:
break;
}
const repo = s?.repo;
const where = repo?.branch ?? (repo?.head ? repo.head.slice(0, SHORT_HASH) : t("vcs.changes.noCommits"));
const g = changes.groups;
if (changes.rows.length === 0) {
return t("vcs.changes.clean", { branch: where });
}
return t("vcs.changes.summary", {
branch: where,
staged: String(g.staged.length),
changes: String(g.changes.length),
untracked: String(g.untracked.length),
});
});
/** The groups with rows, in display order, with each row's flat index. */
const shownGroups = computed(() => {
let index = 0;
const out: { id: GroupId; rows: { row: ChangeRow; index: number }[] }[] = [];
for (const id of ["staged", "changes", "untracked"] as const) {
const rows = changes.groups[id].map((row) => ({ row, index: index++ }));
if (rows.length > 0) {
out.push({ id, rows });
}
}
return out;
});
/** Label of the commit button. */
const commitLabel = computed(() => (changes.amend ? t("vcs.commit.amendButton") : t("vcs.commit.button")));
/** The shortcut hint of the commit button. */
const commitHint = computed(() => t("vcs.commit.hint", { keys: isMac() ? "⌘↩" : "Ctrl+Enter" }));
/**
* The DOM id of a row.
*
* @param index - the row's index in the flat list.
*/
function optionId(index: number): string {
return `changes-row-${String(index)}`;
}
/**
* The directory part of a path, empty at the root.
*
* @param path - project-relative path.
*/
function dirName(path: string): string {
const slash = path.lastIndexOf("/");
return slash < 0 ? "" : path.slice(0, slash);
}
/**
* What a row shows after the name: the directory, and the old path of a
* rename or copy.
*
* @param row - the row.
*/
function detail(row: ChangeRow): string {
const dir = dirName(row.file.path);
const orig = row.file.origPath;
return orig !== undefined && row.group === "staged" ? t("vcs.changes.renamedFrom", { path: orig }) : dir;
}
/**
* The accessible name of a row's checkbox.
*
* @param row - the row.
*/
function toggleLabel(row: ChangeRow): string {
return row.group === "staged"
? t("vcs.changes.unstageFile", { path: row.file.path })
: t("vcs.changes.stageFile", { path: row.file.path });
}
/**
* The accessible name of a group's checkbox.
*
* @param group - the group.
*/
function groupToggleLabel(group: GroupId): string {
return group === "staged" ? t("vcs.changes.unstageAll") : t("vcs.changes.stageGroup", { group: groupTitle(group) });
}
/**
* Opens a row: the diff when `vcs.showDiff` is provided, else the file.
*
* @param row - the row.
*/
async function openRow(row: ChangeRow): Promise<void> {
changes.select(row.key);
const staged = row.group === "staged";
if (commands.isEnabled("vcs.showDiff")) {
await commands.execute("vcs.showDiff", { path: row.file.path, staged });
return;
}
if (row.state === "deleted") {
return;
}
const result = await run("file.open", { path: row.file.path });
if (!result.ok) {
notifications.notifyError("file.open", result.error);
}
}
/**
* Keyboard navigation in the list.
*
* @param event - the key event.
*/
function onListKey(event: KeyboardEvent): void {
const count = changes.rows.length;
const moves: Record<string, number> = {
ArrowDown: 1,
ArrowUp: -1,
PageDown: PAGE,
PageUp: -PAGE,
Home: -count,
End: count,
};
const delta = moves[event.key];
const row = changes.rows[changes.selectedIndex];
if (delta !== undefined) {
event.preventDefault();
changes.move(delta);
} else if (event.key === "Enter" && row) {
event.preventDefault();
void openRow(row);
} else if (event.key === " " && row) {
event.preventDefault();
void changes.toggle(row);
}
}
/**
* Commits on `Mod+Enter` in the message box.
*
* @param event - the key event.
*/
function onMessageKey(event: KeyboardEvent): void {
if (event.key === "Enter" && (isMac() ? event.metaKey : event.ctrlKey)) {
event.preventDefault();
void changes.commit();
}
}
/** Takes the focus `vcs.commit` asked for, once. */
async function takeFocus(): Promise<void> {
if (!changes.focusPending) {
return;
}
changes.focusPending = false;
await nextTick();
messageBox.value?.focus();
}
watch(() => changes.focusRequest, takeFocus);
onMounted(() => {
void takeFocus();
});
watch(
() => changes.selectedIndex,
async (index) => {
await nextTick();
const element = index < 0 ? null : document.getElementById(optionId(index));
if (element && typeof element.scrollIntoView === "function") {
element.scrollIntoView({ block: "nearest" });
}
},
);
</script>
<template>
<div
class="changes"
data-test="changes"
>
<div class="changes__bar">
<div
class="changes__status"
role="status"
data-test="changes-status"
>
{{ statusLine }}
</div>
<button
type="button"
class="changes__icon-button"
:title="t('command.vcs.refresh')"
:aria-label="t('command.vcs.refresh')"
:disabled="changes.loading"
data-test="changes-refresh"
@click="changes.refresh()"
>
<RefreshCw :size="14" />
</button>
</div>
<div
v-if="changes.phase === 'unavailable'"
class="changes__hint"
data-test="changes-hint"
>
{{ t("vcs.changes.unavailableHint") }}
</div>
<div
ref="list"
class="changes__list"
role="listbox"
tabindex="0"
:aria-label="t('vcs.changes.list.label')"
:aria-activedescendant="changes.selectedIndex >= 0 ? optionId(changes.selectedIndex) : undefined"
data-test="changes-list"
@keydown="onListKey"
>
<div
v-for="group in shownGroups"
:key="group.id"
role="group"
:aria-label="groupTitle(group.id)"
:data-test="`changes-group-${group.id}`"
>
<div class="changes__group">
<input
type="checkbox"
class="changes__check"
:checked="group.id === 'staged'"
:disabled="changes.busy"
:aria-label="groupToggleLabel(group.id)"
:title="groupToggleLabel(group.id)"
:data-test="`changes-group-toggle-${group.id}`"
@click.prevent="changes.toggleGroup(group.id)"
>
<span class="changes__group-title">{{ groupTitle(group.id) }}</span>
<span class="changes__group-count">{{ group.rows.length }}</span>
</div>
<div
v-for="{ row, index } in group.rows"
:id="optionId(index)"
:key="row.key"
role="option"
class="changes__row"
:class="{ 'changes__row--selected': changes.selectedKey === row.key }"
:aria-selected="changes.selectedKey === row.key"
:data-state="row.state"
data-test="changes-row"
@click="changes.select(row.key)"
@dblclick="openRow(row)"
>
<input
type="checkbox"
class="changes__check"
tabindex="-1"
:checked="row.group === 'staged'"
:disabled="changes.busy"
:aria-label="toggleLabel(row)"
:title="toggleLabel(row)"
data-test="changes-row-toggle"
@click.stop.prevent="changes.toggle(row)"
>
<span
class="changes__name"
:class="`changes__name--${stateColor(row.state)}`"
>{{ baseName(row.file.path) }}</span>
<span class="changes__dir">{{ detail(row) }}</span>
<span class="changes__state">{{ stateLabel(row.state) }}</span>
</div>
</div>
</div>
<form
class="changes__commit"
data-test="changes-commit"
@submit.prevent="changes.commit()"
>
<textarea
ref="messageBox"
v-model="changes.message"
class="changes__message"
rows="3"
:placeholder="t('vcs.commit.placeholder')"
:aria-label="t('vcs.commit.messageLabel')"
:disabled="changes.phase !== 'ready'"
data-test="changes-message"
@keydown="onMessageKey"
/>
<div class="changes__commit-row">
<label class="changes__amend">
<input
v-model="changes.amend"
type="checkbox"
:disabled="changes.phase !== 'ready' || changes.busy"
data-test="changes-amend"
>
{{ t("vcs.commit.amend") }}
</label>
<button
type="submit"
class="changes__commit-button"
:disabled="!changes.canCommit"
:title="commitHint"
data-test="changes-commit-button"
>
{{ commitLabel }}
</button>
</div>
</form>
</div>
</template>
<style scoped>
.changes {
display: flex;
flex-direction: column;
flex: 1;
min-height: 0;
}
.changes__bar {
display: flex;
align-items: center;
gap: var(--lt-space-s, 4px);
padding: var(--lt-space-xs, 2px) var(--lt-space-m, 8px);
}
.changes__status {
flex: 1;
min-width: 0;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
color: var(--lt-fg-muted);
}
.changes__hint {
padding: 0 var(--lt-space-m, 8px) var(--lt-space-xs, 2px);
color: var(--lt-fg-muted);
}
.changes__icon-button {
display: inline-flex;
align-items: center;
justify-content: center;
padding: var(--lt-space-xs, 2px);
color: var(--lt-fg-muted);
background: transparent;
border: none;
border-radius: var(--lt-radius, 4px);
cursor: pointer;
}
.changes__icon-button:hover:not(:disabled) {
color: var(--lt-fg);
background: color-mix(in srgb, var(--lt-fg) 8%, transparent);
}
.changes__icon-button:focus-visible {
outline: 2px solid var(--lt-accent);
}
.changes__list {
flex: 1;
min-height: 0;
overflow: auto;
border-top: 1px solid var(--lt-border);
outline: none;
}
.changes__list:focus-visible {
box-shadow: inset 0 0 0 1px var(--lt-accent);
}
.changes__group {
display: flex;
align-items: center;
gap: var(--lt-space-s, 4px);
padding: var(--lt-space-xs, 2px) var(--lt-space-m, 8px);
white-space: nowrap;
}
.changes__group-title {
font-weight: 600;
}
.changes__group-count {
color: var(--lt-fg-muted);
}
.changes__check {
flex: none;
margin: 0;
accent-color: var(--lt-accent);
}
.changes__row {
display: flex;
align-items: center;
gap: var(--lt-space-m, 8px);
padding: 0 var(--lt-space-m, 8px) 0 var(--lt-space-l, 24px);
white-space: nowrap;
cursor: default;
}
.changes__row:hover {
background: color-mix(in srgb, var(--lt-fg) 6%, transparent);
}
.changes__row--selected,
.changes__row--selected:hover {
background: var(--lt-editor-selection-bg);
}
.changes__name--added {
color: var(--lt-vcs-added);
}
.changes__name--modified {
color: var(--lt-vcs-modified);
}
.changes__name--deleted {
color: var(--lt-vcs-deleted);
text-decoration: line-through;
}
.changes__name--untracked {
color: var(--lt-vcs-untracked);
}
.changes__name--ignored {
color: var(--lt-vcs-ignored);
}
.changes__name--conflicted {
color: var(--lt-vcs-conflicted);
}
.changes__dir {
overflow: hidden;
text-overflow: ellipsis;
color: var(--lt-fg-muted);
}
.changes__state {
flex: none;
margin-left: auto;
color: var(--lt-fg-muted);
}
.changes__row--selected .changes__dir,
.changes__row--selected .changes__state {
color: var(--lt-fg);
}
.changes__commit {
display: flex;
flex-direction: column;
gap: var(--lt-space-xs, 2px);
padding: var(--lt-space-s, 4px) var(--lt-space-m, 8px);
border-top: 1px solid var(--lt-border);
}
.changes__message {
resize: vertical;
min-height: 3em;
padding: var(--lt-space-xs, 2px) var(--lt-space-s, 4px);
font: inherit;
color: var(--lt-fg);
background: var(--lt-editor-bg);
border: 1px solid var(--lt-border);
border-radius: var(--lt-radius, 4px);
}
.changes__message:focus-visible {
outline: 2px solid var(--lt-accent);
outline-offset: -1px;
}
.changes__commit-row {
display: flex;
align-items: center;
gap: var(--lt-space-m, 8px);
}
.changes__amend {
display: inline-flex;
align-items: center;
gap: var(--lt-space-xs, 2px);
color: var(--lt-fg);
}
.changes__commit-button {
margin-left: auto;
padding: var(--lt-space-xs, 2px) var(--lt-space-l, 24px);
color: var(--lt-fg);
background: color-mix(in srgb, var(--lt-accent) 18%, transparent);
border: 1px solid var(--lt-accent);
border-radius: var(--lt-radius, 4px);
cursor: pointer;
}
.changes__commit-button:disabled {
opacity: 0.5;
cursor: default;
}
.changes__commit-button:focus-visible {
outline: 2px solid var(--lt-fg);
outline-offset: 1px;
}
</style>