From f247858055ee17e6d4a61d7284548febe1d7f54d Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Sun, 22 Aug 2021 15:06:27 +0300 Subject: Support conditional breakpoints --- helix-term/src/commands.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++ helix-term/src/ui/editor.rs | 9 +++++-- 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index cc5ce67c..49d6bf3f 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1924,6 +1924,57 @@ mod cmd { Ok(()) } + fn debug_breakpoint_condition( + cx: &mut compositor::Context, + args: &[&str], + _event: PromptEvent, + ) -> anyhow::Result<()> { + use helix_lsp::block_on; + + let condition = args.join(" "); + let condition = if condition.len() > 0 { + Some(condition) + } else { + None + }; + + let (view, doc) = current!(cx.editor); + let text = doc.text().slice(..); + let pos = doc.selection(view.id).primary().cursor(text); + + let breakpoint = helix_dap::SourceBreakpoint { + line: text.char_to_line(pos) + 1, // convert from 0-indexing to 1-indexing (TODO: could set debugger to 0-indexing on init) + condition, + ..Default::default() + }; + + let path = match doc.path() { + Some(path) => path.to_path_buf(), + None => { + cx.editor + .set_error("Can't edit breakpoint: document has no path".to_string()); + return Ok(()); + } + }; + + // TODO: need to map breakpoints over edits and update them? + // we shouldn't really allow editing while debug is running though + + if let Some(debugger) = &mut cx.editor.debugger { + let breakpoints = debugger.breakpoints.entry(path.clone()).or_default(); + if let Some(pos) = breakpoints.iter().position(|b| b.line == breakpoint.line) { + breakpoints.remove(pos); + breakpoints.push(breakpoint); + + let breakpoints = breakpoints.clone(); + + let request = debugger.set_breakpoints(path, breakpoints); + let _ = block_on(request).unwrap(); + } + } + Ok(()) + } + pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ TypableCommand { name: "quit", @@ -2169,6 +2220,13 @@ mod cmd { doc: "Evaluate expression in current debug context.", fun: debug_eval, completer: None, + }, + TypableCommand { + name: "debug-breakpoint-condition", + alias: None, + doc: "Set current breakpoint condition.", + fun: debug_breakpoint_condition, + completer: None, } ]; diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs index f8035ae4..8a6f63f9 100644 --- a/helix-term/src/ui/editor.rs +++ b/helix-term/src/ui/editor.rs @@ -472,8 +472,13 @@ impl EditorView { let selected = cursors.contains(&line); if let Some(bps) = breakpoints.as_ref() { - if bps.iter().any(|breakpoint| breakpoint.line - 1 == line) { - surface.set_stringn(viewport.x, viewport.y + i as u16, "▲", 1, warning); + if let Some(breakpoint) = bps.iter().find(|breakpoint| breakpoint.line - 1 == line) + { + if breakpoint.condition.is_some() { + surface.set_stringn(viewport.x, viewport.y + i as u16, "▲", 1, error); + } else { + surface.set_stringn(viewport.x, viewport.y + i as u16, "▲", 1, warning); + } } } -- cgit v1.2.3-70-g09d2