From c5b210df59983a2caa9c389e5cd22aa670fb4a00 Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Mon, 23 Aug 2021 16:48:06 +0300 Subject: [PATCH] Add debug-adapter field to languages.toml --- Cargo.lock | 2 ++ helix-core/Cargo.toml | 2 ++ helix-core/src/syntax.rs | 3 +++ helix-dap/src/client.rs | 10 ++++------ helix-dap/src/types.rs | 8 ++++++++ helix-term/src/commands.rs | 29 +++++++++++++++++++++++++---- languages.toml | 1 + 7 files changed, 45 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 57ddb01c2..fe5c4713e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -306,12 +306,14 @@ version = "0.4.1" dependencies = [ "arc-swap", "etcetera", + "helix-dap", "helix-syntax", "once_cell", "quickcheck", "regex", "ropey", "serde", + "serde_json", "similar", "smallvec", "tendril", diff --git a/helix-core/Cargo.toml b/helix-core/Cargo.toml index 8c83816cf..8fdfa3cef 100644 --- a/helix-core/Cargo.toml +++ b/helix-core/Cargo.toml @@ -14,6 +14,7 @@ include = ["src/**/*", "README.md"] [dependencies] helix-syntax = { version = "0.4", path = "../helix-syntax" } +helix-dap = { version = "0.4", path = "../helix-dap" } ropey = "1.3" smallvec = "1.4" @@ -28,6 +29,7 @@ arc-swap = "1" regex = "1" serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" toml = "0.5" similar = "1.3" diff --git a/helix-core/src/syntax.rs b/helix-core/src/syntax.rs index 4bceb73b3..f3272cff1 100644 --- a/helix-core/src/syntax.rs +++ b/helix-core/src/syntax.rs @@ -5,6 +5,7 @@ use crate::{ Rope, RopeSlice, Tendril, }; +use helix_dap::DebugAdapterConfig; pub use helix_syntax::get_language; use arc_swap::ArcSwap; @@ -55,6 +56,8 @@ pub struct LanguageConfiguration { #[serde(skip)] pub(crate) indent_query: OnceCell>, + #[serde(skip_serializing_if = "Option::is_none")] + pub debug_adapter: Option, } #[derive(Debug, Serialize, Deserialize)] diff --git a/helix-dap/src/client.rs b/helix-dap/src/client.rs index 98b88e239..79c65cf7d 100644 --- a/helix-dap/src/client.rs +++ b/helix-dap/src/client.rs @@ -113,16 +113,14 @@ impl Client { } pub async fn tcp_process( - cmd: &str, - args: Vec<&str>, - port_format: &str, + config: DebugAdapterConfig, id: usize, ) -> Result<(Self, UnboundedReceiver)> { let port = Self::get_port().await.unwrap(); - let process = Command::new(cmd) - .args(args) - .args(port_format.replace("{}", &port.to_string()).split(' ')) + let process = Command::new(config.command) + .args(config.args) + .args(config.port_arg.replace("{}", &port.to_string()).split(' ')) // silence messages .stdin(Stdio::null()) .stdout(Stdio::null()) diff --git a/helix-dap/src/types.rs b/helix-dap/src/types.rs index 26cd69fb2..152996a15 100644 --- a/helix-dap/src/types.rs +++ b/helix-dap/src/types.rs @@ -2,6 +2,14 @@ use serde::{Deserialize, Serialize}; use serde_json::Value; use std::path::PathBuf; +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "kebab-case")] +pub struct DebugAdapterConfig { + pub command: String, + pub args: Vec, + pub port_arg: String, +} + pub trait Request { type Arguments: serde::de::DeserializeOwned + serde::Serialize; type Result: serde::de::DeserializeOwned + serde::Serialize; diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index e35b5d51d..3b8e10221 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -4336,12 +4336,33 @@ fn dap_start(cx: &mut Context) { 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 - // if multiple available, open picker + // TODO config picker + let path = match doc.path() { + Some(path) => path.to_path_buf(), + None => { + cx.editor + .set_error("Can't start debug: document has no path".to_string()); + return; + } + }; - let started = Client::tcp_process("dlv", vec!["dap"], "-l 127.0.0.1:{}", 0); + let config = cx + .editor + .syn_loader + .language_config_for_file_name(&path) + .and_then(|x| x.debug_adapter.clone()); + let config = match config { + Some(c) => c, + None => { + cx.editor.set_error( + "Can't start debug: no debug adapter available for language".to_string(), + ); + return; + } + }; + let started = Client::tcp_process(config, 0); let (mut debugger, events) = block_on(started).unwrap(); let request = debugger.initialize("go".to_owned()); diff --git a/languages.toml b/languages.toml index 47155523b..61b68e590 100644 --- a/languages.toml +++ b/languages.toml @@ -93,6 +93,7 @@ comment-token = "//" language-server = { command = "gopls" } # TODO: gopls needs utf-8 offsets? indent = { tab-width = 4, unit = "\t" } +debug-adapter = { command = "dlv", args = ["dap"], port-arg = "-l 127.0.0.1:{}" } [[language]] name = "javascript"