debug_format.rs

  1use schemars::{gen::SchemaSettings, JsonSchema};
  2use serde::{Deserialize, Serialize};
  3use std::collections::HashMap;
  4use std::net::Ipv4Addr;
  5use std::path::PathBuf;
  6use util::ResultExt;
  7
  8use crate::{TaskTemplate, TaskTemplates, TaskType};
  9
 10impl Default for DebugConnectionType {
 11    fn default() -> Self {
 12        DebugConnectionType::TCP(TCPHost::default())
 13    }
 14}
 15
 16/// Represents the host information of the debug adapter
 17#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
 18pub struct TCPHost {
 19    /// The port that the debug adapter is listening on
 20    ///
 21    /// Default: We will try to find an open port
 22    pub port: Option<u16>,
 23    /// The host that the debug adapter is listening too
 24    ///
 25    /// Default: 127.0.0.1
 26    pub host: Option<Ipv4Addr>,
 27    /// The max amount of time in milliseconds to connect to a tcp DAP before returning an error
 28    ///
 29    /// Default: 2000ms
 30    pub timeout: Option<u64>,
 31}
 32
 33impl TCPHost {
 34    /// Get the host or fallback to the default host
 35    pub fn host(&self) -> Ipv4Addr {
 36        self.host.unwrap_or_else(|| Ipv4Addr::new(127, 0, 0, 1))
 37    }
 38}
 39
 40/// Represents the attach request information of the debug adapter
 41#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
 42pub struct AttachConfig {
 43    /// The processId to attach to, if left empty we will show a process picker
 44    #[serde(default)]
 45    pub process_id: Option<u32>,
 46}
 47
 48/// Represents the type that will determine which request to call on the debug adapter
 49#[derive(Default, Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
 50#[serde(rename_all = "lowercase")]
 51pub enum DebugRequestType {
 52    /// Call the `launch` request on the debug adapter
 53    #[default]
 54    Launch,
 55    /// Call the `attach` request on the debug adapter
 56    Attach(AttachConfig),
 57}
 58
 59/// The Debug adapter to use
 60#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
 61#[serde(rename_all = "lowercase", tag = "adapter")]
 62pub enum DebugAdapterKind {
 63    /// Manually setup starting a debug adapter
 64    /// The argument within is used to start the DAP
 65    Custom(CustomArgs),
 66    /// Use debugpy
 67    Python(TCPHost),
 68    /// Use vscode-php-debug
 69    Php(TCPHost),
 70    /// Use vscode-js-debug
 71    Javascript(TCPHost),
 72    /// Use delve
 73    Go(TCPHost),
 74    /// Use lldb
 75    Lldb,
 76    /// Use GDB's built-in DAP support
 77    #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
 78    Gdb,
 79    /// Used for integration tests
 80    #[cfg(any(test, feature = "test-support"))]
 81    #[serde(skip)]
 82    Fake((bool, dap_types::Capabilities)),
 83}
 84
 85impl DebugAdapterKind {
 86    /// Returns the display name for the adapter kind
 87    pub fn display_name(&self) -> &str {
 88        match self {
 89            Self::Custom(_) => "Custom",
 90            Self::Python(_) => "Python",
 91            Self::Php(_) => "PHP",
 92            Self::Javascript(_) => "JavaScript",
 93            Self::Lldb => "LLDB",
 94            #[cfg(any(target_arch = "x86", target_arch = "x86_64"))]
 95            Self::Gdb => "GDB",
 96            Self::Go(_) => "Go",
 97            #[cfg(any(test, feature = "test-support"))]
 98            Self::Fake(_) => "Fake",
 99        }
100    }
101}
102
103/// Custom arguments used to setup a custom debugger
104#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
105pub struct CustomArgs {
106    /// The connection that a custom debugger should use
107    #[serde(flatten)]
108    pub connection: DebugConnectionType,
109    /// The cli command used to start the debug adapter e.g. `python3`, `node` or the adapter binary
110    pub command: String,
111    /// The cli arguments used to start the debug adapter
112    pub args: Option<Vec<String>>,
113    /// The cli envs used to start the debug adapter
114    pub envs: Option<HashMap<String, String>>,
115}
116
117/// Represents the configuration for the debug adapter
118#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
119#[serde(rename_all = "snake_case")]
120pub struct DebugAdapterConfig {
121    /// Name of the debug task
122    pub label: String,
123    /// The type of adapter you want to use
124    #[serde(flatten)]
125    pub kind: DebugAdapterKind,
126    /// The type of request that should be called on the debug adapter
127    #[serde(default)]
128    pub request: DebugRequestType,
129    /// The program that you trying to debug
130    pub program: Option<String>,
131    /// The current working directory of your project
132    pub cwd: Option<PathBuf>,
133    /// Additional initialization arguments to be sent on DAP initialization
134    pub initialize_args: Option<serde_json::Value>,
135    /// Whether the debug adapter supports attaching to a running process.
136    pub supports_attach: bool,
137}
138
139/// Represents the type of the debugger adapter connection
140#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
141#[serde(rename_all = "lowercase", tag = "connection")]
142pub enum DebugConnectionType {
143    /// Connect to the debug adapter via TCP
144    TCP(TCPHost),
145    /// Connect to the debug adapter via STDIO
146    STDIO,
147}
148
149/// This struct represent a user created debug task
150#[derive(Deserialize, Serialize, PartialEq, Eq, JsonSchema, Clone, Debug)]
151#[serde(rename_all = "snake_case")]
152pub struct DebugTaskDefinition {
153    /// The adapter to run
154    #[serde(flatten)]
155    kind: DebugAdapterKind,
156    /// The type of request that should be called on the debug adapter
157    #[serde(default)]
158    request: DebugRequestType,
159    /// Name of the debug task
160    label: String,
161    /// Program to run the debugger on
162    program: Option<String>,
163    /// The current working directory of your project
164    cwd: Option<String>,
165    /// Additional initialization arguments to be sent on DAP initialization
166    initialize_args: Option<serde_json::Value>,
167}
168
169impl DebugTaskDefinition {
170    /// Translate from debug definition to a task template
171    pub fn to_zed_format(self) -> anyhow::Result<TaskTemplate> {
172        let command = "".to_string();
173        let cwd = self.cwd.clone().map(PathBuf::from).take_if(|p| p.exists());
174
175        let task_type = TaskType::Debug(DebugAdapterConfig {
176            label: self.label.clone(),
177            kind: self.kind,
178            request: self.request,
179            program: self.program,
180            cwd: cwd.clone(),
181            initialize_args: self.initialize_args,
182            supports_attach: true,
183        });
184
185        let args: Vec<String> = Vec::new();
186
187        Ok(TaskTemplate {
188            label: self.label,
189            command,
190            args,
191            task_type,
192            cwd: self.cwd,
193            ..Default::default()
194        })
195    }
196}
197
198/// A group of Debug Tasks defined in a JSON file.
199#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
200#[serde(transparent)]
201pub struct DebugTaskFile(pub Vec<DebugTaskDefinition>);
202
203impl DebugTaskFile {
204    /// Generates JSON schema of Tasks JSON template format.
205    pub fn generate_json_schema() -> serde_json_lenient::Value {
206        let schema = SchemaSettings::draft07()
207            .with(|settings| settings.option_add_null_type = false)
208            .into_generator()
209            .into_root_schema_for::<Self>();
210
211        serde_json_lenient::to_value(schema).unwrap()
212    }
213}
214
215impl TryFrom<DebugTaskFile> for TaskTemplates {
216    type Error = anyhow::Error;
217
218    fn try_from(value: DebugTaskFile) -> Result<Self, Self::Error> {
219        let templates = value
220            .0
221            .into_iter()
222            .filter_map(|debug_definition| debug_definition.to_zed_format().log_err())
223            .collect();
224
225        Ok(Self(templates))
226    }
227}