Skip to content

Commit e05d285

Browse files
committed
Debugger prototype for Ruby
This is a prototype implementation of Ruby debugger implementation, based on existing impl in core Zed. Input is most welcome on what it looks like and all that jazz.
1 parent 0d06487 commit e05d285

File tree

4 files changed

+86
-5
lines changed

4 files changed

+86
-5
lines changed

Cargo.lock

Lines changed: 1 addition & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ crate-type = ["cdylib"]
1111

1212
[dependencies]
1313
regex = "1.11.1"
14-
zed_extension_api = "0.4.0"
14+
zed_extension_api = { path = "../../zed/crates/extension_api" }

extension.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ version = "0.7.3"
55
schema_version = 1
66
authors = ["Vitaly Slobodin <[email protected]>"]
77
repository = "https://github.com/zed-extensions/ruby"
8+
debug_adapters = ["Ruby"]
9+
810

911
[language_servers.solargraph]
1012
name = "Solargraph"

src/ruby.rs

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,20 @@
11
mod bundler;
22
mod gemset;
33
mod language_servers;
4+
5+
use std::net::{Ipv4Addr, SocketAddrV4, TcpListener};
6+
use std::path::Path;
7+
48
use language_servers::{LanguageServer, Rubocop, RubyLsp, Solargraph};
59

610
use zed::lsp::{Completion, Symbol};
711
use zed::settings::LspSettings;
812
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+
};
1018

1119
#[derive(Default)]
1220
struct RubyExtension {
@@ -79,6 +87,79 @@ impl zed::Extension for RubyExtension {
7987

8088
Ok(Some(serde_json::json!(initialization_options)))
8189
}
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+
}
82163
}
83164

84165
zed::register_extension!(RubyExtension);

0 commit comments

Comments
 (0)