Файли
openidea/crates/ide-host/tests/plugin_project_view.rs
T
astesiaтаkirito1105 05a88394fb feat(plugins): Project view panes and Git branch menu commands (API 1.21) (#7980)
Plugins can add panes to the Project tool window and commands to the Git branch menus:

- `[[contributes.projectViewPanes]]` with the `project-view-provider` export regroups a directory's native children under virtual folders. Existing nodes keep their icons, Git status and actions. `project-view.refresh` / `select-in` and `projectViewContext` menu items work against these panes. Requires `project.read`.
- `git.branches.popup` and `git.branch.context` menus. `invocation.git-action-context` gives the command the repository roots and the full ref of the clicked branch. Requires `vcs.read`.
- `views.open-revision-comparison` opens the IDE's comparison of two full commit IDs as a Compare tool window tab, the same one Compare Versions opens. The repository must be a mapped Git root of the invoking trusted project. Requires `vcs.read`.

All of this is part of plugin API 1.21. A project-view test fixture and host tests cover the provider, the branch menu context and the comparison call. The plugin components and bindings are rebuilt.

Co-authored-by: kirito1105 <[email protected]>
2026-12-05 12:24:10 +08:00

109 рядки
4.6 KiB
Rust

//! API 1.21 Project view panes and Git branch menus, through the fixture plugin.
mod common;
use ide_core::ipc_path::to_ipc;
use ide_host::plugins::commands::{plugin_project_view_modify, plugin_run_git_command};
use ide_host::plugins::{Consent, HostWorkspace, PluginHost};
use ide_host::{Host, ops};
use ide_plugin::{
GitActionBranch, GitActionContext, Limits, ProjectViewNativeNode, ProjectViewNodeKind, ProjectViewProjectionNode,
};
use std::path::{Path, PathBuf};
use std::sync::Arc;
fn fixture() -> PathBuf {
Path::new(env!("CARGO_MANIFEST_DIR")).join("../ide-plugin/tests/fixtures/project-view")
}
fn node(path: &Path, kind: ProjectViewNodeKind) -> ProjectViewNativeNode {
ProjectViewNativeNode {
id: to_ipc(path),
path: to_ipc(path),
label: path.file_name().unwrap().to_string_lossy().into_owned(),
kind,
}
}
async fn setup(tmp: &Path) -> (Arc<Host>, Arc<PluginHost>, PathBuf) {
let root = dunce::canonicalize(tmp).unwrap().join("project");
std::fs::create_dir_all(root.join("src")).unwrap();
for file in ["mod_a.go", "mod_b.go", "main.go"] {
std::fs::write(root.join("src").join(file), "package main\n").unwrap();
}
ide_vcs::init(&root).unwrap();
let host = Arc::new(Host::new(common::host_config(tmp)));
host.open_project("main", &root).unwrap();
ops::project::project_trust_set(&host, root.clone(), true, false).await.unwrap();
host.project("main").unwrap().refresh_repositories();
let plugins =
Arc::new(PluginHost::new(tmp.join("plugins"), Arc::new(|_: Option<&str>, _| {}), Limits::default()).unwrap());
plugins.set_workspace(Some(Arc::new(HostWorkspace(Arc::downgrade(&host)))));
plugins.install_from_dir(&fixture(), &Consent::all()).unwrap();
(host, plugins, root)
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_pane_groups_native_children_under_a_virtual_folder() {
let tmp = tempfile::tempdir().unwrap();
let (host, plugins, root) = setup(tmp.path()).await;
let src = root.join("src");
let parent = node(&src, ProjectViewNodeKind::Directory);
let children: Vec<_> =
["mod_a.go", "mod_b.go", "main.go"].map(|file| node(&src.join(file), ProjectViewNodeKind::File)).into();
let modify = |view: &str, settings: &str| {
plugin_project_view_modify(
&host,
&plugins,
"main",
"test.project-view".into(),
view.into(),
parent.clone(),
children.clone(),
settings.into(),
None,
)
};
let nodes = modify("grouped", "{}").await.unwrap().expect("the pane rearranges the children");
assert_eq!(
nodes,
[
ProjectViewProjectionNode::VirtualFolder {
id: "mod".into(),
parent: None,
label: "mod".into(),
icon: None
},
ProjectViewProjectionNode::ExistingNode { id: children[0].id.clone(), parent: Some("mod".into()) },
ProjectViewProjectionNode::ExistingNode { id: children[1].id.clone(), parent: Some("mod".into()) },
]
);
assert_eq!(modify("grouped", r#"{"plain":true}"#).await.unwrap(), None, "the plugin keeps the native list");
assert_eq!(modify("other", "{}").await.unwrap(), None, "a pane the plugin does not declare");
assert_eq!(modify("grouped", "[]").await.unwrap(), None, "settings must be a JSON object");
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn a_git_branch_menu_command_receives_the_repository_and_full_ref() {
let tmp = tempfile::tempdir().unwrap();
let (host, plugins, _root) = setup(tmp.path()).await;
let repo = to_ipc(host.project("main").unwrap().repos().primary().unwrap().root());
let context = |repositories: Vec<String>| GitActionContext {
repositories,
branch: Some(GitActionBranch { name: "main".into(), full_ref: "refs/heads/main".into(), remote: false }),
};
let run = |context: GitActionContext| {
plugin_run_git_command(&host, &plugins, "main", "test.project-view".into(), "branch".into(), context, None)
};
let answer = run(context(vec![repo.clone(), repo.clone()])).await.unwrap();
assert_eq!(answer.as_deref(), Some(format!("{repo}|refs/heads/main").as_str()));
let outside = tempfile::tempdir().unwrap();
ide_vcs::init(outside.path()).unwrap();
assert!(run(context(vec![to_ipc(outside.path())])).await.is_err(), "a repository outside the project");
let mut wrong_ref = context(vec![repo]);
wrong_ref.branch.as_mut().unwrap().full_ref = "refs/remotes/main".into();
assert!(run(wrong_ref).await.is_err(), "the full ref must name the branch");
}