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

@ -0,0 +1,201 @@
<script setup lang="ts">
/**
* The project tree's context menu for a file (M4.3): Open and Show Diff
* (`vcs.showDiff {path}`, dimmed while the command is disabled).
*
* Follows the WAI-ARIA menu pattern: it takes the focus when it opens,
* Up/Down move between items (wrapping), Enter or Space runs one, Escape,
* Tab or a press outside closes it. The position is clamped into the
* window.
*/
import { computed, nextTick, onBeforeUnmount, onMounted, ref } from "vue";
import { useCommandsStore } from "../../commands/store";
import { t } from "../../i18n";
import { useEditorStore } from "../../stores/editor";
import { SHOW_DIFF_COMMAND } from "../vcs/diff/diff";
const props = defineProps<{
/** Project-relative path of the file. */
path: string;
/** File name, for the accessible label. */
name: string;
/** Window x of the pointer (or the row) in pixels. */
x: number;
/** Window y of the pointer (or the row) in pixels. */
y: number;
}>();
const emit = defineEmits<{
/** The menu should close (an item ran or the user dismissed it). */
close: [];
}>();
const editor = useEditorStore();
const commands = useCommandsStore();
/** The menu element. */
const menuEl = ref<HTMLElement | null>(null);
/** Index of the focused item. */
const focused = ref(0);
/** One item of the menu. */
interface MenuItem {
/** Stable id, also used by tests. */
id: string;
/** Text shown. */
label: string;
/** Whether it can run now. */
enabled: boolean;
/** Runs it. */
run: () => Promise<void>;
}
/** The items, in order. */
const items = computed<MenuItem[]>(() => [
{
id: "open",
label: t("project.tree.menu.open"),
enabled: true,
run: async () => {
await editor.openFile(props.path);
},
},
{
id: "showDiff",
label: t("project.tree.menu.showDiff"),
enabled: commands.isEnabled(SHOW_DIFF_COMMAND),
run: () => commands.execute(SHOW_DIFF_COMMAND, { path: props.path }),
},
]);
/** The menu's position, kept inside the window. */
const position = computed(() => ({
left: `${String(Math.max(0, Math.min(props.x, window.innerWidth - 200)))}px`,
top: `${String(Math.max(0, Math.min(props.y, window.innerHeight - 80)))}px`,
}));
/**
* Runs an item and closes the menu.
*
* @param item - the item.
*/
async function choose(item: MenuItem): Promise<void> {
if (!item.enabled) {
return;
}
emit("close");
await item.run();
}
/**
* Keyboard handling of the menu.
*
* @param event - the key event.
*/
function onKeydown(event: KeyboardEvent): void {
const count = items.value.length;
switch (event.key) {
case "ArrowDown":
focused.value = (focused.value + 1) % count;
break;
case "ArrowUp":
focused.value = (focused.value - 1 + count) % count;
break;
case "Enter":
case " ": {
const item = items.value[focused.value];
if (item) {
void choose(item);
}
break;
}
case "Escape":
case "Tab":
emit("close");
break;
default:
return;
}
event.preventDefault();
event.stopPropagation();
}
/**
* Closes the menu on a press outside it.
*
* @param event - the pointer event on the window.
*/
function onPointerDown(event: PointerEvent): void {
if (menuEl.value && event.target instanceof Node && !menuEl.value.contains(event.target)) {
emit("close");
}
}
onMounted(() => {
window.addEventListener("pointerdown", onPointerDown, true);
void nextTick(() => menuEl.value?.focus());
});
onBeforeUnmount(() => {
window.removeEventListener("pointerdown", onPointerDown, true);
});
</script>
<template>
<div
ref="menuEl"
class="tree-menu"
role="menu"
tabindex="-1"
:aria-label="t('project.tree.menu.label', { name })"
:aria-activedescendant="`lt-tree-menu-${items[focused]?.id ?? ''}`"
:style="position"
data-test="tree-menu"
@keydown="onKeydown"
@contextmenu.prevent
>
<div
v-for="(item, index) in items"
:id="`lt-tree-menu-${item.id}`"
:key="item.id"
class="tree-menu__item"
:class="{ 'tree-menu__item--focused': index === focused, 'tree-menu__item--disabled': !item.enabled }"
role="menuitem"
:aria-disabled="!item.enabled"
:data-item="item.id"
data-test="tree-menu-item"
@mouseenter="focused = index"
@click="choose(item)"
>
{{ item.label }}
</div>
</div>
</template>
<style scoped>
.tree-menu {
position: fixed;
z-index: 50;
min-width: 160px;
padding: 4px 0;
color: var(--lt-fg);
background: var(--lt-bg);
border: 1px solid var(--lt-border);
border-radius: 4px;
outline: none;
}
.tree-menu__item {
padding: 4px 16px;
cursor: default;
white-space: nowrap;
}
.tree-menu__item--focused {
background: var(--lt-editor-selection-bg);
}
.tree-menu__item--disabled {
color: var(--lt-fg-muted);
}
</style>