Skip to content

Commit 177bece

Browse files
committed
Add proc-macro cli command for rust-analyzer
1 parent ca7dc69 commit 177bece

File tree

14 files changed

+47
-26
lines changed

14 files changed

+47
-26
lines changed

.github/workflows/ci.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ jobs:
8585
- name: Compile
8686
run: cargo test --no-run
8787

88-
# We have to build ra_proc_macro_srv first for running related heavy tests
89-
- name: Build ra_proc_macro_srv
90-
run: cargo build -p ra_proc_macro_srv
88+
# We have to build rust-analyzer first for running related heavy tests
89+
- name: Build rust-analyzer
90+
run: cargo build -p rust-analyzer
9191

9292
- name: Test
9393
run: cargo test

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/ra_proc_macro/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,11 @@ pub struct ProcMacroClient {
5656
}
5757

5858
impl ProcMacroClient {
59-
pub fn extern_process(process_path: &Path) -> Result<ProcMacroClient, std::io::Error> {
60-
let (thread, process) = ProcMacroProcessSrv::run(process_path)?;
59+
pub fn extern_process<T: AsRef<str>>(
60+
process_path: &Path,
61+
args: &[T],
62+
) -> Result<ProcMacroClient, std::io::Error> {
63+
let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?;
6164
Ok(ProcMacroClient {
6265
kind: ProcMacroClientKind::Process { process: Arc::new(process), thread },
6366
})

crates/ra_proc_macro/src/process.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,9 @@ impl Drop for Process {
4444
}
4545

4646
impl Process {
47-
fn run(process_path: &Path) -> Result<Process, io::Error> {
47+
fn run<T: AsRef<str>>(process_path: &Path, args: &[T]) -> Result<Process, io::Error> {
4848
let child = Command::new(process_path.clone())
49+
.args(args.iter().map(|it| it.as_ref()))
4950
.stdin(Stdio::piped())
5051
.stdout(Stdio::piped())
5152
.stderr(Stdio::null())
@@ -74,10 +75,11 @@ impl Process {
7475
}
7576

7677
impl ProcMacroProcessSrv {
77-
pub fn run(
78+
pub fn run<T: AsRef<str>>(
7879
process_path: &Path,
80+
args: &[T],
7981
) -> Result<(ProcMacroProcessThread, ProcMacroProcessSrv), io::Error> {
80-
let process = Process::run(process_path)?;
82+
let process = Process::run(process_path, args)?;
8183

8284
let (task_tx, task_rx) = bounded(0);
8385
let handle = jod_thread::spawn(move || {

crates/ra_proc_macro_srv/src/main.rs renamed to crates/ra_proc_macro_srv/src/cli.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Driver for proc macro server
22
3+
use crate::{expand_task, list_macros};
34
use ra_proc_macro::msg::{self, Message};
4-
use ra_proc_macro_srv::{expand_task, list_macros};
55

66
use std::io;
77

@@ -24,7 +24,8 @@ fn write_response(res: Result<msg::Response, String>) -> Result<(), io::Error> {
2424
let mut stdout = stdout.lock();
2525
msg.write(&mut stdout)
2626
}
27-
fn main() {
27+
28+
pub fn run() {
2829
loop {
2930
let req = match read_request() {
3031
Err(err) => {

crates/ra_proc_macro_srv/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ mod dylib;
2222
use proc_macro::bridge::client::TokenStream;
2323
use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
2424

25-
pub fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
25+
pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
2626
let expander = dylib::Expander::new(&task.lib)
2727
.expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
2828

@@ -39,7 +39,7 @@ pub fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
3939
}
4040
}
4141

42-
pub fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
42+
pub(crate) fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
4343
let expander = dylib::Expander::new(&task.lib)
4444
.expect(&format!("Cannot expand with provided libraries: ${:?}", &task.lib));
4545

@@ -53,5 +53,7 @@ pub fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
5353
}
5454
}
5555

56+
pub mod cli;
57+
5658
#[cfg(test)]
5759
mod tests;

crates/rust-analyzer/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ ra_db = { path = "../ra_db" }
4646
hir = { path = "../ra_hir", package = "ra_hir" }
4747
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
4848
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
49-
49+
ra_proc_macro_srv = { path = "../ra_proc_macro_srv" }
5050

5151
[target.'cfg(windows)'.dependencies]
5252
winapi = "0.3.8"

crates/rust-analyzer/src/bin/args.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ pub(crate) enum Command {
4545
/// this would include the parser test files.
4646
all: bool,
4747
},
48+
ProcMacro,
4849
RunServer,
4950
Version,
5051
}
@@ -264,6 +265,7 @@ ARGS:
264265

265266
Command::Diagnostics { path, load_output_dirs, with_proc_macro, all }
266267
}
268+
"proc-macro" => Command::ProcMacro,
267269
_ => {
268270
eprintln!(
269271
"\

crates/rust-analyzer/src/bin/main.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ fn main() -> Result<()> {
5151
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
5252
}
5353

54+
args::Command::ProcMacro => run_proc_macro_sv()?,
5455
args::Command::RunServer => run_server()?,
5556
args::Command::Version => println!("rust-analyzer {}", env!("REV")),
5657
}
@@ -64,6 +65,11 @@ fn setup_logging() -> Result<()> {
6465
Ok(())
6566
}
6667

68+
fn run_proc_macro_sv() -> Result<()> {
69+
ra_proc_macro_srv::cli::run();
70+
Ok(())
71+
}
72+
6773
fn run_server() -> Result<()> {
6874
log::info!("lifecycle: server started");
6975

crates/rust-analyzer/src/cli/load_cargo.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,10 @@ pub(crate) fn load_cargo(
7373
let proc_macro_client = if !with_proc_macro {
7474
ProcMacroClient::dummy()
7575
} else {
76-
ProcMacroClient::extern_process(Path::new("ra_proc_macro_srv")).unwrap()
76+
let mut path = std::env::current_exe()?;
77+
path.pop();
78+
path.push("rust-analyzer");
79+
ProcMacroClient::extern_process(&path, &["proc-macro"]).unwrap()
7780
};
7881
let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
7982
Ok((host, source_roots))

0 commit comments

Comments
 (0)