From 177becea98bddcd995a2abec59c6b60bac6b5a2b Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Thu, 16 Apr 2020 21:13:57 +0800 Subject: Add proc-macro cli command for rust-analyzer --- crates/ra_proc_macro_srv/src/cli.rs | 56 ++++++++++++++++++++++++++++++++++++ crates/ra_proc_macro_srv/src/lib.rs | 6 ++-- crates/ra_proc_macro_srv/src/main.rs | 55 ----------------------------------- 3 files changed, 60 insertions(+), 57 deletions(-) create mode 100644 crates/ra_proc_macro_srv/src/cli.rs delete mode 100644 crates/ra_proc_macro_srv/src/main.rs (limited to 'crates/ra_proc_macro_srv') diff --git a/crates/ra_proc_macro_srv/src/cli.rs b/crates/ra_proc_macro_srv/src/cli.rs new file mode 100644 index 000000000..c771f2b38 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/cli.rs @@ -0,0 +1,56 @@ +//! Driver for proc macro server + +use crate::{expand_task, list_macros}; +use ra_proc_macro::msg::{self, Message}; + +use std::io; + +fn read_request() -> Result, io::Error> { + let stdin = io::stdin(); + let mut stdin = stdin.lock(); + msg::Request::read(&mut stdin) +} + +fn write_response(res: Result) -> Result<(), io::Error> { + let msg: msg::Response = match res { + Ok(res) => res, + Err(err) => msg::Response::Error(msg::ResponseError { + code: msg::ErrorCode::ExpansionError, + message: err, + }), + }; + + let stdout = io::stdout(); + let mut stdout = stdout.lock(); + msg.write(&mut stdout) +} + +pub fn run() { + loop { + let req = match read_request() { + Err(err) => { + eprintln!("Read message error on ra_proc_macro_srv: {}", err.to_string()); + continue; + } + Ok(None) => continue, + Ok(Some(req)) => req, + }; + + match req { + msg::Request::ListMacro(task) => { + if let Err(err) = + write_response(list_macros(&task).map(|it| msg::Response::ListMacro(it))) + { + eprintln!("Write message error on list macro: {}", err); + } + } + msg::Request::ExpansionMacro(task) => { + if let Err(err) = + write_response(expand_task(&task).map(|it| msg::Response::ExpansionMacro(it))) + { + eprintln!("Write message error on expansion macro: {}", err); + } + } + } + } +} diff --git a/crates/ra_proc_macro_srv/src/lib.rs b/crates/ra_proc_macro_srv/src/lib.rs index 59716cbb3..c62b0ed89 100644 --- a/crates/ra_proc_macro_srv/src/lib.rs +++ b/crates/ra_proc_macro_srv/src/lib.rs @@ -22,7 +22,7 @@ mod dylib; use proc_macro::bridge::client::TokenStream; use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask}; -pub fn expand_task(task: &ExpansionTask) -> Result { +pub(crate) fn expand_task(task: &ExpansionTask) -> Result { let expander = dylib::Expander::new(&task.lib) .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); @@ -39,7 +39,7 @@ pub fn expand_task(task: &ExpansionTask) -> Result { } } -pub fn list_macros(task: &ListMacrosTask) -> Result { +pub(crate) fn list_macros(task: &ListMacrosTask) -> Result { let expander = dylib::Expander::new(&task.lib) .expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib)); @@ -53,5 +53,7 @@ pub fn list_macros(task: &ListMacrosTask) -> Result { } } +pub mod cli; + #[cfg(test)] mod tests; diff --git a/crates/ra_proc_macro_srv/src/main.rs b/crates/ra_proc_macro_srv/src/main.rs deleted file mode 100644 index 70743c1f4..000000000 --- a/crates/ra_proc_macro_srv/src/main.rs +++ /dev/null @@ -1,55 +0,0 @@ -//! Driver for proc macro server - -use ra_proc_macro::msg::{self, Message}; -use ra_proc_macro_srv::{expand_task, list_macros}; - -use std::io; - -fn read_request() -> Result, io::Error> { - let stdin = io::stdin(); - let mut stdin = stdin.lock(); - msg::Request::read(&mut stdin) -} - -fn write_response(res: Result) -> Result<(), io::Error> { - let msg: msg::Response = match res { - Ok(res) => res, - Err(err) => msg::Response::Error(msg::ResponseError { - code: msg::ErrorCode::ExpansionError, - message: err, - }), - }; - - let stdout = io::stdout(); - let mut stdout = stdout.lock(); - msg.write(&mut stdout) -} -fn main() { - loop { - let req = match read_request() { - Err(err) => { - eprintln!("Read message error on ra_proc_macro_srv: {}", err.to_string()); - continue; - } - Ok(None) => continue, - Ok(Some(req)) => req, - }; - - match req { - msg::Request::ListMacro(task) => { - if let Err(err) = - write_response(list_macros(&task).map(|it| msg::Response::ListMacro(it))) - { - eprintln!("Write message error on list macro: {}", err); - } - } - msg::Request::ExpansionMacro(task) => { - if let Err(err) = - write_response(expand_task(&task).map(|it| msg::Response::ExpansionMacro(it))) - { - eprintln!("Write message error on expansion macro: {}", err); - } - } - } - } -} -- cgit v1.2.3