summaryrefslogtreecommitdiff
path: root/helix-term
diff options
context:
space:
mode:
authorDmitry Sharshakov2021-08-21 14:21:35 +0000
committerDmitry Sharshakov2021-08-21 14:21:35 +0000
commita938f5a87a2b67dd8667337cd569cc6627d0f666 (patch)
treeecce252fd9c451f23c583bb734dc9912d39c13e7 /helix-term
parent3fc501c99f8f8c539dc4714a200bac45a7169138 (diff)
refactor: handle DAP events in editor main loop
Diffstat (limited to 'helix-term')
-rw-r--r--helix-term/Cargo.toml2
-rw-r--r--helix-term/src/application.rs24
-rw-r--r--helix-term/src/commands.rs36
-rw-r--r--helix-term/src/ui/editor.rs19
4 files changed, 37 insertions, 44 deletions
diff --git a/helix-term/Cargo.toml b/helix-term/Cargo.toml
index 6ed60e02..c0b2cfe5 100644
--- a/helix-term/Cargo.toml
+++ b/helix-term/Cargo.toml
@@ -34,7 +34,7 @@ num_cpus = "1"
tui = { path = "../helix-tui", package = "helix-tui", default-features = false, features = ["crossterm"] }
crossterm = { version = "0.20", features = ["event-stream"] }
signal-hook = "0.3"
-
+tokio-stream = "0.1"
futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
# Logging
diff --git a/helix-term/src/application.rs b/helix-term/src/application.rs
index 011dafee..dad6df2b 100644
--- a/helix-term/src/application.rs
+++ b/helix-term/src/application.rs
@@ -1,4 +1,5 @@
use helix_core::syntax;
+use helix_dap::Payload;
use helix_lsp::{lsp, util::lsp_pos_to_pos, LspProgressMap};
use helix_view::{theme, Editor};
@@ -184,6 +185,29 @@ impl Application {
last_render = Instant::now();
}
}
+ Some(payload) = self.editor.debugger_events.next() => {
+ let mut debugger = self.editor.debugger.as_mut().unwrap();
+ match payload {
+ Payload::Event(ev) => {
+ match &ev.event[..] {
+ "stopped" => {
+ let main = debugger
+ .threads()
+ .await
+ .ok()
+ .and_then(|threads| threads.get(0).cloned());
+ if let Some(main) = main {
+ let (bt, _) = debugger.stack_trace(main.id).await.unwrap();
+ debugger.stack_pointer = bt.get(0).cloned();
+ }
+ }
+ _ => {}
+ }
+ },
+ Payload::Response(_) => unreachable!(),
+ Payload::Request(_) => todo!(),
+ }
+ }
Some(callback) = self.jobs.futures.next() => {
self.jobs.handle_callback(&mut self.editor, &mut self.compositor, callback);
self.render();
diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs
index 9a8e1ee6..34a146c2 100644
--- a/helix-term/src/commands.rs
+++ b/helix-term/src/commands.rs
@@ -24,17 +24,17 @@ use helix_lsp::{
};
use insert::*;
use movement::Movement;
-use tokio::sync::{mpsc::Receiver, Mutex};
use crate::{
compositor::{self, Component, Compositor},
ui::{self, FilePicker, Picker, Popup, Prompt, PromptEvent},
};
+use tokio_stream::wrappers::UnboundedReceiverStream;
use crate::job::{self, Job, Jobs};
use futures_util::FutureExt;
+use std::num::NonZeroUsize;
use std::{collections::HashMap, fmt, future::Future};
-use std::{num::NonZeroUsize, sync::Arc};
use std::{
borrow::Cow,
@@ -4255,8 +4255,8 @@ fn dap_start(cx: &mut Context) {
// look up config for filetype
// if multiple available, open picker
- let debugger = Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0);
- let mut debugger = block_on(debugger).unwrap();
+ let started = Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0);
+ let (mut debugger, events) = block_on(started).unwrap();
let request = debugger.initialize("go".to_owned());
let _ = block_on(request).unwrap();
@@ -4268,32 +4268,10 @@ fn dap_start(cx: &mut Context) {
let request = debugger.launch(to_value(args).unwrap());
let _ = block_on(request).unwrap();
- let stopped = block_on(debugger.listen_for_event("stopped".to_owned()));
- let debugger = Arc::new(Mutex::new(debugger));
- tokio::spawn(dap_listen_stopped(stopped, Arc::clone(&debugger)));
-
// TODO: either await "initialized" or buffer commands until event is received
cx.editor.debugger = Some(debugger);
-}
-
-async fn dap_listen_stopped(
- mut stopped: Receiver<helix_dap::Event>,
- debugger: Arc<Mutex<helix_dap::Client>>,
-) {
- loop {
- stopped.recv().await;
-
- let mut dbg = debugger.lock().await;
- let main = dbg
- .threads()
- .await
- .ok()
- .and_then(|threads| threads.get(0).cloned());
- if let Some(main) = main {
- let (a, _) = dbg.stack_trace(main.id).await.unwrap();
- dbg.stack_pointer = a.get(0).cloned();
- }
- }
+ let stream = UnboundedReceiverStream::new(events);
+ cx.editor.debugger_events.push(stream);
}
fn dap_toggle_breakpoint(cx: &mut Context) {
@@ -4321,7 +4299,6 @@ fn dap_toggle_breakpoint(cx: &mut Context) {
// we shouldn't really allow editing while debug is running though
if let Some(debugger) = &mut cx.editor.debugger {
- let mut debugger = block_on(debugger.lock());
let breakpoints = debugger.breakpoints.entry(path.clone()).or_default();
if let Some(pos) = breakpoints.iter().position(|b| b.line == breakpoint.line) {
breakpoints.remove(pos);
@@ -4340,7 +4317,6 @@ fn dap_run(cx: &mut Context) {
use helix_lsp::block_on;
if let Some(debugger) = &mut cx.editor.debugger {
- let mut debugger = block_on(debugger.lock());
let request = debugger.configuration_done();
let _ = block_on(request).unwrap();
}
diff --git a/helix-term/src/ui/editor.rs b/helix-term/src/ui/editor.rs
index 7f441a7d..f8035ae4 100644
--- a/helix-term/src/ui/editor.rs
+++ b/helix-term/src/ui/editor.rs
@@ -25,8 +25,7 @@ use helix_view::{
keyboard::{KeyCode, KeyModifiers},
Document, Editor, Theme, View,
};
-use std::{borrow::Cow, sync::Arc};
-use tokio::sync::Mutex;
+use std::borrow::Cow;
use crossterm::event::{Event, MouseButton, MouseEvent, MouseEventKind};
use tui::buffer::Buffer as Surface;
@@ -73,7 +72,7 @@ impl EditorView {
is_focused: bool,
loader: &syntax::Loader,
config: &helix_view::editor::Config,
- debugger: Option<Arc<Mutex<helix_dap::Client>>>,
+ debugger: &Option<helix_dap::Client>,
) {
let inner = view.inner_area();
let area = view.area;
@@ -414,9 +413,8 @@ impl EditorView {
theme: &Theme,
is_focused: bool,
config: &helix_view::editor::Config,
- debugger: Option<Arc<Mutex<helix_dap::Client>>>,
+ debugger: &Option<helix_dap::Client>,
) {
- use helix_lsp::block_on;
let text = doc.text().slice(..);
let last_line = view.last_line(doc);
@@ -449,9 +447,8 @@ impl EditorView {
let mut stack_pointer: Option<StackFrame> = None;
if let Some(debugger) = debugger {
if let Some(path) = doc.path() {
- let dbg = block_on(debugger.lock());
- breakpoints = dbg.breakpoints.get(path).cloned();
- stack_pointer = dbg.stack_pointer.clone()
+ breakpoints = debugger.breakpoints.get(path).cloned();
+ stack_pointer = debugger.stack_pointer.clone()
}
}
@@ -1047,10 +1044,6 @@ impl Component for EditorView {
for (view, is_focused) in cx.editor.tree.views() {
let doc = cx.editor.document(view.doc).unwrap();
let loader = &cx.editor.syn_loader;
- let mut dbg: Option<Arc<Mutex<helix_dap::Client>>> = None;
- if let Some(debugger) = &cx.editor.debugger {
- dbg = Some(Arc::clone(debugger));
- }
self.render_view(
doc,
view,
@@ -1060,7 +1053,7 @@ impl Component for EditorView {
is_focused,
loader,
&cx.editor.config,
- dbg,
+ &cx.editor.debugger,
);
}