internal: remove unreasonable crate dependency

Proc macro expansion shouldn't know about salsa at all.
This commit is contained in:
Aleksey Kladov 2021-08-22 14:05:12 +03:00
parent 881d71a489
commit e86388689f
6 changed files with 71 additions and 38 deletions

View file

@ -52,6 +52,7 @@ vfs = { path = "../vfs", version = "0.0.0" }
vfs-notify = { path = "../vfs-notify", version = "0.0.0" }
cfg = { path = "../cfg", version = "0.0.0" }
toolchain = { path = "../toolchain", version = "0.0.0" }
tt = { path = "../tt", version = "0.0.0" }
proc_macro_api = { path = "../proc_macro_api", version = "0.0.0" }
# This should only be used in CLI

View file

@ -11,7 +11,7 @@ use proc_macro_api::ProcMacroClient;
use project_model::{CargoConfig, ProjectManifest, ProjectWorkspace, WorkspaceBuildScripts};
use vfs::{loader::Handle, AbsPath, AbsPathBuf};
use crate::reload::{ProjectFolders, SourceRootConfig};
use crate::reload::{load_proc_macro, ProjectFolders, SourceRootConfig};
// Note: Since this type is used by external tools that use rust-analyzer as a library
// what otherwise would be `pub(crate)` has to be `pub` here instead.
@ -69,9 +69,7 @@ pub fn load_workspace(
});
let crate_graph = ws.to_crate_graph(
&mut |path: &AbsPath| {
proc_macro_client.as_ref().map(|it| it.by_dylib_path(path)).unwrap_or_default()
},
&mut |path: &AbsPath| load_proc_macro(proc_macro_client.as_ref(), path),
&mut |path: &AbsPath| {
let contents = loader.load_sync(path);
let path = vfs::VfsPath::from(path.to_path_buf());

View file

@ -4,7 +4,9 @@ use std::{mem, sync::Arc};
use flycheck::{FlycheckConfig, FlycheckHandle};
use hir::db::DefDatabase;
use ide::Change;
use ide_db::base_db::{CrateGraph, SourceRoot, VfsPath};
use ide_db::base_db::{
CrateGraph, Env, ProcMacro, ProcMacroExpander, ProcMacroKind, SourceRoot, VfsPath,
};
use proc_macro_api::ProcMacroClient;
use project_model::{ProjectWorkspace, WorkspaceBuildScripts};
use vfs::{file_set::FileSetConfig, AbsPath, AbsPathBuf, ChangeKind};
@ -398,9 +400,8 @@ impl GlobalState {
// Create crate graph from all the workspaces
let crate_graph = {
let proc_macro_client = self.proc_macro_client.as_ref();
let mut load_proc_macro = move |path: &AbsPath| {
proc_macro_client.map(|it| it.by_dylib_path(path)).unwrap_or_default()
};
let mut load_proc_macro =
move |path: &AbsPath| load_proc_macro(proc_macro_client, path);
let vfs = &mut self.vfs.write().0;
let loader = &mut self.loader;
@ -587,3 +588,38 @@ impl SourceRootConfig {
.collect()
}
}
pub(crate) fn load_proc_macro(client: Option<&ProcMacroClient>, path: &AbsPath) -> Vec<ProcMacro> {
return client
.map(|it| it.by_dylib_path(path))
.unwrap_or_default()
.into_iter()
.map(expander_to_proc_macro)
.collect();
fn expander_to_proc_macro(expander: proc_macro_api::ProcMacroProcessExpander) -> ProcMacro {
let name = expander.name().into();
let kind = match expander.kind() {
proc_macro_api::ProcMacroKind::CustomDerive => ProcMacroKind::CustomDerive,
proc_macro_api::ProcMacroKind::FuncLike => ProcMacroKind::FuncLike,
proc_macro_api::ProcMacroKind::Attr => ProcMacroKind::Attr,
};
let expander = Arc::new(Expander(expander));
ProcMacro { name, kind, expander }
}
#[derive(Debug)]
struct Expander(proc_macro_api::ProcMacroProcessExpander);
impl ProcMacroExpander for Expander {
fn expand(
&self,
subtree: &tt::Subtree,
attrs: Option<&tt::Subtree>,
env: &Env,
) -> Result<tt::Subtree, tt::ExpansionError> {
let env = env.iter().map(|(k, v)| (k.to_string(), v.to_string())).collect();
self.0.expand(subtree, attrs, env)
}
}
}