дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 01:31:41 +02:00
document-changed gave plugins the path relative to the file's own project but not which project, so with two open projects holding the same relative path a plugin couldn't tell which document changed. The event now carries project (the root the path is relative to); the SDK's DocumentChanged exposes it as Option<String>. The payload is JSON through other, so WIT is unchanged; plugin components are rebuilt for the SDK change.
250 рядки
11 KiB
Rust
250 рядки
11 KiB
Rust
//! Project-relative event filters, kept with runtime subscriptions across idle reclaim.
|
|
use super::{LoadedPlugin, PluginHost};
|
|
use globset::{GlobBuilder, GlobMatcher};
|
|
use ide_extension::Capability;
|
|
use ide_plugin::{EventKind, HostError, WorkbenchEvent};
|
|
use std::{
|
|
collections::{BTreeSet, HashMap},
|
|
path::{Path, PathBuf},
|
|
};
|
|
|
|
#[derive(Clone, Default)]
|
|
pub(super) struct Subscriptions {
|
|
pub kinds: HashMap<String, BTreeSet<EventKind>>,
|
|
pub paths: HashMap<String, HashMap<String, PathFilter>>,
|
|
}
|
|
|
|
#[derive(Clone)]
|
|
pub(super) struct PathFilter {
|
|
pub globs: Vec<String>,
|
|
matchers: Vec<GlobMatcher>,
|
|
}
|
|
|
|
impl PathFilter {
|
|
pub fn new(kind: &str, globs: Vec<String>) -> Result<Self, HostError> {
|
|
if !matches!(kind, "file-opened" | "file-saved" | "files-changed" | "document-changed") {
|
|
return Err(HostError::Invalid(
|
|
"subscribe-paths supports file-opened, file-saved, files-changed and document-changed".into(),
|
|
));
|
|
}
|
|
if globs.len() > 64 {
|
|
return Err(HostError::Invalid("at most 64 event path globs".into()));
|
|
}
|
|
let mut matchers = Vec::new();
|
|
for input in &globs {
|
|
let glob = input.trim_start_matches("./");
|
|
if glob.is_empty()
|
|
|| input.len() > 1024
|
|
|| glob.starts_with('/')
|
|
|| glob.contains('\\')
|
|
|| glob.contains(':')
|
|
|| glob.chars().any(char::is_control)
|
|
|| glob.split('/').any(|c| c == "..")
|
|
{
|
|
return Err(HostError::Invalid(
|
|
"event globs must be project-relative paths, at most 1024 bytes each".into(),
|
|
));
|
|
}
|
|
matchers.push(
|
|
GlobBuilder::new(glob)
|
|
.literal_separator(true)
|
|
.build()
|
|
.map_err(|e| HostError::Invalid(format!("invalid event glob: {e}")))?
|
|
.compile_matcher(),
|
|
);
|
|
}
|
|
Ok(Self { globs, matchers })
|
|
}
|
|
|
|
pub fn matches(&self, root: &Path, path: &Path) -> bool {
|
|
let path = ide_plugin::resolve_real(path);
|
|
let root = ide_plugin::resolve_real(root);
|
|
path.strip_prefix(&root).is_ok_and(|relative| self.matchers.iter().any(|m| m.is_match(relative)))
|
|
}
|
|
}
|
|
|
|
impl Subscriptions {
|
|
pub fn remove(&mut self, plugin: &str) -> bool {
|
|
self.kinds.remove(plugin).is_some() | self.paths.remove(plugin).is_some()
|
|
}
|
|
/// Subscribes to every event of `kind`, dropping its path filter.
|
|
pub fn set(&mut self, plugin: &str, kind: EventKind) -> bool {
|
|
let removed = self.paths.get_mut(plugin).is_some_and(|filters| filters.remove(kind.name()).is_some());
|
|
self.kinds.entry(plugin.to_owned()).or_default().insert(kind) | removed
|
|
}
|
|
pub fn set_paths(&mut self, plugin: &str, kind: &str, filter: PathFilter) -> bool {
|
|
let mut changed = false;
|
|
if let Some(kind) = EventKind::parse(kind) {
|
|
changed = self.kinds.entry(plugin.to_owned()).or_default().insert(kind);
|
|
}
|
|
let previous = self.paths.entry(plugin.to_owned()).or_default().insert(kind.to_owned(), filter.clone());
|
|
previous.is_none_or(|old| old.globs != filter.globs) | changed
|
|
}
|
|
pub fn unsubscribe(&mut self, plugin: &str, kind: &str) -> bool {
|
|
let removed = self.paths.get_mut(plugin).is_some_and(|filters| filters.remove(kind).is_some());
|
|
let off = EventKind::parse(kind)
|
|
.is_some_and(|kind| self.kinds.get_mut(plugin).is_some_and(|kinds| kinds.remove(&kind)));
|
|
removed | off
|
|
}
|
|
}
|
|
|
|
impl PluginHost {
|
|
pub(super) fn filter_path_events(
|
|
shared: &super::api::Shared,
|
|
plugin: &LoadedPlugin,
|
|
client: Option<&str>,
|
|
events: &[WorkbenchEvent],
|
|
subs: &Subscriptions,
|
|
) -> Vec<WorkbenchEvent> {
|
|
let filters = subs.paths.get(plugin.id());
|
|
let workspace = shared.workspace();
|
|
events
|
|
.iter()
|
|
.filter_map(|event| {
|
|
if let WorkbenchEvent::FilesChanged(batch) = event {
|
|
let Some(filter) = filters.and_then(|filters| filters.get(event.kind().name())) else {
|
|
return Some(event.clone());
|
|
};
|
|
let root = workspace.as_ref()?.project_of(client?)?;
|
|
if plugin.dev_root.as_deref().is_some_and(|r| r != root) {
|
|
return None;
|
|
}
|
|
let paths = |paths: &[String]| {
|
|
paths
|
|
.iter()
|
|
.filter(|path| {
|
|
let path = Path::new(path);
|
|
path.is_absolute() && filter.matches(&root, path)
|
|
})
|
|
.cloned()
|
|
.collect::<Vec<_>>()
|
|
};
|
|
let batch = ide_plugin::FilesChanged {
|
|
created: paths(&batch.created),
|
|
modified: paths(&batch.modified),
|
|
removed: paths(&batch.removed),
|
|
};
|
|
return (!(batch.created.is_empty() && batch.modified.is_empty() && batch.removed.is_empty()))
|
|
.then_some(WorkbenchEvent::FilesChanged(batch));
|
|
}
|
|
let path = match event {
|
|
WorkbenchEvent::FileOpened(path) | WorkbenchEvent::FileSaved(path) => Path::new(path),
|
|
WorkbenchEvent::EditorFocused(Some(path)) => Path::new(path),
|
|
WorkbenchEvent::DocumentChanged(change) => Path::new(&change.path),
|
|
_ => return Some(event.clone()),
|
|
};
|
|
if !path.is_absolute() {
|
|
return None;
|
|
}
|
|
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(_) = 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)
|
|
|| !granted.contains(&Capability::EditorRead)
|
|
|| !filter.matches(&root, path)
|
|
{
|
|
return None;
|
|
}
|
|
return Some(event.clone());
|
|
}
|
|
filters
|
|
.and_then(|filters| filters.get(event.kind().name()))
|
|
.is_none_or(|filter| filter.matches(&root, path))
|
|
.then(|| event.clone())
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
/// What a plugin receives for `document-changed`: the path relative to the file's own
|
|
/// project, and that project's root, since two open projects can hold the same relative path.
|
|
pub(super) fn relativize_documents(
|
|
shared: &super::api::Shared,
|
|
client: Option<&str>,
|
|
events: Vec<WorkbenchEvent>,
|
|
) -> Vec<WorkbenchEvent> {
|
|
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;
|
|
change.project = Some(ide_core::ipc_path::to_ipc(&root));
|
|
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<PathBuf> {
|
|
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)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
#[test]
|
|
fn glob_validation_is_bounded_and_project_relative() {
|
|
for invalid in ["", "/etc/*", "../*", "a/../b", "C:/a", "a\\b", "a\nb", "["] {
|
|
assert!(PathFilter::new("file-opened", vec![invalid.into()]).is_err(), "{invalid:?}");
|
|
}
|
|
assert!(PathFilter::new("editor-focused", vec!["*".into()]).is_err());
|
|
assert!(PathFilter::new("file-saved", vec!["*".into(); 65]).is_err());
|
|
assert!(PathFilter::new("file-saved", vec!["./".repeat(513)]).is_err());
|
|
assert!(PathFilter::new("file-saved", vec![]).is_ok());
|
|
assert!(PathFilter::new("files-changed", vec![]).is_ok());
|
|
assert!(PathFilter::new("document-changed", vec!["src/**/*.rs".into()]).is_ok());
|
|
}
|
|
|
|
#[test]
|
|
fn matchers_use_or_semantics_and_component_boundaries() {
|
|
let root = tempfile::tempdir().unwrap();
|
|
let root = dunce::canonicalize(root.path()).unwrap();
|
|
let filter = PathFilter::new("file-opened", vec!["./config/*.yaml".into(), "src/**/*.rs".into()]).unwrap();
|
|
for path in ["config/a.yaml", "src/main.rs", "src/深い/a.rs"] {
|
|
assert!(filter.matches(&root, &root.join(path)), "{path}");
|
|
}
|
|
for path in ["config/nested/a.yaml", "other/a.yaml", "src/a.ts"] {
|
|
assert!(!filter.matches(&root, &root.join(path)), "{path}");
|
|
}
|
|
assert!(!filter.matches(&root, &root.with_extension("other").join("config/a.yaml")));
|
|
assert!(!PathFilter::new("file-opened", vec![]).unwrap().matches(&root, &root.join("a")));
|
|
}
|
|
|
|
#[test]
|
|
fn subscriptions_replace_atomically_and_legacy_subscribe_clears_filters() {
|
|
let mut subs = Subscriptions::default();
|
|
subs.set_paths("plugin", "file-opened", PathFilter::new("file-opened", vec!["*.rs".into()]).unwrap());
|
|
assert!(subs.kinds["plugin"].contains(&EventKind::FileOpened));
|
|
subs.set_paths("plugin", "file-opened", PathFilter::new("file-opened", vec![]).unwrap());
|
|
assert!(subs.paths["plugin"]["file-opened"].globs.is_empty());
|
|
subs.set("plugin", EventKind::FileOpened);
|
|
assert!(subs.paths["plugin"].is_empty());
|
|
subs.set_paths("plugin", "file-saved", PathFilter::new("file-saved", vec!["*".into()]).unwrap());
|
|
assert!(subs.unsubscribe("plugin", "file-saved"));
|
|
assert!(subs.remove("plugin"));
|
|
assert!(subs.paths.is_empty() && subs.kinds.is_empty());
|
|
}
|
|
}
|