dap: Reply to RunInTerminal

pull/574/head
Blaž Hrastnik 3 years ago
parent d5d1a9b1ae
commit 2b4de41bf0

@ -262,10 +262,11 @@ impl Client {
pub fn reply( pub fn reply(
&self, &self,
request_seq: u64, request_seq: u64,
command: String, command: &str,
result: core::result::Result<Value, Error>, result: core::result::Result<Value, Error>,
) -> impl Future<Output = Result<()>> { ) -> impl Future<Output = Result<()>> {
let server_tx = self.server_tx.clone(); let server_tx = self.server_tx.clone();
let command = command.to_string();
async move { async move {
let response = match result { let response = match result {

@ -545,11 +545,11 @@ pub mod requests {
// Reverse Requests // Reverse Requests
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct RunInTerminalResponse { pub struct RunInTerminalResponse {
pub process_id: Option<usize>, pub process_id: Option<u32>,
pub shell_process_id: Option<usize>, pub shell_process_id: Option<u32>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
@ -557,7 +557,7 @@ pub mod requests {
pub struct RunInTerminalArguments { pub struct RunInTerminalArguments {
pub kind: Option<String>, pub kind: Option<String>,
pub title: Option<String>, pub title: Option<String>,
pub cwd: String, pub cwd: Option<String>,
pub args: Vec<String>, pub args: Vec<String>,
pub env: Option<HashMap<String, Option<String>>>, pub env: Option<HashMap<String, Option<String>>>,
} }

@ -320,161 +320,177 @@ impl Application {
pub async fn handle_debugger_message(&mut self, payload: helix_dap::Payload) { pub async fn handle_debugger_message(&mut self, payload: helix_dap::Payload) {
use crate::commands::dap::{breakpoints_changed, resume_application, select_thread_id}; use crate::commands::dap::{breakpoints_changed, resume_application, select_thread_id};
use dap::requests::RunInTerminal;
use helix_dap::{events, Event}; use helix_dap::{events, Event};
let debugger = match self.editor.debugger.as_mut() {
Some(debugger) => debugger,
None => return,
};
match payload { match payload {
Payload::Event(ev) => { Payload::Event(ev) => match ev {
let debugger = match self.editor.debugger.as_mut() { Event::Stopped(events::Stopped {
Some(debugger) => debugger, thread_id,
None => return, description,
}; text,
match ev { reason,
Event::Stopped(events::Stopped { all_threads_stopped,
thread_id, ..
description, }) => {
text, let all_threads_stopped = all_threads_stopped.unwrap_or_default();
reason,
all_threads_stopped, if all_threads_stopped {
.. if let Ok(threads) = debugger.threads().await {
}) => { for thread in threads {
let all_threads_stopped = all_threads_stopped.unwrap_or_default(); fetch_stack_trace(debugger, thread.id).await;
if all_threads_stopped {
if let Ok(threads) = debugger.threads().await {
for thread in threads {
fetch_stack_trace(debugger, thread.id).await;
}
select_thread_id(
&mut self.editor,
thread_id.unwrap_or_default(),
false,
)
.await;
} }
} else if let Some(thread_id) = thread_id { select_thread_id(
debugger.thread_states.insert(thread_id, reason.clone()); // TODO: dap uses "type" || "reason" here &mut self.editor,
thread_id.unwrap_or_default(),
// whichever thread stops is made "current" (if no previously selected thread). false,
select_thread_id(&mut self.editor, thread_id, false).await; )
.await;
} }
} else if let Some(thread_id) = thread_id {
debugger.thread_states.insert(thread_id, reason.clone()); // TODO: dap uses "type" || "reason" here
let scope = match thread_id { // whichever thread stops is made "current" (if no previously selected thread).
Some(id) => format!("Thread {}", id), select_thread_id(&mut self.editor, thread_id, false).await;
None => "Target".to_owned(), }
};
let mut status = format!("{} stopped because of {}", scope, reason); let scope = match thread_id {
if let Some(desc) = description { Some(id) => format!("Thread {}", id),
status.push_str(&format!(" {}", desc)); None => "Target".to_owned(),
} };
if let Some(text) = text {
status.push_str(&format!(" {}", text));
}
if all_threads_stopped {
status.push_str(" (all threads stopped)");
}
self.editor.set_status(status); let mut status = format!("{} stopped because of {}", scope, reason);
if let Some(desc) = description {
status.push_str(&format!(" {}", desc));
} }
Event::Continued(events::Continued { thread_id, .. }) => { if let Some(text) = text {
debugger status.push_str(&format!(" {}", text));
.thread_states
.insert(thread_id, "running".to_owned());
if debugger.thread_id == Some(thread_id) {
resume_application(debugger)
}
} }
Event::Thread(_) => { if all_threads_stopped {
// TODO: update thread_states, make threads request status.push_str(" (all threads stopped)");
} }
Event::Breakpoint(events::Breakpoint { reason, breakpoint }) => {
match &reason[..] { self.editor.set_status(status);
"new" => { }
if let Some(source) = breakpoint.source { Event::Continued(events::Continued { thread_id, .. }) => {
self.editor debugger
.breakpoints .thread_states
.entry(source.path.unwrap()) // TODO: no unwraps .insert(thread_id, "running".to_owned());
.or_default() if debugger.thread_id == Some(thread_id) {
.push(Breakpoint { resume_application(debugger)
id: breakpoint.id, }
verified: breakpoint.verified, }
message: breakpoint.message, Event::Thread(_) => {
line: breakpoint.line.unwrap().saturating_sub(1), // TODO: no unwrap // TODO: update thread_states, make threads request
column: breakpoint.column, }
..Default::default() Event::Breakpoint(events::Breakpoint { reason, breakpoint }) => {
}); match &reason[..] {
} "new" => {
if let Some(source) = breakpoint.source {
self.editor
.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.editor.breakpoints.values_mut() { "changed" => {
if let Some(i) = for breakpoints in self.editor.breakpoints.values_mut() {
breakpoints.iter().position(|b| b.id == breakpoint.id) if let Some(i) =
{ breakpoints.iter().position(|b| b.id == breakpoint.id)
breakpoints[i].verified = breakpoint.verified; {
breakpoints[i].message = breakpoint.message.clone(); breakpoints[i].verified = breakpoint.verified;
breakpoints[i].line = breakpoints[i].message = breakpoint.message.clone();
breakpoint.line.unwrap().saturating_sub(1); // TODO: no unwrap breakpoints[i].line =
breakpoints[i].column = breakpoint.column; breakpoint.line.unwrap().saturating_sub(1); // TODO: no unwrap
} breakpoints[i].column = breakpoint.column;
} }
} }
"removed" => { }
for breakpoints in self.editor.breakpoints.values_mut() { "removed" => {
if let Some(i) = for breakpoints in self.editor.breakpoints.values_mut() {
breakpoints.iter().position(|b| b.id == breakpoint.id) if let Some(i) =
{ breakpoints.iter().position(|b| b.id == breakpoint.id)
breakpoints.remove(i); {
} breakpoints.remove(i);
} }
} }
reason => { }
warn!("Unknown breakpoint event: {}", reason); reason => {
} warn!("Unknown breakpoint event: {}", reason);
} }
} }
Event::Output(events::Output { }
category, output, .. Event::Output(events::Output {
}) => { category, output, ..
let prefix = match category { }) => {
Some(category) => { let prefix = match category {
if &category == "telemetry" { Some(category) => {
return; if &category == "telemetry" {
} return;
format!("Debug ({}):", category)
} }
None => "Debug:".to_owned(), format!("Debug ({}):", category)
};
log::info!("{}", output);
self.editor.set_status(format!("{} {}", prefix, output));
}
Event::Initialized => {
// send existing breakpoints
for (path, breakpoints) in &mut self.editor.breakpoints {
// TODO: call futures in parallel, await all
let _ = breakpoints_changed(debugger, path.clone(), breakpoints);
} }
// TODO: fetch breakpoints (in case we're attaching) None => "Debug:".to_owned(),
};
if debugger.configuration_done().await.is_ok() { log::info!("{}", output);
self.editor self.editor.set_status(format!("{} {}", prefix, output));
.set_status("Debugged application started".to_owned()); }
}; // TODO: do we need to handle error? Event::Initialized => {
} // send existing breakpoints
ev => { for (path, breakpoints) in &mut self.editor.breakpoints {
log::warn!("Unhandled event {:?}", ev); // TODO: call futures in parallel, await all
return; // return early to skip render let _ = breakpoints_changed(debugger, path.clone(), breakpoints);
} }
// TODO: fetch breakpoints (in case we're attaching)
if debugger.configuration_done().await.is_ok() {
self.editor
.set_status("Debugged application started".to_owned());
}; // TODO: do we need to handle error?
} }
} ev => {
log::warn!("Unhandled event {:?}", ev);
return; // return early to skip render
}
},
Payload::Response(_) => unreachable!(), Payload::Response(_) => unreachable!(),
Payload::Request(request) => match request.command.as_str() { Payload::Request(request) => match request.command.as_str() {
dap::requests::RunInTerminal::COMMAND => { RunInTerminal::COMMAND => {
let arguments: dap::requests::RunInTerminalArguments = let arguments: dap::requests::RunInTerminalArguments =
serde_json::from_value(request.arguments.unwrap_or_default()).unwrap(); serde_json::from_value(request.arguments.unwrap_or_default()).unwrap();
// TODO: no unwrap // TODO: no unwrap
// TODO: dap reply // TODO: handle cwd
let process = std::process::Command::new("tmux")
.arg("split-window")
.arg(arguments.args.join(" ")) // TODO: first arg is wrong, it uses current dir
.spawn()
.unwrap();
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;
} }
_ => log::error!("DAP reverse request not implemented: {:?}", request), _ => log::error!("DAP reverse request not implemented: {:?}", request),
}, },

Loading…
Cancel
Save