Skip to content

Commit

Permalink
Add a request for setting response prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad20012 committed Dec 7, 2023
1 parent 421a0a4 commit e003bc8
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 5 deletions.
4 changes: 3 additions & 1 deletion crates/proc-macro-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,9 @@ impl ProcMacro {
msg::Response::ExpandMacro(it) => {
Ok(it.map(|tree| FlatTree::to_subtree_resolved(tree, version, &span_data_table)))
}
msg::Response::ListMacros(..) | msg::Response::ApiVersionCheck(..) => {
msg::Response::ListMacros(..)
| msg::Response::ApiVersionCheck(..)
| msg::Response::SetExpanderSettings { .. } => {
Err(ServerError { message: "unexpected response".to_string(), io: None })
}
}
Expand Down
5 changes: 4 additions & 1 deletion crates/proc-macro-api/src/msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,24 @@ pub const NO_VERSION_CHECK_VERSION: u32 = 0;
pub const VERSION_CHECK_VERSION: u32 = 1;
pub const ENCODE_CLOSE_SPAN_VERSION: u32 = 2;
pub const HAS_GLOBAL_SPANS: u32 = 3;
pub const EXPAND_MACRO_RESPONSE_PREFIX: u32 = 4;

pub const CURRENT_API_VERSION: u32 = HAS_GLOBAL_SPANS;
pub const CURRENT_API_VERSION: u32 = EXPAND_MACRO_RESPONSE_PREFIX;

#[derive(Debug, Serialize, Deserialize)]
pub enum Request {
ListMacros { dylib_path: PathBuf },
ExpandMacro(ExpandMacro),
ApiVersionCheck {},
SetExpanderSettings { response_prefix: String },
}

#[derive(Debug, Serialize, Deserialize)]
pub enum Response {
ListMacros(Result<Vec<(String, ProcMacroKind)>, String>),
ExpandMacro(Result<FlatTree, PanicMessage>),
ApiVersionCheck(u32),
SetExpanderSettings {},
}

#[derive(Debug, Serialize, Deserialize)]
Expand Down
8 changes: 6 additions & 2 deletions crates/proc-macro-api/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,9 @@ impl ProcMacroProcessSrv {

match response {
Response::ApiVersionCheck(version) => Ok(version),
Response::ExpandMacro { .. } | Response::ListMacros { .. } => {
Response::ExpandMacro { .. }
| Response::ListMacros { .. }
| Response::SetExpanderSettings { .. } => {
Err(ServerError { message: "unexpected response".to_string(), io: None })
}
}
Expand All @@ -78,7 +80,9 @@ impl ProcMacroProcessSrv {

match response {
Response::ListMacros(it) => Ok(it),
Response::ExpandMacro { .. } | Response::ApiVersionCheck { .. } => {
Response::ExpandMacro { .. }
| Response::ApiVersionCheck { .. }
| Response::SetExpanderSettings { .. } => {
Err(ServerError { message: "unexpected response".to_string(), io: None })
}
}
Expand Down
18 changes: 17 additions & 1 deletion crates/proc-macro-srv-cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,22 @@ fn run() -> io::Result<()> {
#[cfg(any(feature = "sysroot-abi", rust_analyzer))]
fn run() -> io::Result<()> {
use proc_macro_api::msg::{self, Message};
use std::cell::RefCell;

let current_response_prefix = RefCell::new(String::new());

let read_request = |buf: &mut String| msg::Request::read(&mut io::stdin().lock(), buf);

let write_response = |msg: msg::Response| msg.write(&mut io::stdout().lock());
let write_response = |msg: msg::Response| {
use std::io::Write;

let out = &mut io::stdout().lock();
let prefix = current_response_prefix.borrow();
if !prefix.is_empty() {
out.write_all(prefix.as_bytes())?
}
msg.write(out)
};

let mut srv = proc_macro_srv::ProcMacroSrv::default();
let mut buf = String::new();
Expand All @@ -43,6 +55,10 @@ fn run() -> io::Result<()> {
msg::Request::ApiVersionCheck {} => {
msg::Response::ApiVersionCheck(proc_macro_api::msg::CURRENT_API_VERSION)
}
msg::Request::SetExpanderSettings { response_prefix } => {
*current_response_prefix.borrow_mut() = response_prefix;
msg::Response::SetExpanderSettings {}
}
};
write_response(res)?
}
Expand Down
1 change: 1 addition & 0 deletions crates/proc-macro-srv/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#![feature(proc_macro_internals, proc_macro_diagnostic, proc_macro_span)]
#![warn(rust_2018_idioms, unused_lifetimes)]
#![allow(unreachable_pub)]
#![allow(internal_features)]

extern crate proc_macro;

Expand Down

0 comments on commit e003bc8

Please sign in to comment.