1use dap::transport::TcpTransport;
2use gpui::AsyncApp;
3use serde_json::Value;
4use std::{collections::HashMap, ffi::OsString, path::PathBuf};
5use sysinfo::{Pid, Process};
6use task::DebugAdapterConfig;
7
8use crate::*;
9
10pub(crate) struct CustomDebugAdapter {
11 custom_args: CustomArgs,
12}
13
14impl CustomDebugAdapter {
15 const ADAPTER_NAME: &'static str = "custom_dap";
16
17 pub(crate) async fn new(custom_args: CustomArgs) -> Result<Self> {
18 Ok(CustomDebugAdapter { custom_args })
19 }
20
21 pub fn attach_processes(processes: &HashMap<Pid, Process>) -> Vec<(&Pid, &Process)> {
22 processes.iter().collect::<Vec<_>>()
23 }
24}
25
26#[async_trait(?Send)]
27impl DebugAdapter for CustomDebugAdapter {
28 fn name(&self) -> DebugAdapterName {
29 DebugAdapterName(Self::ADAPTER_NAME.into())
30 }
31
32 async fn get_binary(
33 &self,
34 _: &dyn DapDelegate,
35 config: &DebugAdapterConfig,
36 _: Option<PathBuf>,
37 _: &mut AsyncApp,
38 ) -> Result<DebugAdapterBinary> {
39 let connection = if let DebugConnectionType::TCP(connection) = &self.custom_args.connection
40 {
41 Some(adapters::TcpArguments {
42 host: connection.host(),
43 port: TcpTransport::port(&connection).await?,
44 timeout: connection.timeout,
45 })
46 } else {
47 None
48 };
49 let ret = DebugAdapterBinary {
50 command: self.custom_args.command.clone(),
51 arguments: self
52 .custom_args
53 .args
54 .clone()
55 .map(|args| args.iter().map(OsString::from).collect()),
56 cwd: config.cwd.clone(),
57 envs: self.custom_args.envs.clone(),
58 connection,
59 };
60 Ok(ret)
61 }
62
63 async fn fetch_latest_adapter_version(&self, _: &dyn DapDelegate) -> Result<AdapterVersion> {
64 bail!("Custom debug adapters don't have latest versions")
65 }
66
67 async fn install_binary(&self, _: AdapterVersion, _: &dyn DapDelegate) -> Result<()> {
68 bail!("Custom debug adapters cannot be installed")
69 }
70
71 async fn get_installed_binary(
72 &self,
73 _: &dyn DapDelegate,
74 _: &DebugAdapterConfig,
75 _: Option<PathBuf>,
76 _: &mut AsyncApp,
77 ) -> Result<DebugAdapterBinary> {
78 bail!("Custom debug adapters cannot be installed")
79 }
80
81 fn request_args(&self, config: &DebugAdapterConfig) -> Value {
82 json!({"program": config.program})
83 }
84}