|
1 | 1 | mod bundler; |
2 | 2 | mod gemset; |
3 | 3 | mod language_servers; |
| 4 | + |
| 5 | +use std::net::{Ipv4Addr, SocketAddrV4, TcpListener}; |
| 6 | +use std::path::Path; |
| 7 | + |
4 | 8 | use language_servers::{LanguageServer, Rubocop, RubyLsp, Solargraph}; |
5 | 9 |
|
6 | 10 | use zed::lsp::{Completion, Symbol}; |
7 | 11 | use zed::settings::LspSettings; |
8 | 12 | use zed::{serde_json, CodeLabel, LanguageServerId}; |
9 | | -use zed_extension_api::{self as zed, Result}; |
| 13 | +use zed::{DebugAdapterBinary, DebugRequest, DebugTaskDefinition}; |
| 14 | +use zed_extension_api::{ |
| 15 | + self as zed, resolve_tcp_template, Command, Result, StartDebuggingRequestArguments, |
| 16 | + StartDebuggingRequestArgumentsRequest, TcpArgumentsTemplate, Worktree, |
| 17 | +}; |
10 | 18 |
|
11 | 19 | #[derive(Default)] |
12 | 20 | struct RubyExtension { |
@@ -79,6 +87,79 @@ impl zed::Extension for RubyExtension { |
79 | 87 |
|
80 | 88 | Ok(Some(serde_json::json!(initialization_options))) |
81 | 89 | } |
| 90 | + fn get_dap_binary( |
| 91 | + &mut self, |
| 92 | + adapter_name: String, |
| 93 | + config: DebugTaskDefinition, |
| 94 | + _: Option<String>, |
| 95 | + worktree: &Worktree, |
| 96 | + ) -> Result<DebugAdapterBinary, String> { |
| 97 | + let mut rdbg_path = Path::new(&adapter_name) |
| 98 | + .join("rdbg") |
| 99 | + .to_string_lossy() |
| 100 | + .into_owned(); |
| 101 | + |
| 102 | + if worktree.which(&rdbg_path).is_none() { |
| 103 | + match worktree.which("rdbg".as_ref()) { |
| 104 | + Some(path) => rdbg_path = path.into(), |
| 105 | + None => { |
| 106 | + let output = Command::new("gem") |
| 107 | + .arg("install") |
| 108 | + .arg("--no-document") |
| 109 | + .arg("--bindir") |
| 110 | + .arg(&adapter_name) |
| 111 | + .arg("debug") |
| 112 | + .output()?; |
| 113 | + if !output.status.is_none_or(|status| status != 0) { |
| 114 | + return Err(format!( |
| 115 | + "Failed to install rdbg:\n{}", |
| 116 | + String::from_utf8_lossy(&output.stderr).to_string() |
| 117 | + )); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + let tcp_connection = |
| 124 | + config |
| 125 | + .tcp_connection |
| 126 | + .clone() |
| 127 | + .unwrap_or_else(|| TcpArgumentsTemplate { |
| 128 | + port: None, |
| 129 | + host: None, |
| 130 | + timeout: None, |
| 131 | + }); |
| 132 | + let connection = resolve_tcp_template(tcp_connection)?; |
| 133 | + let DebugRequest::Launch(launch) = config.request.clone() else { |
| 134 | + return Err("rdbg does not yet support attaching".to_string()); |
| 135 | + }; |
| 136 | + |
| 137 | + let mut arguments = vec![ |
| 138 | + "--open".to_string(), |
| 139 | + format!("--port={}", connection.port), |
| 140 | + format!("--host={}", connection.host), |
| 141 | + ]; |
| 142 | + if worktree.which(launch.program.as_ref()).is_some() { |
| 143 | + arguments.push("--command".to_string()) |
| 144 | + } |
| 145 | + arguments.push(launch.program); |
| 146 | + arguments.extend(launch.args); |
| 147 | + let request = match config.request { |
| 148 | + DebugRequest::Launch(_) => StartDebuggingRequestArgumentsRequest::Launch, |
| 149 | + DebugRequest::Attach(_) => StartDebuggingRequestArgumentsRequest::Attach, |
| 150 | + }; |
| 151 | + Ok(DebugAdapterBinary { |
| 152 | + command: rdbg_path.to_string(), |
| 153 | + arguments, |
| 154 | + connection: Some(connection), |
| 155 | + cwd: launch.cwd, |
| 156 | + envs: launch.envs.into_iter().collect(), |
| 157 | + request_args: StartDebuggingRequestArguments { |
| 158 | + configuration: serde_json::Value::Object(Default::default()).to_string(), |
| 159 | + request, |
| 160 | + }, |
| 161 | + }) |
| 162 | + } |
82 | 163 | } |
83 | 164 |
|
84 | 165 | zed::register_extension!(RubyExtension); |
0 commit comments