on master help sign in
Add stack pointer display when stopped
commit6458edecfd5fda486c9b9a1d0d802aa23bcd90ac authorDmitry Sharshakov <d3dx12.xx@gmail.com> dateSat 21 Aug 2021, 14:15 (5 years ago) parent738e8a4
4 files changed+70−17
helix-dap/src/client.rs+3 helix-term/src/commands.rs+28−2 helix-term/src/ui/editor.rs+37−14 helix-view/src/editor.rs+2−1
helix-dap/src/client.rs+3
@@ -36,6 +36,8 @@ pub struct Client {
3636
3737 //
3838 pub breakpoints: HashMap<PathBuf, Vec<SourceBreakpoint>>,
39+ // TODO: multiple threads support
40+ pub stack_pointer: Option<StackFrame>,
3941}
4042
4143impl Client {
@@ -56,6 +58,7 @@ impl Client {
5658 awaited_events: Arc::new(Mutex::new(HashMap::default())),
5759 //
5860 breakpoints: HashMap::new(),
61+ stack_pointer: None,
5962 };
6063
6164 tokio::spawn(Self::recv(Arc::clone(&client.awaited_events), server_rx));
helix-term/src/commands.rs+28−2
@@ -24,6 +24,7 @@ use helix_lsp::{
2424};
2525use insert::*;
2626use movement::Movement;
27+use tokio::sync::{mpsc::Receiver, Mutex};
2728
2829use crate::{
2930 compositor::{self, Component, Compositor},
@@ -32,8 +33,8 @@ use crate::{
3233
3334use crate::job::{self, Job, Jobs};
3435use futures_util::FutureExt;
35−use std::num::NonZeroUsize;
3636use std::{collections::HashMap, fmt, future::Future};
37+use std::{num::NonZeroUsize, sync::Arc};
3738
3839use std::{
3940 borrow::Cow,
@@ -4267,11 +4268,34 @@ fn dap_start(cx: &mut Context) {
42674268 let request = debugger.launch(to_value(args).unwrap());
42684269 let _ = block_on(request).unwrap();
42694270
4270− // TODO: either await "initialized" or buffer commands until event is received
4271+ let stopped = block_on(debugger.listen_for_event("stopped".to_owned()));
4272+ let debugger = Arc::new(Mutex::new(debugger));
4273+ tokio::spawn(dap_listen_stopped(stopped, Arc::clone(&debugger)));
42714274
4275+ // TODO: either await "initialized" or buffer commands until event is received
42724276 cx.editor.debugger = Some(debugger);
42734277}
42744278
4279+async fn dap_listen_stopped(
4280+ mut stopped: Receiver<helix_dap::Event>,
4281+ debugger: Arc<Mutex<helix_dap::Client>>,
4282+) {
4283+ loop {
4284+ stopped.recv().await;
4285+
4286+ let mut dbg = debugger.lock().await;
4287+ let main = dbg
4288+ .threads()
4289+ .await
4290+ .ok()
4291+ .and_then(|threads| threads.get(0).and_then(|x| Some(x.clone())));
4292+ if let Some(main) = main {
4293+ let (a, _) = dbg.stack_trace(main.id).await.unwrap();
4294+ dbg.stack_pointer = a.get(0).and_then(|x| Some(x.clone()));
4295+ }
4296+ }
4297+}
4298+
42754299fn dap_toggle_breakpoint(cx: &mut Context) {
42764300 use helix_lsp::block_on;
42774301
@@ -4297,6 +4321,7 @@ fn dap_toggle_breakpoint(cx: &mut Context) {
42974321 // we shouldn't really allow editing while debug is running though
42984322
42994323 if let Some(debugger) = &mut cx.editor.debugger {
4324+ let mut debugger = block_on(debugger.lock());
43004325 let breakpoints = debugger.breakpoints.entry(path.clone()).or_default();
43014326 if let Some(pos) = breakpoints.iter().position(|b| b.line == breakpoint.line) {
43024327 breakpoints.remove(pos);
@@ -4315,6 +4340,7 @@ fn dap_run(cx: &mut Context) {
43154340 use helix_lsp::block_on;
43164341
43174342 if let Some(debugger) = &mut cx.editor.debugger {
4343+ let mut debugger = block_on(debugger.lock());
43184344 let request = debugger.configuration_done();
43194345 let _ = block_on(request).unwrap();
43204346 }
helix-term/src/ui/editor.rs+37−14
@@ -15,6 +15,7 @@ use helix_core::{
1515 unicode::width::UnicodeWidthStr,
1616 LineEnding, Position, Range, Selection,
1717};
18+use helix_dap::{SourceBreakpoint, StackFrame};
1819use helix_view::{
1920 document::Mode,
2021 editor::LineNumber,
@@ -24,7 +25,8 @@ use helix_view::{
2425 keyboard::{KeyCode, KeyModifiers},
2526 Document, Editor, Theme, View,
2627};
27−use std::borrow::Cow;
28+use std::{borrow::Cow, sync::Arc};
29+use tokio::sync::Mutex;
2830
2931use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
3032use tui::buffer::Buffer as Surface;
@@ -71,7 +73,7 @@ impl EditorView {
7173 is_focused: bool,
7274 loader: &syntax::Loader,
7375 config: &helix_view::editor::Config,
74− debugger: Option<&helix_dap::Client>,
76+ debugger: Option<Arc<Mutex<helix_dap::Client>>>,
7577 ) {
7678 let inner = view.inner_area();
7779 let area = view.area;
@@ -412,8 +414,9 @@ impl EditorView {
412414 theme: &Theme,
413415 is_focused: bool,
414416 config: &helix_view::editor::Config,
415− debugger: Option<&helix_dap::Client>,
417+ debugger: Option<Arc<Mutex<helix_dap::Client>>>,
416418 ) {
419+ use helix_lsp::block_on;
417420 let text = doc.text().slice(..);
418421 let last_line = view.last_line(doc);
419422
@@ -442,9 +445,15 @@ impl EditorView {
442445 .map(|range| range.cursor_line(text))
443446 .collect();
444447
445− let breakpoints = doc
446− .path()
447− .and_then(|path| debugger.and_then(|debugger| debugger.breakpoints.get(path)));
448+ let mut breakpoints: Option<Vec<SourceBreakpoint>> = None;
449+ let mut stack_pointer: Option<StackFrame> = None;
450+ if let Some(debugger) = debugger {
451+ if let Some(path) = doc.path() {
452+ let dbg = block_on(debugger.lock());
453+ breakpoints = dbg.breakpoints.get(path).and_then(|bps| Some(bps.clone()));
454+ stack_pointer = dbg.stack_pointer.clone()
455+ }
456+ }
448457
449458 for (i, line) in (view.offset.row..(last_line + 1)).enumerate() {
450459 use helix_core::diagnostic::Severity;
@@ -465,12 +474,23 @@ impl EditorView {
465474
466475 let selected = cursors.contains(&line);
467476
468− if let Some(_breakpoint) = breakpoints.and_then(|breakpoints| {
469− breakpoints
470− .iter()
471− .find(|breakpoint| breakpoint.line == line)
472− }) {
473− surface.set_stringn(viewport.x, viewport.y + i as u16, "▲", 1, warning);
477+ if let Some(bps) = breakpoints.as_ref() {
478+ if let Some(_) = bps.iter().find(|breakpoint| breakpoint.line == line) {
479+ surface.set_stringn(viewport.x, viewport.y + i as u16, "▲", 1, warning);
480+ }
481+ }
482+
483+ if let Some(sp) = stack_pointer.as_ref() {
484+ if let Some(src) = sp.source.as_ref() {
485+ if doc
486+ .path()
487+ .and_then(|path| Some(src.path == Some(path.clone())))
488+ .unwrap_or(false)
489+ && sp.line == line
490+ {
491+ surface.set_stringn(viewport.x, viewport.y + i as u16, "⇒", 1, warning);
492+ }
493+ }
474494 }
475495
476496 let text = if line == last_line && !draw_last {
@@ -1023,7 +1043,10 @@ impl Component for EditorView {
10231043 for (view, is_focused) in cx.editor.tree.views() {
10241044 let doc = cx.editor.document(view.doc).unwrap();
10251045 let loader = &cx.editor.syn_loader;
1026− let debugger = cx.editor.debugger.as_ref();
1046+ let mut dbg: Option<Arc<Mutex<helix_dap::Client>>> = None;
1047+ if let Some(debugger) = &cx.editor.debugger {
1048+ dbg = Some(Arc::clone(&debugger));
1049+ }
10271050 self.render_view(
10281051 doc,
10291052 view,
@@ -1033,7 +1056,7 @@ impl Component for EditorView {
10331056 is_focused,
10341057 loader,
10351058 &cx.editor.config,
1036− debugger,
1059+ dbg,
10371060 );
10381061 }
10391062
helix-view/src/editor.rs+2−1
@@ -8,6 +8,7 @@ use crate::{
88
99use futures_util::future;
1010use futures_util::stream::select_all::SelectAll;
11+use tokio::sync::Mutex;
1112use tokio_stream::wrappers::UnboundedReceiverStream;
1213
1314use std::{
@@ -74,7 +75,7 @@ pub struct Editor {
7475 pub theme: Theme,
7576 pub language_servers: helix_lsp::Registry,
7677
77− pub debugger: Option<helix_dap::Client>,
78+ pub debugger: Option<Arc<Mutex<helix_dap::Client>>>,
7879 pub debuggers: SelectAll<UnboundedReceiverStream<helix_dap::Payload>>,
7980
8081 pub clipboard_provider: Box<dyn ClipboardProvider>,