| 1 |
use super::{Context, Editor}; |
| 2 |
use crate::{ |
| 3 |
compositor::{self, Compositor}, |
| 4 |
job::{Callback, Jobs}, |
| 5 |
ui::{self, overlay::overlaid, Picker, Popup, Prompt, PromptEvent, Text}, |
| 6 |
}; |
| 7 |
use dap::{StackFrame, Thread, ThreadStates}; |
| 8 |
use helix_core::syntax::config::{DebugConfigCompletion, DebugTemplate}; |
| 9 |
use helix_dap::{self as dap, requests::TerminateArguments}; |
| 10 |
use helix_lsp::block_on; |
| 11 |
use helix_view::editor::Breakpoint; |
| 12 |
|
| 13 |
use serde_json::{to_value, Value}; |
| 14 |
use tui::text::Spans; |
| 15 |
|
| 16 |
use std::collections::HashMap; |
| 17 |
use std::future::Future; |
| 18 |
use std::path::PathBuf; |
| 19 |
|
| 20 |
use anyhow::{anyhow, bail}; |
| 21 |
|
| 22 |
use helix_view::handlers::dap::{breakpoints_changed, jump_to_stack_frame, select_thread_id}; |
| 23 |
|
| 24 |
fn thread_picker( |
| 25 |
cx: &mut Context, |
| 26 |
callback_fn: impl Fn(&mut Editor, &dap::Thread) + Send + 'static, |
| 27 |
) { |
| 28 |
let debugger = debugger!(cx.editor); |
| 29 |
|
| 30 |
let future = debugger.threads(); |
| 31 |
dap_callback( |
| 32 |
cx.jobs, |
| 33 |
future, |
| 34 |
move |editor, compositor, response: dap::requests::ThreadsResponse| { |
| 35 |
let threads = response.threads; |
| 36 |
if threads.len() == 1 { |
| 37 |
callback_fn(editor, &threads[0]); |
| 38 |
return; |
| 39 |
} |
| 40 |
let debugger = debugger!(editor); |
| 41 |
|
| 42 |
let thread_states = debugger.thread_states.clone(); |
| 43 |
let columns = [ |
| 44 |
ui::PickerColumn::new("name", |item: &Thread, _| item.name.as_str().into()), |
| 45 |
ui::PickerColumn::new("state", |item: &Thread, thread_states: &ThreadStates| { |
| 46 |
thread_states |
| 47 |
.get(&item.id) |
| 48 |
.map(|state| state.as_str()) |
| 49 |
.unwrap_or("unknown") |
| 50 |
.into() |
| 51 |
}), |
| 52 |
]; |
| 53 |
let picker = Picker::new( |
| 54 |
columns, |
| 55 |
0, |
| 56 |
threads, |
| 57 |
thread_states, |
| 58 |
move |cx, thread, _action| callback_fn(cx.editor, thread), |
| 59 |
) |
| 60 |
.with_preview(move |editor, thread| { |
| 61 |
let frames = editor |
| 62 |
.debug_adapters |
| 63 |
.get_active_client() |
| 64 |
.as_ref()? |
| 65 |
.stack_frames |
| 66 |
.get(&thread.id)?; |
| 67 |
let frame = frames.first()?; |
| 68 |
let path = frame.source.as_ref()?.path.as_ref()?.as_path(); |
| 69 |
let pos = Some(( |
| 70 |
frame.line.saturating_sub(1), |
| 71 |
frame.end_line.unwrap_or(frame.line).saturating_sub(1), |
| 72 |
)); |
| 73 |
Some((path.into(), pos)) |
| 74 |
}); |
| 75 |
compositor.push(Box::new(picker)); |
| 76 |
}, |
| 77 |
); |
| 78 |
} |
| 79 |
|
| 80 |
fn get_breakpoint_at_current_line(editor: &mut Editor) -> Option<(usize, Breakpoint)> { |
| 81 |
let (view, doc) = current!(editor); |
| 82 |
let text = doc.text().slice(..); |
| 83 |
|
| 84 |
let line = doc.selection(view.id).primary().cursor_line(text); |
| 85 |
let path = doc.path()?; |
| 86 |
editor.breakpoints.get(path).and_then(|breakpoints| { |
| 87 |
let i = breakpoints.iter().position(|b| b.line == line); |
| 88 |
i.map(|i| (i, breakpoints[i].clone())) |
| 89 |
}) |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
fn dap_callback<T, F>( |
| 95 |
jobs: &mut Jobs, |
| 96 |
call: impl Future<Output = helix_dap::Result<serde_json::Value>> + 'static + Send, |
| 97 |
callback: F, |
| 98 |
) where |
| 99 |
T: for<'de> serde::Deserialize<'de> + Send + 'static, |
| 100 |
F: FnOnce(&mut Editor, &mut Compositor, T) + Send + 'static, |
| 101 |
{ |
| 102 |
let callback = Box::pin(async move { |
| 103 |
let json = call.await?; |
| 104 |
let response = serde_json::from_value(json)?; |
| 105 |
let call: Callback = Callback::EditorCompositor(Box::new( |
| 106 |
move |editor: &mut Editor, compositor: &mut Compositor| { |
| 107 |
callback(editor, compositor, response) |
| 108 |
}, |
| 109 |
)); |
| 110 |
Ok(call) |
| 111 |
}); |
| 112 |
|
| 113 |
jobs.callback(callback); |
| 114 |
} |
| 115 |
|
| 116 |
pub fn dap_start_impl( |
| 117 |
cx: &mut compositor::Context, |
| 118 |
name: Option<&str>, |
| 119 |
socket: Option<std::net::SocketAddr>, |
| 120 |
params: Option<Vec<std::borrow::Cow<str>>>, |
| 121 |
) -> Result<(), anyhow::Error> { |
| 122 |
|
| 123 |
let workspace = doc!(cx.editor).workspace_root().to_path_buf(); |
| 124 |
if !cx |
| 125 |
.editor |
| 126 |
.workspace_trust |
| 127 |
.query(&workspace, helix_loader::workspace_trust::TrustQuery::Dap) |
| 128 |
.is_trusted() |
| 129 |
{ |
| 130 |
bail!("Workspace is not trusted. Run `:workspace-trust` to enable the debug adapter."); |
| 131 |
} |
| 132 |
|
| 133 |
let doc = doc!(cx.editor); |
| 134 |
let config = doc |
| 135 |
.language_config() |
| 136 |
.and_then(|config| config.debugger.as_ref()) |
| 137 |
.ok_or_else(|| anyhow!("No debug adapter available for language"))?; |
| 138 |
|
| 139 |
let id = cx |
| 140 |
.editor |
| 141 |
.debug_adapters |
| 142 |
.start_client(socket, config) |
| 143 |
.map_err(|e| anyhow!("Failed to start debug client: {}", e))?; |
| 144 |
|
| 145 |
|
| 146 |
let template = match name { |
| 147 |
Some(name) => config.templates.iter().find(|t| t.name == name), |
| 148 |
None => config.templates.first(), |
| 149 |
} |
| 150 |
.ok_or_else(|| anyhow!("No debug config with given name"))?; |
| 151 |
|
| 152 |
let mut args: HashMap<&str, Value> = if let Some(params) = params.as_ref() { |
| 153 |
let preprocessed_params = prepare_dap_params(template, params); |
| 154 |
template |
| 155 |
.args |
| 156 |
.iter() |
| 157 |
.map(|(k, v)| (k.as_str(), map_value(v, &preprocessed_params))) |
| 158 |
.collect() |
| 159 |
} else { |
| 160 |
template |
| 161 |
.args |
| 162 |
.iter() |
| 163 |
.map(|(k, v)| (k.as_str(), v.clone())) |
| 164 |
.collect() |
| 165 |
}; |
| 166 |
|
| 167 |
args.insert("cwd", to_value(helix_stdx::env::current_working_dir())?); |
| 168 |
|
| 169 |
let args = to_value(args).unwrap(); |
| 170 |
|
| 171 |
let callback = |_editor: &mut Editor, _compositor: &mut Compositor, _response: Value| { |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
}; |
| 176 |
|
| 177 |
let debugger = match cx.editor.debug_adapters.get_client_mut(id) { |
| 178 |
Some(child) => child, |
| 179 |
None => { |
| 180 |
bail!("Failed to get child debugger."); |
| 181 |
} |
| 182 |
}; |
| 183 |
|
| 184 |
match &template.request[..] { |
| 185 |
"launch" => { |
| 186 |
let call = debugger.launch(args); |
| 187 |
dap_callback(cx.jobs, call, callback); |
| 188 |
} |
| 189 |
"attach" => { |
| 190 |
let call = debugger.attach(args); |
| 191 |
dap_callback(cx.jobs, call, callback); |
| 192 |
} |
| 193 |
request => bail!("Unsupported request '{}'", request), |
| 194 |
}; |
| 195 |
|
| 196 |
|
| 197 |
Ok(()) |
| 198 |
} |
| 199 |
|
| 200 |
fn prepare_dap_params(template: &DebugTemplate, params: &[std::borrow::Cow<str>]) -> Vec<String> { |
| 201 |
params |
| 202 |
.iter() |
| 203 |
.enumerate() |
| 204 |
.map(|(i, x)| { |
| 205 |
let mut param = x.to_string(); |
| 206 |
if let Some(DebugConfigCompletion::Advanced(cfg)) = template.completion.get(i) { |
| 207 |
if matches!(cfg.completion.as_deref(), Some("filename" | "directory")) { |
| 208 |
param = std::fs::canonicalize(x.as_ref()) |
| 209 |
.ok() |
| 210 |
.and_then(|pb| pb.into_os_string().into_string().ok()) |
| 211 |
.unwrap_or_else(|| x.to_string()); |
| 212 |
} |
| 213 |
} |
| 214 |
param |
| 215 |
}) |
| 216 |
.collect() |
| 217 |
} |
| 218 |
|
| 219 |
fn map_value(value: &Value, params: &[String]) -> Value { |
| 220 |
match value { |
| 221 |
Value::String(string) => { |
| 222 |
let mut string = string.clone(); |
| 223 |
for (i, x) in params.iter().enumerate() { |
| 224 |
let pattern = format!("{{{}}}", i); |
| 225 |
string = string.replace(&pattern, x); |
| 226 |
} |
| 227 |
if let Ok(integer) = string.parse::<usize>() { |
| 228 |
to_value(integer).unwrap() |
| 229 |
} else { |
| 230 |
to_value(string).unwrap() |
| 231 |
} |
| 232 |
} |
| 233 |
Value::Array(array) => Value::Array(array.iter().map(|x| map_value(x, params)).collect()), |
| 234 |
Value::Object(object) => Value::Object( |
| 235 |
object |
| 236 |
.iter() |
| 237 |
.map(|(k, v)| (k.clone(), map_value(v, params))) |
| 238 |
.collect(), |
| 239 |
), |
| 240 |
|
| 241 |
_ => value.clone(), |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
pub fn dap_launch(cx: &mut Context) { |
| 246 |
|
| 247 |
if cx.editor.debug_adapters.get_active_client().is_some() { |
| 248 |
cx.editor.set_error("Debugger is already running"); |
| 249 |
return; |
| 250 |
} |
| 251 |
|
| 252 |
let doc = doc!(cx.editor); |
| 253 |
|
| 254 |
let config = match doc |
| 255 |
.language_config() |
| 256 |
.and_then(|config| config.debugger.as_ref()) |
| 257 |
{ |
| 258 |
Some(c) => c, |
| 259 |
None => { |
| 260 |
cx.editor |
| 261 |
.set_error("No debug adapter available for language"); |
| 262 |
return; |
| 263 |
} |
| 264 |
}; |
| 265 |
|
| 266 |
let templates = config.templates.clone(); |
| 267 |
|
| 268 |
let columns = [ui::PickerColumn::new( |
| 269 |
"template", |
| 270 |
|item: &DebugTemplate, _| item.name.as_str().into(), |
| 271 |
)]; |
| 272 |
|
| 273 |
cx.push_layer(Box::new(overlaid(Picker::new( |
| 274 |
columns, |
| 275 |
0, |
| 276 |
templates, |
| 277 |
(), |
| 278 |
|cx, template, _action| { |
| 279 |
if template.completion.is_empty() { |
| 280 |
if let Err(err) = dap_start_impl(cx, Some(&template.name), None, None) { |
| 281 |
cx.editor.set_error(err.to_string()); |
| 282 |
} |
| 283 |
} else { |
| 284 |
let completions = template.completion.clone(); |
| 285 |
let name = template.name.clone(); |
| 286 |
let callback = Box::pin(async move { |
| 287 |
let call: Callback = |
| 288 |
Callback::EditorCompositor(Box::new(move |_editor, compositor| { |
| 289 |
let prompt = debug_parameter_prompt(completions, name, Vec::new()); |
| 290 |
compositor.push(Box::new(prompt)); |
| 291 |
})); |
| 292 |
Ok(call) |
| 293 |
}); |
| 294 |
cx.jobs.callback(callback); |
| 295 |
} |
| 296 |
}, |
| 297 |
)))); |
| 298 |
} |
| 299 |
|
| 300 |
pub fn dap_restart(cx: &mut Context) { |
| 301 |
let debugger = match cx.editor.debug_adapters.get_active_client() { |
| 302 |
Some(debugger) => debugger, |
| 303 |
None => { |
| 304 |
cx.editor.set_error("Debugger is not running"); |
| 305 |
return; |
| 306 |
} |
| 307 |
}; |
| 308 |
if !debugger |
| 309 |
.capabilities() |
| 310 |
.supports_restart_request |
| 311 |
.unwrap_or(false) |
| 312 |
{ |
| 313 |
cx.editor |
| 314 |
.set_error("Debugger does not support session restarts"); |
| 315 |
return; |
| 316 |
} |
| 317 |
if debugger.starting_request_args().is_none() { |
| 318 |
cx.editor |
| 319 |
.set_error("No arguments found with which to restart the sessions"); |
| 320 |
return; |
| 321 |
} |
| 322 |
|
| 323 |
dap_callback( |
| 324 |
cx.jobs, |
| 325 |
debugger.restart(), |
| 326 |
|editor, _compositor, _resp: ()| editor.set_status("Debugging session restarted"), |
| 327 |
); |
| 328 |
} |
| 329 |
|
| 330 |
fn debug_parameter_prompt( |
| 331 |
completions: Vec<DebugConfigCompletion>, |
| 332 |
config_name: String, |
| 333 |
mut params: Vec<String>, |
| 334 |
) -> Prompt { |
| 335 |
let completion = completions.get(params.len()).unwrap(); |
| 336 |
let field_type = if let DebugConfigCompletion::Advanced(cfg) = completion { |
| 337 |
cfg.completion.as_deref().unwrap_or("") |
| 338 |
} else { |
| 339 |
"" |
| 340 |
}; |
| 341 |
let name = match completion { |
| 342 |
DebugConfigCompletion::Advanced(cfg) => cfg.name.as_deref().unwrap_or(field_type), |
| 343 |
DebugConfigCompletion::Named(name) => name.as_str(), |
| 344 |
}; |
| 345 |
let default_val = match completion { |
| 346 |
DebugConfigCompletion::Advanced(cfg) => cfg.default.as_deref().unwrap_or(""), |
| 347 |
_ => "", |
| 348 |
} |
| 349 |
.to_owned(); |
| 350 |
|
| 351 |
let completer = match field_type { |
| 352 |
"filename" => |editor: &Editor, input: &str| { |
| 353 |
ui::completers::filename_with_git_ignore(editor, input, false) |
| 354 |
}, |
| 355 |
"directory" => |editor: &Editor, input: &str| { |
| 356 |
ui::completers::directory_with_git_ignore(editor, input, false) |
| 357 |
}, |
| 358 |
_ => ui::completers::none, |
| 359 |
}; |
| 360 |
|
| 361 |
Prompt::new( |
| 362 |
format!("{}: ", name).into(), |
| 363 |
None, |
| 364 |
completer, |
| 365 |
move |cx, input: &str, event: PromptEvent| { |
| 366 |
if event != PromptEvent::Validate { |
| 367 |
return; |
| 368 |
} |
| 369 |
|
| 370 |
let mut value = input.to_owned(); |
| 371 |
if value.is_empty() { |
| 372 |
value = default_val.clone(); |
| 373 |
} |
| 374 |
params.push(value); |
| 375 |
|
| 376 |
if params.len() < completions.len() { |
| 377 |
let completions = completions.clone(); |
| 378 |
let config_name = config_name.clone(); |
| 379 |
let params = params.clone(); |
| 380 |
let callback = Box::pin(async move { |
| 381 |
let call: Callback = |
| 382 |
Callback::EditorCompositor(Box::new(move |_editor, compositor| { |
| 383 |
let prompt = debug_parameter_prompt(completions, config_name, params); |
| 384 |
compositor.push(Box::new(prompt)); |
| 385 |
})); |
| 386 |
Ok(call) |
| 387 |
}); |
| 388 |
cx.jobs.callback(callback); |
| 389 |
} else if let Err(err) = dap_start_impl( |
| 390 |
cx, |
| 391 |
Some(&config_name), |
| 392 |
None, |
| 393 |
Some(params.iter().map(|x| x.into()).collect()), |
| 394 |
) { |
| 395 |
cx.editor.set_error(err.to_string()); |
| 396 |
} |
| 397 |
}, |
| 398 |
) |
| 399 |
} |
| 400 |
|
| 401 |
pub fn dap_toggle_breakpoint(cx: &mut Context) { |
| 402 |
let (view, doc) = current!(cx.editor); |
| 403 |
|
| 404 |
let Some(path) = doc.path().map(ToOwned::to_owned) else { |
| 405 |
cx.editor |
| 406 |
.set_error("Can't set breakpoint: document has no path"); |
| 407 |
return; |
| 408 |
}; |
| 409 |
|
| 410 |
let text = doc.text().slice(..); |
| 411 |
let line = doc.selection(view.id).primary().cursor_line(text); |
| 412 |
dap_toggle_breakpoint_impl(cx, path, line); |
| 413 |
} |
| 414 |
|
| 415 |
pub fn dap_toggle_breakpoint_impl(cx: &mut Context, path: PathBuf, line: usize) { |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
let breakpoints = cx.editor.breakpoints.entry(path.clone()).or_default(); |
| 420 |
|
| 421 |
if let Some(pos) = breakpoints |
| 422 |
.iter() |
| 423 |
.position(|breakpoint| breakpoint.line == line) |
| 424 |
{ |
| 425 |
breakpoints.remove(pos); |
| 426 |
} else { |
| 427 |
breakpoints.push(Breakpoint { |
| 428 |
line, |
| 429 |
..Default::default() |
| 430 |
}); |
| 431 |
} |
| 432 |
|
| 433 |
let debugger = debugger!(cx.editor); |
| 434 |
|
| 435 |
if let Err(e) = breakpoints_changed(debugger, path, breakpoints) { |
| 436 |
cx.editor |
| 437 |
.set_error(format!("Failed to set breakpoints: {}", e)); |
| 438 |
} |
| 439 |
} |
| 440 |
|
| 441 |
pub fn dap_continue(cx: &mut Context) { |
| 442 |
let debugger = debugger!(cx.editor); |
| 443 |
|
| 444 |
if let Some(thread_id) = debugger.thread_id { |
| 445 |
let request = debugger.continue_thread(thread_id); |
| 446 |
|
| 447 |
dap_callback( |
| 448 |
cx.jobs, |
| 449 |
request, |
| 450 |
|editor, _compositor, _response: dap::requests::ContinueResponse| { |
| 451 |
debugger!(editor).resume_application(); |
| 452 |
}, |
| 453 |
); |
| 454 |
} else { |
| 455 |
cx.editor |
| 456 |
.set_error("Currently active thread is not stopped. Switch the thread."); |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
pub fn dap_pause(cx: &mut Context) { |
| 461 |
thread_picker(cx, |editor, thread| { |
| 462 |
let debugger = debugger!(editor); |
| 463 |
let request = debugger.pause(thread.id); |
| 464 |
|
| 465 |
if let Err(e) = block_on(request) { |
| 466 |
editor.set_error(format!("Failed to pause: {}", e)); |
| 467 |
} |
| 468 |
}) |
| 469 |
} |
| 470 |
|
| 471 |
pub fn dap_step_in(cx: &mut Context) { |
| 472 |
let debugger = debugger!(cx.editor); |
| 473 |
|
| 474 |
if let Some(thread_id) = debugger.thread_id { |
| 475 |
let request = debugger.step_in(thread_id); |
| 476 |
|
| 477 |
dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| { |
| 478 |
debugger!(editor).resume_application(); |
| 479 |
}); |
| 480 |
} else { |
| 481 |
cx.editor |
| 482 |
.set_error("Currently active thread is not stopped. Switch the thread."); |
| 483 |
} |
| 484 |
} |
| 485 |
|
| 486 |
pub fn dap_step_out(cx: &mut Context) { |
| 487 |
let debugger = debugger!(cx.editor); |
| 488 |
|
| 489 |
if let Some(thread_id) = debugger.thread_id { |
| 490 |
let request = debugger.step_out(thread_id); |
| 491 |
dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| { |
| 492 |
debugger!(editor).resume_application(); |
| 493 |
}); |
| 494 |
} else { |
| 495 |
cx.editor |
| 496 |
.set_error("Currently active thread is not stopped. Switch the thread."); |
| 497 |
} |
| 498 |
} |
| 499 |
|
| 500 |
pub fn dap_next(cx: &mut Context) { |
| 501 |
let debugger = debugger!(cx.editor); |
| 502 |
|
| 503 |
if let Some(thread_id) = debugger.thread_id { |
| 504 |
let request = debugger.next(thread_id); |
| 505 |
dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| { |
| 506 |
debugger!(editor).resume_application(); |
| 507 |
}); |
| 508 |
} else { |
| 509 |
cx.editor |
| 510 |
.set_error("Currently active thread is not stopped. Switch the thread."); |
| 511 |
} |
| 512 |
} |
| 513 |
|
| 514 |
pub fn dap_variables(cx: &mut Context) { |
| 515 |
let debugger = debugger!(cx.editor); |
| 516 |
|
| 517 |
if debugger.thread_id.is_none() { |
| 518 |
cx.editor |
| 519 |
.set_status("Cannot access variables while target is running."); |
| 520 |
return; |
| 521 |
} |
| 522 |
let (frame, thread_id) = match (debugger.active_frame, debugger.thread_id) { |
| 523 |
(Some(frame), Some(thread_id)) => (frame, thread_id), |
| 524 |
_ => { |
| 525 |
cx.editor |
| 526 |
.set_status("Cannot find current stack frame to access variables."); |
| 527 |
return; |
| 528 |
} |
| 529 |
}; |
| 530 |
|
| 531 |
let thread_frame = match debugger.stack_frames.get(&thread_id) { |
| 532 |
Some(thread_frame) => thread_frame, |
| 533 |
None => { |
| 534 |
cx.editor |
| 535 |
.set_error(format!("Failed to get stack frame for thread: {thread_id}")); |
| 536 |
return; |
| 537 |
} |
| 538 |
}; |
| 539 |
let stack_frame = match thread_frame.get(frame) { |
| 540 |
Some(stack_frame) => stack_frame, |
| 541 |
None => { |
| 542 |
cx.editor.set_error(format!( |
| 543 |
"Failed to get stack frame for thread {thread_id} and frame {frame}." |
| 544 |
)); |
| 545 |
return; |
| 546 |
} |
| 547 |
}; |
| 548 |
|
| 549 |
let frame_id = stack_frame.id; |
| 550 |
let scopes = match block_on(debugger.scopes(frame_id)) { |
| 551 |
Ok(s) => s, |
| 552 |
Err(e) => { |
| 553 |
cx.editor.set_error(format!("Failed to get scopes: {}", e)); |
| 554 |
return; |
| 555 |
} |
| 556 |
}; |
| 557 |
|
| 558 |
|
| 559 |
let mut variables = Vec::new(); |
| 560 |
|
| 561 |
let theme = &cx.editor.theme; |
| 562 |
let scope_style = theme.get("ui.linenr.selected"); |
| 563 |
let type_style = theme.get("ui.text"); |
| 564 |
let text_style = theme.get("ui.text.focus"); |
| 565 |
|
| 566 |
for scope in scopes.iter() { |
| 567 |
|
| 568 |
use tui::text::Span; |
| 569 |
let response = block_on(debugger.variables(scope.variables_reference)); |
| 570 |
|
| 571 |
variables.push(Spans::from(Span::styled( |
| 572 |
format!("▸ {}", scope.name), |
| 573 |
scope_style, |
| 574 |
))); |
| 575 |
|
| 576 |
if let Ok(vars) = response { |
| 577 |
variables.reserve(vars.len()); |
| 578 |
for var in vars { |
| 579 |
let mut spans = Vec::with_capacity(5); |
| 580 |
|
| 581 |
spans.push(Span::styled(var.name.to_owned(), text_style)); |
| 582 |
if let Some(ty) = var.ty { |
| 583 |
spans.push(Span::raw(": ")); |
| 584 |
spans.push(Span::styled(ty.to_owned(), type_style)); |
| 585 |
} |
| 586 |
spans.push(Span::raw(" = ")); |
| 587 |
spans.push(Span::styled(var.value.to_owned(), text_style)); |
| 588 |
variables.push(Spans::from(spans)); |
| 589 |
} |
| 590 |
} |
| 591 |
} |
| 592 |
|
| 593 |
let contents = Text::from(tui::text::Text::from(variables)); |
| 594 |
let popup = Popup::new("dap-variables", contents); |
| 595 |
cx.replace_or_push_layer("dap-variables", popup); |
| 596 |
} |
| 597 |
|
| 598 |
pub fn dap_terminate(cx: &mut Context) { |
| 599 |
cx.editor.set_status("Terminating debug session..."); |
| 600 |
let debugger = debugger!(cx.editor); |
| 601 |
|
| 602 |
if debugger |
| 603 |
.caps |
| 604 |
.as_ref() |
| 605 |
.is_some_and(|c| c.supports_terminate_request.unwrap_or_default()) |
| 606 |
{ |
| 607 |
let terminate_arguments = Some(TerminateArguments { |
| 608 |
restart: Some(false), |
| 609 |
}); |
| 610 |
|
| 611 |
let request = debugger.terminate(terminate_arguments); |
| 612 |
dap_callback(cx.jobs, request, |editor, _compositor, _response: ()| { |
| 613 |
|
| 614 |
editor.debug_adapters.unset_active_client(); |
| 615 |
}); |
| 616 |
} else { |
| 617 |
cx.editor.debug_adapters.unset_active_client(); |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
pub fn dap_enable_exceptions(cx: &mut Context) { |
| 622 |
let debugger = debugger!(cx.editor); |
| 623 |
|
| 624 |
let filters = match &debugger.capabilities().exception_breakpoint_filters { |
| 625 |
Some(filters) => filters.iter().map(|f| f.filter.clone()).collect(), |
| 626 |
None => return, |
| 627 |
}; |
| 628 |
|
| 629 |
let request = debugger.set_exception_breakpoints(filters); |
| 630 |
|
| 631 |
dap_callback( |
| 632 |
cx.jobs, |
| 633 |
request, |
| 634 |
|_editor, _compositor, _response: dap::requests::SetExceptionBreakpointsResponse| { |
| 635 |
|
| 636 |
}, |
| 637 |
) |
| 638 |
} |
| 639 |
|
| 640 |
pub fn dap_disable_exceptions(cx: &mut Context) { |
| 641 |
let debugger = debugger!(cx.editor); |
| 642 |
|
| 643 |
let request = debugger.set_exception_breakpoints(Vec::new()); |
| 644 |
|
| 645 |
dap_callback( |
| 646 |
cx.jobs, |
| 647 |
request, |
| 648 |
|_editor, _compositor, _response: dap::requests::SetExceptionBreakpointsResponse| { |
| 649 |
|
| 650 |
}, |
| 651 |
) |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
pub fn dap_edit_condition(cx: &mut Context) { |
| 656 |
if let Some((pos, breakpoint)) = get_breakpoint_at_current_line(cx.editor) { |
| 657 |
let Some(path) = doc!(cx.editor).path().map(ToOwned::to_owned) else { |
| 658 |
return; |
| 659 |
}; |
| 660 |
let callback = Box::pin(async move { |
| 661 |
let call: Callback = Callback::EditorCompositor(Box::new(move |editor, compositor| { |
| 662 |
let mut prompt = Prompt::new( |
| 663 |
"condition:".into(), |
| 664 |
None, |
| 665 |
ui::completers::none, |
| 666 |
move |cx, input: &str, event: PromptEvent| { |
| 667 |
if event != PromptEvent::Validate { |
| 668 |
return; |
| 669 |
} |
| 670 |
|
| 671 |
let breakpoints = &mut cx.editor.breakpoints.get_mut(&path).unwrap(); |
| 672 |
breakpoints[pos].condition = match input { |
| 673 |
"" => None, |
| 674 |
input => Some(input.to_owned()), |
| 675 |
}; |
| 676 |
|
| 677 |
let debugger = debugger!(cx.editor); |
| 678 |
|
| 679 |
if let Err(e) = breakpoints_changed(debugger, path.clone(), breakpoints) { |
| 680 |
cx.editor |
| 681 |
.set_error(format!("Failed to set breakpoints: {}", e)); |
| 682 |
} |
| 683 |
}, |
| 684 |
); |
| 685 |
if let Some(condition) = breakpoint.condition { |
| 686 |
prompt.insert_str(&condition, editor) |
| 687 |
} |
| 688 |
compositor.push(Box::new(prompt)); |
| 689 |
})); |
| 690 |
Ok(call) |
| 691 |
}); |
| 692 |
cx.jobs.callback(callback); |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
pub fn dap_edit_log(cx: &mut Context) { |
| 697 |
if let Some((pos, breakpoint)) = get_breakpoint_at_current_line(cx.editor) { |
| 698 |
let Some(path) = doc!(cx.editor).path().map(ToOwned::to_owned) else { |
| 699 |
return; |
| 700 |
}; |
| 701 |
let callback = Box::pin(async move { |
| 702 |
let call: Callback = Callback::EditorCompositor(Box::new(move |editor, compositor| { |
| 703 |
let mut prompt = Prompt::new( |
| 704 |
"log-message:".into(), |
| 705 |
None, |
| 706 |
ui::completers::none, |
| 707 |
move |cx, input: &str, event: PromptEvent| { |
| 708 |
if event != PromptEvent::Validate { |
| 709 |
return; |
| 710 |
} |
| 711 |
|
| 712 |
let breakpoints = &mut cx.editor.breakpoints.get_mut(&path).unwrap(); |
| 713 |
breakpoints[pos].log_message = match input { |
| 714 |
"" => None, |
| 715 |
input => Some(input.to_owned()), |
| 716 |
}; |
| 717 |
|
| 718 |
let debugger = debugger!(cx.editor); |
| 719 |
if let Err(e) = breakpoints_changed(debugger, path.clone(), breakpoints) { |
| 720 |
cx.editor |
| 721 |
.set_error(format!("Failed to set breakpoints: {}", e)); |
| 722 |
} |
| 723 |
}, |
| 724 |
); |
| 725 |
if let Some(log_message) = breakpoint.log_message { |
| 726 |
prompt.insert_str(&log_message, editor); |
| 727 |
} |
| 728 |
compositor.push(Box::new(prompt)); |
| 729 |
})); |
| 730 |
Ok(call) |
| 731 |
}); |
| 732 |
cx.jobs.callback(callback); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
pub fn dap_switch_thread(cx: &mut Context) { |
| 737 |
thread_picker(cx, |editor, thread| { |
| 738 |
block_on(select_thread_id(editor, thread.id, true)); |
| 739 |
}) |
| 740 |
} |
| 741 |
pub fn dap_switch_stack_frame(cx: &mut Context) { |
| 742 |
let debugger = debugger!(cx.editor); |
| 743 |
|
| 744 |
let thread_id = match debugger.thread_id { |
| 745 |
Some(thread_id) => thread_id, |
| 746 |
None => { |
| 747 |
cx.editor.set_error("No thread is currently active"); |
| 748 |
return; |
| 749 |
} |
| 750 |
}; |
| 751 |
|
| 752 |
let frames = debugger.stack_frames[&thread_id] |
| 753 |
.iter() |
| 754 |
.cloned() |
| 755 |
.enumerate() |
| 756 |
.collect::<Vec<_>>(); |
| 757 |
let thread_state = debugger |
| 758 |
.thread_states |
| 759 |
.get(&thread_id) |
| 760 |
.cloned() |
| 761 |
.unwrap_or_else(|| "unknown".to_string()); |
| 762 |
|
| 763 |
let columns = [ui::PickerColumn::new( |
| 764 |
"frame", |
| 765 |
|item: &(usize, StackFrame), thread_state: &String| { |
| 766 |
format!("{} ({})", item.1.name, thread_state).into() |
| 767 |
}, |
| 768 |
)]; |
| 769 |
let picker = Picker::new( |
| 770 |
columns, |
| 771 |
0, |
| 772 |
frames, |
| 773 |
thread_state, |
| 774 |
move |cx, (index, frame), _action| { |
| 775 |
let debugger = debugger!(cx.editor); |
| 776 |
debugger.active_frame = Some(*index); |
| 777 |
jump_to_stack_frame(cx.editor, frame); |
| 778 |
}, |
| 779 |
) |
| 780 |
.with_preview(move |_editor, (_index, frame)| { |
| 781 |
frame.source.as_ref().and_then(|source| { |
| 782 |
source.path.as_ref().map(|path| { |
| 783 |
( |
| 784 |
path.as_path().into(), |
| 785 |
Some(( |
| 786 |
frame.line.saturating_sub(1), |
| 787 |
frame.end_line.unwrap_or(frame.line).saturating_sub(1), |
| 788 |
)), |
| 789 |
) |
| 790 |
}) |
| 791 |
}) |
| 792 |
}); |
| 793 |
cx.push_layer(Box::new(picker)) |
| 794 |
} |