on master help sign in
Start integrating into the editor's event loop
commitd39baa3b4e1f5bc1a03533e7b22af0043ec1eac9 authorBlaž Hrastnik <blaz@mxxn.io> dateMon 16 Aug 2021, 18:16 (5 years ago) parent0300dbd
11 files changed+86−27
Cargo.lock+4−1 helix-dap/Cargo.toml+1−1 helix-dap/src/client.rs+10−12 helix-dap/src/transport.rs+9−12 helix-lsp/Cargo.toml+1−1 helix-term/Cargo.toml+1 helix-term/src/application.rs+9 helix-term/src/commands.rs+42 helix-view/Cargo.toml+2 helix-view/src/document.rs+2 helix-view/src/editor.rs+5
Cargo.lock+4−1
@@ -324,7 +324,7 @@ dependencies = [
324324
325325[[package]]
326326name = "helix-dap"
327−version = "0.3.0"
327+version = "0.4.0"
328328dependencies = [
329329 "anyhow",
330330 "fern",
@@ -375,6 +375,7 @@ dependencies = [
375375 "futures-util",
376376 "fuzzy-matcher",
377377 "helix-core",
378+ "helix-dap",
378379 "helix-lsp",
379380 "helix-tui",
380381 "helix-view",
@@ -416,6 +417,7 @@ dependencies = [
416417 "encoding_rs",
417418 "futures-util",
418419 "helix-core",
420+ "helix-dap",
419421 "helix-lsp",
420422 "helix-tui",
421423 "log",
@@ -423,6 +425,7 @@ dependencies = [
423425 "serde",
424426 "slotmap",
425427 "tokio",
428+ "tokio-stream",
426429 "toml",
427430 "url",
428431 "which",
helix-dap/Cargo.toml+1−1
@@ -1,6 +1,6 @@
11[package]
22name = "helix-dap"
3−version = "0.3.0"
3+version = "0.4.0"
44authors = ["Blaž Hrastnik <blaz@mxxn.io>"]
55edition = "2018"
66license = "MPL-2.0"
helix-dap/src/client.rs+10−12
@@ -186,12 +186,12 @@ impl Client {
186186 &self,
187187 arguments: R::Arguments,
188188 ) -> Result<R::Result> {
189− let (callback_rx, mut callback_tx) = channel(1);
189+ let (callback_tx, mut callback_rx) = channel(1);
190190
191191 let arguments = Some(serde_json::to_value(arguments)?);
192192
193193 let req = Request {
194− back_ch: Some(callback_rx),
194+ back_ch: Some(callback_tx),
195195 seq: self.next_request_id(),
196196 command: R::COMMAND.to_string(),
197197 arguments,
@@ -201,7 +201,7 @@ impl Client {
201201 .send(req)
202202 .expect("Failed to send request to debugger");
203203
204− let response = callback_tx.recv().await.unwrap()?;
204+ let response = callback_rx.recv().await.unwrap()?;
205205 let response = serde_json::from_value(response.body.unwrap_or_default())?;
206206 Ok(response)
207207 }
@@ -209,7 +209,7 @@ impl Client {
209209 pub fn capabilities(&self) -> &DebuggerCapabilities {
210210 self.capabilities
211211 .as_ref()
212− .expect("language server not yet initialized!")
212+ .expect("debugger not yet initialized!")
213213 }
214214
215215 pub async fn initialize(&mut self, adapter_id: String) -> Result<()> {
@@ -240,21 +240,19 @@ impl Client {
240240 }
241241
242242 pub async fn launch(&mut self, args: serde_json::Value) -> Result<()> {
243− let mut initialized = self.listen_for_event("initialized".to_owned()).await;
243+ // TODO: buffer these until initialized arrives
244244
245− let res = self.request::<requests::Launch>(args);
246− let ev = initialized.recv();
247− join!(res, ev).0?;
245+ let response = self.request::<requests::Launch>(args).await?;
246+ log::error!("launch response {}", response);
248247
249248 Ok(())
250249 }
251250
252251 pub async fn attach(&mut self, args: serde_json::Value) -> Result<()> {
253− let mut initialized = self.listen_for_event("initialized".to_owned()).await;
252+ // TODO: buffer these until initialized arrives
254253
255− let res = self.request::<requests::Attach>(args);
256− let ev = initialized.recv();
257− join!(res, ev).0?;
254+ let response = self.request::<requests::Attach>(args).await?;
255+ log::error!("attach response {}", response);
258256
259257 Ok(())
260258 }
helix-dap/src/transport.rs+9−12
@@ -159,20 +159,17 @@ impl Transport {
159159 }
160160
161161 fn process_response(res: Response) -> Result<Response> {
162− match res.success {
163− true => {
164− info!("<- DAP success in response to {}", res.request_seq);
162+ if res.success {
163+ info!("<- DAP success in response to {}", res.request_seq);
165164
166− Ok(res)
167− }
168− false => {
169− error!(
170− "<- DAP error {:?} ({:?}) for command #{} {}",
171− res.message, res.body, res.request_seq, res.command
172− );
165+ Ok(res)
166+ } else {
167+ error!(
168+ "<- DAP error {:?} ({:?}) for command #{} {}",
169+ res.message, res.body, res.request_seq, res.command
170+ );
173171
174− Err(Error::Other(anyhow::format_err!("{:?}", res.body)))
175− }
172+ Err(Error::Other(anyhow::format_err!("{:?}", res.body)))
176173 }
177174 }
178175
helix-lsp/Cargo.toml+1−1
@@ -24,4 +24,4 @@ serde = { version = "1.0", features = ["derive"] }
2424serde_json = "1.0"
2525thiserror = "1.0"
2626tokio = { version = "1.9", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot", "sync"] }
27−tokio-stream = "0.1.7"
27+tokio-stream = "0.1"
helix-term/Cargo.toml+1
@@ -24,6 +24,7 @@ path = "src/main.rs"
2424helix-core = { version = "0.4", path = "../helix-core" }
2525helix-view = { version = "0.4", path = "../helix-view" }
2626helix-lsp = { version = "0.4", path = "../helix-lsp" }
27+helix-dap = { version = "0.4", path = "../helix-dap" }
2728
2829anyhow = "1"
2930once_cell = "1.8"
helix-term/src/application.rs+9
@@ -245,6 +245,15 @@ impl Application {
245245 }
246246 }
247247
248+ pub async fn handle_debugger_message(
249+ &mut self,
250+ call: (),
251+ server_id: usize,
252+ ) {
253+
254+ //
255+ }
256+
248257 pub async fn handle_language_server_message(
249258 &mut self,
250259 call: helix_lsp::Call,
helix-term/src/commands.rs+42
@@ -1900,6 +1900,41 @@ mod cmd {
19001900 Ok(())
19011901 }
19021902
1903+ fn debug(
1904+ cx: &mut compositor::Context,
1905+ _args: &[&str],
1906+ _event: PromptEvent,
1907+ ) -> anyhow::Result<()> {
1908+ use helix_dap::Client;
1909+ use helix_lsp::block_on;
1910+ use serde_json::to_value;
1911+ let (_, doc) = current!(cx.editor);
1912+
1913+ // look up config for filetype
1914+ // if multiple available, open picker
1915+
1916+ log::error!("1");
1917+
1918+ let client = Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0);
1919+ let mut client = block_on(client)?;
1920+ log::error!("2");
1921+
1922+ let request = client.initialize("go".to_owned());
1923+ let _ = block_on(request)?;
1924+ log::error!("3");
1925+
1926+ let mut args = HashMap::new();
1927+ args.insert("mode", "debug");
1928+ // args.insert("program", "path/to/program");
1929+
1930+ let request = client.launch(to_value(args)?);
1931+ let _ = block_on(request)?;
1932+
1933+ log::error!("4");
1934+ doc.debugger = Some(client);
1935+ Ok(())
1936+ }
1937+
19031938 pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[
19041939 TypableCommand {
19051940 name: "quit",
@@ -2138,6 +2173,13 @@ mod cmd {
21382173 doc: "Display tree sitter scopes, primarily for theming and development.",
21392174 fun: tree_sitter_scopes,
21402175 completer: None,
2176+ },
2177+ TypableCommand {
2178+ name: "debug",
2179+ alias: None,
2180+ doc: "Start a debug session.",
2181+ fun: debug,
2182+ completer: None,
21412183 }
21422184 ];
21432185
helix-view/Cargo.toml+2
@@ -18,6 +18,7 @@ bitflags = "1.3"
1818anyhow = "1"
1919helix-core = { version = "0.4", path = "../helix-core" }
2020helix-lsp = { version = "0.4", path = "../helix-lsp"}
21+helix-dap = { version = "0.4", path = "../helix-dap"}
2122crossterm = { version = "0.20", optional = true }
2223
2324# Conversion traits
@@ -25,6 +26,7 @@ once_cell = "1.8"
2526url = "2"
2627
2728tokio = { version = "1", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] }
29+tokio-stream = "0.1"
2830futures-util = { version = "0.3", features = ["std", "async-await"], default-features = false }
2931
3032slotmap = "1"
helix-view/src/document.rs+2
@@ -100,6 +100,7 @@ pub struct Document {
100100
101101 diagnostics: Vec<Diagnostic>,
102102 language_server: Option<Arc<helix_lsp::Client>>,
103+ pub debugger: Option<helix_dap::Client>,
103104}
104105
105106use std::fmt;
@@ -425,6 +426,7 @@ impl Document {
425426 history: Cell::new(History::default()),
426427 last_saved_revision: 0,
427428 language_server: None,
429+ debugger: None,
428430 line_ending: DEFAULT_LINE_ENDING,
429431 }
430432 }
helix-view/src/editor.rs+5
@@ -7,6 +7,9 @@ use crate::{
77};
88
99use futures_util::future;
10+use futures_util::stream::select_all::SelectAll;
11+use tokio_stream::wrappers::UnboundedReceiverStream;
12+
1013use std::{
1114 path::{Path, PathBuf},
1215 sync::Arc,
@@ -70,6 +73,7 @@ pub struct Editor {
7073 pub registers: Registers,
7174 pub theme: Theme,
7275 pub language_servers: helix_lsp::Registry,
76+ pub debuggers: SelectAll<UnboundedReceiverStream<(usize, helix_dap::Payload)>>,
7377 pub clipboard_provider: Box<dyn ClipboardProvider>,
7478
7579 pub syn_loader: Arc<syntax::Loader>,
@@ -107,6 +111,7 @@ impl Editor {
107111 selected_register: RegisterSelection::default(),
108112 theme: themes.default(),
109113 language_servers,
114+ debuggers: SelectAll::new(),
110115 syn_loader: config_loader,
111116 theme_loader: themes,
112117 registers: Registers::default(),