fix(vcs): keep manual merge result edits valid across changes (#8127)

Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-12-05 11:47:17 +08:00
1 джерело af416b0572
коміт 2389f55bae
3 files changed
+47 -6

No files matched your search

+24
Переглянути файл
@@ -184,6 +184,30 @@ test("Apply saves a CRLF resolution with UTF-16 text as resolved", async ({ page
expect(await saved(page)).toBe(ours.replace(/\r\n/g, "\n"));
});
test("a manual edit spanning several changes stays editable and can be applied", async ({ page }) => {
await mountMerge(page);
await captureWrites(page);
await page.getByRole("button", { name: "Resolve Simple Conflicts" }).click();
await expect(status(page)).toHaveText("1 change left, 1 conflict");
const ours = page.getByRole("region", { name: "Your version" });
const theirs = page.getByRole("region", { name: "Their version" });
await ours.locator(".cm-merge-accept").click();
await theirs.locator(".cm-merge-ignore").click();
await expect(status(page)).toHaveText("All changes processed");
const content = result(page).locator(".cm-content");
await content.click();
await page.keyboard.press("Meta+A");
const combined = BASE.replace("price + tax", "cost + fee")
.replace("\nc\n", "\nC\n")
.replace("\nsame\n", "\ncombined\n");
await page.keyboard.insertText(combined);
await expect.poll(async () => (await text(result(page))).trimEnd()).toBe(combined.trimEnd());
await page.locator("#merge-host").getByRole("button", { name: "Apply", exact: true }).click();
await expect.poll(async () => (await writes(page)).some((call) => call.command === "vcsStage")).toBe(true);
expect(await saved(page)).toBe(combined);
});
test("Apply writes and stages unresolved text only after confirmation", async ({ page }) => {
await mountMerge(page);
await captureWrites(page);
+17 -1
Переглянути файл
@@ -1,6 +1,7 @@
import { history, redo, undo } from "@codemirror/commands";
import { EditorState, Transaction, type TransactionSpec } from "@codemirror/state";
import { describe, expect, it } from "vitest";
import { resultMarks } from "../mergeGutters";
import {
applyNonConflicting,
applyTo,
@@ -27,7 +28,7 @@ function merge(left: string, base: string, right: string) {
const start = startMerge(left, base, right);
let state = EditorState.create({
doc: start.text,
extensions: [history(), mergeHistory, mergeField.init(() => start.changes)],
extensions: [history(), mergeHistory, mergeField.init(() => start.changes), resultMarks],
});
const view = {
get state() {
@@ -97,6 +98,21 @@ describe("the model", () => {
expect(m.changes[0]).toMatchObject({ from: 2, to: 4 });
});
it("keeps merge result decorations valid when a manual edit spans several changes", () => {
const m = merge(left, base, right);
m.act([0], { type: "accept", side: "left", resolve: true });
m.act([1], { type: "accept", side: "right", resolve: true });
const before = m.text;
const combined = lines("a", "combined", "d", "e", "f", "g");
m.run({ changes: { from: 0, to: before.length, insert: combined }, userEvent: "input" });
expect(m.text).toBe(combined);
expect(unresolved(m.view.state)).toHaveLength(1);
expect(m.undo()).toBe(true);
expect(m.text).toBe(before);
expect(m.redo()).toBe(true);
expect(m.text).toBe(combined);
});
it("has no changes for identical versions, and one whole-file conflict for files too different to compare", () => {
expect(merge(base, base, base).changes).toEqual([]);
const many = (p: string) => Array.from({ length: 70_000 }, (_, i) => `${p}${i}`).join("\n");
+6 -5
Переглянути файл
@@ -3,7 +3,7 @@
* cross beside each change in the side panes, a magic wand beside a conflict of the
* result that can be resolved without a decision, and the changes tinted in the result.
*/
import { RangeSetBuilder, StateEffect, StateField, type Extension } from "@codemirror/state";
import { StateEffect, StateField, type Extension, type Range } from "@codemirror/state";
import { Decoration, EditorView, GutterMarker, WidgetType, gutter, type ViewUpdate } from "@codemirror/view";
import { scrollbarMarks, type ScrollbarMark } from "@/editor/api";
import { overviewLane } from "@/editor/core/cm/lanes";
@@ -177,19 +177,20 @@ class PointWidget extends WidgetType {
/** The changes tinted in the result: conflicts, the other changes and the settled ones. */
export const resultMarks = EditorView.decorations.compute([mergeField], (state) => {
const out = new RangeSetBuilder<Decoration>();
const out: Range<Decoration>[] = [];
const doc = state.doc;
for (const c of state.field(mergeField)) {
const className = isResolved(c) ? "cm-merge-resolved" : isConflict(c) ? "cm-merge-conflict" : "cm-merge-change";
if (c.to > c.from) {
const last = doc.lineAt(c.to - 1).number;
for (let n = doc.lineAt(c.from).number; n <= last; n++)
out.add(doc.line(n).from, doc.line(n).from, Decoration.line({ class: className }));
out.push(Decoration.line({ class: className }).range(doc.line(n).from));
} else if (!isResolved(c)) {
out.add(c.from, c.from, Decoration.widget({ widget: new PointWidget(className), block: true, side: -1 }));
out.push(Decoration.widget({ widget: new PointWidget(className), block: true, side: -1 }).range(c.from));
}
}
return out.finish();
// Manual edits can make the mapped ranges of distinct changes overlap.
return Decoration.set(out, true);
});
/** A change's type (MergeConflictType): a conflict, else how the changed side(s) differ from the base. */