Add proc-macro cli command for rust-analyzer

This commit is contained in:
Edwin Cheng 2020-04-16 21:13:57 +08:00
parent ca7dc69a8e
commit 177becea98
14 changed files with 47 additions and 26 deletions

View File

@ -85,9 +85,9 @@ jobs:
- name: Compile
run: cargo test --no-run
# We have to build ra_proc_macro_srv first for running related heavy tests
- name: Build ra_proc_macro_srv
run: cargo build -p ra_proc_macro_srv
# We have to build rust-analyzer first for running related heavy tests
- name: Build rust-analyzer
run: cargo build -p rust-analyzer
- name: Test
run: cargo test

1
Cargo.lock generated
View File

@ -1341,6 +1341,7 @@ dependencies = [
"ra_hir_def",
"ra_hir_ty",
"ra_ide",
"ra_proc_macro_srv",
"ra_prof",
"ra_project_model",
"ra_syntax",

View File

@ -56,8 +56,11 @@ pub struct ProcMacroClient {
}
impl ProcMacroClient {
pub fn extern_process(process_path: &Path) -> Result<ProcMacroClient, std::io::Error> {
let (thread, process) = ProcMacroProcessSrv::run(process_path)?;
pub fn extern_process<T: AsRef<str>>(
process_path: &Path,
args: &[T],
) -> Result<ProcMacroClient, std::io::Error> {
let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?;
Ok(ProcMacroClient {
kind: ProcMacroClientKind::Process { process: Arc::new(process), thread },
})

View File

@ -44,8 +44,9 @@ impl Drop for Process {
}
impl Process {
fn run(process_path: &Path) -> Result<Process, io::Error> {
fn run<T: AsRef<str>>(process_path: &Path, args: &[T]) -> Result<Process, io::Error> {
let child = Command::new(process_path.clone())
.args(args.iter().map(|it| it.as_ref()))
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::null())
@ -74,10 +75,11 @@ impl Process {
}
impl ProcMacroProcessSrv {
pub fn run(
pub fn run<T: AsRef<str>>(
process_path: &Path,
args: &[T],
) -> Result<(ProcMacroProcessThread, ProcMacroProcessSrv), io::Error> {
let process = Process::run(process_path)?;
let process = Process::run(process_path, args)?;
let (task_tx, task_rx) = bounded(0);
let handle = jod_thread::spawn(move || {

View File

@ -1,7 +1,7 @@
//! Driver for proc macro server
use crate::{expand_task, list_macros};
use ra_proc_macro::msg::{self, Message};
use ra_proc_macro_srv::{expand_task, list_macros};
use std::io;
@ -24,7 +24,8 @@ fn write_response(res: Result<msg::Response, String>) -> Result<(), io::Error> {
let mut stdout = stdout.lock();
msg.write(&mut stdout)
}
fn main() {
pub fn run() {
loop {
let req = match read_request() {
Err(err) => {

View File

@ -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<ExpansionResult, String> {
pub(crate) fn expand_task(task: &ExpansionTask) -> Result<ExpansionResult, String> {
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<ExpansionResult, String> {
}
}
pub fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
pub(crate) fn list_macros(task: &ListMacrosTask) -> Result<ListMacrosResult, String> {
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<ListMacrosResult, String> {
}
}
pub mod cli;
#[cfg(test)]
mod tests;

View File

@ -46,7 +46,7 @@ ra_db = { path = "../ra_db" }
hir = { path = "../ra_hir", package = "ra_hir" }
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
ra_proc_macro_srv = { path = "../ra_proc_macro_srv" }
[target.'cfg(windows)'.dependencies]
winapi = "0.3.8"

View File

@ -45,6 +45,7 @@ pub(crate) enum Command {
/// this would include the parser test files.
all: bool,
},
ProcMacro,
RunServer,
Version,
}
@ -264,6 +265,7 @@ ARGS:
Command::Diagnostics { path, load_output_dirs, with_proc_macro, all }
}
"proc-macro" => Command::ProcMacro,
_ => {
eprintln!(
"\

View File

@ -51,6 +51,7 @@ fn main() -> Result<()> {
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
}
args::Command::ProcMacro => run_proc_macro_sv()?,
args::Command::RunServer => run_server()?,
args::Command::Version => println!("rust-analyzer {}", env!("REV")),
}
@ -64,6 +65,11 @@ fn setup_logging() -> Result<()> {
Ok(())
}
fn run_proc_macro_sv() -> Result<()> {
ra_proc_macro_srv::cli::run();
Ok(())
}
fn run_server() -> Result<()> {
log::info!("lifecycle: server started");

View File

@ -73,7 +73,10 @@ pub(crate) fn load_cargo(
let proc_macro_client = if !with_proc_macro {
ProcMacroClient::dummy()
} else {
ProcMacroClient::extern_process(Path::new("ra_proc_macro_srv")).unwrap()
let mut path = std::env::current_exe()?;
path.pop();
path.push("rust-analyzer");
ProcMacroClient::extern_process(&path, &["proc-macro"]).unwrap()
};
let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
Ok((host, source_roots))

View File

@ -20,7 +20,7 @@ pub struct Config {
pub with_sysroot: bool,
pub publish_diagnostics: bool,
pub lru_capacity: Option<usize>,
pub proc_macro_srv: Option<String>,
pub proc_macro_srv: Option<(String, Vec<String>)>,
pub files: FilesConfig,
pub notifications: NotificationsConfig,
@ -134,7 +134,11 @@ impl Config {
match get::<bool>(value, "/procMacro/enabled") {
Some(true) => {
set(value, "/procMacro/serverPath", &mut self.proc_macro_srv);
if let Ok(mut path) = std::env::current_exe() {
path.pop();
path.push("rust-analyzer");
self.proc_macro_srv = Some((path.to_string_lossy().to_string(), vec!["proc-macro".to_string()]));
}
}
_ => self.proc_macro_srv = None,
}

View File

@ -148,9 +148,9 @@ impl WorldState {
let proc_macro_client = match &config.proc_macro_srv {
None => ProcMacroClient::dummy(),
Some(srv) => {
let path = Path::new(&srv);
match ProcMacroClient::extern_process(path) {
Some((path, args)) => {
let path = std::path::Path::new(path);
match ProcMacroClient::extern_process(path, args) {
Ok(it) => it,
Err(err) => {
log::error!(

View File

@ -692,13 +692,15 @@ pub fn foo(_input: TokenStream) -> TokenStream {
"###,
)
.with_config(|config| {
// FIXME: Use env!("CARGO_BIN_EXE_ra-analyzer") instead after
// https://github.com/rust-lang/cargo/pull/7697 landed
let macro_srv_path = std::path::Path::new(std::env!("CARGO_MANIFEST_DIR"))
.join("../../target/debug/ra_proc_macro_srv")
.join("../../target/debug/rust-analyzer")
.to_string_lossy()
.to_string();
config.cargo.load_out_dirs_from_check = true;
config.proc_macro_srv = Some(macro_srv_path)
config.proc_macro_srv = Some((macro_srv_path, vec!["proc-macro".to_string()]));
})
.root("foo")
.root("bar")

View File

@ -393,11 +393,6 @@
"description": "Enable Proc macro support, cargo.loadOutDirsFromCheck must be enabled.",
"type": "boolean",
"default": false
},
"rust-analyzer.procMacro.serverPath": {
"description": "Proc macro server path",
"type": "string",
"default": "ra_proc_macro_srv"
}
}
},