Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'helix-view/src/handlers/dap.rs')
| -rw-r--r-- | helix-view/src/handlers/dap.rs | 432 |
1 files changed, 221 insertions, 211 deletions
diff --git a/helix-view/src/handlers/dap.rs b/helix-view/src/handlers/dap.rs index 81766dd5..56eb8efa 100644 --- a/helix-view/src/handlers/dap.rs +++ b/helix-view/src/handlers/dap.rs @@ -5,6 +5,7 @@ use helix_core::Selection; use helix_dap::{self as dap, Client, ConnectionType, Payload, Request, ThreadId}; use helix_lsp::block_on; use log::warn; +use serde_json::json; use std::fmt::Write; use std::path::PathBuf; @@ -141,7 +142,6 @@ pub fn breakpoints_changed( impl Editor { pub async fn handle_debugger_message(&mut self, payload: helix_dap::Payload) -> bool { - use dap::requests::RunInTerminal; use helix_dap::{events, Event}; let debugger = match self.debugger.as_mut() { @@ -149,250 +149,260 @@ impl Editor { None => return false, }; match payload { - Payload::Event(ev) => match *ev { - Event::Stopped(events::Stopped { - thread_id, - description, - text, - reason, - all_threads_stopped, - .. - }) => { - let all_threads_stopped = all_threads_stopped.unwrap_or_default(); - - if all_threads_stopped { - if let Ok(response) = debugger.request::<dap::requests::Threads>(()).await { - for thread in response.threads { - fetch_stack_trace(debugger, thread.id).await; + Payload::Event(event) => { + let event = match Event::parse(&event.event, event.body) { + Ok(event) => event, + Err(dap::Error::Unhandled) => { + log::info!("Discarding unknown DAP event '{}'", event.event); + return false; + } + Err(err) => { + log::warn!("Discarding invalid DAP event '{}': {err}", event.event); + return false; + } + }; + match event { + Event::Stopped(events::StoppedBody { + thread_id, + description, + text, + reason, + all_threads_stopped, + .. + }) => { + let all_threads_stopped = all_threads_stopped.unwrap_or_default(); + + if all_threads_stopped { + if let Ok(response) = + debugger.request::<dap::requests::Threads>(()).await + { + for thread in response.threads { + fetch_stack_trace(debugger, thread.id).await; + } + select_thread_id(self, thread_id.unwrap_or_default(), false).await; } - select_thread_id(self, thread_id.unwrap_or_default(), false).await; + } else if let Some(thread_id) = thread_id { + debugger.thread_states.insert(thread_id, reason.clone()); // TODO: dap uses "type" || "reason" here + + // whichever thread stops is made "current" (if no previously selected thread). + select_thread_id(self, thread_id, false).await; } - } else if let Some(thread_id) = thread_id { - debugger.thread_states.insert(thread_id, reason.clone()); // TODO: dap uses "type" || "reason" here - // whichever thread stops is made "current" (if no previously selected thread). - select_thread_id(self, thread_id, false).await; - } + let scope = match thread_id { + Some(id) => format!("Thread {}", id), + None => "Target".to_owned(), + }; - let scope = match thread_id { - Some(id) => format!("Thread {}", id), - None => "Target".to_owned(), - }; + let mut status = format!("{} stopped because of {}", scope, reason); + if let Some(desc) = description { + write!(status, " {}", desc).unwrap(); + } + if let Some(text) = text { + write!(status, " {}", text).unwrap(); + } + if all_threads_stopped { + status.push_str(" (all threads stopped)"); + } - let mut status = format!("{} stopped because of {}", scope, reason); - if let Some(desc) = description { - write!(status, " {}", desc).unwrap(); - } - if let Some(text) = text { - write!(status, " {}", text).unwrap(); + self.set_status(status); } - if all_threads_stopped { - status.push_str(" (all threads stopped)"); + Event::Continued(events::ContinuedBody { thread_id, .. }) => { + debugger + .thread_states + .insert(thread_id, "running".to_owned()); + if debugger.thread_id == Some(thread_id) { + debugger.resume_application(); + } } - - self.set_status(status); - } - Event::Continued(events::Continued { thread_id, .. }) => { - debugger - .thread_states - .insert(thread_id, "running".to_owned()); - if debugger.thread_id == Some(thread_id) { - debugger.resume_application(); + Event::Thread(_) => { + // TODO: update thread_states, make threads request } - } - Event::Thread(_) => { - // TODO: update thread_states, make threads request - } - Event::Breakpoint(events::Breakpoint { reason, breakpoint }) => { - match &reason[..] { - "new" => { - if let Some(source) = breakpoint.source { - self.breakpoints - .entry(source.path.unwrap()) // TODO: no unwraps - .or_default() - .push(Breakpoint { - id: breakpoint.id, - verified: breakpoint.verified, - message: breakpoint.message, - line: breakpoint.line.unwrap().saturating_sub(1), // TODO: no unwrap - column: breakpoint.column, - ..Default::default() - }); + Event::Breakpoint(events::BreakpointBody { reason, breakpoint }) => { + match &reason[..] { + "new" => { + if let Some(source) = breakpoint.source { + self.breakpoints + .entry(source.path.unwrap()) // TODO: no unwraps + .or_default() + .push(Breakpoint { + id: breakpoint.id, + verified: breakpoint.verified, + message: breakpoint.message, + line: breakpoint.line.unwrap().saturating_sub(1), // TODO: no unwrap + column: breakpoint.column, + ..Default::default() + }); + } } - } - "changed" => { - for breakpoints in self.breakpoints.values_mut() { - if let Some(i) = - breakpoints.iter().position(|b| b.id == breakpoint.id) - { - breakpoints[i].verified = breakpoint.verified; - breakpoints[i].message = breakpoint - .message - .clone() - .or_else(|| breakpoints[i].message.take()); - breakpoints[i].line = breakpoint - .line - .map_or(breakpoints[i].line, |line| line.saturating_sub(1)); - breakpoints[i].column = - breakpoint.column.or(breakpoints[i].column); + "changed" => { + for breakpoints in self.breakpoints.values_mut() { + if let Some(i) = + breakpoints.iter().position(|b| b.id == breakpoint.id) + { + breakpoints[i].verified = breakpoint.verified; + breakpoints[i].message = breakpoint + .message + .clone() + .or_else(|| breakpoints[i].message.take()); + breakpoints[i].line = + breakpoint.line.map_or(breakpoints[i].line, |line| { + line.saturating_sub(1) + }); + breakpoints[i].column = + breakpoint.column.or(breakpoints[i].column); + } } } - } - "removed" => { - for breakpoints in self.breakpoints.values_mut() { - if let Some(i) = - breakpoints.iter().position(|b| b.id == breakpoint.id) - { - breakpoints.remove(i); + "removed" => { + for breakpoints in self.breakpoints.values_mut() { + if let Some(i) = + breakpoints.iter().position(|b| b.id == breakpoint.id) + { + breakpoints.remove(i); + } } } - } - reason => { - warn!("Unknown breakpoint event: {}", reason); + reason => { + warn!("Unknown breakpoint event: {}", reason); + } } } - } - Event::Output(events::Output { - category, output, .. - }) => { - let prefix = match category { - Some(category) => { - if &category == "telemetry" { - return false; + Event::Output(events::OutputBody { + category, output, .. + }) => { + let prefix = match category { + Some(category) => { + if &category == "telemetry" { + return false; + } + format!("Debug ({}):", category) } - format!("Debug ({}):", category) - } - None => "Debug:".to_owned(), - }; + None => "Debug:".to_owned(), + }; - log::info!("{}", output); - self.set_status(format!("{} {}", prefix, output)); - } - Event::Initialized(_) => { - // send existing breakpoints - for (path, breakpoints) in &mut self.breakpoints { - // TODO: call futures in parallel, await all - let _ = breakpoints_changed(debugger, path.clone(), breakpoints); + log::info!("{}", output); + self.set_status(format!("{} {}", prefix, output)); } - // TODO: fetch breakpoints (in case we're attaching) - - if debugger.configuration_done().await.is_ok() { - self.set_status("Debugged application started"); - }; // TODO: do we need to handle error? - } - Event::Terminated(terminated) => { - let restart_args = if let Some(terminated) = terminated { - terminated.restart - } else { - None - }; - - let disconnect_args = Some(DisconnectArguments { - restart: Some(restart_args.is_some()), - terminate_debuggee: None, - suspend_debuggee: None, - }); + Event::Initialized(_) => { + // send existing breakpoints + for (path, breakpoints) in &mut self.breakpoints { + // TODO: call futures in parallel, await all + let _ = breakpoints_changed(debugger, path.clone(), breakpoints); + } + // TODO: fetch breakpoints (in case we're attaching) - if let Err(err) = debugger.disconnect(disconnect_args).await { - self.set_error(format!( - "Cannot disconnect debugger upon terminated event receival {:?}", - err - )); - return false; + if debugger.configuration_done().await.is_ok() { + self.set_status("Debugged application started"); + }; // TODO: do we need to handle error? } + Event::Terminated(terminated) => { + let restart_args = if let Some(terminated) = terminated { + terminated.restart + } else { + None + }; + + let disconnect_args = Some(DisconnectArguments { + restart: Some(restart_args.is_some()), + terminate_debuggee: None, + suspend_debuggee: None, + }); + + if let Err(err) = debugger.disconnect(disconnect_args).await { + self.set_error(format!( + "Cannot disconnect debugger upon terminated event receival {:?}", + err + )); + return false; + } - match restart_args { - Some(restart_args) => { - log::info!("Attempting to restart debug session."); - let connection_type = match debugger.connection_type() { - Some(connection_type) => connection_type, - None => { - self.set_error("No starting request found, to be used in restarting the debugging session."); - return false; + match restart_args { + Some(restart_args) => { + log::info!("Attempting to restart debug session."); + let connection_type = match debugger.connection_type() { + Some(connection_type) => connection_type, + None => { + self.set_error("No starting request found, to be used in restarting the debugging session."); + return false; + } + }; + + let relaunch_resp = if let ConnectionType::Launch = connection_type + { + debugger.launch(restart_args).await + } else { + debugger.attach(restart_args).await + }; + + if let Err(err) = relaunch_resp { + self.set_error(format!( + "Failed to restart debugging session: {:?}", + err + )); } - }; - - let relaunch_resp = if let ConnectionType::Launch = connection_type { - debugger.launch(restart_args).await - } else { - debugger.attach(restart_args).await - }; - - if let Err(err) = relaunch_resp { - self.set_error(format!( - "Failed to restart debugging session: {:?}", - err - )); + } + None => { + self.debugger = None; + self.set_status( + "Terminated debugging session and disconnected debugger.", + ); } } - None => { - self.debugger = None; - self.set_status( - "Terminated debugging session and disconnected debugger.", - ); + } + Event::Exited(resp) => { + let exit_code = resp.exit_code; + if exit_code != 0 { + self.set_error(format!( + "Debuggee failed to exit successfully (exit code: {exit_code})." + )); } } - } - Event::Exited(resp) => { - let exit_code = resp.exit_code; - if exit_code != 0 { - self.set_error(format!( - "Debuggee failed to exit successfully (exit code: {exit_code})." - )); + ev => { + log::warn!("Unhandled event {:?}", ev); + return false; // return early to skip render } } - ev => { - log::warn!("Unhandled event {:?}", ev); - return false; // return early to skip render - } - }, + } Payload::Response(_) => unreachable!(), - Payload::Request(request) => match request.command.as_str() { - RunInTerminal::COMMAND => { - let arguments: dap::requests::RunInTerminalArguments = - serde_json::from_value(request.arguments.unwrap_or_default()).unwrap(); - // TODO: no unwrap - - let config = match self.config().terminal.clone() { - Some(config) => config, - None => { + Payload::Request(request) => { + let reply = match Request::parse(&request.command, request.arguments) { + Ok(Request::RunInTerminal(arguments)) => { + let config = self.config(); + let Some(config) = config.terminal.as_ref() else { self.set_error("No external terminal defined"); return true; - } - }; - - // Re-borrowing debugger to avoid issues when loading config - let debugger = match self.debugger.as_mut() { - Some(debugger) => debugger, - None => return false, - }; + }; + + let process = match std::process::Command::new(&config.command) + .args(&config.args) + .arg(arguments.args.join(" ")) + .spawn() + { + Ok(process) => process, + Err(err) => { + self.set_error(format!( + "Error starting external terminal: {}", + err + )); + return true; + } + }; - let process = match std::process::Command::new(config.command) - .args(config.args) - .arg(arguments.args.join(" ")) - .spawn() - { - Ok(process) => process, - Err(err) => { - self.set_error(format!("Error starting external terminal: {}", err)); - return true; - } - }; + Ok(json!(dap::requests::RunInTerminalResponse { + process_id: Some(process.id()), + shell_process_id: None, + })) + } + Err(err) => Err(err), + }; - let _ = debugger - .reply( - request.seq, - dap::requests::RunInTerminal::COMMAND, - serde_json::to_value(dap::requests::RunInTerminalResponse { - process_id: Some(process.id()), - shell_process_id: None, - }) - .map_err(|e| e.into()), - ) - .await; + if let Some(debugger) = self.debugger.as_mut() { + debugger + .reply(request.seq, &request.command, reply) + .await + .ok(); } - _ => log::error!("DAP reverse request not implemented: {:?}", request), - }, + } } true } |