дзеркало
https://github.com/l0ng-ai/openidea.git
синхронізовано 2026-12-05 17:47:08 +02:00
Plugin API 1.21 adds the optional completion-resolve-provider export: completion returns items with an opaque data token, and resolve is called when the user focuses one. As with LSP completionItem/resolve, the resolved detail, documentation and additional edits are filled in while the label and what the item inserts stay as listed. Plugins without the export keep the existing completion providers. Includes the SDK trait and registration, a fixture plugin with a host integration test, rebuilt plugin components and README notes. Co-authored-by: RuyimgByCN <[email protected]>
95 рядки
4.0 KiB
Rust
95 рядки
4.0 KiB
Rust
//! API 1.21 `completion-resolve-provider`: listing items with opaque `data`, capability
|
|
//! enforcement inside lazy `resolve`, and resolving detail/docs/additional_edits on focus.
|
|
mod common;
|
|
|
|
use ide_core::{Pos, RequestId};
|
|
use ide_host::plugins::{Consent, HostWorkspace, PluginHost, set_editor_providers};
|
|
use ide_host::{Host, HostConfig, ops};
|
|
use ide_plugin::Limits;
|
|
use std::{
|
|
path::{Path, PathBuf},
|
|
sync::Arc,
|
|
time::Duration,
|
|
};
|
|
|
|
fn fixture() -> PathBuf {
|
|
Path::new(env!("CARGO_MANIFEST_DIR")).join("../ide-plugin/tests/fixtures/completion-resolve")
|
|
}
|
|
|
|
struct Clear;
|
|
impl Drop for Clear {
|
|
fn drop(&mut self) {
|
|
set_editor_providers(None);
|
|
}
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
async fn completion_resolve_preserves_data_and_resolves_with_capabilities() {
|
|
let tmp = tempfile::tempdir().unwrap();
|
|
let root = dunce::canonicalize(tmp.path()).unwrap().join("project");
|
|
std::fs::create_dir(&root).unwrap();
|
|
let file = root.join("query.txt");
|
|
std::fs::write(&file, "select").unwrap();
|
|
std::fs::write(root.join("detail.txt"), "SELECT * FROM users").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();
|
|
let doc = ops::doc::doc_open(&host, "main", file).await.unwrap();
|
|
|
|
// 1. Without `project.read`: listing returns item with `data`, resolve is denied.
|
|
let denied = 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.set_workspace(Some(Arc::new(HostWorkspace(Arc::downgrade(&host)))));
|
|
denied.install_from_dir(&fixture(), &Consent { optional: vec![], signer_change: true }).unwrap();
|
|
set_editor_providers(Some(&denied));
|
|
let _clear = Clear;
|
|
|
|
let list = ops::lsp::lsp_completion(&host, "main", RequestId(1), doc.id, Pos::new(0, 6), None).await.unwrap();
|
|
let item = list.items.into_iter().find(|i| i.label == "selectByKey").expect("plugin completion item");
|
|
assert!(item.data.is_some(), "expected opaque resolve token");
|
|
assert!(item.detail.is_none());
|
|
let denied_resolve = ops::lsp::lsp_resolve_completion(&host, "main", doc.id, item).await;
|
|
assert!(denied_resolve.is_err(), "resolve requiring ungranted project.read must fail");
|
|
|
|
// 2. With `project.read` granted: `resolve` reads detail.txt and enriches item.
|
|
set_editor_providers(None);
|
|
let granted = 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.set_workspace(Some(Arc::new(HostWorkspace(Arc::downgrade(&host)))));
|
|
granted.install_from_dir(&fixture(), &Consent::all()).unwrap();
|
|
set_editor_providers(Some(&granted));
|
|
|
|
let list = ops::lsp::lsp_completion(&host, "main", RequestId(2), doc.id, Pos::new(0, 6), None).await.unwrap();
|
|
let item = list.items.into_iter().find(|i| i.label == "selectByKey").expect("plugin completion item");
|
|
let resolved = ops::lsp::lsp_resolve_completion(&host, "main", doc.id, item).await.unwrap();
|
|
assert_eq!(resolved.detail.as_deref(), Some("Resolved:token:selectByKey"));
|
|
assert_eq!(resolved.documentation.as_ref().map(|m| m.value.as_str()), Some("SELECT * FROM users"));
|
|
assert_eq!(resolved.additional_edits.len(), 1);
|
|
assert_eq!(resolved.additional_edits[0].new_text, "// import\n");
|
|
assert!(resolved.data.is_none());
|
|
}
|