Порівняти коміти

...
Автор SHA1 Повідомлення Дата
l0ng-ai 7878ba01fd fix(plugins): activate plugins whose activation event fired before they loaded
A remote project's server receives the desktop's plugin packages after the window has opened its files, so the on-language:twig event 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 of them matches. The window also reports on-language again when the open file's language changes (the server redetects it once it has the plugin's language).

Fixes #7014
2026-11-30 21:16:19 +08:00
4 змінених файлів з 78 додано та 4 видалено

No files matched your search

+24 -2
Переглянути файл
@@ -422,6 +422,10 @@ pub struct PluginHost {
initial_load: (Mutex<bool>, Condvar),
/// `on-startup-finished` has fired.
startup_finished: AtomicBool,
/// Language, file and view activation events fired so far (with their project), so a plugin
/// installed or synced afterwards (a remote server gets its packages after the window opened
/// files) still activates for a Twig file that is already open.
fired_activations: Mutex<Vec<(ActivationEvent, Option<PathBuf>)>>,
/// The theme last announced with `ThemeChanged`.
announced_theme: Mutex<Option<ActiveTheme>>,
/// [`PluginLanguages::revision`] last announced with `LanguagesChanged`.
@@ -541,6 +545,7 @@ impl PluginHost {
lifecycle: Mutex::new(()),
initial_load: (Mutex::new(false), Condvar::new()),
startup_finished: AtomicBool::new(false),
fired_activations: Mutex::new(Vec::new()),
announced_theme: Mutex::new(None),
announced_languages: AtomicU64::new(0),
announced_skills: AtomicU64::new(0),
@@ -699,6 +704,16 @@ impl PluginHost {
/// Fires an activation event from the UI (`on-language:rust`, `on-file:…`, `on-view:…`).
pub fn activation_event(&self, event: &ActivationEvent, project: Option<&Path>) {
{
let mut fired = self.fired_activations.lock();
let entry = (event.clone(), project.map(Path::to_path_buf));
if !fired.contains(&entry) {
if fired.len() >= MAX_FIRED_ACTIVATIONS {
fired.remove(0);
}
fired.push(entry);
}
}
let targets: Vec<_> = self
.plugins
.read()
@@ -3096,8 +3111,12 @@ impl PluginHost {
meta.pending = pending;
meta.status = if plugin.manifest().wasm.is_some() { PluginStatus::Inactive } else { PluginStatus::Static };
}
let eager = plugin.manifest().activation.contains(&ActivationEvent::StartupFinished)
&& self.startup_finished.load(Ordering::Acquire);
let eager = (plugin.manifest().activation.contains(&ActivationEvent::StartupFinished)
&& self.startup_finished.load(Ordering::Acquire))
|| self.fired_activations.lock().iter().any(|(event, project)| {
(plugin.dev_root.is_none() || plugin.dev_root == *project)
&& plugin.manifest().activation.iter().any(|a| activation_matches(a, event))
});
if eager {
let _ = self.ensure_active(plugin, None);
}
@@ -3725,6 +3744,9 @@ fn counts(m: &Manifest) -> Vec<ContributionCount> {
m.declaration_counts().into_iter().map(|(point, count)| ContributionCount { point, count }).collect()
}
/// How many fired activation events [`PluginHost::activation_event`] remembers (oldest dropped first).
const MAX_FIRED_ACTIVATIONS: usize = 512;
fn activation_matches(declared: &ActivationEvent, fired: &ActivationEvent) -> bool {
match (declared, fired) {
(ActivationEvent::Language(a), ActivationEvent::Language(b)) => a == b || a == "*",
+22
Переглянути файл
@@ -1590,6 +1590,28 @@ fn misbehave_with_language(dir: &Path) -> PathBuf {
pkg
}
/// A language activation fired before the plugin was there (a remote server gets the desktop's
/// packages after the window opened its files; a plugin installed with such a file open)
/// activates it once it loads; one that never fired doesn't.
#[test]
fn plugins_loaded_after_their_activation_event_fired_activate() {
use ide_extension::ActivationEvent;
let tmp = tempfile::tempdir().unwrap();
let host = Arc::new(
PluginHost::new(tmp.path().join("plugins"), Arc::new(|_: Option<&str>, _| {}), Limits::default()).unwrap(),
);
host.load_all();
host.startup_finished();
host.activation_event(&ActivationEvent::Language("other".into()), None);
host.install_from_dir(&misbehave_with_language(tmp.path()), &Consent::all()).unwrap();
assert_eq!(host.info_of("misbehave").unwrap().status, PluginStatus::Inactive);
host.uninstall("misbehave").unwrap();
host.activation_event(&ActivationEvent::Language("mis".into()), None);
host.install_from_dir(&misbehave_with_language(tmp.path()), &Consent::all()).unwrap();
assert!(eventually(|| host.info_of("misbehave").is_ok_and(|i| i.status == PluginStatus::Active)));
}
/// Edit-path calls stop waiting at the soft deadline (the result is
/// dropped, the instance kept) and are interrupted at the hard one.
#[test]
+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);
});
}