fix(editor): keep saving edits when touched ranges are out of date (#5638)

A range past the end of the document in ModifiedRanges made ChangeSet.mapPos throw inside onLocal, after the edit reached the backend but before the document was marked dirty, so autosave never wrote a reopened external file. Such ranges are now dropped with a warning. Fixes #5528.
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-24 21:19:16 +08:00
1 джерело 74a009dcc3
коміт 82d13207a9
2 files changed
+25 -2

No files matched your search

+19 -1
Переглянути файл
@@ -1,5 +1,5 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { EditorState, StateEffect } from "@codemirror/state";
import { ChangeSet, EditorState, StateEffect } from "@codemirror/state";
import { showTooltip, type Tooltip } from "@codemirror/view";
import { indentUnit, language as languageFacet } from "@codemirror/language";
import { HEAVY_LIMIT, onEditorEvent, registerSaveParticipant, remoteChange, shownAgain } from "@/editor/api";
@@ -675,6 +675,24 @@ describe("DocumentManager + slots", () => {
});
});
describe("modified ranges", () => {
it("drops a range past the end instead of throwing out of the edit", () => {
const m = new ModifiedRanges();
m.ranges = [
[2, 4],
[580, 587],
];
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
m.add(ChangeSet.of({ from: 0, insert: "ab" }, 10));
expect(m.ranges).toEqual([
[4, 6],
[0, 2],
]);
expect(warn).toHaveBeenCalledOnce();
warn.mockRestore();
});
});
describe("save cleanup", () => {
it("keeps the caret line and untouched lines", () => {
const state = EditorState.create({ doc: "a \nb \nc " });
+6 -1
Переглянути файл
@@ -146,7 +146,12 @@ export class ModifiedRanges {
map(changes: ChangeSet) {
if (this.all) return;
this.ranges = this.ranges.map(([a, b]): [number, number] => [changes.mapPos(a, -1), changes.mapPos(b, 1)]);
// A range past the end belongs to another version of the text; mapping it would throw out of the
// edit (before the document is marked dirty, so the edit would never be saved). Drop it instead.
const kept = this.ranges.filter(([, b]) => b <= changes.length);
if (kept.length < this.ranges.length)
console.warn(`modified ranges past the end (${changes.length}) dropped`, this.ranges.length - kept.length);
this.ranges = kept.map(([a, b]): [number, number] => [changes.mapPos(a, -1), changes.mapPos(b, 1)]);
}
add(changes: ChangeSet) {