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