on master help sign in
dap: Toggle breakpoints without changing selection, fix offset calc
commit85b4410703fdcf414502daa974061216c64115e8 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 22 Nov 2021, 16:30 (5 years ago) parent177b6fc
2 files changed+30−32
helix-term/src/commands/dap.rs+14−19 helix-term/src/ui/editor.rs+16−13
helix-term/src/commands/dap.rs+14−19
@@ -392,22 +392,25 @@ fn debug_parameter_prompt(
392392pub fn dap_toggle_breakpoint(cx: &mut Context) {
393393 // TODO: accept line instead of current selection
394394 let (view, doc) = current!(cx.editor);
395− let text = doc.text().slice(..);
396− let pos = doc.selection(view.id).primary().cursor(text);
397−
398− let breakpoint = helix_dap::SourceBreakpoint {
399− line: text.char_to_line(pos) + 1, // convert from 0-indexing to 1-indexing (TODO: could set debugger to 0-indexing on init)
400− ..Default::default()
401− };
402−
403395 let path = match doc.path() {
404− Some(path) => path,
396+ Some(path) => path.clone(),
405397 None => {
406398 cx.editor
407399 .set_error("Can't set breakpoint: document has no path".to_string());
408400 return;
409401 }
410402 };
403+ let text = doc.text().slice(..);
404+ let pos = doc.selection(view.id).primary().cursor(text);
405+ let line = text.char_to_line(pos);
406+ dap_toggle_breakpoint_impl(cx, path, line);
407+}
408+
409+pub fn dap_toggle_breakpoint_impl(cx: &mut Context, path: std::path::PathBuf, line: usize) {
410+ let breakpoint = helix_dap::SourceBreakpoint {
411+ line: line + 1, // convert from 0-indexing to 1-indexing (TODO: could set debugger to 0-indexing on init)
412+ ..Default::default()
413+ };
411414
412415 // TODO: need to map breakpoints over edits and update them?
413416 // we shouldn't really allow editing while debug is running though
@@ -429,16 +432,8 @@ pub fn dap_toggle_breakpoint(cx: &mut Context) {
429432 let request = debugger.set_breakpoints(path.clone(), breakpoints);
430433 match block_on(request) {
431434 Ok(Some(breakpoints)) => {
432− // TODO: avoid this clone here
433− let old_breakpoints = std::mem::replace(&mut debugger.breakpoints, breakpoints.clone());
434− for bp in breakpoints {
435− if !old_breakpoints.iter().any(|b| b.message == bp.message) {
436− if let Some(msg) = &bp.message {
437− cx.editor.set_status(format!("Breakpoint set: {}", msg));
438− break;
439− }
440− }
441− }
435+ // TODO: handle breakpoint.message
436+ debugger.breakpoints = breakpoints;
442437 }
443438 Err(e) => cx
444439 .editor
helix-term/src/ui/editor.rs+16−13
@@ -985,14 +985,19 @@ impl EditorView {
985985 if let Some((coords, view_id)) = result {
986986 editor.tree.focus = view_id;
987987
988− let doc = editor
989− .documents
990− .get_mut(&editor.tree.get(view_id).doc)
991− .unwrap();
992− if let Ok(pos) = doc.text().try_line_to_char(coords.row) {
993− doc.set_selection(view_id, Selection::point(pos));
994− commands::dap_toggle_breakpoint(cxt);
988+ let view = editor.tree.get(view_id);
989+ let doc = editor.documents.get_mut(&view.doc).unwrap();
990+
991+ let path = match doc.path() {
992+ Some(path) => path.clone(),
993+ None => {
994+ return EventResult::Ignored;
995+ }
996+ };
995997
998+ let line = coords.row + view.offset.row;
999+ if line < doc.text().len_lines() {
1000+ commands::dap_toggle_breakpoint_impl(cxt, path, line);
9961001 return EventResult::Consumed(None);
9971002 }
9981003 }
@@ -1087,12 +1092,10 @@ impl EditorView {
10871092 if let Some((coords, view_id)) = result {
10881093 cxt.editor.tree.focus = view_id;
10891094
1090− let doc = cxt
1091− .editor
1092− .documents
1093− .get_mut(&cxt.editor.tree.get(view_id).doc)
1094− .unwrap();
1095− if let Ok(pos) = doc.text().try_line_to_char(coords.row) {
1095+ let view = cxt.editor.tree.get(view_id);
1096+ let doc = cxt.editor.documents.get_mut(&view.doc).unwrap();
1097+ let line = coords.row + view.offset.row;
1098+ if let Ok(pos) = doc.text().try_line_to_char(line) {
10961099 doc.set_selection(view_id, Selection::point(pos));
10971100 if modifiers == crossterm::event::KeyModifiers::ALT {
10981101 commands::Command::dap_edit_log.execute(cxt);