mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-27 12:25:05 +00:00
Add proc-macro cli command for rust-analyzer
This commit is contained in:
parent
ca7dc69a8e
commit
177becea98
14 changed files with 47 additions and 26 deletions
6
.github/workflows/ci.yaml
vendored
6
.github/workflows/ci.yaml
vendored
|
@ -85,9 +85,9 @@ jobs:
|
||||||
- name: Compile
|
- name: Compile
|
||||||
run: cargo test --no-run
|
run: cargo test --no-run
|
||||||
|
|
||||||
# We have to build ra_proc_macro_srv first for running related heavy tests
|
# We have to build rust-analyzer first for running related heavy tests
|
||||||
- name: Build ra_proc_macro_srv
|
- name: Build rust-analyzer
|
||||||
run: cargo build -p ra_proc_macro_srv
|
run: cargo build -p rust-analyzer
|
||||||
|
|
||||||
- name: Test
|
- name: Test
|
||||||
run: cargo test
|
run: cargo test
|
||||||
|
|
1
Cargo.lock
generated
1
Cargo.lock
generated
|
@ -1341,6 +1341,7 @@ dependencies = [
|
||||||
"ra_hir_def",
|
"ra_hir_def",
|
||||||
"ra_hir_ty",
|
"ra_hir_ty",
|
||||||
"ra_ide",
|
"ra_ide",
|
||||||
|
"ra_proc_macro_srv",
|
||||||
"ra_prof",
|
"ra_prof",
|
||||||
"ra_project_model",
|
"ra_project_model",
|
||||||
"ra_syntax",
|
"ra_syntax",
|
||||||
|
|
|
@ -56,8 +56,11 @@ pub struct ProcMacroClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProcMacroClient {
|
impl ProcMacroClient {
|
||||||
pub fn extern_process(process_path: &Path) -> Result<ProcMacroClient, std::io::Error> {
|
pub fn extern_process<T: AsRef<str>>(
|
||||||
let (thread, process) = ProcMacroProcessSrv::run(process_path)?;
|
process_path: &Path,
|
||||||
|
args: &[T],
|
||||||
|
) -> Result<ProcMacroClient, std::io::Error> {
|
||||||
|
let (thread, process) = ProcMacroProcessSrv::run(process_path, args)?;
|
||||||
Ok(ProcMacroClient {
|
Ok(ProcMacroClient {
|
||||||
kind: ProcMacroClientKind::Process { process: Arc::new(process), thread },
|
kind: ProcMacroClientKind::Process { process: Arc::new(process), thread },
|
||||||
})
|
})
|
||||||
|
|
|
@ -44,8 +44,9 @@ impl Drop for Process {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl 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())
|
let child = Command::new(process_path.clone())
|
||||||
|
.args(args.iter().map(|it| it.as_ref()))
|
||||||
.stdin(Stdio::piped())
|
.stdin(Stdio::piped())
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::null())
|
.stderr(Stdio::null())
|
||||||
|
@ -74,10 +75,11 @@ impl Process {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProcMacroProcessSrv {
|
impl ProcMacroProcessSrv {
|
||||||
pub fn run(
|
pub fn run<T: AsRef<str>>(
|
||||||
process_path: &Path,
|
process_path: &Path,
|
||||||
|
args: &[T],
|
||||||
) -> Result<(ProcMacroProcessThread, ProcMacroProcessSrv), io::Error> {
|
) -> 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 (task_tx, task_rx) = bounded(0);
|
||||||
let handle = jod_thread::spawn(move || {
|
let handle = jod_thread::spawn(move || {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
//! Driver for proc macro server
|
//! Driver for proc macro server
|
||||||
|
|
||||||
|
use crate::{expand_task, list_macros};
|
||||||
use ra_proc_macro::msg::{self, Message};
|
use ra_proc_macro::msg::{self, Message};
|
||||||
use ra_proc_macro_srv::{expand_task, list_macros};
|
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
|
|
||||||
|
@ -24,7 +24,8 @@ fn write_response(res: Result<msg::Response, String>) -> Result<(), io::Error> {
|
||||||
let mut stdout = stdout.lock();
|
let mut stdout = stdout.lock();
|
||||||
msg.write(&mut stdout)
|
msg.write(&mut stdout)
|
||||||
}
|
}
|
||||||
fn main() {
|
|
||||||
|
pub fn run() {
|
||||||
loop {
|
loop {
|
||||||
let req = match read_request() {
|
let req = match read_request() {
|
||||||
Err(err) => {
|
Err(err) => {
|
|
@ -22,7 +22,7 @@ mod dylib;
|
||||||
use proc_macro::bridge::client::TokenStream;
|
use proc_macro::bridge::client::TokenStream;
|
||||||
use ra_proc_macro::{ExpansionResult, ExpansionTask, ListMacrosResult, ListMacrosTask};
|
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)
|
let expander = dylib::Expander::new(&task.lib)
|
||||||
.expect(&format!("Cannot expand with provided libraries: ${:?}", &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)
|
let expander = dylib::Expander::new(&task.lib)
|
||||||
.expect(&format!("Cannot expand with provided libraries: ${:?}", &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)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
|
@ -46,7 +46,7 @@ ra_db = { path = "../ra_db" }
|
||||||
hir = { path = "../ra_hir", package = "ra_hir" }
|
hir = { path = "../ra_hir", package = "ra_hir" }
|
||||||
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
|
hir_def = { path = "../ra_hir_def", package = "ra_hir_def" }
|
||||||
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
|
hir_ty = { path = "../ra_hir_ty", package = "ra_hir_ty" }
|
||||||
|
ra_proc_macro_srv = { path = "../ra_proc_macro_srv" }
|
||||||
|
|
||||||
[target.'cfg(windows)'.dependencies]
|
[target.'cfg(windows)'.dependencies]
|
||||||
winapi = "0.3.8"
|
winapi = "0.3.8"
|
||||||
|
|
|
@ -45,6 +45,7 @@ pub(crate) enum Command {
|
||||||
/// this would include the parser test files.
|
/// this would include the parser test files.
|
||||||
all: bool,
|
all: bool,
|
||||||
},
|
},
|
||||||
|
ProcMacro,
|
||||||
RunServer,
|
RunServer,
|
||||||
Version,
|
Version,
|
||||||
}
|
}
|
||||||
|
@ -264,6 +265,7 @@ ARGS:
|
||||||
|
|
||||||
Command::Diagnostics { path, load_output_dirs, with_proc_macro, all }
|
Command::Diagnostics { path, load_output_dirs, with_proc_macro, all }
|
||||||
}
|
}
|
||||||
|
"proc-macro" => Command::ProcMacro,
|
||||||
_ => {
|
_ => {
|
||||||
eprintln!(
|
eprintln!(
|
||||||
"\
|
"\
|
||||||
|
|
|
@ -51,6 +51,7 @@ fn main() -> Result<()> {
|
||||||
cli::diagnostics(path.as_ref(), load_output_dirs, with_proc_macro, all)?
|
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::RunServer => run_server()?,
|
||||||
args::Command::Version => println!("rust-analyzer {}", env!("REV")),
|
args::Command::Version => println!("rust-analyzer {}", env!("REV")),
|
||||||
}
|
}
|
||||||
|
@ -64,6 +65,11 @@ fn setup_logging() -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn run_proc_macro_sv() -> Result<()> {
|
||||||
|
ra_proc_macro_srv::cli::run();
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
fn run_server() -> Result<()> {
|
fn run_server() -> Result<()> {
|
||||||
log::info!("lifecycle: server started");
|
log::info!("lifecycle: server started");
|
||||||
|
|
||||||
|
|
|
@ -73,7 +73,10 @@ pub(crate) fn load_cargo(
|
||||||
let proc_macro_client = if !with_proc_macro {
|
let proc_macro_client = if !with_proc_macro {
|
||||||
ProcMacroClient::dummy()
|
ProcMacroClient::dummy()
|
||||||
} else {
|
} 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);
|
let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
|
||||||
Ok((host, source_roots))
|
Ok((host, source_roots))
|
||||||
|
|
|
@ -20,7 +20,7 @@ pub struct Config {
|
||||||
pub with_sysroot: bool,
|
pub with_sysroot: bool,
|
||||||
pub publish_diagnostics: bool,
|
pub publish_diagnostics: bool,
|
||||||
pub lru_capacity: Option<usize>,
|
pub lru_capacity: Option<usize>,
|
||||||
pub proc_macro_srv: Option<String>,
|
pub proc_macro_srv: Option<(String, Vec<String>)>,
|
||||||
pub files: FilesConfig,
|
pub files: FilesConfig,
|
||||||
pub notifications: NotificationsConfig,
|
pub notifications: NotificationsConfig,
|
||||||
|
|
||||||
|
@ -134,7 +134,11 @@ impl Config {
|
||||||
|
|
||||||
match get::<bool>(value, "/procMacro/enabled") {
|
match get::<bool>(value, "/procMacro/enabled") {
|
||||||
Some(true) => {
|
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,
|
_ => self.proc_macro_srv = None,
|
||||||
}
|
}
|
||||||
|
|
|
@ -148,9 +148,9 @@ impl WorldState {
|
||||||
|
|
||||||
let proc_macro_client = match &config.proc_macro_srv {
|
let proc_macro_client = match &config.proc_macro_srv {
|
||||||
None => ProcMacroClient::dummy(),
|
None => ProcMacroClient::dummy(),
|
||||||
Some(srv) => {
|
Some((path, args)) => {
|
||||||
let path = Path::new(&srv);
|
let path = std::path::Path::new(path);
|
||||||
match ProcMacroClient::extern_process(path) {
|
match ProcMacroClient::extern_process(path, args) {
|
||||||
Ok(it) => it,
|
Ok(it) => it,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
log::error!(
|
log::error!(
|
||||||
|
|
|
@ -692,13 +692,15 @@ pub fn foo(_input: TokenStream) -> TokenStream {
|
||||||
"###,
|
"###,
|
||||||
)
|
)
|
||||||
.with_config(|config| {
|
.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"))
|
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_lossy()
|
||||||
.to_string();
|
.to_string();
|
||||||
|
|
||||||
config.cargo.load_out_dirs_from_check = true;
|
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("foo")
|
||||||
.root("bar")
|
.root("bar")
|
||||||
|
|
|
@ -393,11 +393,6 @@
|
||||||
"description": "Enable Proc macro support, cargo.loadOutDirsFromCheck must be enabled.",
|
"description": "Enable Proc macro support, cargo.loadOutDirsFromCheck must be enabled.",
|
||||||
"type": "boolean",
|
"type": "boolean",
|
||||||
"default": false
|
"default": false
|
||||||
},
|
|
||||||
"rust-analyzer.procMacro.serverPath": {
|
|
||||||
"description": "Proc macro server path",
|
|
||||||
"type": "string",
|
|
||||||
"default": "ra_proc_macro_srv"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
Loading…
Reference in a new issue