From a7a46087d9d6822eb89e3e435a1ab629eb92f7b8 Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Mon, 9 Aug 2021 18:25:01 +0300 Subject: [PATCH 1/4] helix-term: add config option for specifying log file Signed-off-by: Dmitry Sharshakov --- helix-term/src/config.rs | 2 ++ helix-term/src/main.rs | 20 +++++++++++++------- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/helix-term/src/config.rs b/helix-term/src/config.rs index 13917656a231..4c5e0a175db0 100644 --- a/helix-term/src/config.rs +++ b/helix-term/src/config.rs @@ -3,8 +3,10 @@ use serde::Deserialize; use crate::keymap::Keymaps; #[derive(Debug, Default, Clone, PartialEq, Deserialize)] +#[serde(rename_all = "kebab-case")] pub struct Config { pub theme: Option, + pub log_file: Option, #[serde(default)] pub lsp: LspConfig, #[serde(default)] diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index 180dacd1faf2..e9c43b64c356 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -35,12 +35,8 @@ fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> { #[tokio::main] async fn main() -> Result<()> { - let cache_dir = helix_core::cache_dir(); - if !cache_dir.exists() { - std::fs::create_dir_all(&cache_dir).ok(); - } + let log_path_default = helix_core::cache_dir().join("helix.log"); - let logpath = cache_dir.join("helix.log"); let help = format!( "\ {} {} @@ -63,7 +59,7 @@ FLAGS: env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"), env!("CARGO_PKG_DESCRIPTION"), - logpath.display(), + log_path_default.display(), ); let args = Args::parse_args().context("could not parse arguments")?; @@ -90,7 +86,17 @@ FLAGS: Err(err) => return Err(Error::new(err)), }; - setup_logging(logpath, args.verbosity).context("failed to initialize logging")?; + let log_path = config + .log_file + .clone() + .map(PathBuf::from) + .unwrap_or(log_path_default); + let log_dir = log_path.parent().unwrap(); + + if !log_dir.exists() { + std::fs::create_dir_all(log_dir).ok(); + } + setup_logging(log_path, args.verbosity).context("failed to initialize logging")?; // TODO: use the thread local executor to spawn the application task separately from the work pool let mut app = Application::new(args, config).context("unable to create new application")?; From 2ca9c3a4b9db30f4cec3e6af04f43a6498bcba94 Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Thu, 12 Aug 2021 10:31:03 +0300 Subject: [PATCH 2/4] helix-term: show configured logfile in help --- helix-dap/Cargo.toml | 20 +++ helix-dap/examples/dap-basic.rs | 44 +++++ helix-dap/src/client.rs | 264 ++++++++++++++++++++++++++++++ helix-dap/src/lib.rs | 21 +++ helix-dap/src/transport.rs | 282 ++++++++++++++++++++++++++++++++ helix-term/src/main.rs | 37 ++--- 6 files changed, 649 insertions(+), 19 deletions(-) create mode 100644 helix-dap/Cargo.toml create mode 100644 helix-dap/examples/dap-basic.rs create mode 100644 helix-dap/src/client.rs create mode 100644 helix-dap/src/lib.rs create mode 100644 helix-dap/src/transport.rs diff --git a/helix-dap/Cargo.toml b/helix-dap/Cargo.toml new file mode 100644 index 000000000000..6adaaeddf27f --- /dev/null +++ b/helix-dap/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "helix-dap" +version = "0.3.0" +authors = ["Blaž Hrastnik "] +edition = "2018" +license = "MPL-2.0" +description = "DAP client implementation for Helix project" +categories = ["editor"] +repository = "https://github.com/helix-editor/helix" +homepage = "https://helix-editor.com" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +anyhow = "1.0" +log = "0.4" +serde = { version = "1.0", features = ["derive"] } +serde_json = "1.0" +thiserror = "1.0" +tokio = { version = "1.9", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] } diff --git a/helix-dap/examples/dap-basic.rs b/helix-dap/examples/dap-basic.rs new file mode 100644 index 000000000000..44a55ae0ae6d --- /dev/null +++ b/helix-dap/examples/dap-basic.rs @@ -0,0 +1,44 @@ +use helix_dap::{Client, Result, SourceBreakpoint}; + +#[tokio::main] +pub async fn main() -> Result<()> { + let mut client = Client::start("nc", vec!["127.0.0.1", "7777"], 0)?; + + println!("init: {:?}", client.initialize().await); + println!("caps: {:?}", client.capabilities()); + println!( + "launch: {:?}", + client.launch("/tmp/godebug/main".to_owned()).await + ); + + println!( + "breakpoints: {:?}", + client + .set_breakpoints( + "/tmp/godebug/main.go".to_owned(), + vec![SourceBreakpoint { + line: 6, + column: Some(2), + }] + ) + .await + ); + + let mut _in = String::new(); + std::io::stdin() + .read_line(&mut _in) + .expect("Failed to read line"); + + println!("configurationDone: {:?}", client.configuration_done().await); + + println!("stopped: {:?}", client.wait_for_stopped().await); + + let mut _in = String::new(); + std::io::stdin() + .read_line(&mut _in) + .expect("Failed to read line"); + + println!("disconnect: {:?}", client.disconnect().await); + + Ok(()) +} diff --git a/helix-dap/src/client.rs b/helix-dap/src/client.rs new file mode 100644 index 000000000000..b1a14c398819 --- /dev/null +++ b/helix-dap/src/client.rs @@ -0,0 +1,264 @@ +use crate::{ + transport::{Event, Payload, Request, Response, Transport}, + Result, +}; +use serde::{Deserialize, Serialize}; +use serde_json::{from_value, to_value, Value}; +use std::process::Stdio; +use std::sync::atomic::{AtomicU64, Ordering}; +use tokio::{ + io::{BufReader, BufWriter}, + process::{Child, Command}, + sync::mpsc::{channel, UnboundedReceiver, UnboundedSender}, +}; + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct DebuggerCapabilities { + supports_configuration_done_request: bool, + supports_function_breakpoints: bool, + supports_conditional_breakpoints: bool, + supports_exception_info_request: bool, + support_terminate_debuggee: bool, + supports_delayed_stack_trace_loading: bool, +} + +#[derive(Debug)] +pub struct Client { + id: usize, + _process: Child, + server_tx: UnboundedSender, + server_rx: UnboundedReceiver, + request_counter: AtomicU64, + capabilities: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct InitializeArguments { + client_id: String, + client_name: String, + adapter_id: String, + locale: String, + #[serde(rename = "linesStartAt1")] + lines_start_at_one: bool, + #[serde(rename = "columnsStartAt1")] + columns_start_at_one: bool, + path_format: String, + supports_variable_type: bool, + supports_variable_paging: bool, + supports_run_in_terminal_request: bool, + supports_memory_references: bool, + supports_progress_reporting: bool, + supports_invalidated_event: bool, +} + +// TODO: split out +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct LaunchArguments { + mode: String, + program: String, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Source { + path: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct SourceBreakpoint { + pub line: usize, + pub column: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct SetBreakpointsArguments { + source: Source, + breakpoints: Option>, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +pub struct Breakpoint { + pub id: Option, + pub verified: bool, + pub message: Option, + pub source: Option, + pub line: Option, + pub column: Option, + pub end_line: Option, + pub end_column: Option, + pub instruction_reference: Option, + pub offset: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +#[serde(rename_all = "camelCase")] +struct SetBreakpointsResponseBody { + breakpoints: Option>, +} + +impl Client { + pub fn start(cmd: &str, args: Vec<&str>, id: usize) -> Result { + let process = Command::new(cmd) + .args(args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + // make sure the process is reaped on drop + .kill_on_drop(true) + .spawn(); + + let mut process = process?; + + // TODO: do we need bufreader/writer here? or do we use async wrappers on unblock? + let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin")); + let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout")); + + let (server_rx, server_tx) = Transport::start(reader, writer, id); + + let client = Self { + id, + _process: process, + server_tx, + server_rx, + request_counter: AtomicU64::new(0), + capabilities: None, + }; + + // TODO: async client.initialize() + // maybe use an arc flag + + Ok(client) + } + + pub fn id(&self) -> usize { + self.id + } + + fn next_request_id(&self) -> u64 { + self.request_counter.fetch_add(1, Ordering::Relaxed) + } + + async fn request(&self, command: String, arguments: Option) -> Result { + let (callback_rx, mut callback_tx) = channel(1); + + let req = Request { + back_ch: Some(callback_rx), + seq: self.next_request_id(), + msg_type: "request".to_owned(), + command, + arguments, + }; + + self.server_tx + .send(req) + .expect("Failed to send request to debugger"); + + callback_tx + .recv() + .await + .expect("Failed to receive response") + } + + pub fn capabilities(&self) -> &DebuggerCapabilities { + self.capabilities + .as_ref() + .expect("language server not yet initialized!") + } + + pub async fn initialize(&mut self) -> Result<()> { + let args = InitializeArguments { + client_id: "hx".to_owned(), + client_name: "helix".to_owned(), + adapter_id: "go".to_owned(), + locale: "en-us".to_owned(), + lines_start_at_one: true, + columns_start_at_one: true, + path_format: "path".to_owned(), + supports_variable_type: false, + supports_variable_paging: false, + supports_run_in_terminal_request: false, + supports_memory_references: false, + supports_progress_reporting: true, + supports_invalidated_event: true, + }; + + let response = self + .request("initialize".to_owned(), to_value(args).ok()) + .await?; + self.capabilities = from_value(response.body.unwrap()).ok(); + + Ok(()) + } + + pub async fn disconnect(&mut self) -> Result<()> { + self.request("disconnect".to_owned(), None).await?; + Ok(()) + } + + pub async fn launch(&mut self, executable: String) -> Result<()> { + let args = LaunchArguments { + mode: "exec".to_owned(), + program: executable, + }; + + self.request("launch".to_owned(), to_value(args).ok()) + .await?; + + match self + .server_rx + .recv() + .await + .expect("Expected initialized event") + { + Payload::Event(Event { event, .. }) => { + if event == "initialized".to_owned() { + Ok(()) + } else { + unreachable!() + } + } + _ => unreachable!(), + } + } + + pub async fn set_breakpoints( + &mut self, + file: String, + breakpoints: Vec, + ) -> Result>> { + let args = SetBreakpointsArguments { + source: Source { path: Some(file) }, + breakpoints: Some(breakpoints), + }; + + let response = self + .request("setBreakpoints".to_owned(), to_value(args).ok()) + .await?; + let body: Option = from_value(response.body.unwrap()).ok(); + + Ok(body.map(|b| b.breakpoints).unwrap()) + } + + pub async fn configuration_done(&mut self) -> Result<()> { + self.request("configurationDone".to_owned(), None).await?; + Ok(()) + } + + pub async fn wait_for_stopped(&mut self) -> Result<()> { + match self.server_rx.recv().await.expect("Expected stopped event") { + Payload::Event(Event { event, .. }) => { + if event == "stopped".to_owned() { + Ok(()) + } else { + unreachable!() + } + } + _ => unreachable!(), + } + } +} diff --git a/helix-dap/src/lib.rs b/helix-dap/src/lib.rs new file mode 100644 index 000000000000..1e545fd89c68 --- /dev/null +++ b/helix-dap/src/lib.rs @@ -0,0 +1,21 @@ +mod client; +mod transport; + +pub use client::{Breakpoint, Client, SourceBreakpoint}; +pub use transport::{Event, Payload, Request, Response, Transport}; + +use thiserror::Error; +#[derive(Error, Debug)] +pub enum Error { + #[error("failed to parse: {0}")] + Parse(#[from] serde_json::Error), + #[error("IO Error: {0}")] + IO(#[from] std::io::Error), + #[error("request timed out")] + Timeout, + #[error("server closed the stream")] + StreamClosed, + #[error(transparent)] + Other(#[from] anyhow::Error), +} +pub type Result = core::result::Result; diff --git a/helix-dap/src/transport.rs b/helix-dap/src/transport.rs new file mode 100644 index 000000000000..1c004cfeae60 --- /dev/null +++ b/helix-dap/src/transport.rs @@ -0,0 +1,282 @@ +use crate::{Error, Result}; +use anyhow::Context; +use log::error; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::collections::HashMap; +use std::sync::Arc; +use tokio::{ + io::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, + process::{ChildStdin, ChildStdout}, + sync::{ + mpsc::{unbounded_channel, Sender, UnboundedReceiver, UnboundedSender}, + Mutex, + }, +}; + +#[derive(Debug, Clone, Deserialize, Serialize)] +pub struct Request { + #[serde(skip)] + pub back_ch: Option>>, + pub seq: u64, + #[serde(rename = "type")] + pub msg_type: String, + pub command: String, + pub arguments: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +pub struct Response { + pub seq: u64, + #[serde(rename = "type")] + pub msg_type: String, + pub request_seq: u64, + pub success: bool, + pub command: String, + pub message: Option, + pub body: Option, +} + +#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] +pub struct Event { + pub seq: u64, + #[serde(rename = "type")] + pub msg_type: String, + pub event: String, + pub body: Option, +} + +#[derive(Debug, Clone, Deserialize, Serialize)] +#[serde(untagged)] +pub enum Payload { + // type = "event" + Event(Event), + // type = "response" + Response(Response), + // type = "request" + Request(Request), +} + +#[derive(Debug)] +pub struct Transport { + id: usize, + pending_requests: Mutex>>>, +} + +impl Transport { + pub fn start( + server_stdout: BufReader, + server_stdin: BufWriter, + id: usize, + ) -> (UnboundedReceiver, UnboundedSender) { + let (client_tx, rx) = unbounded_channel(); + let (tx, client_rx) = unbounded_channel(); + + let transport = Self { + id, + pending_requests: Mutex::new(HashMap::default()), + }; + + let transport = Arc::new(transport); + + tokio::spawn(Self::recv(transport.clone(), server_stdout, client_tx)); + tokio::spawn(Self::send(transport, server_stdin, client_rx)); + + (rx, tx) + } + + async fn recv_server_message( + reader: &mut (impl AsyncBufRead + Unpin + Send), + buffer: &mut String, + ) -> Result { + let mut content_length = None; + loop { + buffer.truncate(0); + reader.read_line(buffer).await?; + let header = buffer.trim(); + + if header.is_empty() { + break; + } + + let mut parts = header.split(": "); + + match (parts.next(), parts.next(), parts.next()) { + (Some("Content-Length"), Some(value), None) => { + content_length = Some(value.parse().context("invalid content length")?); + } + (Some(_), Some(_), None) => {} + _ => { + return Err(std::io::Error::new( + std::io::ErrorKind::Other, + "Failed to parse header", + ) + .into()); + } + } + } + + let content_length = content_length.context("missing content length")?; + + //TODO: reuse vector + let mut content = vec![0; content_length]; + reader.read_exact(&mut content).await?; + let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?; + + // TODO: `info!` here + println!("<- DAP {}", msg); + + // try parsing as output (server response) or call (server request) + let output: serde_json::Result = serde_json::from_str(msg); + + Ok(output?) + } + + async fn send_payload_to_server( + &self, + server_stdin: &mut BufWriter, + req: Request, + ) -> Result<()> { + let json = serde_json::to_string(&req)?; + match req.back_ch { + Some(back) => { + self.pending_requests.lock().await.insert(req.seq, back); + () + } + None => {} + } + self.send_string_to_server(server_stdin, json).await + } + + async fn send_string_to_server( + &self, + server_stdin: &mut BufWriter, + request: String, + ) -> Result<()> { + // TODO: `info!` here + println!("-> DAP {}", request); + + // send the headers + server_stdin + .write_all(format!("Content-Length: {}\r\n\r\n", request.len()).as_bytes()) + .await?; + + // send the body + server_stdin.write_all(request.as_bytes()).await?; + + server_stdin.flush().await?; + + Ok(()) + } + + async fn process_server_message( + &self, + client_tx: &UnboundedSender, + msg: Payload, + ) -> Result<()> { + let (id, result) = match msg { + Payload::Response(Response { + success: true, + seq, + request_seq, + .. + }) => { + // TODO: `info!` here + println!("<- DAP success ({}, in response to {})", seq, request_seq); + if let Payload::Response(val) = msg { + (request_seq, Ok(val)) + } else { + unreachable!(); + } + } + Payload::Response(Response { + success: false, + message, + body, + request_seq, + command, + .. + }) => { + // TODO: `error!` here + println!( + "<- DAP error {:?} ({:?}) for command #{} {}", + message, body, request_seq, command + ); + ( + request_seq, + Err(Error::Other(anyhow::format_err!("{:?}", body))), + ) + } + Payload::Request(Request { + ref command, + ref seq, + .. + }) => { + // TODO: `info!` here + println!("<- DAP request {} #{}", command, seq); + client_tx.send(msg).expect("Failed to send"); + return Ok(()); + } + Payload::Event(Event { + ref event, ref seq, .. + }) => { + // TODO: `info!` here + println!("<- DAP event {} #{}", event, seq); + client_tx.send(msg).expect("Failed to send"); + return Ok(()); + } + }; + + let tx = self + .pending_requests + .lock() + .await + .remove(&id) + .expect("pending_request with id not found!"); + + match tx.send(result).await { + Ok(_) => (), + Err(_) => error!( + "Tried sending response into a closed channel (id={:?}), original request likely timed out", + id + ), + }; + + Ok(()) + } + + async fn recv( + transport: Arc, + mut server_stdout: BufReader, + client_tx: UnboundedSender, + ) { + let mut recv_buffer = String::new(); + loop { + match Self::recv_server_message(&mut server_stdout, &mut recv_buffer).await { + Ok(msg) => { + transport + .process_server_message(&client_tx, msg) + .await + .unwrap(); + } + Err(err) => { + error!("err: <- {:?}", err); + break; + } + } + } + } + + async fn send( + transport: Arc, + mut server_stdin: BufWriter, + mut client_rx: UnboundedReceiver, + ) { + while let Some(req) = client_rx.recv().await { + transport + .send_payload_to_server(&mut server_stdin, req) + .await + .unwrap() + } + } +} diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index e9c43b64c356..de6fa69fd0cd 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -35,7 +35,22 @@ fn setup_logging(logpath: PathBuf, verbosity: u64) -> Result<()> { #[tokio::main] async fn main() -> Result<()> { - let log_path_default = helix_core::cache_dir().join("helix.log"); + let conf_dir = helix_core::config_dir(); + if !conf_dir.exists() { + std::fs::create_dir_all(&conf_dir).ok(); + } + + let config = match std::fs::read_to_string(conf_dir.join("config.toml")) { + Ok(config) => merge_keys(toml::from_str(&config)?), + Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(), + Err(err) => return Err(Error::new(err)), + }; + + let log_path = config + .log_file + .clone() + .map(PathBuf::from) + .unwrap_or(helix_core::cache_dir().join("helix.log")); let help = format!( "\ @@ -52,14 +67,14 @@ ARGS: FLAGS: -h, --help Prints help information -v Increases logging verbosity each use for up to 3 times - (default file: {}) + (file: {}) -V, --version Prints version information ", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), env!("CARGO_PKG_AUTHORS"), env!("CARGO_PKG_DESCRIPTION"), - log_path_default.display(), + log_path.display(), ); let args = Args::parse_args().context("could not parse arguments")?; @@ -75,22 +90,6 @@ FLAGS: std::process::exit(0); } - let conf_dir = helix_core::config_dir(); - if !conf_dir.exists() { - std::fs::create_dir_all(&conf_dir).ok(); - } - - let config = match std::fs::read_to_string(conf_dir.join("config.toml")) { - Ok(config) => merge_keys(toml::from_str(&config)?), - Err(err) if err.kind() == std::io::ErrorKind::NotFound => Config::default(), - Err(err) => return Err(Error::new(err)), - }; - - let log_path = config - .log_file - .clone() - .map(PathBuf::from) - .unwrap_or(log_path_default); let log_dir = log_path.parent().unwrap(); if !log_dir.exists() { From b75416a89f682321bd9e6ab8840bc17110874eb7 Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Thu, 12 Aug 2021 13:36:21 +0300 Subject: [PATCH 3/4] restore default variable for readability --- helix-term/src/main.rs | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/helix-term/src/main.rs b/helix-term/src/main.rs index de6fa69fd0cd..3f12dd175854 100644 --- a/helix-term/src/main.rs +++ b/helix-term/src/main.rs @@ -46,11 +46,12 @@ async fn main() -> Result<()> { Err(err) => return Err(Error::new(err)), }; + let default_log_path = helix_core::cache_dir().join("helix.log"); let log_path = config .log_file .clone() .map(PathBuf::from) - .unwrap_or(helix_core::cache_dir().join("helix.log")); + .unwrap_or(default_log_path); let help = format!( "\ From 85917069483d6469ced62c2e743898b5c1620289 Mon Sep 17 00:00:00 2001 From: Dmitry Sharshakov Date: Sun, 15 Aug 2021 13:03:59 +0300 Subject: [PATCH 4/4] remove mistakenly added files --- helix-dap/Cargo.toml | 20 --- helix-dap/examples/dap-basic.rs | 44 ----- helix-dap/src/client.rs | 264 ------------------------------ helix-dap/src/lib.rs | 21 --- helix-dap/src/transport.rs | 282 -------------------------------- 5 files changed, 631 deletions(-) delete mode 100644 helix-dap/Cargo.toml delete mode 100644 helix-dap/examples/dap-basic.rs delete mode 100644 helix-dap/src/client.rs delete mode 100644 helix-dap/src/lib.rs delete mode 100644 helix-dap/src/transport.rs diff --git a/helix-dap/Cargo.toml b/helix-dap/Cargo.toml deleted file mode 100644 index 6adaaeddf27f..000000000000 --- a/helix-dap/Cargo.toml +++ /dev/null @@ -1,20 +0,0 @@ -[package] -name = "helix-dap" -version = "0.3.0" -authors = ["Blaž Hrastnik "] -edition = "2018" -license = "MPL-2.0" -description = "DAP client implementation for Helix project" -categories = ["editor"] -repository = "https://github.com/helix-editor/helix" -homepage = "https://helix-editor.com" - -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - -[dependencies] -anyhow = "1.0" -log = "0.4" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" -thiserror = "1.0" -tokio = { version = "1.9", features = ["rt", "rt-multi-thread", "io-util", "io-std", "time", "process", "macros", "fs", "parking_lot"] } diff --git a/helix-dap/examples/dap-basic.rs b/helix-dap/examples/dap-basic.rs deleted file mode 100644 index 44a55ae0ae6d..000000000000 --- a/helix-dap/examples/dap-basic.rs +++ /dev/null @@ -1,44 +0,0 @@ -use helix_dap::{Client, Result, SourceBreakpoint}; - -#[tokio::main] -pub async fn main() -> Result<()> { - let mut client = Client::start("nc", vec!["127.0.0.1", "7777"], 0)?; - - println!("init: {:?}", client.initialize().await); - println!("caps: {:?}", client.capabilities()); - println!( - "launch: {:?}", - client.launch("/tmp/godebug/main".to_owned()).await - ); - - println!( - "breakpoints: {:?}", - client - .set_breakpoints( - "/tmp/godebug/main.go".to_owned(), - vec![SourceBreakpoint { - line: 6, - column: Some(2), - }] - ) - .await - ); - - let mut _in = String::new(); - std::io::stdin() - .read_line(&mut _in) - .expect("Failed to read line"); - - println!("configurationDone: {:?}", client.configuration_done().await); - - println!("stopped: {:?}", client.wait_for_stopped().await); - - let mut _in = String::new(); - std::io::stdin() - .read_line(&mut _in) - .expect("Failed to read line"); - - println!("disconnect: {:?}", client.disconnect().await); - - Ok(()) -} diff --git a/helix-dap/src/client.rs b/helix-dap/src/client.rs deleted file mode 100644 index b1a14c398819..000000000000 --- a/helix-dap/src/client.rs +++ /dev/null @@ -1,264 +0,0 @@ -use crate::{ - transport::{Event, Payload, Request, Response, Transport}, - Result, -}; -use serde::{Deserialize, Serialize}; -use serde_json::{from_value, to_value, Value}; -use std::process::Stdio; -use std::sync::atomic::{AtomicU64, Ordering}; -use tokio::{ - io::{BufReader, BufWriter}, - process::{Child, Command}, - sync::mpsc::{channel, UnboundedReceiver, UnboundedSender}, -}; - -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct DebuggerCapabilities { - supports_configuration_done_request: bool, - supports_function_breakpoints: bool, - supports_conditional_breakpoints: bool, - supports_exception_info_request: bool, - support_terminate_debuggee: bool, - supports_delayed_stack_trace_loading: bool, -} - -#[derive(Debug)] -pub struct Client { - id: usize, - _process: Child, - server_tx: UnboundedSender, - server_rx: UnboundedReceiver, - request_counter: AtomicU64, - capabilities: Option, -} - -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -struct InitializeArguments { - client_id: String, - client_name: String, - adapter_id: String, - locale: String, - #[serde(rename = "linesStartAt1")] - lines_start_at_one: bool, - #[serde(rename = "columnsStartAt1")] - columns_start_at_one: bool, - path_format: String, - supports_variable_type: bool, - supports_variable_paging: bool, - supports_run_in_terminal_request: bool, - supports_memory_references: bool, - supports_progress_reporting: bool, - supports_invalidated_event: bool, -} - -// TODO: split out -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -struct LaunchArguments { - mode: String, - program: String, -} - -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Source { - path: Option, -} - -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct SourceBreakpoint { - pub line: usize, - pub column: Option, -} - -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -struct SetBreakpointsArguments { - source: Source, - breakpoints: Option>, -} - -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -pub struct Breakpoint { - pub id: Option, - pub verified: bool, - pub message: Option, - pub source: Option, - pub line: Option, - pub column: Option, - pub end_line: Option, - pub end_column: Option, - pub instruction_reference: Option, - pub offset: Option, -} - -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -#[serde(rename_all = "camelCase")] -struct SetBreakpointsResponseBody { - breakpoints: Option>, -} - -impl Client { - pub fn start(cmd: &str, args: Vec<&str>, id: usize) -> Result { - let process = Command::new(cmd) - .args(args) - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - // make sure the process is reaped on drop - .kill_on_drop(true) - .spawn(); - - let mut process = process?; - - // TODO: do we need bufreader/writer here? or do we use async wrappers on unblock? - let writer = BufWriter::new(process.stdin.take().expect("Failed to open stdin")); - let reader = BufReader::new(process.stdout.take().expect("Failed to open stdout")); - - let (server_rx, server_tx) = Transport::start(reader, writer, id); - - let client = Self { - id, - _process: process, - server_tx, - server_rx, - request_counter: AtomicU64::new(0), - capabilities: None, - }; - - // TODO: async client.initialize() - // maybe use an arc flag - - Ok(client) - } - - pub fn id(&self) -> usize { - self.id - } - - fn next_request_id(&self) -> u64 { - self.request_counter.fetch_add(1, Ordering::Relaxed) - } - - async fn request(&self, command: String, arguments: Option) -> Result { - let (callback_rx, mut callback_tx) = channel(1); - - let req = Request { - back_ch: Some(callback_rx), - seq: self.next_request_id(), - msg_type: "request".to_owned(), - command, - arguments, - }; - - self.server_tx - .send(req) - .expect("Failed to send request to debugger"); - - callback_tx - .recv() - .await - .expect("Failed to receive response") - } - - pub fn capabilities(&self) -> &DebuggerCapabilities { - self.capabilities - .as_ref() - .expect("language server not yet initialized!") - } - - pub async fn initialize(&mut self) -> Result<()> { - let args = InitializeArguments { - client_id: "hx".to_owned(), - client_name: "helix".to_owned(), - adapter_id: "go".to_owned(), - locale: "en-us".to_owned(), - lines_start_at_one: true, - columns_start_at_one: true, - path_format: "path".to_owned(), - supports_variable_type: false, - supports_variable_paging: false, - supports_run_in_terminal_request: false, - supports_memory_references: false, - supports_progress_reporting: true, - supports_invalidated_event: true, - }; - - let response = self - .request("initialize".to_owned(), to_value(args).ok()) - .await?; - self.capabilities = from_value(response.body.unwrap()).ok(); - - Ok(()) - } - - pub async fn disconnect(&mut self) -> Result<()> { - self.request("disconnect".to_owned(), None).await?; - Ok(()) - } - - pub async fn launch(&mut self, executable: String) -> Result<()> { - let args = LaunchArguments { - mode: "exec".to_owned(), - program: executable, - }; - - self.request("launch".to_owned(), to_value(args).ok()) - .await?; - - match self - .server_rx - .recv() - .await - .expect("Expected initialized event") - { - Payload::Event(Event { event, .. }) => { - if event == "initialized".to_owned() { - Ok(()) - } else { - unreachable!() - } - } - _ => unreachable!(), - } - } - - pub async fn set_breakpoints( - &mut self, - file: String, - breakpoints: Vec, - ) -> Result>> { - let args = SetBreakpointsArguments { - source: Source { path: Some(file) }, - breakpoints: Some(breakpoints), - }; - - let response = self - .request("setBreakpoints".to_owned(), to_value(args).ok()) - .await?; - let body: Option = from_value(response.body.unwrap()).ok(); - - Ok(body.map(|b| b.breakpoints).unwrap()) - } - - pub async fn configuration_done(&mut self) -> Result<()> { - self.request("configurationDone".to_owned(), None).await?; - Ok(()) - } - - pub async fn wait_for_stopped(&mut self) -> Result<()> { - match self.server_rx.recv().await.expect("Expected stopped event") { - Payload::Event(Event { event, .. }) => { - if event == "stopped".to_owned() { - Ok(()) - } else { - unreachable!() - } - } - _ => unreachable!(), - } - } -} diff --git a/helix-dap/src/lib.rs b/helix-dap/src/lib.rs deleted file mode 100644 index 1e545fd89c68..000000000000 --- a/helix-dap/src/lib.rs +++ /dev/null @@ -1,21 +0,0 @@ -mod client; -mod transport; - -pub use client::{Breakpoint, Client, SourceBreakpoint}; -pub use transport::{Event, Payload, Request, Response, Transport}; - -use thiserror::Error; -#[derive(Error, Debug)] -pub enum Error { - #[error("failed to parse: {0}")] - Parse(#[from] serde_json::Error), - #[error("IO Error: {0}")] - IO(#[from] std::io::Error), - #[error("request timed out")] - Timeout, - #[error("server closed the stream")] - StreamClosed, - #[error(transparent)] - Other(#[from] anyhow::Error), -} -pub type Result = core::result::Result; diff --git a/helix-dap/src/transport.rs b/helix-dap/src/transport.rs deleted file mode 100644 index 1c004cfeae60..000000000000 --- a/helix-dap/src/transport.rs +++ /dev/null @@ -1,282 +0,0 @@ -use crate::{Error, Result}; -use anyhow::Context; -use log::error; -use serde::{Deserialize, Serialize}; -use serde_json::Value; -use std::collections::HashMap; -use std::sync::Arc; -use tokio::{ - io::{AsyncBufRead, AsyncBufReadExt, AsyncReadExt, AsyncWriteExt, BufReader, BufWriter}, - process::{ChildStdin, ChildStdout}, - sync::{ - mpsc::{unbounded_channel, Sender, UnboundedReceiver, UnboundedSender}, - Mutex, - }, -}; - -#[derive(Debug, Clone, Deserialize, Serialize)] -pub struct Request { - #[serde(skip)] - pub back_ch: Option>>, - pub seq: u64, - #[serde(rename = "type")] - pub msg_type: String, - pub command: String, - pub arguments: Option, -} - -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -pub struct Response { - pub seq: u64, - #[serde(rename = "type")] - pub msg_type: String, - pub request_seq: u64, - pub success: bool, - pub command: String, - pub message: Option, - pub body: Option, -} - -#[derive(Debug, PartialEq, Clone, Deserialize, Serialize)] -pub struct Event { - pub seq: u64, - #[serde(rename = "type")] - pub msg_type: String, - pub event: String, - pub body: Option, -} - -#[derive(Debug, Clone, Deserialize, Serialize)] -#[serde(untagged)] -pub enum Payload { - // type = "event" - Event(Event), - // type = "response" - Response(Response), - // type = "request" - Request(Request), -} - -#[derive(Debug)] -pub struct Transport { - id: usize, - pending_requests: Mutex>>>, -} - -impl Transport { - pub fn start( - server_stdout: BufReader, - server_stdin: BufWriter, - id: usize, - ) -> (UnboundedReceiver, UnboundedSender) { - let (client_tx, rx) = unbounded_channel(); - let (tx, client_rx) = unbounded_channel(); - - let transport = Self { - id, - pending_requests: Mutex::new(HashMap::default()), - }; - - let transport = Arc::new(transport); - - tokio::spawn(Self::recv(transport.clone(), server_stdout, client_tx)); - tokio::spawn(Self::send(transport, server_stdin, client_rx)); - - (rx, tx) - } - - async fn recv_server_message( - reader: &mut (impl AsyncBufRead + Unpin + Send), - buffer: &mut String, - ) -> Result { - let mut content_length = None; - loop { - buffer.truncate(0); - reader.read_line(buffer).await?; - let header = buffer.trim(); - - if header.is_empty() { - break; - } - - let mut parts = header.split(": "); - - match (parts.next(), parts.next(), parts.next()) { - (Some("Content-Length"), Some(value), None) => { - content_length = Some(value.parse().context("invalid content length")?); - } - (Some(_), Some(_), None) => {} - _ => { - return Err(std::io::Error::new( - std::io::ErrorKind::Other, - "Failed to parse header", - ) - .into()); - } - } - } - - let content_length = content_length.context("missing content length")?; - - //TODO: reuse vector - let mut content = vec![0; content_length]; - reader.read_exact(&mut content).await?; - let msg = std::str::from_utf8(&content).context("invalid utf8 from server")?; - - // TODO: `info!` here - println!("<- DAP {}", msg); - - // try parsing as output (server response) or call (server request) - let output: serde_json::Result = serde_json::from_str(msg); - - Ok(output?) - } - - async fn send_payload_to_server( - &self, - server_stdin: &mut BufWriter, - req: Request, - ) -> Result<()> { - let json = serde_json::to_string(&req)?; - match req.back_ch { - Some(back) => { - self.pending_requests.lock().await.insert(req.seq, back); - () - } - None => {} - } - self.send_string_to_server(server_stdin, json).await - } - - async fn send_string_to_server( - &self, - server_stdin: &mut BufWriter, - request: String, - ) -> Result<()> { - // TODO: `info!` here - println!("-> DAP {}", request); - - // send the headers - server_stdin - .write_all(format!("Content-Length: {}\r\n\r\n", request.len()).as_bytes()) - .await?; - - // send the body - server_stdin.write_all(request.as_bytes()).await?; - - server_stdin.flush().await?; - - Ok(()) - } - - async fn process_server_message( - &self, - client_tx: &UnboundedSender, - msg: Payload, - ) -> Result<()> { - let (id, result) = match msg { - Payload::Response(Response { - success: true, - seq, - request_seq, - .. - }) => { - // TODO: `info!` here - println!("<- DAP success ({}, in response to {})", seq, request_seq); - if let Payload::Response(val) = msg { - (request_seq, Ok(val)) - } else { - unreachable!(); - } - } - Payload::Response(Response { - success: false, - message, - body, - request_seq, - command, - .. - }) => { - // TODO: `error!` here - println!( - "<- DAP error {:?} ({:?}) for command #{} {}", - message, body, request_seq, command - ); - ( - request_seq, - Err(Error::Other(anyhow::format_err!("{:?}", body))), - ) - } - Payload::Request(Request { - ref command, - ref seq, - .. - }) => { - // TODO: `info!` here - println!("<- DAP request {} #{}", command, seq); - client_tx.send(msg).expect("Failed to send"); - return Ok(()); - } - Payload::Event(Event { - ref event, ref seq, .. - }) => { - // TODO: `info!` here - println!("<- DAP event {} #{}", event, seq); - client_tx.send(msg).expect("Failed to send"); - return Ok(()); - } - }; - - let tx = self - .pending_requests - .lock() - .await - .remove(&id) - .expect("pending_request with id not found!"); - - match tx.send(result).await { - Ok(_) => (), - Err(_) => error!( - "Tried sending response into a closed channel (id={:?}), original request likely timed out", - id - ), - }; - - Ok(()) - } - - async fn recv( - transport: Arc, - mut server_stdout: BufReader, - client_tx: UnboundedSender, - ) { - let mut recv_buffer = String::new(); - loop { - match Self::recv_server_message(&mut server_stdout, &mut recv_buffer).await { - Ok(msg) => { - transport - .process_server_message(&client_tx, msg) - .await - .unwrap(); - } - Err(err) => { - error!("err: <- {:?}", err); - break; - } - } - } - } - - async fn send( - transport: Arc, - mut server_stdin: BufWriter, - mut client_rx: UnboundedReceiver, - ) { - while let Some(req) = client_rx.recv().await { - transport - .send_payload_to_server(&mut server_stdin, req) - .await - .unwrap() - } - } -}