fix(plugins): ask plugin line markers again when a tab hidden during a refresh is shown (#8252)

A kept editor tab could show stale plugin gutter markers after it was shown again (for example a plugin disabled while it was hidden): MarkersPlugin.run returned on a detached view without setting skipped, so shownAgain never asked again. It now marks the request skipped there.

Refs #8145

Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-12-06 02:29:09 +08:00
1 джерело 67c48409c8
коміт 90f1ad67d4
2 files changed
+101 -3

No files matched your search

+95 -2
Переглянути файл
@@ -1,7 +1,7 @@
import { afterEach, expect, it, vi } from "vitest";
import { EditorState } from "@codemirror/state";
import { EditorView } from "@codemirror/view";
import type { EditorDocContext } from "@/editor/api";
import { viewShown, type EditorDocContext } from "@/editor/api";
import type { PluginEvent } from "@/bindings";
import { iconAreaExtension } from "@/editor/core/cm/iconArea";
import { pluginLineMarkerGutter } from "./lineMarkerGutter";
@@ -32,7 +32,8 @@ afterEach(() => {
view?.destroy();
view = undefined;
vi.useRealTimers();
vi.clearAllMocks();
// Unconsumed `…Once` answers must not leak into the next test.
vi.resetAllMocks();
});
it("withdraws old interactive markers on reload and does not refresh on idle status changes", async () => {
vi.useFakeTimers();
@@ -121,3 +122,95 @@ it("refreshes a fallback when the package icon declaration arrives after its mar
remove();
}
});
// A recently used tab keeps its view, detached from the document, while hidden.
const marker = (tooltip: string) => ({ pluginId: "test", line: 0, icon: "navigate", tooltip, target: null });
const labels = (view: EditorView) =>
[...view.dom.querySelectorAll(".cm-plugin-mapping-button")].map((el) => el.getAttribute("aria-label"));
async function shownEditor() {
mock.markers.mockResolvedValue({ status: "ok", data: [marker("old")] });
const created = new EditorView({
parent: document.body,
state: EditorState.create({
doc: "select",
extensions: [iconAreaExtension, pluginLineMarkerGutter({ doc: 1 } as EditorDocContext)],
}),
});
await vi.advanceTimersByTimeAsync(450);
expect(labels(created)).toEqual(["old"]);
return created;
}
async function showAgain(view: EditorView) {
document.body.append(view.dom);
view.dispatch({ effects: viewShown.of(null) });
await vi.advanceTimersByTimeAsync(450);
}
it("drops markers withdrawn while the tab was hidden when it is shown again", async () => {
vi.useFakeTimers();
view = await shownEditor();
view.dom.remove();
mock.markers.mockResolvedValue({ status: "ok", data: [] });
mock.event!({ type: "documentIndexesChanged" });
await vi.advanceTimersByTimeAsync(450);
await showAgain(view);
expect(labels(view)).toEqual([]);
});
it("asks again when shown after its document changed while hidden", async () => {
vi.useFakeTimers();
view = await shownEditor();
view.dom.remove();
mock.markers.mockResolvedValue({ status: "ok", data: [marker("new")] });
view.dispatch({ changes: { from: 0, insert: "x" } });
await vi.advanceTimersByTimeAsync(450);
await showAgain(view);
expect(labels(view)).toEqual(["new"]);
});
it("asks again when shown if the tab was hidden while an answer was in flight", async () => {
vi.useFakeTimers();
view = await shownEditor();
let answer!: (value: unknown) => void;
mock.markers.mockReturnValueOnce(new Promise((resolve) => (answer = resolve)));
mock.event!({ type: "documentIndexesChanged" });
await vi.advanceTimersByTimeAsync(450);
view.dom.remove();
answer({ status: "ok", data: [marker("dropped")] });
await vi.advanceTimersByTimeAsync(0);
mock.markers.mockResolvedValue({ status: "ok", data: [marker("new")] });
await showAgain(view);
expect(labels(view)).toEqual(["new"]);
});
it("keeps asking on show until a request made on showing succeeds", async () => {
vi.useFakeTimers();
view = await shownEditor();
view.dom.remove();
mock.event!({ type: "documentIndexesChanged" });
await vi.advanceTimersByTimeAsync(450);
mock.markers.mockResolvedValueOnce({ status: "error", error: { kind: "Internal", message: "busy" } });
await showAgain(view);
expect(labels(view)).toEqual(["old"]);
view.dom.remove();
mock.markers.mockResolvedValue({ status: "ok", data: [marker("new")] });
await showAgain(view);
expect(labels(view)).toEqual(["new"]);
});
it("does not show an older answer over a newer one asked while the tab was hidden", async () => {
vi.useFakeTimers();
view = await shownEditor();
let older!: (value: unknown) => void;
mock.markers.mockReturnValueOnce(new Promise((resolve) => (older = resolve)));
mock.event!({ type: "documentIndexesChanged" });
await vi.advanceTimersByTimeAsync(450);
view.dom.remove();
mock.markers.mockResolvedValue({ status: "ok", data: [] });
mock.event!({ type: "documentIndexesChanged" });
await vi.advanceTimersByTimeAsync(450);
await showAgain(view);
older({ status: "ok", data: [marker("stale")] });
await vi.advanceTimersByTimeAsync(0);
expect(labels(view)).toEqual([]);
});
+6 -1
Переглянути файл
@@ -148,7 +148,12 @@ class MarkersPlugin {
const text = this.view.state.doc;
try {
const markers = await commands.pluginLineMarkers(this.doc);
if (!this.refresh.current(ticket) || this.view.state.doc !== text || !this.view.dom.isConnected) return;
if (!this.refresh.current(ticket) || this.view.state.doc !== text) return;
// Hidden meanwhile: ask again once shown (`shownAgain`), not when the next edit comes.
if (!this.view.dom.isConnected) {
this.skipped = true;
return;
}
if (markers.status !== "ok") return;
const resolved = await Promise.all(
markers.data.map(async (marker) => ({