This PR reworks how the Assistant Panel references slash commands, context servers, and tools. Previously we were always reading them from the global registries, but now we store individual collections on each Assistant Panel instance so that there can be different ones registered for each project. Release Notes: - N/A --------- Co-authored-by: Max <max@zed.dev> Co-authored-by: Antonio <antonio@zed.dev> Co-authored-by: Joseph <joseph@zed.dev> Co-authored-by: Max Brunsfeld <maxbrunsfeld@gmail.com>
67 lines
2.1 KiB
Rust
67 lines
2.1 KiB
Rust
use assistant_slash_command::{SlashCommand, SlashCommandRegistry};
|
|
use collections::HashMap;
|
|
use gpui::AppContext;
|
|
use parking_lot::Mutex;
|
|
use std::sync::Arc;
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Hash, Default)]
|
|
pub struct SlashCommandId(usize);
|
|
|
|
/// A working set of slash commands for use in one instance of the Assistant Panel.
|
|
#[derive(Default)]
|
|
pub struct SlashCommandWorkingSet {
|
|
state: Mutex<WorkingSetState>,
|
|
}
|
|
|
|
#[derive(Default)]
|
|
struct WorkingSetState {
|
|
context_server_commands_by_id: HashMap<SlashCommandId, Arc<dyn SlashCommand>>,
|
|
context_server_commands_by_name: HashMap<String, Arc<dyn SlashCommand>>,
|
|
next_command_id: SlashCommandId,
|
|
}
|
|
|
|
impl SlashCommandWorkingSet {
|
|
pub fn command(&self, name: &str, cx: &AppContext) -> Option<Arc<dyn SlashCommand>> {
|
|
self.state
|
|
.lock()
|
|
.context_server_commands_by_name
|
|
.get(name)
|
|
.cloned()
|
|
.or_else(|| SlashCommandRegistry::global(cx).command(name))
|
|
}
|
|
|
|
pub fn featured_command_names(&self, cx: &AppContext) -> Vec<Arc<str>> {
|
|
SlashCommandRegistry::global(cx).featured_command_names()
|
|
}
|
|
|
|
pub fn insert(&self, command: Arc<dyn SlashCommand>) -> SlashCommandId {
|
|
let mut state = self.state.lock();
|
|
let command_id = state.next_command_id;
|
|
state.next_command_id.0 += 1;
|
|
state
|
|
.context_server_commands_by_id
|
|
.insert(command_id, command.clone());
|
|
state.slash_commands_changed();
|
|
command_id
|
|
}
|
|
|
|
pub fn remove(&self, command_ids_to_remove: &[SlashCommandId]) {
|
|
let mut state = self.state.lock();
|
|
state
|
|
.context_server_commands_by_id
|
|
.retain(|id, _| !command_ids_to_remove.contains(id));
|
|
state.slash_commands_changed();
|
|
}
|
|
}
|
|
|
|
impl WorkingSetState {
|
|
fn slash_commands_changed(&mut self) {
|
|
self.context_server_commands_by_name.clear();
|
|
self.context_server_commands_by_name.extend(
|
|
self.context_server_commands_by_id
|
|
.values()
|
|
.map(|command| (command.name(), command.clone())),
|
|
);
|
|
}
|
|
}
|