Enable stdio transport via config

pull/574/head
Dmitry Sharshakov 3 years ago
parent f55a012fb7
commit 839d210573
No known key found for this signature in database
GPG Key ID: 471FD32E15FD8473

@ -1,8 +1,9 @@
use crate::{ use crate::{
transport::{Payload, Request, Transport}, transport::{Payload, Request, Transport},
types::*, types::*,
Result, Error, Result,
}; };
use anyhow::anyhow;
pub use log::{error, info}; pub use log::{error, info};
use std::{ use std::{
collections::HashMap, collections::HashMap,
@ -35,6 +36,33 @@ pub struct Client {
} }
impl Client { impl Client {
// Spawn a process and communicate with it by either TCP or stdio
pub async fn process(
cfg: DebugAdapterConfig,
id: usize,
) -> Result<(Self, UnboundedReceiver<Payload>)> {
if cfg.transport == "tcp" && cfg.port_arg.is_some() {
Self::tcp_process(
&cfg.command,
cfg.args.iter().map(|s| s.as_str()).collect(),
&cfg.port_arg.unwrap(),
id,
)
.await
} else if cfg.transport == "stdio" {
Self::stdio(
&cfg.command,
cfg.args.iter().map(|s| s.as_str()).collect(),
id,
)
} else {
Result::Err(Error::Other(anyhow!(
"Incorrect transport {}",
cfg.transport
)))
}
}
pub fn streams( pub fn streams(
rx: Box<dyn AsyncBufRead + Unpin + Send>, rx: Box<dyn AsyncBufRead + Unpin + Send>,
tx: Box<dyn AsyncWrite + Unpin + Send>, tx: Box<dyn AsyncWrite + Unpin + Send>,
@ -113,14 +141,16 @@ impl Client {
} }
pub async fn tcp_process( pub async fn tcp_process(
config: DebugAdapterConfig, cmd: &str,
args: Vec<&str>,
port_format: &str,
id: usize, id: usize,
) -> Result<(Self, UnboundedReceiver<Payload>)> { ) -> Result<(Self, UnboundedReceiver<Payload>)> {
let port = Self::get_port().await.unwrap(); let port = Self::get_port().await.unwrap();
let process = Command::new(config.command) let process = Command::new(cmd)
.args(config.args) .args(args)
.args(config.port_arg.replace("{}", &port.to_string()).split(' ')) .args(port_format.replace("{}", &port.to_string()).split(' '))
// silence messages // silence messages
.stdin(Stdio::null()) .stdin(Stdio::null())
.stdout(Stdio::null()) .stdout(Stdio::null())

@ -5,9 +5,10 @@ use std::path::PathBuf;
#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] #[derive(Debug, PartialEq, Clone, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub struct DebugAdapterConfig { pub struct DebugAdapterConfig {
pub transport: String,
pub command: String, pub command: String,
pub args: Vec<String>, pub args: Vec<String>,
pub port_arg: String, pub port_arg: Option<String>,
} }
pub trait Request { pub trait Request {

@ -4362,7 +4362,7 @@ fn dap_start(cx: &mut Context) {
return; return;
} }
}; };
let started = Client::tcp_process(config, 0); let started = Client::process(config, 0);
let (mut debugger, events) = block_on(started).unwrap(); let (mut debugger, events) = block_on(started).unwrap();
let request = debugger.initialize("go".to_owned()); let request = debugger.initialize("go".to_owned());

@ -93,7 +93,7 @@ comment-token = "//"
language-server = { command = "gopls" } language-server = { command = "gopls" }
# TODO: gopls needs utf-8 offsets? # TODO: gopls needs utf-8 offsets?
indent = { tab-width = 4, unit = "\t" } indent = { tab-width = 4, unit = "\t" }
debug-adapter = { command = "dlv", args = ["dap"], port-arg = "-l 127.0.0.1:{}" } debug-adapter = { transport = "tcp", command = "dlv", args = ["dap"], port-arg = "-l 127.0.0.1:{}" }
[[language.debug-configs]] [[language.debug-configs]]
request = "launch" request = "launch"

Loading…
Cancel
Save