1use dap::{StartDebuggingRequestArguments, adapters::DebugTaskDefinition};
2use gpui::AsyncApp;
3use std::{collections::HashMap, ffi::OsStr, path::PathBuf};
4
5use crate::*;
6
7#[derive(Default, Debug)]
8pub(crate) struct GoDebugAdapter;
9
10impl GoDebugAdapter {
11 const ADAPTER_NAME: &'static str = "Delve";
12 fn request_args(&self, config: &DebugTaskDefinition) -> StartDebuggingRequestArguments {
13 let mut args = match &config.request {
14 dap::DebugRequest::Attach(attach_config) => {
15 json!({
16 "processId": attach_config.process_id,
17 })
18 }
19 dap::DebugRequest::Launch(launch_config) => json!({
20 "program": launch_config.program,
21 "cwd": launch_config.cwd,
22 "args": launch_config.args,
23 "env": launch_config.env_json()
24 }),
25 };
26
27 let map = args.as_object_mut().unwrap();
28
29 if let Some(stop_on_entry) = config.stop_on_entry {
30 map.insert("stopOnEntry".into(), stop_on_entry.into());
31 }
32
33 StartDebuggingRequestArguments {
34 configuration: args,
35 request: config.request.to_dap(),
36 }
37 }
38}
39
40#[async_trait(?Send)]
41impl DebugAdapter for GoDebugAdapter {
42 fn name(&self) -> DebugAdapterName {
43 DebugAdapterName(Self::ADAPTER_NAME.into())
44 }
45
46 async fn get_binary(
47 &self,
48 delegate: &dyn DapDelegate,
49 config: &DebugTaskDefinition,
50 _user_installed_path: Option<PathBuf>,
51 _cx: &mut AsyncApp,
52 ) -> Result<DebugAdapterBinary> {
53 let delve_path = delegate
54 .which(OsStr::new("dlv"))
55 .and_then(|p| p.to_str().map(|p| p.to_string()))
56 .ok_or(anyhow!("Dlv not found in path"))?;
57
58 let tcp_connection = config.tcp_connection.clone().unwrap_or_default();
59 let (host, port, timeout) = crate::configure_tcp_connection(tcp_connection).await?;
60
61 Ok(DebugAdapterBinary {
62 command: delve_path,
63 arguments: vec![
64 "dap".into(),
65 "--listen".into(),
66 format!("{}:{}", host, port),
67 ],
68 cwd: None,
69 envs: HashMap::default(),
70 connection: Some(adapters::TcpArguments {
71 host,
72 port,
73 timeout,
74 }),
75 request_args: self.request_args(config),
76 })
77 }
78}