From fa8dd2465a2104e97ded54558fa069815061c0ef Mon Sep 17 00:00:00 2001 From: Bert Verhelst Date: Wed, 18 Nov 2026 04:51:41 +0100 Subject: [PATCH] fix(vcs): the unified diff viewer shows whole added and deleted blocks as blocks (#4036) A chunk without inner (word) fragments stored its whole block as changes, so @codemirror/merge's unified view marked every line of an added or deleted block as changed text and the block looked broken into per-line bars. Such chunks, and the coarse fallback chunks used when the comparison gives up on repetitive files, now carry no changes. Whole deleted blocks take the full deleted colour; only the old lines of a word-compared chunk keep the lighter one. Closes #3913 Co-authored-by: bertyhell <1710840+bertyhell@users.noreply.github.com> --- .../vcs/__tests__/diffOptions.test.ts | 35 +++++++++++++++++-- .../vcs/__tests__/diffToolbar.test.tsx | 14 ++++++++ src/features/vcs/cm.ts | 10 +++--- src/features/vcs/diffOptions.ts | 14 ++++---- 4 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/features/vcs/__tests__/diffOptions.test.ts b/src/features/vcs/__tests__/diffOptions.test.ts index 545006f8e..f67f71ed6 100644 --- a/src/features/vcs/__tests__/diffOptions.test.ts +++ b/src/features/vcs/__tests__/diffOptions.test.ts @@ -1,7 +1,7 @@ import { describe, expect, it } from "vitest"; -import { Chunk, MergeView } from "@codemirror/merge"; +import { Chunk, MergeView, unifiedMergeView } from "@codemirror/merge"; import { ChangeSet, Text } from "@codemirror/state"; -import type { EditorView } from "@codemirror/view"; +import { EditorView } from "@codemirror/view"; import { applyChunk, arrowControls } from "../diffControls"; import { DEFAULT_DIFF_OPTIONS, @@ -363,6 +363,35 @@ describe("diff options", () => { parent.remove(); }); + it("paints whole inserted and deleted blocks of the unified viewer as blocks, without word marks", () => { + const parent = document.createElement("div"); + document.body.appendChild(parent); + const view = new EditorView({ + doc: "keep\nvalue = new\nmid\n first();\n second();\nend\n", + parent, + extensions: [ + viewerBase({ readOnly: true }), + diffWords, + unifiedMergeView({ + original: "dropped\nkeep\nvalue = old\nmid\nend\n gone();\n away();\n", + highlightChanges: true, + mergeControls: false, + diffConfig: ideaDiffConfig("none", "words"), + }), + ], + }); + const texts = (selector: string) => [...view.dom.querySelectorAll(selector)].map((e) => e.textContent); + // A whole block (IDEA's fragment without inner ones) has no words to compare: a full-width line tint only. + expect(texts(".cm-diffLine-inserted:not(.cm-diffLine-fine)")).toEqual([" first();", " second();"]); + expect(texts(".cm-changedText")).toEqual(["new"]); + expect(texts(".cm-deletedChunk .cm-deletedLine")).toEqual(["dropped", "value = old", " gone();", " away();"]); + expect(texts(".cm-deletedText")).toEqual(["old"]); + // The old lines of the compared chunk take its lighter colour, whole deleted blocks the full one. + expect(texts(".cm-diffWords .cm-deletedChunk:has(+ .cm-diffLine-fine)")).toEqual(["value = old"]); + view.destroy(); + parent.remove(); + }); + it("gives a read-only side a tab stop and leaves an editable side alone", () => { const parent = document.createElement("div"); document.body.appendChild(parent); @@ -467,6 +496,8 @@ describe("oversized line comparison", () => { [updatedA, nextA, b], ] as const) { expect(chunks.length).toBeGreaterThan(0); + // No words compared: the unified viewer paints whole blocks, as for IDEA's own chunks. + expect(chunks.every((chunk) => chunk.changes.length === 0)).toBe(true); let restored = target; for (const chunk of [...chunks].reverse()) { const edit = transferChunk(chunk, "a", source, restored); diff --git a/src/features/vcs/__tests__/diffToolbar.test.tsx b/src/features/vcs/__tests__/diffToolbar.test.tsx index 7933b1bc3..6209b7382 100644 --- a/src/features/vcs/__tests__/diffToolbar.test.tsx +++ b/src/features/vcs/__tests__/diffToolbar.test.tsx @@ -126,6 +126,20 @@ describe("Diff toolbar", () => { mounted.unmount(); localStorage.removeItem("openidea.vcs.diffOptions"); }); + it("shows lines added to two texts as one block in the unified viewer, as a plugin's diff", async () => { + localStorage.setItem("openidea.vcs.diffOptions", JSON.stringify({ mode: "unified", highlight: "words" })); + const data = { + path: "plugin-diff:/Rules.java", + left: { type: "text", text: "class Rules {\n int a;\n}\n", label: "Before" }, + right: { type: "text", text: "class Rules {\n int a;\n void check() {\n run();\n }\n}\n", label: "After" }, + }; + const mounted = render(); + await waitFor(() => expect(mounted.container.querySelectorAll(".cm-diffLine-inserted")).toHaveLength(3)); + expect(mounted.container.querySelector(".cm-mergeView")).toBeNull(); + expect(mounted.container.querySelector(".cm-changedText, .cm-deletedText, .cm-diffWord")).toBeNull(); + mounted.unmount(); + localStorage.removeItem("openidea.vcs.diffOptions"); + }); it("shows a binary notice for disk comparisons instead of identical empty text", async () => { probe.mockResolvedValue({ status: "ok", data: { size: 3, binary: true, tooLarge: false } }); const binaryInput = { diff --git a/src/features/vcs/cm.ts b/src/features/vcs/cm.ts index bef80a71e..abecc974e 100644 --- a/src/features/vcs/cm.ts +++ b/src/features/vcs/cm.ts @@ -377,12 +377,14 @@ export const viewerTheme = EditorView.theme( margin: "0 -1px", verticalAlign: "top", }, - // The unified view: old lines deleted, new lines inserted. + // The unified view: old lines deleted, new lines inserted. The old lines of a chunk whose words + // were compared sit right above its first new line and take the lighter colour too. ".cm-deletedChunk": { backgroundColor: diffTint("deleted", 70) }, - "&.cm-diffWords .cm-deletedChunk": { backgroundColor: diffTint("deleted", 28) }, - ".cm-deletedChunk .cm-deletedText": { background: diffTint("deleted", 85) }, + "&.cm-diffWords .cm-deletedChunk:has(+ .cm-diffLine-fine)": { backgroundColor: diffTint("deleted", 28) }, + // Full line height, as `.cm-diffWord`. + ".cm-deletedChunk .cm-deletedText": { display: "inline-block", background: diffTint("deleted", 85) }, "&.cm-merge-a .cm-changedText": { background: diffTint("deleted", 85) }, - "&.cm-merge-b .cm-changedText": { background: diffTint("inserted", 85) }, + "&.cm-merge-b .cm-changedText": { display: "inline-block", background: diffTint("inserted", 85) }, ".cm-changeGutter": { width: "3px", paddingLeft: "0" }, "&.cm-merge-a .cm-changedLineGutter, .cm-deletedLineGutter": { background: "var(--gutter-deleted)" }, "&.cm-merge-b .cm-changedLineGutter": { background: "var(--gutter-modified)" }, diff --git a/src/features/vcs/diffOptions.ts b/src/features/vcs/diffOptions.ts index f1b6e4d64..22b6c9115 100644 --- a/src/features/vcs/diffOptions.ts +++ b/src/features/vcs/diffOptions.ts @@ -140,7 +140,7 @@ export const getDiffInsights = (chunks: readonly Chunk[]): DiffInsights => insig */ export const hasInnerFragments = (chunk: Chunk) => !!fragments.get(chunk)?.inner; -/** IDEA's line fragments as chunks; `changes` hold the inner fragments, or the whole block. */ +/** IDEA's line fragments as chunks; `changes` hold the inner fragments, if any. */ export function ideaChunks(a: Text, b: Text, ignore: IgnorePolicy, highlight: HighlightPolicy): readonly Chunk[] { return toChunks(a, b, ideaOf(ideaDiffConfig(ignore, highlight))!); } @@ -160,16 +160,18 @@ function toChunks(a: Text, b: Text, idea: IdeaDiff): readonly Chunk[] { compared = compareTexts(textA, textB, idea); } catch (error) { if (!(error instanceof DiffTooBigError)) throw error; - // Repeated lines can exhaust IDEA's search. Keep the viewer usable with a bounded, coarser diff. - return remember(build.call(Chunk, a, b, { scanLimit: 500 })); + // Repeated lines can exhaust IDEA's search. Keep the viewer usable with a bounded, coarser diff, + // its blocks without words as IDEA's own. + const coarse = build.call(Chunk, a, b, { scanLimit: 500 }); + return remember(coarse.map((c) => new Chunk([], c.fromA, c.toA, c.fromB, c.toB, c.precise))); } const chunks = compared.map((f) => { // `to` is one past the last line's end, past the document for its last line. const toA = f.startLine1 === f.endLine1 ? f.startOffset1 : f.endLine1 === a.lines ? a.length + 1 : f.endOffset1; const toB = f.startLine2 === f.endLine2 ? f.startOffset2 : f.endLine2 === b.lines ? b.length + 1 : f.endOffset2; - const changes = f.inner - ? f.inner.map((r) => new Change(r.start1, r.end1, r.start2, r.end2)) - : [new Change(0, f.endOffset1 - f.startOffset1, 0, f.endOffset2 - f.startOffset2)]; + // A block without inner fragments has no words to mark (the unified viewer marks `changes`). + // A fresh array each: @codemirror/merge caches deleted-line widgets by it. + const changes = f.inner ? f.inner.map((r) => new Change(r.start1, r.end1, r.start2, r.end2)) : []; const chunk = new Chunk(changes, f.startOffset1, toA, f.startOffset2, toB); fragments.set(chunk, f); return chunk;