Split off dap event handlers into helix-view to allow reuse

This commit is contained in:
Blaž Hrastnik 2022-03-22 12:53:44 +09:00
parent 85264a861a
commit 9a6ee88e66
No known key found for this signature in database
GPG Key ID: 1238B9C4AD889640
8 changed files with 352 additions and 344 deletions

1
Cargo.lock generated
View File

@ -497,6 +497,7 @@ dependencies = [
"log",
"once_cell",
"serde",
"serde_json",
"slotmap",
"tokio",
"tokio-stream",

View File

@ -3,17 +3,13 @@
config::{default_syntax_loader, user_syntax_loader},
pos_at_coords, syntax, Selection,
};
use helix_dap::{self as dap, Payload, Request};
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
use helix_view::{
editor::{Breakpoint, ConfigEvent},
theme, Editor,
};
use helix_view::{align_view, editor::ConfigEvent, theme, Align, Editor};
use serde_json::json;
use crate::{
args::Args,
commands::{align_view, apply_workspace_edit, fetch_stack_trace, Align},
commands::apply_workspace_edit,
compositor::Compositor,
config::Config,
job::Jobs,
@ -233,7 +229,10 @@ pub async fn event_loop(&mut self) {
}
}
Some(payload) = self.editor.debugger_events.next() => {
self.handle_debugger_message(payload).await;
let needs_render = self.editor.handle_debugger_message(payload).await;
if needs_render {
self.render();
}
}
Some(config_event) = self.editor.config_events.1.recv() => {
self.handle_config_events(config_event);
@ -368,184 +367,6 @@ pub fn handle_terminal_events(&mut self, event: Option<Result<Event, crossterm::
}
}
pub async fn handle_debugger_message(&mut self, payload: helix_dap::Payload) {
use crate::commands::dap::{breakpoints_changed, select_thread_id};
use dap::requests::RunInTerminal;
use helix_dap::{events, Event};
let debugger = match self.editor.debugger.as_mut() {
Some(debugger) => debugger,
None => return,
};
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;
}
select_thread_id(
&mut self.editor,
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(&mut self.editor, thread_id, false).await;
}
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 {
status.push_str(&format!(" {}", desc));
}
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);
}
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::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() {
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].line =
breakpoint.line.unwrap().saturating_sub(1); // TODO: no unwrap
breakpoints[i].column = breakpoint.column;
}
}
}
"removed" => {
for breakpoints in self.editor.breakpoints.values_mut() {
if let Some(i) =
breakpoints.iter().position(|b| b.id == breakpoint.id)
{
breakpoints.remove(i);
}
}
}
reason => {
warn!("Unknown breakpoint event: {}", reason);
}
}
}
Event::Output(events::Output {
category, output, ..
}) => {
let prefix = match category {
Some(category) => {
if &category == "telemetry" {
return;
}
format!("Debug ({}):", category)
}
None => "Debug:".to_owned(),
};
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)
if debugger.configuration_done().await.is_ok() {
self.editor.set_status("Debugged application started");
}; // TODO: do we need to handle error?
}
ev => {
log::warn!("Unhandled event {:?}", ev);
return; // 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 process = std::process::Command::new("tmux")
.arg("split-window")
.arg(arguments.args.join(" "))
.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),
},
}
self.render();
}
pub async fn handle_language_server_message(
&mut self,
call: helix_lsp::Call,

View File

@ -118,29 +118,7 @@ pub fn count(&self) -> usize {
}
}
pub enum Align {
Top,
Center,
Bottom,
}
pub fn align_view(doc: &Document, view: &mut View, align: Align) {
let pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let line = doc.text().char_to_line(pos);
let height = view.inner_area().height as usize;
let relative = match align {
Align::Center => height / 2,
Align::Top => 0,
Align::Bottom => height,
};
view.offset.row = line.saturating_sub(relative);
}
use helix_view::{align_view, Align};
/// A MappableCommand is either a static command like "jump_view_up" or a Typable command like
/// :format. It causes a side-effect on the state (usually by creating and applying a transaction).

View File

@ -1,14 +1,11 @@
use super::{align_view, Align, Context, Editor};
use super::{Context, Editor};
use crate::{
compositor::{self, Compositor},
job::{Callback, Jobs},
ui::{self, overlay::overlayed, FilePicker, Picker, Popup, Prompt, PromptEvent, Text},
};
use helix_core::{
syntax::{DebugArgumentValue, DebugConfigCompletion},
Selection,
};
use helix_dap::{self as dap, Client, ThreadId};
use helix_core::syntax::{DebugArgumentValue, DebugConfigCompletion};
use helix_dap::{self as dap, Client};
use helix_lsp::block_on;
use helix_view::editor::Breakpoint;
@ -21,79 +18,7 @@
use anyhow::{anyhow, bail};
#[macro_export]
macro_rules! debugger {
($editor:expr) => {{
match &mut $editor.debugger {
Some(debugger) => debugger,
None => return,
}
}};
}
// general utils:
pub fn dap_pos_to_pos(doc: &helix_core::Rope, line: usize, column: usize) -> Option<usize> {
// 1-indexing to 0 indexing
let line = doc.try_line_to_char(line - 1).ok()?;
let pos = line + column.saturating_sub(1);
// TODO: this is probably utf-16 offsets
Some(pos)
}
pub async fn select_thread_id(editor: &mut Editor, thread_id: ThreadId, force: bool) {
let debugger = debugger!(editor);
if !force && debugger.thread_id.is_some() {
return;
}
debugger.thread_id = Some(thread_id);
fetch_stack_trace(debugger, thread_id).await;
let frame = debugger.stack_frames[&thread_id].get(0).cloned();
if let Some(frame) = &frame {
jump_to_stack_frame(editor, frame);
}
}
pub async fn fetch_stack_trace(debugger: &mut Client, thread_id: ThreadId) {
let (frames, _) = match debugger.stack_trace(thread_id).await {
Ok(frames) => frames,
Err(_) => return,
};
debugger.stack_frames.insert(thread_id, frames);
debugger.active_frame = Some(0);
}
pub fn jump_to_stack_frame(editor: &mut Editor, frame: &helix_dap::StackFrame) {
let path = if let Some(helix_dap::Source {
path: Some(ref path),
..
}) = frame.source
{
path.clone()
} else {
return;
};
if let Err(e) = editor.open(path, helix_view::editor::Action::Replace) {
editor.set_error(format!("Unable to jump to stack frame: {}", e));
return;
}
let (view, doc) = current!(editor);
let text_end = doc.text().len_chars().saturating_sub(1);
let start = dap_pos_to_pos(doc.text(), frame.line, frame.column).unwrap_or(0);
let end = frame
.end_line
.and_then(|end_line| dap_pos_to_pos(doc.text(), end_line, frame.end_column.unwrap_or(0)))
.unwrap_or(start);
let selection = Selection::single(start.min(text_end), end.min(text_end));
doc.set_selection(view.id, selection);
align_view(doc, view, Align::Center);
}
use helix_view::handlers::dap::{breakpoints_changed, jump_to_stack_frame, select_thread_id};
fn thread_picker(
cx: &mut Context,
@ -419,63 +344,6 @@ pub fn dap_toggle_breakpoint(cx: &mut Context) {
dap_toggle_breakpoint_impl(cx, path, line);
}
pub fn breakpoints_changed(
debugger: &mut dap::Client,
path: PathBuf,
breakpoints: &mut [Breakpoint],
) -> Result<(), anyhow::Error> {
// TODO: handle capabilities correctly again, by filterin breakpoints when emitting
// if breakpoint.condition.is_some()
// && !debugger
// .caps
// .as_ref()
// .unwrap()
// .supports_conditional_breakpoints
// .unwrap_or_default()
// {
// bail!(
// "Can't edit breakpoint: debugger does not support conditional breakpoints"
// )
// }
// if breakpoint.log_message.is_some()
// && !debugger
// .caps
// .as_ref()
// .unwrap()
// .supports_log_points
// .unwrap_or_default()
// {
// bail!("Can't edit breakpoint: debugger does not support logpoints")
// }
let source_breakpoints = breakpoints
.iter()
.map(|breakpoint| helix_dap::SourceBreakpoint {
line: breakpoint.line + 1, // convert from 0-indexing to 1-indexing (TODO: could set debugger to 0-indexing on init)
..Default::default()
})
.collect::<Vec<_>>();
let request = debugger.set_breakpoints(path, source_breakpoints);
match block_on(request) {
Ok(Some(dap_breakpoints)) => {
for (breakpoint, dap_breakpoint) in breakpoints.iter_mut().zip(dap_breakpoints) {
breakpoint.id = dap_breakpoint.id;
breakpoint.verified = dap_breakpoint.verified;
breakpoint.message = dap_breakpoint.message;
// TODO: handle breakpoint.message
// TODO: verify source matches
breakpoint.line = dap_breakpoint.line.unwrap_or(0).saturating_sub(1); // convert to 0-indexing
// TODO: no unwrap
breakpoint.column = dap_breakpoint.column;
// TODO: verify end_linef/col instruction reference, offset
}
}
Err(e) => anyhow::bail!("Failed to set breakpoints: {}", e),
_ => {}
};
Ok(())
}
pub fn dap_toggle_breakpoint_impl(cx: &mut Context, path: PathBuf, line: usize) {
// TODO: need to map breakpoints over edits and update them?
// we shouldn't really allow editing while debug is running though

View File

@ -36,6 +36,7 @@ slotmap = "1"
chardetng = "0.1"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
toml = "0.5"
log = "~0.4"

View File

@ -0,0 +1,311 @@
use crate::editor::{Action, Breakpoint};
use crate::{align_view, Align, Editor};
use helix_core::Selection;
use helix_dap::{self as dap, Client, Payload, Request, ThreadId};
use helix_lsp::block_on;
use log::warn;
use std::path::PathBuf;
#[macro_export]
macro_rules! debugger {
($editor:expr) => {{
match &mut $editor.debugger {
Some(debugger) => debugger,
None => return,
}
}};
}
// general utils:
pub fn dap_pos_to_pos(doc: &helix_core::Rope, line: usize, column: usize) -> Option<usize> {
// 1-indexing to 0 indexing
let line = doc.try_line_to_char(line - 1).ok()?;
let pos = line + column.saturating_sub(1);
// TODO: this is probably utf-16 offsets
Some(pos)
}
pub async fn select_thread_id(editor: &mut Editor, thread_id: ThreadId, force: bool) {
let debugger = debugger!(editor);
if !force && debugger.thread_id.is_some() {
return;
}
debugger.thread_id = Some(thread_id);
fetch_stack_trace(debugger, thread_id).await;
let frame = debugger.stack_frames[&thread_id].get(0).cloned();
if let Some(frame) = &frame {
jump_to_stack_frame(editor, frame);
}
}
pub async fn fetch_stack_trace(debugger: &mut Client, thread_id: ThreadId) {
let (frames, _) = match debugger.stack_trace(thread_id).await {
Ok(frames) => frames,
Err(_) => return,
};
debugger.stack_frames.insert(thread_id, frames);
debugger.active_frame = Some(0);
}
pub fn jump_to_stack_frame(editor: &mut Editor, frame: &helix_dap::StackFrame) {
let path = if let Some(helix_dap::Source {
path: Some(ref path),
..
}) = frame.source
{
path.clone()
} else {
return;
};
if let Err(e) = editor.open(path, Action::Replace) {
editor.set_error(format!("Unable to jump to stack frame: {}", e));
return;
}
let (view, doc) = current!(editor);
let text_end = doc.text().len_chars().saturating_sub(1);
let start = dap_pos_to_pos(doc.text(), frame.line, frame.column).unwrap_or(0);
let end = frame
.end_line
.and_then(|end_line| dap_pos_to_pos(doc.text(), end_line, frame.end_column.unwrap_or(0)))
.unwrap_or(start);
let selection = Selection::single(start.min(text_end), end.min(text_end));
doc.set_selection(view.id, selection);
align_view(doc, view, Align::Center);
}
pub fn breakpoints_changed(
debugger: &mut dap::Client,
path: PathBuf,
breakpoints: &mut [Breakpoint],
) -> Result<(), anyhow::Error> {
// TODO: handle capabilities correctly again, by filterin breakpoints when emitting
// if breakpoint.condition.is_some()
// && !debugger
// .caps
// .as_ref()
// .unwrap()
// .supports_conditional_breakpoints
// .unwrap_or_default()
// {
// bail!(
// "Can't edit breakpoint: debugger does not support conditional breakpoints"
// )
// }
// if breakpoint.log_message.is_some()
// && !debugger
// .caps
// .as_ref()
// .unwrap()
// .supports_log_points
// .unwrap_or_default()
// {
// bail!("Can't edit breakpoint: debugger does not support logpoints")
// }
let source_breakpoints = breakpoints
.iter()
.map(|breakpoint| helix_dap::SourceBreakpoint {
line: breakpoint.line + 1, // convert from 0-indexing to 1-indexing (TODO: could set debugger to 0-indexing on init)
..Default::default()
})
.collect::<Vec<_>>();
let request = debugger.set_breakpoints(path, source_breakpoints);
match block_on(request) {
Ok(Some(dap_breakpoints)) => {
for (breakpoint, dap_breakpoint) in breakpoints.iter_mut().zip(dap_breakpoints) {
breakpoint.id = dap_breakpoint.id;
breakpoint.verified = dap_breakpoint.verified;
breakpoint.message = dap_breakpoint.message;
// TODO: handle breakpoint.message
// TODO: verify source matches
breakpoint.line = dap_breakpoint.line.unwrap_or(0).saturating_sub(1); // convert to 0-indexing
// TODO: no unwrap
breakpoint.column = dap_breakpoint.column;
// TODO: verify end_linef/col instruction reference, offset
}
}
Err(e) => anyhow::bail!("Failed to set breakpoints: {}", e),
_ => {}
};
Ok(())
}
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() {
Some(debugger) => debugger,
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;
}
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;
}
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 {
status.push_str(&format!(" {}", desc));
}
if let Some(text) = text {
status.push_str(&format!(" {}", text));
}
if all_threads_stopped {
status.push_str(" (all threads stopped)");
}
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::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()
});
}
}
"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();
breakpoints[i].line =
breakpoint.line.unwrap().saturating_sub(1); // TODO: no unwrap
breakpoints[i].column = breakpoint.column;
}
}
}
"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);
}
}
}
Event::Output(events::Output {
category, output, ..
}) => {
let prefix = match category {
Some(category) => {
if &category == "telemetry" {
return false;
}
format!("Debug ({}):", category)
}
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);
}
// 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?
}
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 process = std::process::Command::new("tmux")
.arg("split-window")
.arg(arguments.args.join(" "))
.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),
},
}
true
}
}

View File

View File

@ -6,6 +6,10 @@
pub mod editor;
pub mod graphics;
pub mod gutter;
pub mod handlers {
pub mod dap;
pub mod lsp;
}
pub mod info;
pub mod input;
pub mod keyboard;
@ -36,6 +40,30 @@ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
pub struct ViewId;
}
pub enum Align {
Top,
Center,
Bottom,
}
pub fn align_view(doc: &Document, view: &mut View, align: Align) {
let pos = doc
.selection(view.id)
.primary()
.cursor(doc.text().slice(..));
let line = doc.text().char_to_line(pos);
let height = view.inner_area().height as usize;
let relative = match align {
Align::Center => height / 2,
Align::Top => 0,
Align::Bottom => height,
};
view.offset.row = line.saturating_sub(relative);
}
pub use document::Document;
pub use editor::Editor;
pub use theme::Theme;