diff --git a/e2e/mergeEditor.spec.ts b/e2e/mergeEditor.spec.ts index f4f76f65a..977ecb3eb 100644 --- a/e2e/mergeEditor.spec.ts +++ b/e2e/mergeEditor.spec.ts @@ -172,6 +172,20 @@ test("accepting a deleted version removes the file before staging", async ({ pag ]); }); +test("a long side shows only the lines it changed against the base", async ({ page }) => { + // Edits spread over a long file make the default diff give up and mark everything between them. + const base = Array.from({ length: 2000 }, (_, i) => `"node_modules/pkg-${i}": "1.0.${i}",`); + const bump = (at: number) => lines(...base.map((l, i) => (i % 100 === at ? l.replace('"1.0.', '"2.0.') : l))); + await mountMerge(page, [lines(...base), bump(3), bump(50)]); + for (const [name, line] of [ + ["Your version", 3], + ["Their version", 50], + ] as const) { + const deleted = page.getByRole("region", { name }).locator(".cm-deletedChunk").first().locator(".cm-deletedLine"); + await expect(deleted).toHaveText([base[line]!]); + } +}); + test("the result starts as the base and every change waits for a decision", async ({ page }) => { await mountMerge(page); await expect(status(page)).toHaveText("3 changes left, 2 conflicts"); diff --git a/src/features/vcs/MergeEditor.tsx b/src/features/vcs/MergeEditor.tsx index ea6eabbba..1cdfa2cdf 100644 --- a/src/features/vcs/MergeEditor.tsx +++ b/src/features/vcs/MergeEditor.tsx @@ -15,6 +15,7 @@ import { Button, Spinner, T } from "@/ui"; import { editorService, type EditorInput } from "@/workbench/contributions"; import { blameSlot, toggleAnnotations } from "./blame"; import { languageFor, viewerBase } from "./cm"; +import { ideaDiffConfig } from "./diffOptions"; import { confirmDialog } from "./dialogs"; import { focusedDiffNavigator, setFocusedDiffNavigator, type DiffNavigator } from "./diffNavigator"; import { syncMergeScroll } from "./mergeScroll"; @@ -92,6 +93,10 @@ const writeAutoApply = (on: boolean) => { } }; +// Each side against the base is compared line by line, as in the diff viewer. The default diff +// gives up on long files and marks the whole file as one changed block. +const SIDE_DIFF = ideaDiffConfig("none", "words"); + export function MergeEditor({ input }: { input: EditorInput }) { const { path } = input.data as MergeData; const [state, setState] = useState<{ @@ -176,7 +181,9 @@ export function MergeEditor({ input }: { input: EditorInput }) { EditorView.updateListener.of((u) => { if (u.selectionSet) refreshMergeActions(actionHost); }), - state.base !== null ? unifiedMergeView({ original: state.base, mergeControls: false, gutter: true }) : [], + state.base !== null + ? unifiedMergeView({ original: state.base, mergeControls: false, gutter: true, diffConfig: SIDE_DIFF }) + : [], sideControls(which, controls), mergeStripe(which), mergeTheme, diff --git a/src/features/vcs/mergeScroll.ts b/src/features/vcs/mergeScroll.ts index 6446c897a..cdd3bea05 100644 --- a/src/features/vcs/mergeScroll.ts +++ b/src/features/vcs/mergeScroll.ts @@ -7,6 +7,7 @@ import { Chunk } from "@codemirror/merge"; import type { Text } from "@codemirror/state"; import type { EditorView } from "@codemirror/view"; import { bandAt, docOffset, extent, transferY } from "./diffScroll"; +import { ideaDiffConfig } from "./diffOptions"; export type MergePane = "ours" | "result" | "theirs"; const PANES: readonly MergePane[] = ["ours", "result", "theirs"]; @@ -32,6 +33,10 @@ export class ScrollEchoes { } } +// Lined up by IDEA's line comparison. `Chunk.build` with no config runs an unbounded +// character diff, which takes minutes on a long file such as a lockfile. +const LINES = ideaDiffConfig("none", "lines"); + /** Starts scrolling `views` together; returns the function that stops it. */ export function syncMergeScroll(views: Record): () => void { const built = new Map(); @@ -40,7 +45,7 @@ export function syncMergeScroll(views: Record): () => voi const b = views.result.state.doc; const hit = built.get(side); if (hit && hit.a === a && hit.b === b) return hit.chunks; - const fresh = Chunk.build(a, b); + const fresh = Chunk.build(a, b, LINES); built.set(side, { a, b, chunks: fresh }); return fresh; };