context_servers: Hide actions when no context servers are configured (#17833)
This PR filters out the context servers actions from the command palette when no context servers are configured. Release Notes: - N/A
This commit is contained in:
parent
d5268c5197
commit
d56fa25830
4 changed files with 39 additions and 16 deletions
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -2780,6 +2780,7 @@ version = "0.1.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"anyhow",
|
"anyhow",
|
||||||
"collections",
|
"collections",
|
||||||
|
"command_palette_hooks",
|
||||||
"futures 0.3.30",
|
"futures 0.3.30",
|
||||||
"gpui",
|
"gpui",
|
||||||
"log",
|
"log",
|
||||||
|
|
|
@ -14,6 +14,7 @@ path = "src/context_servers.rs"
|
||||||
[dependencies]
|
[dependencies]
|
||||||
anyhow.workspace = true
|
anyhow.workspace = true
|
||||||
collections.workspace = true
|
collections.workspace = true
|
||||||
|
command_palette_hooks.workspace = true
|
||||||
futures.workspace = true
|
futures.workspace = true
|
||||||
gpui.workspace = true
|
gpui.workspace = true
|
||||||
log.workspace = true
|
log.workspace = true
|
||||||
|
|
|
@ -12,6 +12,9 @@ pub use registry::*;
|
||||||
|
|
||||||
actions!(context_servers, [Restart]);
|
actions!(context_servers, [Restart]);
|
||||||
|
|
||||||
|
/// The namespace for the context servers actions.
|
||||||
|
const CONTEXT_SERVERS_NAMESPACE: &'static str = "context_servers";
|
||||||
|
|
||||||
pub fn init(cx: &mut AppContext) {
|
pub fn init(cx: &mut AppContext) {
|
||||||
log::info!("initializing context server client");
|
log::info!("initializing context server client");
|
||||||
manager::init(cx);
|
manager::init(cx);
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
//! and react to changes in settings.
|
//! and react to changes in settings.
|
||||||
|
|
||||||
use collections::{HashMap, HashSet};
|
use collections::{HashMap, HashSet};
|
||||||
|
use command_palette_hooks::CommandPaletteFilter;
|
||||||
use gpui::{AppContext, AsyncAppContext, Context, EventEmitter, Global, Model, ModelContext, Task};
|
use gpui::{AppContext, AsyncAppContext, Context, EventEmitter, Global, Model, ModelContext, Task};
|
||||||
use log;
|
use log;
|
||||||
use parking_lot::RwLock;
|
use parking_lot::RwLock;
|
||||||
|
@ -24,6 +25,7 @@ use settings::{Settings, SettingsSources, SettingsStore};
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
use crate::CONTEXT_SERVERS_NAMESPACE;
|
||||||
use crate::{
|
use crate::{
|
||||||
client::{self, Client},
|
client::{self, Client},
|
||||||
types,
|
types,
|
||||||
|
@ -148,26 +150,28 @@ impl ContextServerManager {
|
||||||
cx: &mut ModelContext<Self>,
|
cx: &mut ModelContext<Self>,
|
||||||
) -> Task<anyhow::Result<()>> {
|
) -> Task<anyhow::Result<()>> {
|
||||||
let server_id = config.id.clone();
|
let server_id = config.id.clone();
|
||||||
let server_id2 = config.id.clone();
|
|
||||||
|
|
||||||
if self.servers.contains_key(&server_id) || self.pending_servers.contains(&server_id) {
|
if self.servers.contains_key(&server_id) || self.pending_servers.contains(&server_id) {
|
||||||
return Task::ready(Ok(()));
|
return Task::ready(Ok(()));
|
||||||
}
|
}
|
||||||
|
|
||||||
let task = cx.spawn(|this, mut cx| async move {
|
let task = {
|
||||||
let server = Arc::new(ContextServer::new(config));
|
let server_id = server_id.clone();
|
||||||
server.start(&cx).await?;
|
cx.spawn(|this, mut cx| async move {
|
||||||
this.update(&mut cx, |this, cx| {
|
let server = Arc::new(ContextServer::new(config));
|
||||||
this.servers.insert(server_id.clone(), server);
|
server.start(&cx).await?;
|
||||||
this.pending_servers.remove(&server_id);
|
this.update(&mut cx, |this, cx| {
|
||||||
cx.emit(Event::ServerStarted {
|
this.servers.insert(server_id.clone(), server);
|
||||||
server_id: server_id.clone(),
|
this.pending_servers.remove(&server_id);
|
||||||
});
|
cx.emit(Event::ServerStarted {
|
||||||
})?;
|
server_id: server_id.clone(),
|
||||||
Ok(())
|
});
|
||||||
});
|
})?;
|
||||||
|
Ok(())
|
||||||
|
})
|
||||||
|
};
|
||||||
|
|
||||||
self.pending_servers.insert(server_id2);
|
self.pending_servers.insert(server_id);
|
||||||
task
|
task
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -243,15 +247,20 @@ impl GlobalContextServerManager {
|
||||||
pub fn init(cx: &mut AppContext) {
|
pub fn init(cx: &mut AppContext) {
|
||||||
ContextServerSettings::register(cx);
|
ContextServerSettings::register(cx);
|
||||||
GlobalContextServerManager::register(cx);
|
GlobalContextServerManager::register(cx);
|
||||||
|
|
||||||
|
CommandPaletteFilter::update_global(cx, |filter, _cx| {
|
||||||
|
filter.hide_namespace(CONTEXT_SERVERS_NAMESPACE);
|
||||||
|
});
|
||||||
|
|
||||||
cx.observe_global::<SettingsStore>(|cx| {
|
cx.observe_global::<SettingsStore>(|cx| {
|
||||||
let manager = ContextServerManager::global(cx);
|
let manager = ContextServerManager::global(cx);
|
||||||
cx.update_model(&manager, |manager, cx| {
|
cx.update_model(&manager, |manager, cx| {
|
||||||
let settings = ContextServerSettings::get_global(cx);
|
let settings = ContextServerSettings::get_global(cx);
|
||||||
let current_servers: HashMap<String, ServerConfig> = manager
|
let current_servers = manager
|
||||||
.servers()
|
.servers()
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|server| (server.id.clone(), server.config.clone()))
|
.map(|server| (server.id.clone(), server.config.clone()))
|
||||||
.collect();
|
.collect::<HashMap<_, _>>();
|
||||||
|
|
||||||
let new_servers = settings
|
let new_servers = settings
|
||||||
.servers
|
.servers
|
||||||
|
@ -279,6 +288,15 @@ pub fn init(cx: &mut AppContext) {
|
||||||
for id in servers_to_remove {
|
for id in servers_to_remove {
|
||||||
manager.remove_server(&id, cx).detach_and_log_err(cx);
|
manager.remove_server(&id, cx).detach_and_log_err(cx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let has_any_context_servers = !manager.servers().is_empty();
|
||||||
|
CommandPaletteFilter::update_global(cx, |filter, _cx| {
|
||||||
|
if has_any_context_servers {
|
||||||
|
filter.show_namespace(CONTEXT_SERVERS_NAMESPACE);
|
||||||
|
} else {
|
||||||
|
filter.hide_namespace(CONTEXT_SERVERS_NAMESPACE);
|
||||||
|
}
|
||||||
|
});
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.detach();
|
.detach();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue