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:
parent
468b9928a7
commit
8360ffc2e2
23 changed files with 1836 additions and 24 deletions
338
ui/src/components/vcs/diff/DiffView.vue
Normal file
338
ui/src/components/vcs/diff/DiffView.vue
Normal file
|
|
@ -0,0 +1,338 @@
|
|||
<script setup lang="ts">
|
||||
/**
|
||||
* The active diff tab (M4.3): a side-by-side `@codemirror/merge` view of
|
||||
* the ref (left, read-only) and the working copy (right, editable), or a
|
||||
* notice for binary, too large and failed diffs.
|
||||
*
|
||||
* - **Right side and the buffer:** the right editor starts from the
|
||||
* working copy's {@link BufferSync} state. Every local change of it is
|
||||
* handed to the sync, which sends it to the engine like typing in the
|
||||
* file's own tab (the file's tab is hidden while the diff shows, so the
|
||||
* sync has no view of its own). Engine changes to the buffer (a reload
|
||||
* from disk, an agent's edit) come back through `bufferChanged`: once the
|
||||
* sync has taken them over, the difference is applied to the right
|
||||
* editor outside its undo history.
|
||||
* - **Left side:** replaced in place when the store re-reads it
|
||||
* (`gitChanged` after staging).
|
||||
* - **Colours:** only `--lt-*` tokens; changed lines are tinted with
|
||||
* `--lt-vcs-deleted` (left) and `--lt-vcs-added` (right).
|
||||
* - **Navigation:** a navigator over the merge view is registered for
|
||||
* `vcs.nextChange` / `vcs.previousChange` (`navigation.ts`): the focused
|
||||
* side, else the working copy, moves to the next or previous chunk.
|
||||
*/
|
||||
import { getChunks, goToNextChunk, goToPreviousChunk, MergeView } from "@codemirror/merge";
|
||||
import { Annotation, EditorState, Transaction, type Extension } from "@codemirror/state";
|
||||
import { EditorView } from "@codemirror/view";
|
||||
import { basicSetup } from "codemirror";
|
||||
import { computed, onBeforeUnmount, onMounted, ref, watch } from "vue";
|
||||
import { t } from "../../../i18n";
|
||||
import { onEngineEvent } from "../../../ipc";
|
||||
import { useEditorStore } from "../../../stores/editor";
|
||||
import type { BufferSync } from "../../editor/bufferSync";
|
||||
import { diffDocs } from "../../editor/engineText";
|
||||
import { editorExtensions, lutraEditorTheme } from "../../editor/extensions";
|
||||
import { languageExtensions } from "../../editor/language";
|
||||
import { refFor, useDiffStore } from "./diff";
|
||||
import { setNavigator } from "./navigation";
|
||||
|
||||
const diff = useDiffStore();
|
||||
const editor = useEditorStore();
|
||||
|
||||
/** Element the merge view is mounted in. */
|
||||
const host = ref<HTMLElement | null>(null);
|
||||
/** The merge view, null while none is built. */
|
||||
let merge: MergeView | null = null;
|
||||
/** The sync of the right side, null when the working copy is missing. */
|
||||
let sync: BufferSync | null = null;
|
||||
/** What the current merge view was built for, to rebuild only on a change. */
|
||||
let builtFor = "";
|
||||
/** Ends the engine event subscription. */
|
||||
let unsubscribe: (() => void) | null = null;
|
||||
|
||||
/** Marks right-side transactions that carry the buffer's changes, so they are not sent back. */
|
||||
const fromBuffer = Annotation.define<boolean>();
|
||||
|
||||
/** Colours of the diff, from tokens only. */
|
||||
const diffTheme: Extension = EditorView.theme({
|
||||
"&.cm-merge-a .cm-changedLine, .cm-deletedChunk": {
|
||||
backgroundColor: "color-mix(in srgb, var(--lt-vcs-deleted) 14%, var(--lt-editor-bg))",
|
||||
},
|
||||
"&.cm-merge-b .cm-changedLine, .cm-inlineChangedLine": {
|
||||
backgroundColor: "color-mix(in srgb, var(--lt-vcs-added) 14%, var(--lt-editor-bg))",
|
||||
},
|
||||
"&.cm-merge-a .cm-changedText, .cm-deletedChunk .cm-deletedText": {
|
||||
background: "color-mix(in srgb, var(--lt-vcs-deleted) 35%, transparent)",
|
||||
},
|
||||
"&.cm-merge-b .cm-changedText": {
|
||||
background: "color-mix(in srgb, var(--lt-vcs-added) 35%, transparent)",
|
||||
},
|
||||
"&.cm-merge-b .cm-deletedText": {
|
||||
background: "color-mix(in srgb, var(--lt-vcs-deleted) 35%, transparent)",
|
||||
},
|
||||
".cm-collapsedLines": {
|
||||
color: "var(--lt-fg-muted)",
|
||||
background: "var(--lt-bg)",
|
||||
borderTop: "1px solid var(--lt-border)",
|
||||
borderBottom: "1px solid var(--lt-border)",
|
||||
},
|
||||
"&.cm-merge-a .cm-changedLineGutter, .cm-deletedLineGutter": {
|
||||
background: "var(--lt-vcs-deleted)",
|
||||
},
|
||||
"&.cm-merge-b .cm-changedLineGutter": {
|
||||
background: "var(--lt-vcs-added)",
|
||||
},
|
||||
});
|
||||
|
||||
/** The header line: what is compared with what. */
|
||||
const caption = computed(() => {
|
||||
const tab = diff.activeTab;
|
||||
if (!tab) {
|
||||
return "";
|
||||
}
|
||||
return refFor(tab.staged) === "HEAD"
|
||||
? t("vcs.diff.captionHead", { path: tab.path })
|
||||
: t("vcs.diff.captionIndex", { path: tab.path });
|
||||
});
|
||||
|
||||
/** The notice shown instead of, or above, the diff; null if none. */
|
||||
const notice = computed(() => {
|
||||
const content = diff.activeContent;
|
||||
switch (content?.phase) {
|
||||
case "binary":
|
||||
return t("vcs.diff.binary");
|
||||
case "tooLarge":
|
||||
return t("vcs.diff.tooLarge");
|
||||
case "error":
|
||||
return t("vcs.diff.failed", { reason: content.message ?? "" });
|
||||
case "ready":
|
||||
return content.message;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
});
|
||||
|
||||
/**
|
||||
* Hands local changes of the right editor to the buffer's sync.
|
||||
*
|
||||
* @param transactions - transactions the right editor applied.
|
||||
*/
|
||||
function forward(transactions: readonly Transaction[]): void {
|
||||
if (!sync) {
|
||||
return;
|
||||
}
|
||||
for (const tr of transactions) {
|
||||
if (!tr.docChanged || tr.annotation(fromBuffer) === true) {
|
||||
continue;
|
||||
}
|
||||
const changes = sync.state.doc.eq(tr.startState.doc) ? tr.changes : diffDocs(sync.state.doc, tr.state.doc);
|
||||
const userEvent = tr.annotation(Transaction.userEvent);
|
||||
sync.dispatch(userEvent === undefined ? { changes } : { changes, userEvent });
|
||||
}
|
||||
}
|
||||
|
||||
/** Brings the right editor up to the buffer's text, outside its undo history. */
|
||||
function pullFromBuffer(): void {
|
||||
if (!merge || !sync) {
|
||||
return;
|
||||
}
|
||||
const from = merge.b.state.doc;
|
||||
const to = sync.state.doc;
|
||||
if (from.eq(to)) {
|
||||
return;
|
||||
}
|
||||
merge.b.dispatch({
|
||||
changes: diffDocs(from, to),
|
||||
annotations: [fromBuffer.of(true), Transaction.addToHistory.of(false)],
|
||||
});
|
||||
}
|
||||
|
||||
/** Destroys the merge view. */
|
||||
function destroy(): void {
|
||||
setNavigator(null);
|
||||
merge?.destroy();
|
||||
merge = null;
|
||||
sync = null;
|
||||
builtFor = "";
|
||||
}
|
||||
|
||||
/** Builds the merge view for the active tab, or rebuilds it when its inputs changed. */
|
||||
function build(): void {
|
||||
const tab = diff.activeTab;
|
||||
const content = diff.activeContent;
|
||||
const key = tab && content?.phase === "ready" ? `${tab.key}#${String(content.bufferId ?? "none")}` : "";
|
||||
if (key === builtFor && merge) {
|
||||
return;
|
||||
}
|
||||
destroy();
|
||||
if (!tab || !content || content.phase !== "ready" || !host.value) {
|
||||
return;
|
||||
}
|
||||
const bufferId = content.bufferId;
|
||||
sync = bufferId === null ? null : (editor.syncOf(bufferId) ?? null);
|
||||
const readOnly = [EditorState.readOnly.of(true), EditorView.editable.of(false)];
|
||||
const right: Extension = sync
|
||||
? [
|
||||
editorExtensions(tab.path, { id: sync.id, idle: () => sync?.idle() ?? Promise.resolve() }),
|
||||
diffTheme,
|
||||
EditorView.updateListener.of((update) => {
|
||||
forward(update.transactions);
|
||||
}),
|
||||
]
|
||||
: [basicSetup, lutraEditorTheme, languageExtensions(tab.path), diffTheme, readOnly];
|
||||
merge = new MergeView({
|
||||
a: { doc: content.left, extensions: [basicSetup, lutraEditorTheme, languageExtensions(tab.path), diffTheme, readOnly] },
|
||||
b: sync ? { doc: sync.state.doc, selection: sync.state.selection, extensions: right } : { doc: "", extensions: right },
|
||||
parent: host.value,
|
||||
gutter: true,
|
||||
highlightChanges: true,
|
||||
collapseUnchanged: { margin: 3, minSize: 8 },
|
||||
});
|
||||
builtFor = key;
|
||||
const view = merge;
|
||||
const side = (): EditorView => (view.a.hasFocus ? view.a : view.b);
|
||||
setNavigator({
|
||||
view,
|
||||
hasChanges: () => (getChunks(side().state)?.chunks.length ?? 0) > 0,
|
||||
go: (direction) => {
|
||||
const target = side();
|
||||
const moved = (direction > 0 ? goToNextChunk : goToPreviousChunk)(target);
|
||||
target.focus();
|
||||
return moved;
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
/** Replaces the left document with the store's text after a re-read. */
|
||||
function updateLeft(): void {
|
||||
const content = diff.activeContent;
|
||||
if (!merge || content?.phase !== "ready") {
|
||||
return;
|
||||
}
|
||||
const doc = merge.a.state.doc;
|
||||
if (doc.toString() !== content.left) {
|
||||
merge.a.dispatch({ changes: { from: 0, to: doc.length, insert: content.left } });
|
||||
}
|
||||
}
|
||||
|
||||
watch(
|
||||
() => [diff.activeKey, diff.activeContent] as const,
|
||||
() => {
|
||||
build();
|
||||
},
|
||||
);
|
||||
|
||||
watch(
|
||||
() => diff.leftRevision,
|
||||
() => {
|
||||
updateLeft();
|
||||
},
|
||||
);
|
||||
|
||||
onMounted(() => {
|
||||
build();
|
||||
unsubscribe = onEngineEvent((event) => {
|
||||
if (event.kind !== "bufferChanged" || !sync || event.id !== sync.id) {
|
||||
return;
|
||||
}
|
||||
const target = sync;
|
||||
void Promise.resolve()
|
||||
.then(() => target.idle())
|
||||
.then(() => {
|
||||
if (sync === target) {
|
||||
pullFromBuffer();
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
unsubscribe?.();
|
||||
unsubscribe = null;
|
||||
destroy();
|
||||
});
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div
|
||||
class="diff-view"
|
||||
data-test="diff-view"
|
||||
:data-path="diff.activeTab?.path"
|
||||
>
|
||||
<div
|
||||
class="diff-view__caption"
|
||||
data-test="diff-caption"
|
||||
>
|
||||
{{ caption }}
|
||||
</div>
|
||||
<div
|
||||
v-if="notice"
|
||||
class="diff-view__notice"
|
||||
role="status"
|
||||
data-test="diff-notice"
|
||||
>
|
||||
{{ notice }}
|
||||
</div>
|
||||
<div
|
||||
v-if="diff.activeContent?.phase === 'loading'"
|
||||
class="diff-view__loading"
|
||||
data-test="diff-loading"
|
||||
>
|
||||
{{ t("vcs.diff.loading") }}
|
||||
</div>
|
||||
<div
|
||||
v-show="diff.activeContent?.phase === 'ready'"
|
||||
ref="host"
|
||||
class="diff-view__merge"
|
||||
data-test="diff-merge"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.diff-view {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
background: var(--lt-editor-bg);
|
||||
}
|
||||
|
||||
.diff-view__caption {
|
||||
flex-shrink: 0;
|
||||
padding: 3px 12px;
|
||||
color: var(--lt-fg-muted);
|
||||
background: var(--lt-bg);
|
||||
border-bottom: 1px solid var(--lt-border);
|
||||
white-space: nowrap;
|
||||
overflow: hidden;
|
||||
text-overflow: ellipsis;
|
||||
}
|
||||
|
||||
.diff-view__notice {
|
||||
flex-shrink: 0;
|
||||
padding: 4px 12px;
|
||||
color: var(--lt-fg);
|
||||
background: var(--lt-notice-bg);
|
||||
border-bottom: 1px solid var(--lt-border);
|
||||
}
|
||||
|
||||
.diff-view__loading {
|
||||
padding: 12px;
|
||||
color: var(--lt-fg-muted);
|
||||
}
|
||||
|
||||
.diff-view__merge {
|
||||
flex: 1;
|
||||
min-height: 0;
|
||||
overflow: auto;
|
||||
user-select: text;
|
||||
}
|
||||
|
||||
.diff-view__merge :deep(.cm-mergeView) {
|
||||
min-height: 100%;
|
||||
}
|
||||
|
||||
.diff-view__merge :deep(.cm-mergeViewEditor + .cm-mergeViewEditor) {
|
||||
border-left: 1px solid var(--lt-border);
|
||||
}
|
||||
</style>
|
||||
Loading…
Add table
Add a link
Reference in a new issue