Файли
openidea/crates/ide-host/tests/plugin_rename.rs
T
RuyimgByCN 3a956c72b8 feat(plugins): add rename-provider for cross-language rename (#8115)
Plugin API 1.21 adds the optional rename-provider export (wit/rename.wit, world plugin-rename). A plugin declaring the rename provider kind answers prepare-rename where the language server has no target, and its rename edits are merged into the server's workspace edit (overlapping edits dropped) or form the whole rename when no server handles the file. Edit paths must resolve inside the project's writable roots; calls use the command budget and honour cancellation. Includes the SDK trait and registration, a fixture plugin with a host integration test, rebuilt plugin components, and a README section describing the host policy.

Co-authored-by: RuyimgByCN <[email protected]>
2026-12-05 03:14:06 +08:00

102 рядки
4.1 KiB
Rust

//! API 1.21 `rename-provider`: prepare-rename, cross-file rename via Invocation,
//! capability denial/grant handling, and writable-root path confinement.
mod common;
use ide_core::{FileEdit, Pos, RequestId, error::ErrorKind};
use ide_host::plugins::{Consent, HostWorkspace, PluginHost, set_editor_providers};
use ide_host::{Host, HostConfig, ops};
use ide_plugin::Limits;
use std::{path::Path, sync::Arc, time::Duration};
fn fixture() -> std::path::PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../ide-plugin/tests/fixtures/rename")
}
struct Clear;
impl Drop for Clear {
fn drop(&mut self) {
set_editor_providers(None);
}
}
#[tokio::test(flavor = "multi_thread")]
async fn cross_file_rename_respects_capabilities_invocation_and_roots() {
let tmp = tempfile::tempdir().unwrap();
let root = dunce::canonicalize(tmp.path()).unwrap().join("project");
std::fs::create_dir(&root).unwrap();
let mapper = root.join("mapper.txt");
let sibling = root.join("sibling.txt");
std::fs::write(&mapper, "query symOld\n").unwrap();
std::fs::write(&sibling, "ref symOld\n").unwrap();
let host =
Arc::new(Host::new(HostConfig { local_data_dir: tmp.path().join("local"), ..common::host_config(tmp.path()) }));
host.open_project("main", &root).unwrap();
ops::project::project_trust_set(&host, root.clone(), true, false).await.unwrap();
// Denied capability: without `project.read`, Invocation::project().root() is denied inside rename.
let denied_plugins = Arc::new(
PluginHost::new(
tmp.path().join("plugins-denied"),
Arc::new(|_: Option<&str>, _| {}),
Limits {
timeout: Duration::from_secs(5),
edit_soft: Duration::from_secs(3),
edit_hard: Duration::from_secs(5),
..Default::default()
},
)
.unwrap(),
);
denied_plugins.set_workspace(Some(Arc::new(HostWorkspace(Arc::downgrade(&host)))));
denied_plugins.install_from_dir(&fixture(), &Consent { optional: vec![], signer_change: true }).unwrap();
set_editor_providers(Some(&denied_plugins));
let _clear = Clear;
let doc = ops::doc::doc_open(&host, "main", mapper.clone()).await.unwrap();
let prep = ops::lsp::lsp_prepare_rename(&host, "main", doc.id, Pos::new(0, 6)).await.unwrap();
assert!(prep.is_some());
let denied = ops::lsp::lsp_rename(&host, "main", RequestId(1), doc.id, Pos::new(0, 6), "symNew".into()).await;
assert!(denied.is_err(), "rename needing ungranted capability must not return edits");
// Granted capability: Invocation provides project root & reads sibling.txt for cross-file rename.
set_editor_providers(None);
let granted_plugins = Arc::new(
PluginHost::new(
tmp.path().join("plugins-granted"),
Arc::new(|_: Option<&str>, _| {}),
Limits {
timeout: Duration::from_secs(5),
edit_soft: Duration::from_secs(3),
edit_hard: Duration::from_secs(5),
..Default::default()
},
)
.unwrap(),
);
granted_plugins.set_workspace(Some(Arc::new(HostWorkspace(Arc::downgrade(&host)))));
granted_plugins.install_from_dir(&fixture(), &Consent::all()).unwrap();
set_editor_providers(Some(&granted_plugins));
let renamed =
ops::lsp::lsp_rename(&host, "main", RequestId(2), doc.id, Pos::new(0, 6), "symNew".into()).await.unwrap();
assert!(renamed.preview);
let edited_paths: Vec<_> = renamed
.edit
.files
.iter()
.filter_map(|f| match f {
FileEdit::Edit { path, .. } => Some(path.0.clone()),
_ => None,
})
.collect();
assert!(edited_paths.contains(&mapper), "edited paths: {edited_paths:?}");
assert!(edited_paths.contains(&sibling), "edited paths: {edited_paths:?}");
// Paths escaping project roots are refused.
let escape = ops::lsp::lsp_rename(&host, "main", RequestId(3), doc.id, Pos::new(0, 6), "__escape__".into())
.await
.unwrap_err();
assert_eq!(escape.kind, ErrorKind::InvalidInput);
}