дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
Load Changes from Disk and File > Reload from Disk replaced the text but kept the buffer's old line separator, so after another program rewrote a CRLF file as LF the next save converted every line back; only the watcher's reload re-detected it. The read-only state was read once on open, so a file made read-only outside the IDE still accepted edits and autosave wrote it. The watcher reload, Load Changes from Disk and Reload with Encoding now share take_disk_text, which takes the separator found in the new text (.editorconfig's when set; text without a line break keeps the buffer's) and the file's write permission. The watcher and focus check also follow a permission-only change, and a new IdeEvent::DocReadOnly updates the status bar lock, the read-only banner and the editor (library sources stay read-only). Co-authored-by: ntbang0901 <[email protected]>
1041 рядки
43 KiB
TypeScript
1041 рядки
43 KiB
TypeScript
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
import { ChangeSet, EditorState, StateEffect } from "@codemirror/state";
|
|
import { showTooltip, type Tooltip } from "@codemirror/view";
|
|
import { foldedRanges, indentUnit, language as languageFacet } from "@codemirror/language";
|
|
import {
|
|
HEAVY_LIMIT,
|
|
onEditorEvent,
|
|
registerSaveParticipant,
|
|
remoteChange,
|
|
setLibraryRoots,
|
|
shownAgain,
|
|
} from "@/editor/api";
|
|
import { createMemoryBackend, type MemoryBackend } from "../backend";
|
|
import { _resetLanguages } from "../cm/languages";
|
|
import { heavyExtension } from "../cm/setup";
|
|
import { DocumentManager, ModifiedRanges, saveCleanupChanges, useDocMeta } from "./document";
|
|
import { EditorSlot, type SlotHost } from "./slot";
|
|
import { formatOptionsOf } from "@/editor/lsp/format";
|
|
import { convertIndentsToSpacesSpec, convertIndentsToTabsSpec } from "../cm/lines";
|
|
|
|
const host: SlotHost = {
|
|
onFocus: () => {},
|
|
onBlur: () => {},
|
|
onSelection: () => {},
|
|
onUserEdit: () => {},
|
|
onContextMenu: () => {},
|
|
};
|
|
|
|
const testTooltip: Tooltip = {
|
|
pos: 0,
|
|
create: () => {
|
|
const dom = document.createElement("div");
|
|
dom.className = "test-tab-tooltip";
|
|
return { dom };
|
|
},
|
|
};
|
|
|
|
async function settle() {
|
|
// oxlint-disable-next-line no-await-in-loop -- each tick flushes work scheduled during the previous tick
|
|
for (let i = 0; i < 10; i++) await new Promise((r) => setTimeout(r, 0));
|
|
}
|
|
|
|
function deferred() {
|
|
let resolve!: () => void;
|
|
const promise = new Promise<void>((done) => {
|
|
resolve = done;
|
|
});
|
|
return { promise, resolve };
|
|
}
|
|
|
|
function setup(files: Record<string, string>, settings = {}) {
|
|
_resetLanguages();
|
|
const backend: MemoryBackend = createMemoryBackend(files);
|
|
const scheduled: Array<() => void> = [];
|
|
const manager = new DocumentManager(backend, { settings: () => settings, notify: vi.fn() }, (fn) =>
|
|
scheduled.push(fn),
|
|
);
|
|
manager.start();
|
|
const frame = async () => {
|
|
scheduled.splice(0).forEach((f) => f());
|
|
await settle();
|
|
};
|
|
return { backend, manager, frame };
|
|
}
|
|
|
|
afterEach(() => useDocMeta.setState({}, true));
|
|
|
|
describe("DocumentManager + slots", () => {
|
|
it("emits a versioned document change from a real local edit", async () => {
|
|
const { manager, frame } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
const events: unknown[] = [];
|
|
const off = onEditorEvent((event) => {
|
|
if (event.type === "docChanged") events.push(event);
|
|
});
|
|
try {
|
|
slot.dispatchLocal({ changes: { from: 1, to: 2, insert: "XYZ" }, userEvent: "input.type" });
|
|
await frame();
|
|
expect(events).toEqual([
|
|
{
|
|
type: "docChanged",
|
|
doc: model.doc,
|
|
path: "/p/a.txt",
|
|
baseVersion: 0,
|
|
version: 1,
|
|
changes: [{ from: 1, to: 2, insert: "XYZ" }],
|
|
},
|
|
]);
|
|
} finally {
|
|
off();
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
["opening the file", {}, 0, false, [3, 9]],
|
|
["opening a file that was already loaded", {}, 0, true, [3, 9]],
|
|
["jumping into a short block", {}, 4, false, [9]],
|
|
["disabling short Go block folding", { goShortBlocks: false }, 0, false, []],
|
|
])("applies compact Go folding when %s", async (_reason, folding, caret, loadedFirst, expected) => {
|
|
const path = "/p/main.go";
|
|
const { backend, manager, frame } = setup(
|
|
{
|
|
[path]: [
|
|
"package main",
|
|
"func main() {",
|
|
" if ok {",
|
|
" return",
|
|
" }",
|
|
` if ${"longCondition".repeat(12)} {`,
|
|
" return",
|
|
" }",
|
|
" if done {",
|
|
" return",
|
|
" }",
|
|
"}",
|
|
].join("\n"),
|
|
},
|
|
{ editor: { folding } },
|
|
);
|
|
vi.spyOn(backend, "folding").mockResolvedValue([
|
|
{ start_line: 2, end_line: 4, kind: "compact" },
|
|
{ start_line: 5, end_line: 7, kind: "compact" },
|
|
{ start_line: 8, end_line: 10, kind: "compact" },
|
|
]);
|
|
try {
|
|
const model = await manager.acquire(path);
|
|
if (loadedFirst) await frame();
|
|
const slot = new EditorSlot(model, "go", host);
|
|
if (caret) slot.dispatchLocal({ selection: { anchor: slot.state.doc.line(caret).from } });
|
|
await vi.waitFor(() => {
|
|
const lines: number[] = [];
|
|
foldedRanges(slot.state).between(0, slot.state.doc.length, (from) => {
|
|
lines.push(slot.state.doc.lineAt(from).number);
|
|
});
|
|
expect(lines).toEqual(expected);
|
|
});
|
|
slot.dispose();
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("requests Kotlin declaration highlights while retaining its CodeMirror language", async () => {
|
|
const { backend, manager, frame } = setup({ "/p/Palette.kt": "class Greeter { fun greet() {} }" });
|
|
const highlights = vi.spyOn(backend, "highlights").mockResolvedValue({ kinds: ["function"], data: [20, 25, 0] });
|
|
const model = await manager.acquire("/p/Palette.kt");
|
|
await frame();
|
|
expect(model.language).not.toBeNull();
|
|
expect(model.getMeta().language).toBe("kotlin");
|
|
await vi.waitFor(() => expect(highlights).toHaveBeenCalledWith(model.doc));
|
|
manager.stop();
|
|
});
|
|
|
|
it.each([
|
|
["allowed by the backend", HEAVY_LIMIT + 1, false, true],
|
|
["blocked by the backend", 128, true, false],
|
|
] as const)("loads Go syntax for documents %s", async (_case, size, large, allowed) => {
|
|
const path = "/p/service.pb.go";
|
|
const { backend, manager, frame } = setup({ [path]: "package p\n".padEnd(size, " ") });
|
|
const open = backend.open.bind(backend);
|
|
vi.spyOn(backend, "open").mockImplementation(async (file) => ({ ...(await open(file)), uiMode: "Go", large }));
|
|
const folding = vi.spyOn(backend, "folding");
|
|
try {
|
|
const model = await manager.acquire(path);
|
|
await model.loadLanguage();
|
|
await frame();
|
|
expect(Boolean(model.language)).toBe(allowed);
|
|
expect(!!model.ctx().large).toBe(large);
|
|
expect(model.getMeta().readOnly).toBe(false);
|
|
expect(model.getMeta().banner).toBe(allowed ? null : "codeInsightDisabled");
|
|
if (large) expect(folding).not.toHaveBeenCalled();
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("retains the per-line decoration limit independently of language support", () => {
|
|
expect(heavyExtension(HEAVY_LIMIT)).not.toEqual([]);
|
|
expect(heavyExtension(HEAVY_LIMIT + 1)).toEqual([]);
|
|
});
|
|
|
|
it.each([
|
|
[false, "codeInsightDisabled"],
|
|
[true, "tooLarge"],
|
|
] as const)("distinguishes the editing limit from the insight limit (tooLarge=%s)", async (tooLarge, banner) => {
|
|
const path = "/p/generated.go";
|
|
const { backend, manager } = setup({ [path]: "package p" });
|
|
const open = backend.open.bind(backend);
|
|
vi.spyOn(backend, "open").mockImplementation(async (file) => ({
|
|
...(await open(file)),
|
|
readOnly: true,
|
|
large: true,
|
|
tooLarge,
|
|
}));
|
|
try {
|
|
const model = await manager.acquire(path);
|
|
expect(model.getMeta().banner).toBe(banner);
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it.each([false, true])("honours the backend size flag in a read-only virtual document (large=%s)", async (large) => {
|
|
const uri = "git:/project/logic.pb.go";
|
|
const { backend, manager } = setup({ [uri]: "package p" });
|
|
const openVirtual = backend.openVirtual.bind(backend);
|
|
vi.spyOn(backend, "openVirtual").mockImplementation(async (path) => ({
|
|
...(await openVirtual(path)),
|
|
uiMode: "Go",
|
|
large,
|
|
}));
|
|
try {
|
|
const model = await manager.acquire(uri);
|
|
await model.loadLanguage();
|
|
expect(Boolean(model.language)).toBe(!large);
|
|
expect(model.ctx().virtual).toBe(true);
|
|
expect(model.getMeta().banner).toBeNull();
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("enables language support for an untitled buffer without a size flag", async () => {
|
|
const { backend, manager } = setup({});
|
|
try {
|
|
const model = manager.adopt(await backend.openUntitled(null));
|
|
model.setLanguage("go", "Go");
|
|
await model.loadLanguage();
|
|
expect(model.language).not.toBeNull();
|
|
expect(model.ctx().large).toBeFalsy();
|
|
expect(model.ctx().virtual).toBeFalsy();
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it.each([
|
|
["Palette.kt", "fun greet() {}", 1, false],
|
|
["demo.py", "def greet(): pass", 0, false],
|
|
["demo.ex", "defmodule Demo do\nend", 1, false],
|
|
["Large.kt", " ".repeat(HEAVY_LIMIT + 1), 0, true],
|
|
] as const)("debounces backend highlights after an edit burst in %s", async (name, text, requests, large) => {
|
|
const { backend, manager, frame } = setup({ [`/p/${name}`]: text });
|
|
const open = backend.open.bind(backend);
|
|
vi.spyOn(backend, "open").mockImplementation(async (file) => ({ ...(await open(file)), large }));
|
|
const highlights = vi.spyOn(backend, "highlights");
|
|
try {
|
|
const model = await manager.acquire(`/p/${name}`);
|
|
const slot = new EditorSlot(model, name, host);
|
|
await frame();
|
|
expect(highlights).toHaveBeenCalledTimes(requests);
|
|
highlights.mockClear();
|
|
for (let i = 0; i < 5; i++) slot.dispatchLocal({ changes: { from: 0, insert: " " } });
|
|
await frame();
|
|
expect(highlights).not.toHaveBeenCalled();
|
|
await new Promise((resolve) => setTimeout(resolve, 550));
|
|
expect(highlights).toHaveBeenCalledTimes(requests);
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("gives a new slot the Code Style indent for Reformat Code, not the detected one", async () => {
|
|
const settings = {
|
|
editor: {
|
|
codeStyle: {
|
|
languages: { java: { useTab: true, tabSize: 1, indent: 1 } },
|
|
detectIndents: true,
|
|
editorconfig: false,
|
|
},
|
|
},
|
|
};
|
|
const { manager } = setup({ "/p/A.java": "class A {\n int x;\n}\n" }, settings);
|
|
const model = await manager.acquire("/p/A.java");
|
|
const first = new EditorSlot(model, "t1", host);
|
|
const split = new EditorSlot(model, "t2", host, { from: first });
|
|
for (const slot of [first, split])
|
|
expect(formatOptionsOf(slot.currentState())).toEqual({ tabSize: 1, insertSpaces: false });
|
|
});
|
|
|
|
it("reformats with the Code Style indent also without a language override (#2467)", async () => {
|
|
const settings = { editor: { codeStyle: { languages: {}, detectIndents: true, editorconfig: false } } };
|
|
const { manager } = setup({ "/p/A.java": "class A {\n int x;\n}\n" }, settings);
|
|
const model = await manager.acquire("/p/A.java");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
// Editing follows the 2-space indent detected in the file; Reformat Code uses Java's 4.
|
|
expect(slot.currentState().facet(indentUnit)).toBe(" ");
|
|
expect(formatOptionsOf(slot.currentState())).toEqual({ tabSize: 4, insertSpaces: true });
|
|
});
|
|
|
|
it("detects the indents again after Reformat Code rewrote them", async () => {
|
|
const settings = {
|
|
editor: {
|
|
codeStyle: {
|
|
languages: { java: { useTab: true, tabSize: 1, indent: 1 } },
|
|
detectIndents: true,
|
|
editorconfig: false,
|
|
},
|
|
},
|
|
};
|
|
const { manager } = setup({ "/p/A.java": "class A {\n int x;\n}\n" }, settings);
|
|
const model = await manager.acquire("/p/A.java");
|
|
const first = new EditorSlot(model, "t1", host);
|
|
const split = new EditorSlot(model, "t2", host, { from: first });
|
|
expect(first.currentState().tabSize).toBe(4);
|
|
// Typing keeps the indents detected when the file was opened.
|
|
first.dispatchLocal({ changes: { from: 10, to: 14, insert: "\t" }, userEvent: "input.type" });
|
|
expect(first.currentState().tabSize).toBe(4);
|
|
first.dispatchLocal({ changes: { from: 10, to: 11, insert: " " }, userEvent: "undo" });
|
|
first.dispatchLocal({ changes: { from: 10, to: 14, insert: "\t" }, userEvent: "format.reformat" });
|
|
for (const slot of [first, split]) {
|
|
expect(slot.currentState().tabSize).toBe(1);
|
|
expect(slot.currentState().facet(indentUnit)).toBe("\t");
|
|
}
|
|
expect(useDocMeta.getState()["/p/A.java"]?.indent).toEqual({ tabSize: 1, insertSpaces: false });
|
|
});
|
|
|
|
it("converts indents to tabs with the Code Style tab size, not the detected indent (#5702)", async () => {
|
|
const settings = { editor: { codeStyle: { languages: {}, detectIndents: true, editorconfig: false } } };
|
|
const { manager } = setup({ "/p/A.java": "class A {\n void f() {\n g();\n }\n}\n" }, settings);
|
|
const model = await manager.acquire("/p/A.java");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
// Editing follows the 2-space indent detected in the file; tabs are still 4 columns wide in Java's Code Style.
|
|
expect(slot.currentState().facet(indentUnit)).toBe(" ");
|
|
slot.dispatchLocal(convertIndentsToTabsSpec(slot.currentState())!);
|
|
expect(slot.currentState().doc.toString()).toBe("class A {\n void f() {\n\tg();\n }\n}\n");
|
|
});
|
|
|
|
it("detects the indents again after Convert Indents rewrote them (#5702)", async () => {
|
|
const settings = { editor: { codeStyle: { languages: {}, detectIndents: true, editorconfig: false } } };
|
|
const { manager } = setup(
|
|
{ "/p/A.java": "class A {\n int x;\n}\n", "/p/B.java": "class B {\n\tint x;\n}\n" },
|
|
settings,
|
|
);
|
|
const a = new EditorSlot(await manager.acquire("/p/A.java"), "t1", host);
|
|
a.dispatchLocal(convertIndentsToTabsSpec(a.currentState())!);
|
|
expect(a.currentState().doc.toString()).toBe("class A {\n\tint x;\n}\n");
|
|
// The next Enter indents with a tab, like the rest of the file now.
|
|
expect(a.currentState().facet(indentUnit)).toBe("\t");
|
|
expect(useDocMeta.getState()["/p/A.java"]?.indent).toEqual({ tabSize: 4, insertSpaces: false });
|
|
|
|
const b = new EditorSlot(await manager.acquire("/p/B.java"), "t2", host);
|
|
expect(b.currentState().facet(indentUnit)).toBe("\t");
|
|
b.dispatchLocal(convertIndentsToSpacesSpec(b.currentState())!);
|
|
expect(b.currentState().doc.toString()).toBe("class B {\n int x;\n}\n");
|
|
expect(b.currentState().facet(indentUnit)).toBe(" ");
|
|
});
|
|
|
|
it("shares one document between split slots and syncs edits to the backend", async () => {
|
|
const { backend, manager, frame } = setup({ "/p/a.txt": "hello\n" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
expect(await manager.acquire("/p/a.txt")).toBe(model);
|
|
const left = new EditorSlot(model, "t1", host);
|
|
const right = new EditorSlot(model, "t2", host, { from: left });
|
|
left.dispatchLocal({ changes: { from: 5, insert: " world" } });
|
|
expect(right.currentState().doc.toString()).toBe("hello world\n");
|
|
expect(useDocMeta.getState()["/p/a.txt"]?.dirty).toBe(true);
|
|
await frame();
|
|
expect(backend.docs.get(model.doc)?.text).toBe("hello world\n");
|
|
// Undo history stays per slot: the mirrored change is not undoable on the right.
|
|
right.dispatchLocal({ changes: { from: 0, insert: ">" } });
|
|
await frame();
|
|
expect(left.currentState().doc.toString()).toBe(">hello world\n");
|
|
expect(backend.docs.get(model.doc)?.text).toBe(">hello world\n");
|
|
});
|
|
|
|
it("clears the modified mark when an edit returns the buffer to the saved text", async () => {
|
|
const { manager, frame } = setup({ "/p/a.txt": "hello\n" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
slot.dispatchLocal({ changes: { from: 0, insert: "x" } });
|
|
await frame();
|
|
expect(model.getMeta().dirty).toBe(true);
|
|
slot.dispatchLocal({ changes: { from: 0, to: 1 }, userEvent: "undo" });
|
|
expect(model.getMeta().dirty).toBe(true);
|
|
await frame();
|
|
expect(model.getMeta().dirty).toBe(false);
|
|
});
|
|
|
|
it("applies backend patches to every slot, annotated as remote", async () => {
|
|
const { backend, manager, frame } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const a = new EditorSlot(model, "t1", host);
|
|
const b = new EditorSlot(model, "t2", host);
|
|
let sawRemote = false;
|
|
const applyExternal = a.applyExternal.bind(a);
|
|
const spy = vi.spyOn(a, "applyExternal").mockImplementation((changes, annotations) => {
|
|
sawRemote = annotations.some((x) => x.type === remoteChange);
|
|
applyExternal(changes, annotations);
|
|
});
|
|
backend.patch(model.doc, [{ from: 3, to: 3, insert: "def" }]);
|
|
expect(sawRemote).toBe(true);
|
|
expect(a.currentState().doc.toString()).toBe("abcdef");
|
|
expect(b.currentState().doc.toString()).toBe("abcdef");
|
|
spy.mockRestore();
|
|
// A local edit after the patch uses the new base version.
|
|
b.dispatchLocal({ changes: { from: 0, insert: "0" } });
|
|
await frame();
|
|
expect(backend.docs.get(model.doc)?.text).toBe("0abcdef");
|
|
});
|
|
|
|
it("trims modified lines and adds the final newline on save", async () => {
|
|
const { backend, manager, frame } = setup({ "/p/a.txt": "keep \nline" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
slot.dispatchLocal({ changes: { from: 12, insert: " x " } });
|
|
const save = model.save("explicit");
|
|
await frame();
|
|
await save;
|
|
expect(backend.files.get("/p/a.txt")).toBe("keep \nline x\n");
|
|
expect(useDocMeta.getState()["/p/a.txt"]?.dirty).toBe(false);
|
|
});
|
|
|
|
it.each(["dirty before response", "dirty after response", "clean and dirty after response"] as const)(
|
|
"keeps a newer edit dirty with %s",
|
|
async (order) => {
|
|
const path = "/p/a.txt";
|
|
const { backend, manager } = setup({ [path]: "base\n" }, { files: { autoSave: false } });
|
|
const model = await manager.acquire(path);
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
const events: Array<() => void> = [];
|
|
const originalOnDirty = backend.onDirty.bind(backend);
|
|
let deferDirty = false;
|
|
const onDirty = vi.spyOn(backend, "onDirty").mockImplementation((handler) =>
|
|
originalOnDirty((event) => {
|
|
if (deferDirty) events.push(() => handler(event));
|
|
else handler(event);
|
|
}),
|
|
);
|
|
manager.stop();
|
|
manager.start();
|
|
const response = deferred();
|
|
const originalSave = backend.save.bind(backend);
|
|
const save = vi.spyOn(backend, "save").mockImplementationOnce(async (doc) => {
|
|
await originalSave(doc);
|
|
await response.promise;
|
|
});
|
|
const transitions: boolean[] = [];
|
|
const unsubscribe = useDocMeta.subscribe((state, previous) => {
|
|
const dirty = state[path]?.dirty;
|
|
if (dirty !== undefined && dirty !== previous[path]?.dirty) transitions.push(dirty);
|
|
});
|
|
let pending: Promise<void> | undefined;
|
|
try {
|
|
slot.dispatchLocal({ changes: { from: 4, insert: " saved" } });
|
|
await model.sync.whenIdle();
|
|
expect(transitions).toEqual([true]);
|
|
|
|
deferDirty = order === "clean and dirty after response";
|
|
pending = model.save("explicit");
|
|
await vi.waitFor(() => expect(backend.files.get(path)).toBe("base saved\n"));
|
|
expect(await backend.snapshot(model.doc)).toMatchObject({ text: "base saved\n", dirty: false });
|
|
|
|
transitions.length = 0;
|
|
deferDirty = order !== "dirty before response";
|
|
slot.dispatchLocal({ changes: { from: 10, insert: " newer" } });
|
|
await model.sync.whenIdle();
|
|
expect(await backend.snapshot(model.doc)).toMatchObject({ text: "base saved newer\n", dirty: true });
|
|
|
|
response.resolve();
|
|
await pending;
|
|
expect(slot.currentState().doc.toString()).toBe("base saved newer\n");
|
|
expect(backend.files.get(path)).toBe("base saved\n");
|
|
expect(model.getMeta().dirty).toBe(true);
|
|
const deferredCount = order === "dirty before response" ? 0 : order === "dirty after response" ? 1 : 2;
|
|
expect(events).toHaveLength(deferredCount);
|
|
events.splice(0).forEach((deliver) => deliver());
|
|
expect(useDocMeta.getState()[path]?.dirty).toBe(true);
|
|
expect(transitions).not.toContain(false);
|
|
|
|
deferDirty = false;
|
|
await model.save("explicit");
|
|
expect(backend.files.get(path)).toBe("base saved newer\n");
|
|
expect(await backend.snapshot(model.doc)).toMatchObject({ text: "base saved newer\n", dirty: false });
|
|
expect(useDocMeta.getState()[path]?.dirty).toBe(false);
|
|
expect(transitions.at(-1)).toBe(false);
|
|
} finally {
|
|
response.resolve();
|
|
try {
|
|
await pending;
|
|
} finally {
|
|
unsubscribe();
|
|
save.mockRestore();
|
|
onDirty.mockRestore();
|
|
slot.dispose();
|
|
manager.releaseProjectDocuments();
|
|
manager.stop();
|
|
}
|
|
}
|
|
},
|
|
);
|
|
|
|
it("marks an unchanged buffer clean when its save succeeds", async () => {
|
|
const path = "/p/a.txt";
|
|
const { backend, manager } = setup({ [path]: "base\n" });
|
|
const model = await manager.acquire(path);
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
const transitions: Array<{ dirty: boolean; persisted: string | undefined }> = [];
|
|
const unsubscribe = useDocMeta.subscribe((state, previous) => {
|
|
const dirty = state[path]?.dirty;
|
|
if (dirty !== undefined && dirty !== previous[path]?.dirty)
|
|
transitions.push({ dirty, persisted: backend.files.get(path) });
|
|
});
|
|
try {
|
|
slot.dispatchLocal({ changes: { from: 4, insert: " saved" } });
|
|
await model.save("explicit");
|
|
expect(slot.currentState().doc.toString()).toBe("base saved\n");
|
|
expect(await backend.snapshot(model.doc)).toMatchObject({ text: "base saved\n", dirty: false });
|
|
expect(transitions).toEqual([
|
|
{ dirty: true, persisted: "base\n" },
|
|
{ dirty: false, persisted: "base saved\n" },
|
|
]);
|
|
} finally {
|
|
unsubscribe();
|
|
slot.dispose();
|
|
manager.releaseProjectDocuments();
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("reconciles a save that includes an edit whose acknowledgement is still pending", async () => {
|
|
const path = "/p/a.txt";
|
|
const { backend, manager } = setup({ [path]: "base\n" }, { files: { autoSave: false } });
|
|
const model = await manager.acquire(path);
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
const write = deferred();
|
|
const editReply = deferred();
|
|
const originalSave = backend.save.bind(backend);
|
|
const originalEdit = backend.edit.bind(backend);
|
|
const edit = vi.spyOn(backend, "edit");
|
|
const save = vi.spyOn(backend, "save").mockImplementationOnce(async (doc) => {
|
|
await write.promise;
|
|
await originalSave(doc);
|
|
});
|
|
let pending: Promise<void> | undefined;
|
|
try {
|
|
slot.dispatchLocal({ changes: { from: 4, insert: " first" } });
|
|
await model.sync.whenIdle();
|
|
pending = model.save("explicit");
|
|
await vi.waitFor(() => expect(save).toHaveBeenCalledWith(model.doc));
|
|
|
|
edit.mockImplementationOnce(async (...args) => {
|
|
const version = await originalEdit(...args);
|
|
await editReply.promise;
|
|
return version;
|
|
});
|
|
slot.dispatchLocal({ changes: { from: 10, insert: " second" } });
|
|
const synced = model.sync.whenIdle();
|
|
await vi.waitFor(async () => {
|
|
expect(await backend.snapshot(model.doc)).toMatchObject({ text: "base first second\n", dirty: true });
|
|
});
|
|
write.resolve();
|
|
await pending;
|
|
editReply.resolve();
|
|
await synced;
|
|
expect(slot.currentState().doc.toString()).toBe("base first second\n");
|
|
expect(backend.files.get(path)).toBe("base first second\n");
|
|
expect(useDocMeta.getState()[path]?.dirty).toBe(false);
|
|
} finally {
|
|
editReply.resolve();
|
|
write.resolve();
|
|
try {
|
|
await pending;
|
|
} finally {
|
|
edit.mockRestore();
|
|
save.mockRestore();
|
|
slot.dispose();
|
|
manager.releaseProjectDocuments();
|
|
manager.stop();
|
|
}
|
|
}
|
|
});
|
|
|
|
it.each(["explicit", "auto"] as const)(
|
|
"%s save leaves untouched documents byte-for-byte unchanged",
|
|
async (reason) => {
|
|
const files = {
|
|
"/p/src/main/resources/sql/no-final.sql": "SELECT *\nFROM patients\n\tLIMIT 10 OFFSET 0",
|
|
"/p/trailing.sql": "SELECT id \nFROM patients;\t \n",
|
|
"/p/crlf.sql": "SELECT id\r\nFROM patients;\r\n",
|
|
"/p/Untouched.java": "class Untouched { \n\tint id;\n}",
|
|
"/p/blank.sql": "SELECT 1;\n\n\n",
|
|
"/p/edited.java": "class Edited {}\n",
|
|
};
|
|
const { backend, manager } = setup(files, {
|
|
editor: { general: { stripTrailingScope: "all", removeTrailingBlankLines: true } },
|
|
});
|
|
const participant = vi.fn();
|
|
const unregister = registerSaveParticipant({ id: "test.save", beforeSave: participant });
|
|
const save = vi.spyOn(backend, "save");
|
|
try {
|
|
const slots = await Promise.all(
|
|
Object.keys(files).map(async (path) => new EditorSlot(await manager.acquire(path), path, host)),
|
|
);
|
|
const edited = slots.at(-1)!;
|
|
edited.dispatchLocal({ changes: { from: 0, insert: "// edited\n" } });
|
|
await manager.saveAll(reason);
|
|
for (const [path, text] of Object.entries(files)) {
|
|
if (path !== "/p/edited.java") expect(backend.files.get(path), path).toBe(text);
|
|
}
|
|
expect(backend.files.get("/p/edited.java")).toBe("// edited\nclass Edited {}\n");
|
|
expect(save.mock.calls).toEqual([[edited.model.doc]]);
|
|
expect(participant).toHaveBeenCalledTimes(reason === "explicit" ? 1 : 0);
|
|
if (reason === "explicit") expect(participant.mock.calls[0]![0].path).toBe("/p/edited.java");
|
|
await manager.saveAll(reason);
|
|
expect(save).toHaveBeenCalledTimes(1);
|
|
} finally {
|
|
unregister();
|
|
manager.stop();
|
|
}
|
|
},
|
|
);
|
|
|
|
it("save cleanup keeps what a rollback put back", async () => {
|
|
const { backend, manager } = setup({ "/p/Posts.php": "<?php\n}\n", "/p/a.txt": "x\n" });
|
|
try {
|
|
const php = new EditorSlot(await manager.acquire("/p/Posts.php"), "t1", host);
|
|
// Reverting an added line break at the end: saving doesn't add it back.
|
|
php.dispatchLocal({ changes: { from: 7, to: 8 }, userEvent: "revert" });
|
|
await php.model.save("auto");
|
|
expect(backend.files.get("/p/Posts.php")).toBe("<?php\n}");
|
|
await php.model.save("explicit");
|
|
expect(backend.files.get("/p/Posts.php")).toBe("<?php\n}");
|
|
// The user's own edit at the end ensures it again.
|
|
php.dispatchLocal({ changes: { from: 7, insert: " " }, userEvent: "input.type" });
|
|
await php.model.save("auto");
|
|
expect(backend.files.get("/p/Posts.php")).toBe("<?php\n}\n");
|
|
|
|
// Trailing spaces a rollback restored aren't stripped as an edited line's.
|
|
const txt = new EditorSlot(await manager.acquire("/p/a.txt"), "t2", host);
|
|
txt.dispatchLocal({ changes: { from: 0, to: 1, insert: "y" }, userEvent: "input.type" });
|
|
txt.dispatchLocal({ changes: { from: 0, to: 1, insert: "x " }, userEvent: "input.rollback" });
|
|
await txt.model.save("auto");
|
|
expect(backend.files.get("/p/a.txt")).toBe("x \n");
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("checks dirty state when each queued save starts", async () => {
|
|
const { backend, manager } = setup({ "/p/query.sql": "SELECT 1" });
|
|
const model = await manager.acquire("/p/query.sql");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
slot.dispatchLocal({ changes: { from: 7, to: 8, insert: "2" } });
|
|
const save = vi.spyOn(backend, "save");
|
|
try {
|
|
await Promise.all([model.save("explicit"), model.save("explicit"), model.save("auto")]);
|
|
expect(backend.files.get(model.path)).toBe("SELECT 2\n");
|
|
expect(save).toHaveBeenCalledTimes(1);
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("keeps the buffer dirty when the backend declines an autosave", async () => {
|
|
const { backend, manager } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
slot.dispatchLocal({ changes: { from: 0, to: 1, insert: "X" } });
|
|
backend.autosave = vi.fn(async () => false);
|
|
const save = vi.spyOn(backend, "save");
|
|
try {
|
|
await model.save("auto");
|
|
expect(backend.autosave).toHaveBeenCalledTimes(1);
|
|
expect(save).not.toHaveBeenCalled();
|
|
expect(model.getMeta().dirty).toBe(true);
|
|
await model.save("explicit");
|
|
expect(save).toHaveBeenCalledTimes(1);
|
|
expect(model.getMeta().dirty).toBe(false);
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("closes the backend document when the last slot goes away", async () => {
|
|
const { backend, manager } = setup({ "/p/a.txt": "x" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const a = new EditorSlot(model, "t1", host);
|
|
const b = new EditorSlot(model, "t2", host);
|
|
a.dispose();
|
|
await settle();
|
|
expect(backend.docs.size).toBe(1);
|
|
b.dispose();
|
|
await settle();
|
|
expect(backend.docs.size).toBe(0);
|
|
expect(manager.get("/p/a.txt")).toBeUndefined();
|
|
});
|
|
|
|
it("loads the document's own mode over its language's (Angular templates)", async () => {
|
|
const { manager } = setup({ "/p/angular.json": "{}", "/p/src/app.html": '<p [title]="name()"></p>' });
|
|
const model = await manager.acquire("/p/src/app.html");
|
|
expect(model.getMeta().uiMode).toBe("Angular Template");
|
|
await model.loadLanguage();
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
expect(slot.currentState().facet(languageFacet)?.name).toBe("angular");
|
|
});
|
|
|
|
it("takes the document's mode from a language change", async () => {
|
|
const { manager } = setup({ "/p/app.txt": "<p></p>" });
|
|
const model = await manager.acquire("/p/app.txt");
|
|
model.setLanguage("html", "Angular Template");
|
|
expect(model.getMeta().uiMode).toBe("Angular Template");
|
|
});
|
|
|
|
it("follows a change of the document's mode alone", async () => {
|
|
const { manager } = setup({ "/p/app.html": "<p></p>" });
|
|
const model = await manager.acquire("/p/app.html");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
model.setLanguage("html", "Angular Template");
|
|
await model.loadLanguage();
|
|
expect(model.getMeta().uiMode).toBe("Angular Template");
|
|
expect(slot.currentState().facet(languageFacet)?.name).toBe("angular");
|
|
model.setLanguage("html", null);
|
|
expect(model.getMeta().uiMode).not.toBe("Angular Template");
|
|
expect(slot.currentState().facet(languageFacet)?.name).not.toBe("angular");
|
|
});
|
|
|
|
it("follows the encoding and line ending a reload from disk changed", async () => {
|
|
const { backend, manager } = setup({ "/p/a.txt": "abc" });
|
|
let handler: Parameters<NonNullable<typeof backend.onEncoding>>[0] = () => {};
|
|
backend.onEncoding = (h) => {
|
|
handler = h;
|
|
return () => {};
|
|
};
|
|
manager.stop();
|
|
manager.start();
|
|
const model = await manager.acquire("/p/a.txt");
|
|
try {
|
|
handler({ doc: model.doc, encoding: "utf-16le", hasBom: true, lineEnding: "crLf" });
|
|
expect(useDocMeta.getState()["/p/a.txt"]).toMatchObject({
|
|
encoding: "utf-16le",
|
|
hasBom: true,
|
|
lineEnding: "crLf",
|
|
});
|
|
} finally {
|
|
manager.releaseProjectDocuments();
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("follows a file made read-only or writable outside the IDE", async () => {
|
|
const { backend, manager } = setup({ "/p/a.txt": "abc", "/lib/Dep.java": "class Dep {}" });
|
|
let handler: Parameters<NonNullable<typeof backend.onReadOnly>>[0] = () => {};
|
|
backend.onReadOnly = (h) => {
|
|
handler = h;
|
|
return () => {};
|
|
};
|
|
manager.stop();
|
|
manager.start();
|
|
const removeLibraryRoots = setLibraryRoots("test", ["/lib"]);
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const library = await manager.acquire("/lib/Dep.java");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
try {
|
|
handler({ doc: model.doc, readOnly: true });
|
|
expect(useDocMeta.getState()["/p/a.txt"]).toMatchObject({ readOnly: true, banner: "readOnly" });
|
|
expect(slot.currentState().readOnly).toBe(true);
|
|
handler({ doc: model.doc, readOnly: false });
|
|
expect(useDocMeta.getState()["/p/a.txt"]).toMatchObject({ readOnly: false, banner: null });
|
|
expect(slot.currentState().readOnly).toBe(false);
|
|
// Library sources stay read-only whatever their files allow, as when they open.
|
|
handler({ doc: library.doc, readOnly: false });
|
|
expect(library.getMeta().readOnly).toBe(true);
|
|
} finally {
|
|
slot.dispose();
|
|
removeLibraryRoots();
|
|
manager.releaseProjectDocuments();
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it.each(["ts", "mts", "cts"])("loads Angular inline templates for .%s documents", async (ext) => {
|
|
const path = `/p/src/app.${ext}`;
|
|
const { manager } = setup({
|
|
"/p/angular.json": "{}",
|
|
[path]: "@Component({ template: `<r-card/>` }) class App {}",
|
|
});
|
|
const model = await manager.acquire(path);
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
expect(model.getMeta()).toMatchObject({ language: "typescript", uiMode: "Angular TypeScript" });
|
|
expect(slot.currentState().facet(languageFacet)?.name).toBe("angular-typescript");
|
|
slot.dispose();
|
|
manager.stop();
|
|
});
|
|
|
|
it("keeps a modified file open when its last tab closes without autosave, and reopens it with the changes", async () => {
|
|
const { backend, manager } = setup({ "/p/a.txt": "a\n", "/p/b.txt": "b\n" }, { files: { autoSave: false } });
|
|
try {
|
|
const a = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(a, "t1", host);
|
|
backend.patch(a.doc, [{ from: 0, to: 0, insert: "changed " }]);
|
|
slot.dispose();
|
|
const b = await manager.acquire("/p/b.txt");
|
|
new EditorSlot(b, "t2", host).dispose();
|
|
await new Promise((r) => setTimeout(r, 0));
|
|
expect(backend.files.get("/p/a.txt")).toBe("a\n");
|
|
expect(await manager.acquire("/p/a.txt")).toBe(a);
|
|
expect(a.getMeta().dirty).toBe(true);
|
|
expect(manager.byPath.has("/p/b.txt")).toBe(false);
|
|
await manager.saveAll("explicit");
|
|
expect(backend.files.get("/p/a.txt")).toBe("changed a\n");
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("drops the modified files kept without a tab when the project goes, but keeps untitled buffers", async () => {
|
|
const { backend, manager } = setup({ "/p/a.txt": "a\n" }, { files: { autoSave: false } });
|
|
try {
|
|
const a = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(a, "t1", host);
|
|
backend.patch(a.doc, [{ from: 0, to: 0, insert: "changed " }]);
|
|
slot.dispose();
|
|
const untitled = manager.adopt(await backend.openUntitled(null));
|
|
await new Promise((r) => setTimeout(r, 0));
|
|
expect(manager.byPath.get("/p/a.txt")).toBe(a);
|
|
manager.releaseProjectDocuments();
|
|
expect(a.closed).toBe(true);
|
|
expect(manager.byPath.has("/p/a.txt")).toBe(false);
|
|
expect(manager.byId.has(a.doc)).toBe(false);
|
|
expect(useDocMeta.getState()["/p/a.txt"]).toBeUndefined();
|
|
expect(manager.byPath.get(untitled.path)).toBe(untitled);
|
|
expect(await manager.acquire("/p/a.txt")).not.toBe(a);
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("Save All rejects a failed document save after saving the remaining dirty documents", async () => {
|
|
const { backend, manager } = setup({ "/p/a.txt": "a\n", "/p/b.txt": "b\n" }, { files: { autoSave: false } });
|
|
try {
|
|
const a = await manager.acquire("/p/a.txt");
|
|
const b = await manager.acquire("/p/b.txt");
|
|
backend.patch(a.doc, [{ from: 0, to: 0, insert: "changed " }]);
|
|
backend.patch(b.doc, [{ from: 0, to: 0, insert: "changed " }]);
|
|
const save = backend.save.bind(backend);
|
|
vi.spyOn(backend, "save").mockImplementation(async (id) => {
|
|
if (id === a.doc) throw new Error("disk full");
|
|
await save(id);
|
|
});
|
|
const logged = vi.spyOn(console, "error").mockImplementation(() => {});
|
|
await expect(manager.saveAll("explicit")).rejects.toThrow("disk full");
|
|
expect(logged).toHaveBeenCalledWith("saving /p/a.txt failed", expect.objectContaining({ message: "disk full" }));
|
|
logged.mockRestore();
|
|
expect(backend.files.get("/p/a.txt")).toBe("a\n");
|
|
expect(backend.files.get("/p/b.txt")).toBe("changed b\n");
|
|
expect(a.getMeta().dirty).toBe(true);
|
|
expect(b.getMeta().dirty).toBe(false);
|
|
} finally {
|
|
manager.stop();
|
|
}
|
|
});
|
|
|
|
it("keeps a hidden tab's view and shows it again instead of building a new one", async () => {
|
|
const { backend, manager } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
const shown: boolean[] = [];
|
|
slot.listeners.add((u) => shown.push(shownAgain(u)));
|
|
const view = slot.mount(document.body);
|
|
slot.unmount();
|
|
expect(slot.view).toBeNull();
|
|
expect(view.dom.isConnected).toBe(false);
|
|
// Changes while hidden go to the kept view.
|
|
backend.patch(model.doc, [{ from: 3, to: 3, insert: "def" }]);
|
|
expect(view.state.doc.toString()).toBe("abcdef");
|
|
expect(slot.currentState()).toBe(view.state);
|
|
shown.length = 0;
|
|
expect(slot.mount(document.body)).toBe(view);
|
|
expect(view.dom.isConnected).toBe(true);
|
|
expect(shown).toContain(true);
|
|
slot.dispose();
|
|
manager.stop();
|
|
});
|
|
|
|
it("takes a hidden tab's tooltips off the page with its view, and back when it shows again (#3647)", async () => {
|
|
const { manager } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
const other = new EditorSlot(model, "t2", host);
|
|
const tooltip = () => document.querySelector(".cm-tooltip.test-tab-tooltip");
|
|
const view = slot.mount(document.body);
|
|
// Any tooltip of the tab (a hint, hover, completion…): they live in `document.body`, outside the view.
|
|
view.dispatch({ effects: StateEffect.appendConfig.of(showTooltip.of(testTooltip)) });
|
|
expect(tooltip()).not.toBeNull();
|
|
slot.unmount();
|
|
other.mount(document.body);
|
|
expect(tooltip()).toBeNull();
|
|
other.unmount();
|
|
expect(slot.mount(document.body)).toBe(view);
|
|
expect(tooltip()).not.toBeNull();
|
|
// Disposing the tab leaves nothing of it in the page.
|
|
slot.dispose();
|
|
other.dispose();
|
|
expect(tooltip()).toBeNull();
|
|
manager.stop();
|
|
});
|
|
|
|
it("destroys the least recently hidden views beyond the kept ones, and kept views on dispose", async () => {
|
|
const { manager } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slots = Array.from({ length: 8 }, (_, i) => new EditorSlot(model, `t${i}`, host));
|
|
const views = slots.map((slot) => {
|
|
const view = slot.mount(document.body);
|
|
slot.unmount();
|
|
return view;
|
|
});
|
|
const destroyed = views.map((view) => vi.spyOn(view, "destroy"));
|
|
slots.push(new EditorSlot(model, "t8", host));
|
|
slots[8]!.mount(document.body);
|
|
slots[8]!.unmount();
|
|
expect(destroyed[0]).toHaveBeenCalled();
|
|
expect(destroyed[1]).not.toHaveBeenCalled();
|
|
// A destroyed view leaves its state behind; showing the tab builds a new view on it.
|
|
expect(slots[0]!.mount(document.body)).not.toBe(views[0]);
|
|
slots[1]!.dispose();
|
|
expect(destroyed[1]).toHaveBeenCalled();
|
|
for (const slot of slots) slot.dispose();
|
|
manager.stop();
|
|
});
|
|
|
|
it("restores a destroyed kept view at its exact scroll offset, not its top line", async () => {
|
|
const { manager } = setup({ "/p/a.txt": "line\n".repeat(500) });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(model, "t0", host);
|
|
const view = slot.mount(document.body);
|
|
Object.defineProperty(view.scrollDOM, "scrollTop", { value: 137, configurable: true });
|
|
view.scrollDOM.dispatchEvent(new Event("scroll"));
|
|
slot.unmount();
|
|
const expected = view.scrollSnapshot().value as { range: unknown; yMargin: number; isSnapshot: boolean };
|
|
const others = Array.from({ length: 8 }, (_, i) => new EditorSlot(model, `t${i + 1}`, host));
|
|
for (const other of others) {
|
|
other.mount(document.body);
|
|
other.unmount();
|
|
}
|
|
const restored = (slot as unknown as { scroll: { value: typeof expected } }).scroll.value;
|
|
expect(restored.isSnapshot).toBe(true);
|
|
expect(restored.range).toEqual(expected.range);
|
|
expect(restored.yMargin).toBe(expected.yMargin);
|
|
for (const s of [slot, ...others]) s.dispose();
|
|
manager.stop();
|
|
});
|
|
});
|
|
|
|
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 reliability", () => {
|
|
afterEach(() => vi.restoreAllMocks());
|
|
|
|
it("clears the touched ranges when a clean model is reused", async () => {
|
|
const { manager } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
model.modified.ranges = [[0, 2]];
|
|
expect(await manager.acquire("/p/a.txt")).toBe(model);
|
|
expect(model.modified.ranges).toEqual([]);
|
|
manager.stop();
|
|
});
|
|
|
|
it("keeps the ranges of a dirty model that is reopened", async () => {
|
|
const { manager } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
model.setMeta({ dirty: true });
|
|
model.modified.ranges = [[0, 2]];
|
|
await manager.acquire("/p/a.txt");
|
|
expect(model.modified.ranges).toEqual([[0, 2]]);
|
|
manager.stop();
|
|
});
|
|
|
|
it("keeps the document dirty when its touched ranges cannot be mapped", async () => {
|
|
const { manager } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
vi.spyOn(console, "warn").mockImplementation(() => {});
|
|
vi.spyOn(model.modified, "add").mockImplementation(() => {
|
|
throw new RangeError("stale range");
|
|
});
|
|
slot.dispatchLocal({ changes: { from: 0, insert: "x" }, userEvent: "input.type" });
|
|
expect(model.getMeta().dirty).toBe(true);
|
|
slot.dispose();
|
|
manager.stop();
|
|
});
|
|
|
|
it("writes the buffer when its dirty flag was lost but an edit is still pending", async () => {
|
|
const { backend, manager } = setup({ "/p/a.txt": "abc\n" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
slot.dispatchLocal({ changes: { from: 4, insert: "d" }, userEvent: "input.type" });
|
|
model.setMeta({ dirty: false });
|
|
await model.save("explicit");
|
|
expect(backend.files.get("/p/a.txt")).toBe("abc\nd\n");
|
|
slot.dispose();
|
|
manager.stop();
|
|
});
|
|
|
|
it("routes backend dirty events to every model sharing a document", async () => {
|
|
const { backend, manager, frame } = setup({ "/p/a.txt": "abc" });
|
|
const model = await manager.acquire("/p/a.txt");
|
|
const twin = manager.adopt(await backend.open("/p/a.txt"));
|
|
const slot = new EditorSlot(model, "t1", host);
|
|
slot.dispatchLocal({ changes: { from: 3, insert: "d" }, userEvent: "input.type" });
|
|
await frame();
|
|
expect(twin.getMeta().dirty).toBe(true);
|
|
slot.dispose();
|
|
manager.stop();
|
|
});
|
|
});
|
|
|
|
describe("save cleanup", () => {
|
|
it("keeps the caret line and untouched lines", () => {
|
|
const state = EditorState.create({ doc: "a \nb \nc " });
|
|
const m = new ModifiedRanges();
|
|
m.ranges = [
|
|
[0, 3],
|
|
[8, 11],
|
|
];
|
|
const changes = saveCleanupChanges(state, m, { trim: true, finalNewline: true, protectLines: new Set([3]) });
|
|
expect(changes).toEqual([
|
|
{ from: 1, to: 3 },
|
|
{ from: 11, to: 11, insert: "\n" },
|
|
]);
|
|
});
|
|
});
|