dap: Toggle breakpoints without changing selection, fix offset calc

pull/574/head
Blaž Hrastnik 3 years ago
parent 177b6fcdc9
commit 85b4410703

@ -392,22 +392,25 @@ fn debug_parameter_prompt(
pub fn dap_toggle_breakpoint(cx: &mut Context) { pub fn dap_toggle_breakpoint(cx: &mut Context) {
// TODO: accept line instead of current selection // TODO: accept line instead of current selection
let (view, doc) = current!(cx.editor); 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)
..Default::default()
};
let path = match doc.path() { let path = match doc.path() {
Some(path) => path, Some(path) => path.clone(),
None => { None => {
cx.editor cx.editor
.set_error("Can't set breakpoint: document has no path".to_string()); .set_error("Can't set breakpoint: document has no path".to_string());
return; return;
} }
}; };
let text = doc.text().slice(..);
let pos = doc.selection(view.id).primary().cursor(text);
let line = text.char_to_line(pos);
dap_toggle_breakpoint_impl(cx, path, line);
}
pub fn dap_toggle_breakpoint_impl(cx: &mut Context, path: std::path::PathBuf, line: usize) {
let breakpoint = helix_dap::SourceBreakpoint {
line: line + 1, // convert from 0-indexing to 1-indexing (TODO: could set debugger to 0-indexing on init)
..Default::default()
};
// TODO: need to map breakpoints over edits and update them? // TODO: need to map breakpoints over edits and update them?
// we shouldn't really allow editing while debug is running though // we shouldn't really allow editing while debug is running though
@ -429,16 +432,8 @@ pub fn dap_toggle_breakpoint(cx: &mut Context) {
let request = debugger.set_breakpoints(path.clone(), breakpoints); let request = debugger.set_breakpoints(path.clone(), breakpoints);
match block_on(request) { match block_on(request) {
Ok(Some(breakpoints)) => { Ok(Some(breakpoints)) => {
// TODO: avoid this clone here // TODO: handle breakpoint.message
let old_breakpoints = std::mem::replace(&mut debugger.breakpoints, breakpoints.clone()); debugger.breakpoints = breakpoints;
for bp in breakpoints {
if !old_breakpoints.iter().any(|b| b.message == bp.message) {
if let Some(msg) = &bp.message {
cx.editor.set_status(format!("Breakpoint set: {}", msg));
break;
}
}
}
} }
Err(e) => cx Err(e) => cx
.editor .editor

@ -985,14 +985,19 @@ impl EditorView {
if let Some((coords, view_id)) = result { if let Some((coords, view_id)) = result {
editor.tree.focus = view_id; editor.tree.focus = view_id;
let doc = editor let view = editor.tree.get(view_id);
.documents let doc = editor.documents.get_mut(&view.doc).unwrap();
.get_mut(&editor.tree.get(view_id).doc)
.unwrap(); let path = match doc.path() {
if let Ok(pos) = doc.text().try_line_to_char(coords.row) { Some(path) => path.clone(),
doc.set_selection(view_id, Selection::point(pos)); None => {
commands::dap_toggle_breakpoint(cxt); return EventResult::Ignored;
}
};
let line = coords.row + view.offset.row;
if line < doc.text().len_lines() {
commands::dap_toggle_breakpoint_impl(cxt, path, line);
return EventResult::Consumed(None); return EventResult::Consumed(None);
} }
} }
@ -1087,12 +1092,10 @@ impl EditorView {
if let Some((coords, view_id)) = result { if let Some((coords, view_id)) = result {
cxt.editor.tree.focus = view_id; cxt.editor.tree.focus = view_id;
let doc = cxt let view = cxt.editor.tree.get(view_id);
.editor let doc = cxt.editor.documents.get_mut(&view.doc).unwrap();
.documents let line = coords.row + view.offset.row;
.get_mut(&cxt.editor.tree.get(view_id).doc) if let Ok(pos) = doc.text().try_line_to_char(line) {
.unwrap();
if let Ok(pos) = doc.text().try_line_to_char(coords.row) {
doc.set_selection(view_id, Selection::point(pos)); doc.set_selection(view_id, Selection::point(pos));
if modifiers == crossterm::event::KeyModifiers::ALT { if modifiers == crossterm::event::KeyModifiers::ALT {
commands::Command::dap_edit_log.execute(cxt); commands::Command::dap_edit_log.execute(cxt);

Loading…
Cancel
Save