dap: Split out launch from init

This commit is contained in:
Blaž Hrastnik 2021-08-20 14:06:55 +09:00
parent 94a1951d40
commit a54b09e3fe
4 changed files with 43 additions and 27 deletions

View file

@ -265,7 +265,7 @@ impl Client {
pub async fn set_breakpoints( pub async fn set_breakpoints(
&mut self, &mut self,
file: String, file: PathBuf,
breakpoints: Vec<SourceBreakpoint>, breakpoints: Vec<SourceBreakpoint>,
) -> Result<Option<Vec<Breakpoint>>> { ) -> Result<Option<Vec<Breakpoint>>> {
let args = requests::SetBreakpointsArguments { let args = requests::SetBreakpointsArguments {

View file

@ -1,5 +1,6 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use std::path::PathBuf;
pub trait Request { pub trait Request {
type Arguments: serde::de::DeserializeOwned + serde::Serialize; type Arguments: serde::de::DeserializeOwned + serde::Serialize;
@ -91,7 +92,7 @@ pub struct Checksum {
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct Source { pub struct Source {
pub name: Option<String>, pub name: Option<String>,
pub path: Option<String>, pub path: Option<PathBuf>,
pub source_reference: Option<usize>, pub source_reference: Option<usize>,
pub presentation_hint: Option<String>, pub presentation_hint: Option<String>,
pub origin: Option<String>, pub origin: Option<String>,
@ -207,7 +208,7 @@ pub struct Variable {
pub mod requests { pub mod requests {
use super::*; use super::*;
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct InitializeArguments { pub struct InitializeArguments {
#[serde(rename = "clientID")] #[serde(rename = "clientID")]
@ -274,7 +275,7 @@ pub mod requests {
const COMMAND: &'static str = "configurationDone"; const COMMAND: &'static str = "configurationDone";
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct SetBreakpointsArguments { pub struct SetBreakpointsArguments {
pub source: Source, pub source: Source,
@ -283,7 +284,7 @@ pub mod requests {
pub source_modified: Option<bool>, pub source_modified: Option<bool>,
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct SetBreakpointsResponse { pub struct SetBreakpointsResponse {
pub breakpoints: Option<Vec<Breakpoint>>, pub breakpoints: Option<Vec<Breakpoint>>,
@ -298,7 +299,7 @@ pub mod requests {
const COMMAND: &'static str = "setBreakpoints"; const COMMAND: &'static str = "setBreakpoints";
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct ContinueArguments { pub struct ContinueArguments {
pub thread_id: usize, pub thread_id: usize,
@ -319,7 +320,7 @@ pub mod requests {
const COMMAND: &'static str = "continue"; const COMMAND: &'static str = "continue";
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct StackTraceArguments { pub struct StackTraceArguments {
pub thread_id: usize, pub thread_id: usize,
@ -380,7 +381,7 @@ pub mod requests {
const COMMAND: &'static str = "scopes"; const COMMAND: &'static str = "scopes";
} }
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, Default, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")] #[serde(rename_all = "camelCase")]
pub struct VariablesArguments { pub struct VariablesArguments {
pub variables_reference: usize, pub variables_reference: usize,

View file

@ -33,7 +33,7 @@ use crate::{
use crate::job::{self, Job, Jobs}; use crate::job::{self, Job, Jobs};
use futures_util::FutureExt; use futures_util::FutureExt;
use std::num::NonZeroUsize; use std::num::NonZeroUsize;
use std::{fmt, future::Future}; use std::{collections::HashMap, fmt, future::Future};
use std::{ use std::{
borrow::Cow, borrow::Cow,
@ -302,7 +302,8 @@ impl Command {
surround_delete, "Surround delete", surround_delete, "Surround delete",
select_textobject_around, "Select around object", select_textobject_around, "Select around object",
select_textobject_inner, "Select inside object", select_textobject_inner, "Select inside object",
toggle_breakpoint, "Toggle breakpoint", dap_toggle_breakpoint, "Toggle breakpoint",
dap_launch, "Launch debugger",
suspend, "Suspend" suspend, "Suspend"
); );
} }
@ -1337,7 +1338,6 @@ fn append_mode(cx: &mut Context) {
mod cmd { mod cmd {
use super::*; use super::*;
use std::collections::HashMap;
use helix_view::editor::Action; use helix_view::editor::Action;
use ui::completers::{self, Completer}; use ui::completers::{self, Completer};
@ -1908,27 +1908,20 @@ mod cmd {
) -> anyhow::Result<()> { ) -> anyhow::Result<()> {
use helix_dap::Client; use helix_dap::Client;
use helix_lsp::block_on; use helix_lsp::block_on;
use serde_json::to_value;
let (_, doc) = current!(cx.editor); let (_, doc) = current!(cx.editor);
// look up config for filetype // look up config for filetype
// if multiple available, open picker // if multiple available, open picker
let client = Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0); let debugger = Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0);
let mut client = block_on(client)?; let mut debugger = block_on(debugger)?;
let request = client.initialize("go".to_owned()); let request = debugger.initialize("go".to_owned());
let _ = block_on(request)?; let _ = block_on(request)?;
let mut args = HashMap::new(); // TODO: either await "initialized" or buffer commands until event is received
args.insert("mode", "debug");
args.insert("program", "main.go");
let request = client.launch(to_value(args)?); cx.editor.debugger = Some(debugger);
let _ = block_on(request)?;
log::error!("4");
cx.editor.debugger = Some(client);
Ok(()) Ok(())
} }
@ -4281,13 +4274,15 @@ fn suspend(_cx: &mut Context) {
} }
// DAP // DAP
fn toggle_breakpoint(cx: &mut Context) { fn dap_toggle_breakpoint(cx: &mut Context) {
use helix_lsp::block_on;
let (view, doc) = current!(cx.editor); let (view, doc) = current!(cx.editor);
let text = doc.text().slice(..); let text = doc.text().slice(..);
let pos = doc.selection(view.id).primary().cursor(text); let pos = doc.selection(view.id).primary().cursor(text);
let breakpoint = helix_dap::SourceBreakpoint { let breakpoint = helix_dap::SourceBreakpoint {
line: text.char_to_line(pos), 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() ..Default::default()
}; };
@ -4304,11 +4299,30 @@ fn toggle_breakpoint(cx: &mut Context) {
// we shouldn't really allow editing while debug is running though // we shouldn't really allow editing while debug is running though
if let Some(debugger) = &mut cx.editor.debugger { if let Some(debugger) = &mut cx.editor.debugger {
let breakpoints = debugger.breakpoints.entry(path).or_default(); let breakpoints = debugger.breakpoints.entry(path.clone()).or_default();
if let Some(pos) = breakpoints.iter().position(|b| b.line == breakpoint.line) { if let Some(pos) = breakpoints.iter().position(|b| b.line == breakpoint.line) {
breakpoints.remove(pos); breakpoints.remove(pos);
} else { } else {
breakpoints.push(breakpoint); breakpoints.push(breakpoint);
} }
let breakpoints = breakpoints.clone();
let request = debugger.set_breakpoints(path, breakpoints);
let _ = block_on(request).unwrap();
}
}
fn dap_launch(cx: &mut Context) {
use helix_lsp::block_on;
use serde_json::to_value;
if let Some(debugger) = &mut cx.editor.debugger {
let mut args = HashMap::new();
args.insert("mode", "debug");
args.insert("program", "main.go");
let request = debugger.launch(to_value(args).unwrap());
let _ = block_on(request).unwrap();
} }
} }

View file

@ -486,7 +486,8 @@ impl Default for Keymaps {
"a" => code_action, "a" => code_action,
"'" => last_picker, "'" => last_picker,
"d" => { "Debug" "d" => { "Debug"
"b" => toggle_breakpoint, "b" => dap_toggle_breakpoint,
"r" => dap_launch,
}, },
"w" => { "Window" "w" => { "Window"
"C-w" | "w" => rotate_view, "C-w" | "w" => rotate_view,