on master help sign in
dap: Split out launch from init
commita54b09e3fe6eaf72ab7a4f3664efc0b8b7cf13fd authorBlaž Hrastnik <blaz@mxxn.io> dateFri 20 Aug 2021, 14:06 (5 years ago) parent94a1951
4 files changed+43−27
helix-dap/src/client.rs+1−1 helix-dap/src/types.rs+8−7 helix-term/src/commands.rs+32−18 helix-term/src/keymap.rs+2−1
helix-dap/src/client.rs+1−1
@@ -265,7 +265,7 @@ impl Client {
265265
266266 pub async fn set_breakpoints(
267267 &mut self,
268− file: String,
268+ file: PathBuf,
269269 breakpoints: Vec<SourceBreakpoint>,
270270 ) -> Result<Option<Vec<Breakpoint>>> {
271271 let args = requests::SetBreakpointsArguments {
helix-dap/src/types.rs+8−7
@@ -1,5 +1,6 @@
11use serde::{Deserialize, Serialize};
22use serde_json::Value;
3+use std::path::PathBuf;
34
45pub trait Request {
56 type Arguments: serde::de::DeserializeOwned + serde::Serialize;
@@ -91,7 +92,7 @@ pub struct Checksum {
9192#[serde(rename_all = "camelCase")]
9293pub struct Source {
9394 pub name: Option<String>,
94− pub path: Option<String>,
95+ pub path: Option<PathBuf>,
9596 pub source_reference: Option<usize>,
9697 pub presentation_hint: Option<String>,
9798 pub origin: Option<String>,
@@ -207,7 +208,7 @@ pub struct Variable {
207208
208209pub mod requests {
209210 use super::*;
210− #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
211+ #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
211212 #[serde(rename_all = "camelCase")]
212213 pub struct InitializeArguments {
213214 #[serde(rename = "clientID")]
@@ -274,7 +275,7 @@ pub mod requests {
274275 const COMMAND: &'static str = "configurationDone";
275276 }
276277
277− #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
278+ #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
278279 #[serde(rename_all = "camelCase")]
279280 pub struct SetBreakpointsArguments {
280281 pub source: Source,
@@ -283,7 +284,7 @@ pub mod requests {
283284 pub source_modified: Option<bool>,
284285 }
285286
286− #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
287+ #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
287288 #[serde(rename_all = "camelCase")]
288289 pub struct SetBreakpointsResponse {
289290 pub breakpoints: Option<Vec<Breakpoint>>,
@@ -298,7 +299,7 @@ pub mod requests {
298299 const COMMAND: &'static str = "setBreakpoints";
299300 }
300301
301− #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
302+ #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
302303 #[serde(rename_all = "camelCase")]
303304 pub struct ContinueArguments {
304305 pub thread_id: usize,
@@ -319,7 +320,7 @@ pub mod requests {
319320 const COMMAND: &'static str = "continue";
320321 }
321322
322− #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
323+ #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
323324 #[serde(rename_all = "camelCase")]
324325 pub struct StackTraceArguments {
325326 pub thread_id: usize,
@@ -380,7 +381,7 @@ pub mod requests {
380381 const COMMAND: &'static str = "scopes";
381382 }
382383
383− #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
384+ #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
384385 #[serde(rename_all = "camelCase")]
385386 pub struct VariablesArguments {
386387 pub variables_reference: usize,
helix-term/src/commands.rs+32−18
@@ -33,7 +33,7 @@ use crate::{
3333use crate::job::{self, Job, Jobs};
3434use futures_util::FutureExt;
3535use std::num::NonZeroUsize;
36−use std::{fmt, future::Future};
36+use std::{collections::HashMap, fmt, future::Future};
3737
3838use std::{
3939 borrow::Cow,
@@ -302,7 +302,8 @@ impl Command {
302302 surround_delete, "Surround delete",
303303 select_textobject_around, "Select around object",
304304 select_textobject_inner, "Select inside object",
305− toggle_breakpoint, "Toggle breakpoint",
305+ dap_toggle_breakpoint, "Toggle breakpoint",
306+ dap_launch, "Launch debugger",
306307 suspend, "Suspend"
307308 );
308309}
@@ -1337,7 +1338,6 @@ fn append_mode(cx: &mut Context) {
13371338
13381339mod cmd {
13391340 use super::*;
1340− use std::collections::HashMap;
13411341
13421342 use helix_view::editor::Action;
13431343 use ui::completers::{self, Completer};
@@ -1908,27 +1908,20 @@ mod cmd {
19081908 ) -> anyhow::Result<()> {
19091909 use helix_dap::Client;
19101910 use helix_lsp::block_on;
1911− use serde_json::to_value;
19121911 let (_, doc) = current!(cx.editor);
19131912
19141913 // look up config for filetype
19151914 // if multiple available, open picker
19161915
1917− let client = Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0);
1918− let mut client = block_on(client)?;
1916+ let debugger = Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0);
1917+ let mut debugger = block_on(debugger)?;
19191918
1920− let request = client.initialize("go".to_owned());
1919+ let request = debugger.initialize("go".to_owned());
19211920 let _ = block_on(request)?;
19221921
1923− let mut args = HashMap::new();
1924− args.insert("mode", "debug");
1925− args.insert("program", "main.go");
1926−
1927− let request = client.launch(to_value(args)?);
1928− let _ = block_on(request)?;
1922+ // TODO: either await "initialized" or buffer commands until event is received
19291923
1930− log::error!("4");
1931− cx.editor.debugger = Some(client);
1924+ cx.editor.debugger = Some(debugger);
19321925 Ok(())
19331926 }
19341927
@@ -4281,13 +4274,15 @@ fn suspend(_cx: &mut Context) {
42814274}
42824275
42834276// DAP
4284−fn toggle_breakpoint(cx: &mut Context) {
4277+fn dap_toggle_breakpoint(cx: &mut Context) {
4278+ use helix_lsp::block_on;
4279+
42854280 let (view, doc) = current!(cx.editor);
42864281 let text = doc.text().slice(..);
42874282 let pos = doc.selection(view.id).primary().cursor(text);
42884283
42894284 let breakpoint = helix_dap::SourceBreakpoint {
4290− line: text.char_to_line(pos),
4285+ line: text.char_to_line(pos) + 1, // convert from 0-indexing to 1-indexing (TODO: could set debugger to 0-indexing on init)
42914286 ..Default::default()
42924287 };
42934288
@@ -4304,11 +4299,30 @@ fn toggle_breakpoint(cx: &mut Context) {
43044299 // we shouldn't really allow editing while debug is running though
43054300
43064301 if let Some(debugger) = &mut cx.editor.debugger {
4307− let breakpoints = debugger.breakpoints.entry(path).or_default();
4302+ let breakpoints = debugger.breakpoints.entry(path.clone()).or_default();
43084303 if let Some(pos) = breakpoints.iter().position(|b| b.line == breakpoint.line) {
43094304 breakpoints.remove(pos);
43104305 } else {
43114306 breakpoints.push(breakpoint);
43124307 }
4308+
4309+ let breakpoints = breakpoints.clone();
4310+
4311+ let request = debugger.set_breakpoints(path, breakpoints);
4312+ let _ = block_on(request).unwrap();
4313+ }
4314+}
4315+
4316+fn dap_launch(cx: &mut Context) {
4317+ use helix_lsp::block_on;
4318+ use serde_json::to_value;
4319+
4320+ if let Some(debugger) = &mut cx.editor.debugger {
4321+ let mut args = HashMap::new();
4322+ args.insert("mode", "debug");
4323+ args.insert("program", "main.go");
4324+
4325+ let request = debugger.launch(to_value(args).unwrap());
4326+ let _ = block_on(request).unwrap();
43134327 }
43144328}
helix-term/src/keymap.rs+2−1
@@ -486,7 +486,8 @@ impl Default for Keymaps {
486486 "a" => code_action,
487487 "'" => last_picker,
488488 "d" => { "Debug"
489− "b" => toggle_breakpoint,
489+ "b" => dap_toggle_breakpoint,
490+ "r" => dap_launch,
490491 },
491492 "w" => { "Window"
492493 "C-w" | "w" => rotate_view,