M4.3: side-by-side diff viewer with next/previous change

vcs.showDiff {path?, staged?} (Mod+Alt+Shift+D) opens a "Diff: <name>"
tab in the editor area: @codemirror/merge (MIT, loaded lazily, 27 kB /
9.7 kB gzip), with the index (or HEAD for the staged view) read-only on
the left, line breaks normalised to LF, and the working copy on the right,
editable through its normal engine buffer. vcs.nextChange (Alt+Shift+])
and vcs.previousChange (Alt+Shift+[) move between chunks. Binary, too
large, failed and deleted files show a notice. Entry points: the Changes
window, a new project tree context menu (Open, Show Diff; right click or
Shift+F10) and the palette/menu. Colours come from the --lt-vcs-* tokens.
21 Vitest tests; parity docs updated (Git 6% -> 8%, est.).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
danny 2026-10-26 17:23:52 +01:00
parent 468b9928a7
commit 8360ffc2e2
23 changed files with 1836 additions and 24 deletions

View file

@ -7,9 +7,11 @@
* palette (`commands/store.ts`): the area supplies their behaviour with the
* active buffer as context and says when they can run. It also supplies the
* UI commands `editor.reveal` (`reveal.ts`) and the assist commands
* (`assist/commands.ts`).
* (`assist/commands.ts`). Diff tabs (M4.3, `vcs/diff/`) share the tab bar;
* while one is active the diff view replaces the editor, and the area
* supplies the diff commands.
*/
import { onBeforeUnmount, onMounted } from "vue";
import { defineAsyncComponent, onBeforeUnmount, onMounted } from "vue";
import { useCommandsStore } from "../../commands/store";
import { t } from "../../i18n";
import { useEditorStore } from "../../stores/editor";
@ -18,9 +20,15 @@ import CodeEditor from "./CodeEditor.vue";
import EditorTabs from "./EditorTabs.vue";
import { reveal, REVEAL_COMMAND } from "./reveal";
import * as assist from "./assist/commands";
import { provideDiffCommands } from "../vcs/diff/commands";
import { useDiffStore } from "../vcs/diff/diff";
/** The diff view, loaded on first use so `@codemirror/merge` stays out of the start bundle. */
const DiffView = defineAsyncComponent(() => import("../vcs/diff/DiffView.vue"));
const editor = useEditorStore();
const commands = useCommandsStore();
const diff = useDiffStore();
/** Withdraws the command handlers; filled while mounted. */
let withdraw: (() => void)[] = [];
@ -36,6 +44,7 @@ onMounted(() => {
commands.provide(assist.COMPLETE_COMMAND, { run: assist.completeAtCursor, enabled: assist.assistEnabled }),
commands.provide(assist.QUICK_DOC_COMMAND, { run: assist.quickDocumentation, enabled: assist.assistEnabled }),
commands.provide(assist.PARAMETER_INFO_COMMAND, { run: assist.parameterInfo, enabled: assist.assistEnabled }),
...provideDiffCommands(commands),
];
});
@ -53,10 +62,10 @@ onBeforeUnmount(() => {
class="editor-area"
data-test="editor-area"
>
<template v-if="editor.tabs.length > 0">
<template v-if="editor.tabs.length > 0 || diff.tabs.length > 0">
<EditorTabs />
<div
v-if="editor.activeTab?.notice"
v-if="!diff.visible && editor.activeTab?.notice"
class="editor-area__notice"
:class="{ 'editor-area__notice--error': editor.activeTab.notice.kind === 'error' }"
role="status"
@ -72,7 +81,8 @@ onBeforeUnmount(() => {
{{ t("common.dismiss") }}
</button>
</div>
<CodeEditor />
<DiffView v-if="diff.visible" />
<CodeEditor v-else-if="editor.activeTab" />
</template>
<div
v-else

View file

@ -5,15 +5,51 @@
* A click activates a tab, a middle click or the close button closes it,
* Left/Right move between tabs. A dot marks unsaved changes. Closing a
* dirty tab asks first (see the editor store). Names are coloured by their
* Git status (`--lt-vcs-*`, M4.4).
* Git status (`--lt-vcs-*`, M4.4). Diff tabs (M4.3, `vcs/diff/diff.ts`)
* follow the editor tabs; while one is active, no editor tab is marked
* active.
*/
import { X } from "@lucide/vue";
import { t } from "../../i18n";
import { useEditorStore } from "../../stores/editor";
import { useVcsStatusStore } from "../../stores/vcsStatus";
import { useDiffStore } from "../vcs/diff/diff";
const editor = useEditorStore();
const vcs = useVcsStatusStore();
const diff = useDiffStore();
/**
* Whether an editor tab is the active one.
*
* @param id - buffer id of the tab.
*/
function isActive(id: number): boolean {
return id === editor.activeId && !diff.visible;
}
/**
* Shows an editor tab (and hides an active diff).
*
* @param id - buffer id of the tab.
*/
function activateTab(id: number): void {
diff.deactivate();
editor.activate(id);
}
/**
* Closes a diff tab on a middle click.
*
* @param event - the auxiliary click.
* @param key - key of the diff tab.
*/
function onDiffAuxClick(event: MouseEvent, key: string): void {
if (event.button === 1) {
event.preventDefault();
diff.close(key);
}
}
/**
* Closes a tab on a middle click.
@ -42,7 +78,7 @@ function onKeydown(event: KeyboardEvent): void {
event.preventDefault();
const next = editor.tabs[(index + step + editor.tabs.length) % editor.tabs.length];
if (next) {
editor.activate(next.id);
activateTab(next.id);
}
}
</script>
@ -59,14 +95,14 @@ function onKeydown(event: KeyboardEvent): void {
v-for="tab in editor.tabs"
:key="tab.id"
class="tabs__tab"
:class="{ 'tabs__tab--active': tab.id === editor.activeId }"
:class="{ 'tabs__tab--active': isActive(tab.id) }"
role="tab"
:tabindex="tab.id === editor.activeId ? 0 : -1"
:aria-selected="tab.id === editor.activeId"
:tabindex="isActive(tab.id) ? 0 : -1"
:aria-selected="isActive(tab.id)"
:title="tab.path"
:data-id="tab.id"
data-test="editor-tab"
@click="editor.activate(tab.id)"
@click="activateTab(tab.id)"
@mousedown.middle.prevent
@auxclick="onAuxClick($event, tab.id)"
>
@ -92,6 +128,33 @@ function onKeydown(event: KeyboardEvent): void {
<X :size="12" />
</button>
</div>
<div
v-for="tab in diff.tabs"
:key="tab.key"
class="tabs__tab"
:class="{ 'tabs__tab--active': tab.key === diff.activeKey }"
role="tab"
:tabindex="tab.key === diff.activeKey ? 0 : -1"
:aria-selected="tab.key === diff.activeKey"
:title="tab.path"
:data-key="tab.key"
data-test="diff-tab"
@click="diff.activate(tab.key)"
@mousedown.middle.prevent
@auxclick="onDiffAuxClick($event, tab.key)"
>
<span class="tabs__name">{{ tab.staged ? t("vcs.diff.tabTitleStaged", { name: tab.name }) : t("vcs.diff.tabTitle", { name: tab.name }) }}</span>
<button
class="tabs__close"
type="button"
tabindex="-1"
:aria-label="t('vcs.diff.closeTab', { name: tab.name })"
data-test="diff-tab-close"
@click.stop="diff.close(tab.key)"
>
<X :size="12" />
</button>
</div>
</div>
</template>