1use anyhow::Result;
2use async_trait::async_trait;
3use collections::FxHashMap;
4use gpui::{App, Global};
5use parking_lot::RwLock;
6use task::{DebugRequest, SpawnInTerminal};
7
8use crate::adapters::{DebugAdapter, DebugAdapterName};
9use std::{collections::BTreeMap, sync::Arc};
10
11/// Given a user build configuration, locator creates a fill-in debug target ([DebugRequest]) on behalf of the user.
12#[async_trait]
13pub trait DapLocator: Send + Sync {
14 /// Determines whether this locator can generate debug target for given task.
15 fn accepts(&self, build_config: &SpawnInTerminal) -> bool;
16 async fn run(&self, build_config: SpawnInTerminal) -> Result<DebugRequest>;
17}
18
19#[derive(Default)]
20struct DapRegistryState {
21 adapters: BTreeMap<DebugAdapterName, Arc<dyn DebugAdapter>>,
22 locators: FxHashMap<String, Arc<dyn DapLocator>>,
23}
24
25#[derive(Clone, Default)]
26/// Stores available debug adapters.
27pub struct DapRegistry(Arc<RwLock<DapRegistryState>>);
28impl Global for DapRegistry {}
29
30impl DapRegistry {
31 pub fn global(cx: &mut App) -> &mut Self {
32 let ret = cx.default_global::<Self>();
33
34 #[cfg(any(test, feature = "test-support"))]
35 if ret.adapter(crate::FakeAdapter::ADAPTER_NAME).is_none() {
36 ret.add_adapter(Arc::new(crate::FakeAdapter::new()));
37 }
38
39 ret
40 }
41
42 pub fn add_adapter(&self, adapter: Arc<dyn DebugAdapter>) {
43 let name = adapter.name();
44 let _previous_value = self.0.write().adapters.insert(name, adapter);
45 debug_assert!(
46 _previous_value.is_none(),
47 "Attempted to insert a new debug adapter when one is already registered"
48 );
49 }
50
51 pub fn add_locator(&self, name: String, locator: Arc<dyn DapLocator>) {
52 let _previous_value = self.0.write().locators.insert(name, locator);
53 debug_assert!(
54 _previous_value.is_none(),
55 "Attempted to insert a new debug locator when one is already registered"
56 );
57 }
58
59 pub fn locators(&self) -> FxHashMap<String, Arc<dyn DapLocator>> {
60 self.0.read().locators.clone()
61 }
62
63 pub fn adapter(&self, name: &str) -> Option<Arc<dyn DebugAdapter>> {
64 self.0.read().adapters.get(name).cloned()
65 }
66
67 pub fn enumerate_adapters(&self) -> Vec<DebugAdapterName> {
68 self.0.read().adapters.keys().cloned().collect()
69 }
70}