Debian Codegraph @

helix-dap/src/registry.rs

raw symbols SCIP
1 use crate::{Client, Payload, Result, StackFrame};
2 use futures_executor::block_on;
3 use futures_util::stream::SelectAll;
4 use helix_core::syntax::config::DebugAdapterConfig;
5 use slotmap::SlotMap;
6 use std::fmt;
7 use tokio_stream::wrappers::UnboundedReceiverStream;
8
9 /// The resgistry is a struct that manages and owns multiple debugger clients
10 /// This holds the responsibility of managing the lifecycle of each client
11 /// plus showing the heirarcihical nature betweeen them
12 pub struct Registry {
13 inner: SlotMap<DebugAdapterId, Client>,
14 /// The active debugger client
15 ///
16 /// TODO: You can have multiple active debuggers, so the concept of a single active debugger
17 /// may need to be changed
18 current_client_id: Option<DebugAdapterId>,
19 /// A stream of incoming messages from all debuggers
20 pub incoming: SelectAll<UnboundedReceiverStream<(DebugAdapterId, Payload)>>,
21 }
22
23 impl Registry {
24 /// Creates a new DebuggerService instance
25 pub fn new() -> Self {
26 Self {
27 inner: SlotMap::with_key(),
28 current_client_id: None,
29 incoming: SelectAll::new(),
30 }
31 }
32
33 pub fn start_client(
34 &mut self,
35 socket: Option<std::net::SocketAddr>,
36 config: &DebugAdapterConfig,
37 ) -> Result<DebugAdapterId> {
38 self.inner.try_insert_with_key(|id| {
39 let result = match socket {
40 Some(socket) => block_on(Client::tcp(socket, id)),
41 None => block_on(Client::process(
42 &config.transport,
43 &config.command,
44 config.args.iter().map(|arg| arg.as_str()).collect(),
45 config.port_arg.as_deref(),
46 id,
47 )),
48 };
49
50 let (mut client, receiver) = result?;
51 self.incoming.push(UnboundedReceiverStream::new(receiver));
52
53 client.config = Some(config.clone());
54 block_on(client.initialize(config.name.clone()))?;
55 client.quirks = config.quirks.clone();
56
57 Ok(client)
58 })
59 }
60
61 pub fn remove_client(&mut self, id: DebugAdapterId) {
62 self.inner.remove(id);
63 }
64
65 pub fn get_client(&self, id: DebugAdapterId) -> Option<&Client> {
66 self.inner.get(id)
67 }
68
69 pub fn get_client_mut(&mut self, id: DebugAdapterId) -> Option<&mut Client> {
70 self.inner.get_mut(id)
71 }
72
73 pub fn get_active_client(&self) -> Option<&Client> {
74 self.current_client_id.and_then(|id| self.get_client(id))
75 }
76
77 pub fn get_active_client_mut(&mut self) -> Option<&mut Client> {
78 self.current_client_id
79 .and_then(|id| self.get_client_mut(id))
80 }
81
82 pub fn set_active_client(&mut self, id: DebugAdapterId) {
83 if self.get_client(id).is_some() {
84 self.current_client_id = Some(id);
85 } else {
86 self.current_client_id = None;
87 }
88 }
89
90 pub fn unset_active_client(&mut self) {
91 self.current_client_id = None;
92 }
93
94 pub fn current_stack_frame(&self) -> Option<&StackFrame> {
95 self.get_active_client()
96 .and_then(|debugger| debugger.current_stack_frame())
97 }
98 }
99
100 impl Default for Registry {
101 fn default() -> Self {
102 Self::new()
103 }
104 }
105
106 slotmap::new_key_type! {
107 pub struct DebugAdapterId;
108 }
109
110 impl fmt::Display for DebugAdapterId {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
112 write!(f, "{:?}", self.0)
113 }
114 }