fix(plugins): activate plugins whose activation event fired before they loaded (#7054)

A remote project's server receives the desktop's plugin packages after the window has opened its files, so on-language:twig reached the server before the Twig plugin was installed there and was lost; the plugin stayed inactive and Twig files got no highlighting or completion. The same happened locally when a plugin was installed with a matching file already open. The host now remembers the language, file and view activation events fired so far (bounded) and activates a plugin that loads later if one matches, and the window reports on-language again when the active file's language changes.

Fixes #7014
Цей коміт міститься в:
l0ng-ai authored and GitHub committed 2026-11-30 21:16:47 +08:00
1 джерело 3eb6933644
коміт e8cb141d86
4 files changed
+78 -4

No files matched your search

+27
Переглянути файл
@@ -0,0 +1,27 @@
import { afterEach, describe, expect, it, vi } from "vitest";
import { useEditorRegistry } from "@/editor/api";
import { createMemoryPluginsBackend, setPluginsBackend } from "../backend";
import { trackActivationEvents } from "../bridge";
afterEach(() => {
setPluginsBackend(null);
useEditorRegistry.setState({ active: null });
});
describe("activation events", () => {
it("fires on-language when the open file's language is recognized later", () => {
const backend = createMemoryPluginsBackend();
const activationEvent = vi.spyOn(backend, "activationEvent");
setPluginsBackend(backend);
const ctx = (language: string) => ({ active: { ctx: { path: "/p/page.twig", language } } }) as never;
useEditorRegistry.setState(ctx("plaintext"));
const stop = trackActivationEvents();
// A remote server redetects the file once it has the plugin's language.
useEditorRegistry.setState(ctx("twig"));
stop();
expect(activationEvent.mock.calls.filter(([kind]) => kind === "language").map(([, arg]) => arg)).toEqual([
"plaintext",
"twig",
]);
});
});
+5 -2
Переглянути файл
@@ -130,7 +130,7 @@ function scheduleRefresh() {
* Fires `on-language:<id>` and `on-file:<project-relative path>` for files
* opened in the editor; the backend activates the plugins waiting for them.
*/
function trackActivationEvents(): () => void {
export function trackActivationEvents(): () => void {
const seenLanguages = new Set<string>();
const report = (ctx: { path: string | null; language: string } | undefined) => {
if (!ctx?.path) return;
@@ -148,8 +148,11 @@ function trackActivationEvents(): () => void {
}
};
report(useEditorRegistry.getState().active?.ctx);
// Also when the open file's language changes: a remote project learns plugin languages after the
// window restored its tabs, so a `.twig` file is plain text until the server redetects it.
return useEditorRegistry.subscribe((s, prev) => {
if (s.active?.ctx.path !== prev.active?.ctx.path) report(s.active?.ctx);
if (s.active?.ctx.path !== prev.active?.ctx.path || s.active?.ctx.language !== prev.active?.ctx.language)
report(s.active?.ctx);
});
}