1use dap::transport::TcpTransport;
2use gpui::AsyncApp;
3use std::{ffi::OsStr, net::Ipv4Addr, path::PathBuf};
4
5use crate::*;
6
7pub(crate) struct GoDebugAdapter {
8 port: u16,
9 host: Ipv4Addr,
10 timeout: Option<u64>,
11}
12
13impl GoDebugAdapter {
14 const ADAPTER_NAME: &'static str = "delve";
15
16 pub(crate) async fn new(host: &TCPHost) -> Result<Self> {
17 Ok(GoDebugAdapter {
18 port: TcpTransport::port(host).await?,
19 host: host.host(),
20 timeout: host.timeout,
21 })
22 }
23}
24
25#[async_trait(?Send)]
26impl DebugAdapter for GoDebugAdapter {
27 fn name(&self) -> DebugAdapterName {
28 DebugAdapterName(Self::ADAPTER_NAME.into())
29 }
30
31 async fn get_binary(
32 &self,
33 delegate: &dyn DapDelegate,
34 config: &DebugAdapterConfig,
35 user_installed_path: Option<PathBuf>,
36 cx: &mut AsyncApp,
37 ) -> Result<DebugAdapterBinary> {
38 self.get_installed_binary(delegate, config, user_installed_path, cx)
39 .await
40 }
41
42 async fn fetch_latest_adapter_version(
43 &self,
44 _delegate: &dyn DapDelegate,
45 ) -> Result<AdapterVersion> {
46 unimplemented!("This adapter is used from path for now");
47 }
48
49 async fn install_binary(
50 &self,
51 version: AdapterVersion,
52 delegate: &dyn DapDelegate,
53 ) -> Result<()> {
54 adapters::download_adapter_from_github(
55 self.name(),
56 version,
57 adapters::DownloadedFileType::Zip,
58 delegate,
59 )
60 .await?;
61 Ok(())
62 }
63
64 async fn get_installed_binary(
65 &self,
66 delegate: &dyn DapDelegate,
67 config: &DebugAdapterConfig,
68 _: Option<PathBuf>,
69 _: &mut AsyncApp,
70 ) -> Result<DebugAdapterBinary> {
71 let delve_path = delegate
72 .which(OsStr::new("dlv"))
73 .and_then(|p| p.to_str().map(|p| p.to_string()))
74 .ok_or(anyhow!("Dlv not found in path"))?;
75
76 Ok(DebugAdapterBinary {
77 command: delve_path,
78 arguments: Some(vec![
79 "dap".into(),
80 "--listen".into(),
81 format!("{}:{}", self.host, self.port).into(),
82 ]),
83 cwd: config.cwd.clone(),
84 envs: None,
85 connection: Some(adapters::TcpArguments {
86 host: self.host,
87 port: self.port,
88 timeout: self.timeout,
89 }),
90 })
91 }
92
93 fn request_args(&self, config: &DebugAdapterConfig) -> Value {
94 json!({
95 "program": config.program,
96 "cwd": config.cwd,
97 "subProcess": true,
98 })
99 }
100}