дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 09:41:34 +02:00
fix(plugins): keep plugin gutter actions and markers coming while navigation indexes (#8087)
Plugin language visuals, plugin colours, server code lenses and plugin line markers restarted a 300-400 ms debounce on every trigger and threw away the request in flight. While a project indexes, plugin navigation notifies about every 250 ms, so the request was never sent or its answer was always dropped: a new editor never got its gutter actions. A shared CoalescedRefresh now runs at the latest after a maximum wait and only drops a result older than one already shown. Fixes #8056
Цей коміт міститься в:
1 джерело
417ecfed50
коміт
494d1a9bf5
7 files changed
+152
-66
No files matched your search
@@ -0,0 +1,29 @@
|
||||
import { afterEach, expect, it, vi } from "vitest";
|
||||
import { CoalescedRefresh } from "./coalescedRefresh";
|
||||
|
||||
afterEach(() => vi.useRealTimers());
|
||||
|
||||
it("runs after a quiet delay, and at the latest after the maximum wait under steady triggers", () => {
|
||||
vi.useFakeTimers();
|
||||
const runs: number[] = [];
|
||||
const refresh = new CoalescedRefresh((ticket) => runs.push(ticket), 300, 1000);
|
||||
refresh.schedule();
|
||||
vi.advanceTimersByTime(300);
|
||||
expect(runs).toEqual([1]);
|
||||
for (let t = 0; t < 2000; t += 250) {
|
||||
refresh.schedule();
|
||||
vi.advanceTimersByTime(250);
|
||||
}
|
||||
expect(runs.length).toBeGreaterThanOrEqual(2);
|
||||
refresh.close();
|
||||
});
|
||||
|
||||
it("never lets an older run's result replace a newer one", () => {
|
||||
const refresh = new CoalescedRefresh(() => {}, 300);
|
||||
refresh.show(2);
|
||||
expect(refresh.current(1)).toBe(false);
|
||||
expect(refresh.current(2)).toBe(true);
|
||||
expect(refresh.current(3)).toBe(true);
|
||||
refresh.close();
|
||||
expect(refresh.current(3)).toBe(false);
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* Refresh of an editor layer driven by a stream of triggers (edits, provider changes). A refresh
|
||||
* runs `delayMs` after the latest trigger, but no later than `maxWaitMs` after the first trigger
|
||||
* it covers: a steady stream (plugin navigation notifies every ~250 ms while a project indexes)
|
||||
* must not postpone it forever. Runs are numbered; a run's result is shown only when no later
|
||||
* run's result already is, so a slow run never replaces a newer one, and a newer trigger no
|
||||
* longer throws away the run in flight.
|
||||
*/
|
||||
export class CoalescedRefresh {
|
||||
private timer: ReturnType<typeof setTimeout> | undefined;
|
||||
private due = 0;
|
||||
private started = 0;
|
||||
private shown = 0;
|
||||
private closed = false;
|
||||
|
||||
constructor(
|
||||
private readonly run: (ticket: number) => void,
|
||||
private readonly delayMs: number,
|
||||
private readonly maxWaitMs = delayMs * 4,
|
||||
) {}
|
||||
|
||||
schedule(): void {
|
||||
if (this.closed) return;
|
||||
const now = Date.now();
|
||||
if (this.timer === undefined) this.due = now + this.maxWaitMs;
|
||||
clearTimeout(this.timer);
|
||||
this.timer = setTimeout(
|
||||
() => {
|
||||
this.timer = undefined;
|
||||
this.run(++this.started);
|
||||
},
|
||||
Math.max(0, Math.min(this.delayMs, this.due - now)),
|
||||
);
|
||||
}
|
||||
|
||||
/** Whether the result of run `ticket` may be shown: no later run's result is shown yet. */
|
||||
current(ticket: number): boolean {
|
||||
return !this.closed && ticket >= this.shown;
|
||||
}
|
||||
|
||||
/** Records that run `ticket`'s result is shown. */
|
||||
show(ticket: number): void {
|
||||
this.shown = Math.max(this.shown, ticket);
|
||||
}
|
||||
|
||||
close(): void {
|
||||
this.closed = true;
|
||||
clearTimeout(this.timer);
|
||||
this.timer = undefined;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import {
|
||||
} from "@/editor/api";
|
||||
import { call, commands } from "@/platform/ipc";
|
||||
import { onNavigationProvidersChanged } from "@/editor/navigationProviders";
|
||||
import { CoalescedRefresh } from "@/editor/coalescedRefresh";
|
||||
import { posToOffset } from "./positions";
|
||||
import { onLspRefresh, onServerSettled, anyServerBusy } from "./availability";
|
||||
import { editorPrefs } from "@/platform/editorSettings";
|
||||
@@ -44,8 +45,7 @@ export function serverCodeLenses(ctx: EditorDocContext): Extension {
|
||||
ViewPlugin.define(
|
||||
(view) =>
|
||||
new (class {
|
||||
timer: ReturnType<typeof setTimeout> | undefined;
|
||||
generation = 0;
|
||||
refresh = new CoalescedRefresh((ticket) => void this.run(ticket), 400);
|
||||
skipped = false;
|
||||
enabled = editorPrefs().inlay.codeVision;
|
||||
off: () => void;
|
||||
@@ -64,28 +64,27 @@ export function serverCodeLenses(ctx: EditorDocContext): Extension {
|
||||
this.schedule();
|
||||
}
|
||||
schedule() {
|
||||
this.generation++;
|
||||
clearTimeout(this.timer);
|
||||
this.timer = setTimeout(() => void this.run(), 400);
|
||||
this.refresh.schedule();
|
||||
}
|
||||
update(u: ViewUpdate) {
|
||||
if (u.docChanged || (this.skipped && shownAgain(u))) this.schedule();
|
||||
}
|
||||
async run() {
|
||||
const gen = this.generation;
|
||||
async run(ticket: number) {
|
||||
const text = view.state.doc;
|
||||
this.enabled = editorPrefs().inlay.codeVision;
|
||||
// Resolving a server's lenses counts references / implementations over the whole project.
|
||||
if (!this.enabled || isLibrarySource(ctx)) {
|
||||
this.refresh.show(ticket);
|
||||
if (view.state.field(field).length) view.dispatch({ effects: set.of([]) });
|
||||
return;
|
||||
}
|
||||
try {
|
||||
await whenSynced(view);
|
||||
const result = await call(commands.lspCodeLens(ctx.doc));
|
||||
if (gen !== this.generation || text !== view.state.doc) return;
|
||||
if (!this.refresh.current(ticket) || text !== view.state.doc) return;
|
||||
this.skipped = !view.dom.isConnected;
|
||||
if (this.skipped) return;
|
||||
this.refresh.show(ticket);
|
||||
const fresh = result.flatMap((l) =>
|
||||
l.command && !REFERENCE_COUNT.test(l.command.title)
|
||||
? [
|
||||
@@ -107,13 +106,14 @@ export function serverCodeLenses(ctx: EditorDocContext): Extension {
|
||||
} catch {
|
||||
// A failed request while a server is starting or busy: keep the lenses until it
|
||||
// settles. Otherwise the server is gone and so are its lenses.
|
||||
if (gen === this.generation && view.dom.isConnected && !anyServerBusy())
|
||||
if (this.refresh.current(ticket) && view.dom.isConnected && !anyServerBusy()) {
|
||||
this.refresh.show(ticket);
|
||||
view.dispatch({ effects: set.of([]) });
|
||||
}
|
||||
}
|
||||
}
|
||||
destroy() {
|
||||
this.generation++;
|
||||
clearTimeout(this.timer);
|
||||
this.refresh.close();
|
||||
this.off();
|
||||
}
|
||||
})(),
|
||||
|
||||
@@ -3,6 +3,7 @@ import { RangeSet, StateEffect, StateField, type Extension } from "@codemirror/s
|
||||
import { ViewPlugin, gutter, type ViewUpdate } from "@codemirror/view";
|
||||
import { onNavigationProvidersChanged, shownAgain, type EditorDocContext } from "@/editor/api";
|
||||
import { pluginVisuals } from "@/editor/lsp/pluginVisuals";
|
||||
import { CoalescedRefresh } from "@/editor/coalescedRefresh";
|
||||
import { call, commands, type ColorInfo } from "@/platform/ipc";
|
||||
import { ProviderSwatch, mapColors, replaceRange } from "./providerSwatch";
|
||||
import styles from "./ColorPreview.module.css";
|
||||
@@ -39,37 +40,33 @@ export function pluginColorPreview(ctx: EditorDocContext): Extension {
|
||||
ViewPlugin.define(
|
||||
(view) =>
|
||||
new (class {
|
||||
timer: ReturnType<typeof setTimeout> | undefined;
|
||||
generation = 0;
|
||||
skipped = false;
|
||||
off: () => void;
|
||||
refresh = new CoalescedRefresh((ticket) => void this.run(ticket), 300);
|
||||
constructor() {
|
||||
this.off = onNavigationProvidersChanged(() => this.schedule());
|
||||
this.schedule();
|
||||
}
|
||||
schedule() {
|
||||
this.generation++;
|
||||
clearTimeout(this.timer);
|
||||
this.timer = setTimeout(() => void this.run(), 300);
|
||||
this.off = onNavigationProvidersChanged(() => this.refresh.schedule());
|
||||
this.refresh.schedule();
|
||||
}
|
||||
update(u: ViewUpdate) {
|
||||
if (u.docChanged || (this.skipped && shownAgain(u))) this.schedule();
|
||||
if (u.docChanged || (this.skipped && shownAgain(u))) this.refresh.schedule();
|
||||
}
|
||||
async run() {
|
||||
const gen = this.generation;
|
||||
async run(ticket: number) {
|
||||
const text = view.state.doc;
|
||||
try {
|
||||
const r = await pluginVisuals(view, ctx);
|
||||
if (gen !== this.generation || view.state.doc !== text) return;
|
||||
if (!this.refresh.current(ticket) || view.state.doc !== text) return;
|
||||
this.skipped = !view.dom.isConnected;
|
||||
if (!this.skipped) view.dispatch({ effects: setColors.of(r.colors) });
|
||||
if (this.skipped) return;
|
||||
this.refresh.show(ticket);
|
||||
view.dispatch({ effects: setColors.of(r.colors) });
|
||||
} catch {
|
||||
if (gen === this.generation && view.dom.isConnected) view.dispatch({ effects: setColors.of([]) });
|
||||
if (!this.refresh.current(ticket) || !view.dom.isConnected) return;
|
||||
this.refresh.show(ticket);
|
||||
view.dispatch({ effects: setColors.of([]) });
|
||||
}
|
||||
}
|
||||
destroy() {
|
||||
this.generation++;
|
||||
clearTimeout(this.timer);
|
||||
this.refresh.close();
|
||||
this.off();
|
||||
}
|
||||
})(),
|
||||
|
||||
@@ -179,3 +179,29 @@ it("leaves actions inert from an edit until the plugin answers for the new text"
|
||||
view.dom.querySelector<HTMLButtonElement>(".cm-plugin-gutter-action")!.click();
|
||||
expect(ipc.lspExecuteCommand).toHaveBeenCalledWith(7, expect.objectContaining({ arguments: ['"test"'] }));
|
||||
});
|
||||
|
||||
it("shows gutter commands while navigation providers keep changing (#8056)", async () => {
|
||||
vi.useFakeTimers();
|
||||
const visual = {
|
||||
version: 1,
|
||||
injections: [],
|
||||
colors: [],
|
||||
gutterActions: [["test", { line: 0, icon: "run", tooltip: "Run test", command: "run", argument: null }]],
|
||||
};
|
||||
// Each answer takes longer than the gap between two notifications.
|
||||
ipc.pluginLanguageVisuals.mockImplementation(
|
||||
() => new Promise((resolve) => setTimeout(() => resolve({ status: "ok", data: visual }), 400)),
|
||||
);
|
||||
const view = new EditorView({
|
||||
state: EditorState.create({ doc: "test one", extensions: [iconAreaExtension, pluginLanguageVisuals(ctx)] }),
|
||||
parent: document.body,
|
||||
});
|
||||
views.push(view);
|
||||
// Indexing a large project notifies about every 250 ms for a long time.
|
||||
for (let i = 0; i < 20 && !view.dom.querySelector(".cm-plugin-gutter-action"); i++) {
|
||||
invalidateNavigationProviders();
|
||||
await vi.advanceTimersByTimeAsync(250);
|
||||
}
|
||||
expect(ipc.pluginLanguageVisuals).toHaveBeenCalled();
|
||||
expect(view.dom.querySelector(".cm-plugin-gutter-action")).not.toBeNull();
|
||||
});
|
||||
@@ -27,6 +27,7 @@ import {
|
||||
type EditorDocContext,
|
||||
} from "@/editor/api";
|
||||
import { pluginVisuals } from "@/editor/lsp/pluginVisuals";
|
||||
import { CoalescedRefresh } from "@/editor/coalescedRefresh";
|
||||
import { findLanguageDescription, languageInfo, loadSupport } from "@/editor/core/cm/languages";
|
||||
import { call, commands, type GutterAction, type Injection } from "@/platform/ipc";
|
||||
import { gutterIconMarkup } from "@/ui/icons";
|
||||
@@ -149,31 +150,24 @@ export function pluginLanguageVisuals(ctx: EditorDocContext): Extension {
|
||||
ViewPlugin.define(
|
||||
(view) =>
|
||||
new (class {
|
||||
timer: ReturnType<typeof setTimeout> | undefined;
|
||||
generation = 0;
|
||||
skipped = false;
|
||||
off: () => void;
|
||||
hover: HoverActionLayer;
|
||||
refresh = new CoalescedRefresh((ticket) => void this.run(ticket), 300);
|
||||
constructor() {
|
||||
this.hover = hoverActionLayer(view, ctx);
|
||||
const a = onNavigationProvidersChanged(() => this.schedule());
|
||||
const b = onMarkerIconsChanged(() => this.schedule());
|
||||
const a = onNavigationProvidersChanged(() => this.refresh.schedule());
|
||||
const b = onMarkerIconsChanged(() => this.refresh.schedule());
|
||||
this.off = () => {
|
||||
a();
|
||||
b();
|
||||
};
|
||||
this.schedule();
|
||||
}
|
||||
schedule() {
|
||||
this.generation++;
|
||||
clearTimeout(this.timer);
|
||||
this.timer = setTimeout(() => void this.run(), 300);
|
||||
this.refresh.schedule();
|
||||
}
|
||||
update(u: ViewUpdate) {
|
||||
if (u.docChanged || (this.skipped && shownAgain(u))) this.schedule();
|
||||
if (u.docChanged || (this.skipped && shownAgain(u))) this.refresh.schedule();
|
||||
}
|
||||
async run() {
|
||||
const generation = this.generation;
|
||||
async run(ticket: number) {
|
||||
const text = view.state.doc;
|
||||
try {
|
||||
const result = await pluginVisuals(view, ctx);
|
||||
@@ -192,9 +186,10 @@ export function pluginLanguageVisuals(ctx: EditorDocContext): Extension {
|
||||
marker: new ActionMarker(plugin, { ...a, icon: await resolveMarkerIcon(plugin, a.icon) }, ctx),
|
||||
})),
|
||||
);
|
||||
if (generation !== this.generation || view.state.doc !== text) return;
|
||||
if (!this.refresh.current(ticket) || view.state.doc !== text) return;
|
||||
this.skipped = !view.dom.isConnected;
|
||||
if (this.skipped) return;
|
||||
this.refresh.show(ticket);
|
||||
view.dispatch({ effects: [setHighlight.of(deco), setActions.of(placed)] });
|
||||
this.hover.set(hover as PlacedHoverAction[]);
|
||||
} catch (error) {
|
||||
@@ -202,8 +197,7 @@ export function pluginLanguageVisuals(ctx: EditorDocContext): Extension {
|
||||
}
|
||||
}
|
||||
destroy() {
|
||||
this.generation++;
|
||||
clearTimeout(this.timer);
|
||||
this.refresh.close();
|
||||
this.off();
|
||||
this.hover.destroy();
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
import { RangeSet, StateEffect, StateField, type Extension, type Text } from "@codemirror/state";
|
||||
import { EditorView, GutterMarker, ViewPlugin, type ViewUpdate } from "@codemirror/view";
|
||||
import { iconAreaSource, mapLineStart, shownAgain, type EditorDocContext } from "@/editor/api";
|
||||
import { CoalescedRefresh } from "@/editor/coalescedRefresh";
|
||||
import { navigationButton } from "./navigationButton";
|
||||
import { tx } from "@/platform/i18n";
|
||||
import { pluginsBackend } from "./backend";
|
||||
@@ -117,8 +118,7 @@ class PluginMarker extends GutterMarker {
|
||||
}
|
||||
|
||||
class MarkersPlugin {
|
||||
private timer: ReturnType<typeof setTimeout> | null = null;
|
||||
private generation = 0;
|
||||
private refresh = new CoalescedRefresh((ticket) => void this.run(ticket), DEBOUNCE_MS);
|
||||
private skipped = false;
|
||||
private off: () => void;
|
||||
|
||||
@@ -130,36 +130,25 @@ class MarkersPlugin {
|
||||
if (event.type !== "documentIndexesChanged") return;
|
||||
// The shown markers stay (mapped through edits) until the new ones arrive: clearing them
|
||||
// collapsed the icon area after every edit and shifted the code sideways (#7422).
|
||||
this.generation++;
|
||||
this.schedule();
|
||||
});
|
||||
const offIcons = onMarkerIconsChanged(() => {
|
||||
this.generation++;
|
||||
this.schedule();
|
||||
this.refresh.schedule();
|
||||
});
|
||||
const offIcons = onMarkerIconsChanged(() => this.refresh.schedule());
|
||||
this.off = () => {
|
||||
offBackend();
|
||||
offIcons();
|
||||
};
|
||||
this.schedule();
|
||||
this.refresh.schedule();
|
||||
}
|
||||
|
||||
update(u: ViewUpdate) {
|
||||
if (u.docChanged || (this.skipped && shownAgain(u))) this.schedule();
|
||||
if (u.docChanged || (this.skipped && shownAgain(u))) this.refresh.schedule();
|
||||
}
|
||||
|
||||
private schedule() {
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
this.timer = setTimeout(() => void this.run(), DEBOUNCE_MS);
|
||||
}
|
||||
|
||||
private async run() {
|
||||
this.timer = null;
|
||||
const gen = ++this.generation;
|
||||
private async run(ticket: number) {
|
||||
const text = this.view.state.doc;
|
||||
try {
|
||||
const markers = await commands.pluginLineMarkers(this.doc);
|
||||
if (gen !== this.generation || this.view.state.doc !== text || !this.view.dom.isConnected) return;
|
||||
if (!this.refresh.current(ticket) || this.view.state.doc !== text || !this.view.dom.isConnected) return;
|
||||
if (markers.status !== "ok") return;
|
||||
const resolved = await Promise.all(
|
||||
markers.data.map(async (marker) => ({
|
||||
@@ -167,9 +156,10 @@ class MarkersPlugin {
|
||||
icon: await resolveMarkerIcon(marker.pluginId, marker.icon),
|
||||
})),
|
||||
);
|
||||
if (gen !== this.generation || this.view.state.doc !== text) return;
|
||||
if (!this.refresh.current(ticket) || this.view.state.doc !== text) return;
|
||||
this.skipped = !this.view.dom.isConnected;
|
||||
if (this.skipped) return;
|
||||
this.refresh.show(ticket);
|
||||
const placed = placeMarkers(text, resolved);
|
||||
if (!placed.length && !this.view.state.field(markersField).length) return;
|
||||
this.view.dispatch({ effects: setMarkers.of(placed) });
|
||||
@@ -180,8 +170,7 @@ class MarkersPlugin {
|
||||
|
||||
destroy() {
|
||||
this.off();
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
this.generation++;
|
||||
this.refresh.close();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Заблокувати користувача