diff --git a/crates/ide-host/src/plugins/event_paths.rs b/crates/ide-host/src/plugins/event_paths.rs index ad02284952..27c669c047 100644 --- a/crates/ide-host/src/plugins/event_paths.rs +++ b/crates/ide-host/src/plugins/event_paths.rs @@ -5,7 +5,7 @@ use ide_extension::Capability; use ide_plugin::{EventKind, HostError, WorkbenchEvent}; use std::{ collections::{BTreeSet, HashMap}, - path::Path, + path::{Path, PathBuf}, }; #[derive(Clone, Default)] @@ -136,32 +136,14 @@ impl PluginHost { if !path.is_absolute() { return None; } - let workspace = workspace.as_ref()?; - let real = ide_plugin::resolve_real(path); - // The file's own project, not the invoking window's: annotations follow - // the file, so a plugin hears about a file another window has open. - let root = match client { - Some(client) => workspace - .project_of(client) - .filter(|r| real.starts_with(ide_plugin::resolve_real(r))) - .or_else(|| { - workspace - .projects() - .into_iter() - .filter(|r| real.starts_with(ide_plugin::resolve_real(r))) - .max_by_key(|r| r.components().count()) - }), - None => workspace - .projects() - .into_iter() - .filter(|r| real.starts_with(ide_plugin::resolve_real(r))) - .max_by_key(|r| r.components().count()), - }?; + let root = file_project(workspace.as_deref()?, client, path)?; if plugin.dev_root.as_deref().is_some_and(|r| r != root) { return None; } - if let WorkbenchEvent::DocumentChanged(change) = event { + if let WorkbenchEvent::DocumentChanged(_) = event { // Typed text: only for plugins that may read editors, and only for paths they named. + // The path stays absolute until delivery (`relativize_documents`): the batch is + // filtered again then, against the grants and subscriptions of that moment. let filter = filters.and_then(|filters| filters.get(event.kind().name()))?; let granted = &plugin.meta.lock().granted; if !granted.contains(&Capability::ProjectRead) @@ -170,11 +152,7 @@ impl PluginHost { { return None; } - let real = ide_plugin::resolve_real(path); - let relative = real.strip_prefix(ide_plugin::resolve_real(&root)).ok()?; - let mut change = change.clone(); - change.path = relative.to_string_lossy().replace('\\', "/"); - return Some(WorkbenchEvent::DocumentChanged(change)); + return Some(event.clone()); } filters .and_then(|filters| filters.get(event.kind().name())) @@ -183,6 +161,41 @@ impl PluginHost { }) .collect() } + + /// What a plugin receives for `document-changed`: the path relative to the file's own project. + pub(super) fn relativize_documents( + shared: &super::api::Shared, + client: Option<&str>, + events: Vec, + ) -> Vec { + let workspace = shared.workspace(); + events + .into_iter() + .filter_map(|event| { + let WorkbenchEvent::DocumentChanged(mut change) = event else { return Some(event) }; + let path = Path::new(&change.path); + let root = file_project(workspace.as_deref()?, client, path)?; + let relative = ide_plugin::resolve_real(path) + .strip_prefix(ide_plugin::resolve_real(&root)) + .ok()? + .to_string_lossy() + .replace('\\', "/"); + change.path = relative; + Some(WorkbenchEvent::DocumentChanged(change)) + }) + .collect() + } +} + +/// The open project an absolute `path` belongs to: the invoking window's when the file is in +/// it, else the innermost open project containing it. Annotations follow the file, so a +/// plugin hears about a file another window has open. +fn file_project(workspace: &dyn super::api::PluginWorkspace, client: Option<&str>, path: &Path) -> Option { + let real = ide_plugin::resolve_real(path); + let contains = |r: &PathBuf| real.starts_with(ide_plugin::resolve_real(r)); + client + .and_then(|client| workspace.project_of(client).filter(contains)) + .or_else(|| workspace.projects().into_iter().filter(contains).max_by_key(|r| r.components().count())) } #[cfg(test)] diff --git a/crates/ide-host/src/plugins/mod.rs b/crates/ide-host/src/plugins/mod.rs index 82499769a0..e31a43bc51 100644 --- a/crates/ide-host/src/plugins/mod.rs +++ b/crates/ide-host/src/plugins/mod.rs @@ -2214,6 +2214,7 @@ impl PluginHost { if events.is_empty() { return Ok(()); } + let events = Self::relativize_documents(&shared, invocation.client.as_deref(), events); rt.handle_events(events, invocation) }, move |result| { diff --git a/crates/ide-host/tests/plugins.rs b/crates/ide-host/tests/plugins.rs index 5d8c8822c1..a308590ded 100644 --- a/crates/ide-host/tests/plugins.rs +++ b/crates/ide-host/tests/plugins.rs @@ -5284,6 +5284,29 @@ fn plugin_readonly_fields_and_table_row_extensions_round_trip() { } } +#[test] +fn document_changes_reach_plugins_relative_to_the_project() { + let f = fixture(Limits::default()); + f.install(&sample("path-events")); + f.run("test.path-events", "count", None).unwrap(); + let deliver = |path: &str| { + let change = ide_plugin::DocumentChanged { + path: to_ipc(&f.project.join(path)), + base_version: 1, + version: 2, + changes: Vec::new(), + }; + f.host.post_events(Some("main"), vec![WorkbenchEvent::DocumentChanged(change)]); + f.host.flush_events_at(Instant::now() + Duration::from_secs(3)); + assert!(f.host.wait_events(Duration::from_secs(10))); + }; + deliver("src/main.rs"); + assert_eq!(f.run("test.path-events", "document", None).unwrap(), None, "outside the subscribed paths"); + // Queued with the absolute path, filtered again at delivery, handed over project-relative. + deliver("config/app.yaml"); + assert_eq!(f.run("test.path-events", "document", None).unwrap().as_deref(), Some("config/app.yaml")); +} + #[test] fn path_filtered_events_do_not_wake_idle_plugins_and_recheck_queued_filters() { let f = fixture(Limits { idle_timeout: Some(Duration::from_millis(100)), ..Limits::default() }); diff --git a/crates/ide-plugin/tests/fixtures/path-events/dist/plugin.wasm b/crates/ide-plugin/tests/fixtures/path-events/dist/plugin.wasm index d07e83badf..b8a56c0fc0 100644 Binary files a/crates/ide-plugin/tests/fixtures/path-events/dist/plugin.wasm and b/crates/ide-plugin/tests/fixtures/path-events/dist/plugin.wasm differ diff --git a/crates/ide-plugin/tests/fixtures/path-events/dist/plugin.wasm.sources b/crates/ide-plugin/tests/fixtures/path-events/dist/plugin.wasm.sources index 3e77faee7c..2f5bbfe9d7 100644 --- a/crates/ide-plugin/tests/fixtures/path-events/dist/plugin.wasm.sources +++ b/crates/ide-plugin/tests/fixtures/path-events/dist/plugin.wasm.sources @@ -1 +1 @@ -f9c0cb4fca66ede5adcb1951dd05c3938c218224cb61e63a574ba08a93f4a041 +872f94f4bbc441457f36ed7b44b3a2fb6b87021f2f0a66104caf5c807b524147 diff --git a/crates/ide-plugin/tests/fixtures/path-events/plugin.toml b/crates/ide-plugin/tests/fixtures/path-events/plugin.toml index c959419ad8..08dc0fbbf2 100644 --- a/crates/ide-plugin/tests/fixtures/path-events/plugin.toml +++ b/crates/ide-plugin/tests/fixtures/path-events/plugin.toml @@ -1,13 +1,13 @@ id = "test.path-events" name = "Path event fixture" version = "0.1.0" -engines.openidea = "^1.3" +engines.openidea = "^1.6" wasm = "dist/plugin.wasm" activation = ["on-command:*"] [capabilities] required = ["storage"] -optional = ["events", "project.read"] +optional = ["events", "project.read", "editor.read"] [[contributes.providers]] kinds = ["hover"] @@ -29,6 +29,10 @@ title = "all" id = "off" title = "off" +[[contributes.commands]] +id = "document" +title = "document" + [[contributes.commands]] id = "count" title = "count" diff --git a/crates/ide-plugin/tests/fixtures/path-events/src/lib.rs b/crates/ide-plugin/tests/fixtures/path-events/src/lib.rs index fc9a8eed38..0bd36c37e1 100644 --- a/crates/ide-plugin/tests/fixtures/path-events/src/lib.rs +++ b/crates/ide-plugin/tests/fixtures/path-events/src/lib.rs @@ -11,6 +11,7 @@ impl Plugin for Fixture { let globs: Vec<_> = glob.lines().collect(); let _ = host::subscribe_paths("file-opened", &globs); let _ = host::subscribe_paths("file-saved", &["config/*.yaml"]); + let _ = host::subscribe_paths("document-changed", &["config/*.yaml"]); Ok(()) } fn run_command(&mut self, id: &str, _: &Invocation, arg: Option<&str>) -> Result, Error> { @@ -31,12 +32,18 @@ impl Plugin for Fixture { Ok(None) } "count" => Ok(host::storage::get("count")?), + "document" => Ok(host::storage::get("document")?), _ => Err(Error::NotFound(id.into())), } } fn handle_events(&mut self, events: &[Event], _: &Invocation) { let count = host::storage::get("count").ok().flatten().and_then(|n| n.parse::().ok()).unwrap_or(0); host::storage::set("count", &(count + events.len()).to_string()).unwrap(); + for event in events { + if let Event::DocumentChanged(change) = event { + host::storage::set("document", &change.path).unwrap(); + } + } } } impl HoverProvider for Fixture {