aboutsummaryrefslogtreecommitdiff
path: root/crates/project_model/src
diff options
context:
space:
mode:
Diffstat (limited to 'crates/project_model/src')
-rw-r--r--crates/project_model/src/workspace.rs39
1 files changed, 22 insertions, 17 deletions
diff --git a/crates/project_model/src/workspace.rs b/crates/project_model/src/workspace.rs
index a71f96164..ab5cbae11 100644
--- a/crates/project_model/src/workspace.rs
+++ b/crates/project_model/src/workspace.rs
@@ -2,10 +2,14 @@
2//! metadata` or `rust-project.json`) into representation stored in the salsa 2//! metadata` or `rust-project.json`) into representation stored in the salsa
3//! database -- `CrateGraph`. 3//! database -- `CrateGraph`.
4 4
5use std::{fmt, fs, path::Component, process::Command}; 5use std::{
6 fmt, fs,
7 path::{Component, Path},
8 process::Command,
9};
6 10
7use anyhow::{Context, Result}; 11use anyhow::{Context, Result};
8use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId}; 12use base_db::{CrateDisplayName, CrateGraph, CrateId, CrateName, Edition, Env, FileId, ProcMacro};
9use cfg::CfgOptions; 13use cfg::CfgOptions;
10use paths::{AbsPath, AbsPathBuf}; 14use paths::{AbsPath, AbsPathBuf};
11use proc_macro_api::ProcMacroClient; 15use proc_macro_api::ProcMacroClient;
@@ -194,15 +198,20 @@ impl ProjectWorkspace {
194 pub fn to_crate_graph( 198 pub fn to_crate_graph(
195 &self, 199 &self,
196 target: Option<&str>, 200 target: Option<&str>,
197 proc_macro_client: &ProcMacroClient, 201 proc_macro_client: Option<&ProcMacroClient>,
198 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, 202 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
199 ) -> CrateGraph { 203 ) -> CrateGraph {
204 let proc_macro_loader = |path: &Path| match proc_macro_client {
205 Some(client) => client.by_dylib_path(path),
206 None => Vec::new(),
207 };
208
200 let mut crate_graph = match self { 209 let mut crate_graph = match self {
201 ProjectWorkspace::Json { project, sysroot } => { 210 ProjectWorkspace::Json { project, sysroot } => {
202 project_json_to_crate_graph(target, proc_macro_client, load, project, sysroot) 211 project_json_to_crate_graph(target, &proc_macro_loader, load, project, sysroot)
203 } 212 }
204 ProjectWorkspace::Cargo { cargo, sysroot, rustc } => { 213 ProjectWorkspace::Cargo { cargo, sysroot, rustc } => {
205 cargo_to_crate_graph(target, proc_macro_client, load, cargo, sysroot, rustc) 214 cargo_to_crate_graph(target, &proc_macro_loader, load, cargo, sysroot, rustc)
206 } 215 }
207 }; 216 };
208 if crate_graph.patch_cfg_if() { 217 if crate_graph.patch_cfg_if() {
@@ -216,7 +225,7 @@ impl ProjectWorkspace {
216 225
217fn project_json_to_crate_graph( 226fn project_json_to_crate_graph(
218 target: Option<&str>, 227 target: Option<&str>,
219 proc_macro_client: &ProcMacroClient, 228 proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
220 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, 229 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
221 project: &ProjectJson, 230 project: &ProjectJson,
222 sysroot: &Option<Sysroot>, 231 sysroot: &Option<Sysroot>,
@@ -236,8 +245,7 @@ fn project_json_to_crate_graph(
236 }) 245 })
237 .map(|(crate_id, krate, file_id)| { 246 .map(|(crate_id, krate, file_id)| {
238 let env = krate.env.clone().into_iter().collect(); 247 let env = krate.env.clone().into_iter().collect();
239 let proc_macro = 248 let proc_macro = krate.proc_macro_dylib_path.clone().map(|it| proc_macro_loader(&it));
240 krate.proc_macro_dylib_path.clone().map(|it| proc_macro_client.by_dylib_path(&it));
241 249
242 let target = krate.target.as_deref().or(target); 250 let target = krate.target.as_deref().or(target);
243 let target_cfgs = 251 let target_cfgs =
@@ -279,7 +287,7 @@ fn project_json_to_crate_graph(
279 287
280fn cargo_to_crate_graph( 288fn cargo_to_crate_graph(
281 target: Option<&str>, 289 target: Option<&str>,
282 proc_macro_client: &ProcMacroClient, 290 proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
283 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>, 291 load: &mut dyn FnMut(&AbsPath) -> Option<FileId>,
284 cargo: &CargoWorkspace, 292 cargo: &CargoWorkspace,
285 sysroot: &Sysroot, 293 sysroot: &Sysroot,
@@ -309,7 +317,7 @@ fn cargo_to_crate_graph(
309 &mut crate_graph, 317 &mut crate_graph,
310 &cargo[pkg], 318 &cargo[pkg],
311 &cfg_options, 319 &cfg_options,
312 proc_macro_client, 320 proc_macro_loader,
313 file_id, 321 file_id,
314 ); 322 );
315 if cargo[tgt].kind == TargetKind::Lib { 323 if cargo[tgt].kind == TargetKind::Lib {
@@ -385,7 +393,7 @@ fn cargo_to_crate_graph(
385 &mut crate_graph, 393 &mut crate_graph,
386 &rustc_workspace[pkg], 394 &rustc_workspace[pkg],
387 &cfg_options, 395 &cfg_options,
388 proc_macro_client, 396 proc_macro_loader,
389 file_id, 397 file_id,
390 ); 398 );
391 pkg_to_lib_crate.insert(pkg, crate_id); 399 pkg_to_lib_crate.insert(pkg, crate_id);
@@ -433,7 +441,7 @@ fn add_target_crate_root(
433 crate_graph: &mut CrateGraph, 441 crate_graph: &mut CrateGraph,
434 pkg: &cargo_workspace::PackageData, 442 pkg: &cargo_workspace::PackageData,
435 cfg_options: &CfgOptions, 443 cfg_options: &CfgOptions,
436 proc_macro_client: &ProcMacroClient, 444 proc_macro_loader: &dyn Fn(&Path) -> Vec<ProcMacro>,
437 file_id: FileId, 445 file_id: FileId,
438) -> CrateId { 446) -> CrateId {
439 let edition = pkg.edition; 447 let edition = pkg.edition;
@@ -452,11 +460,8 @@ fn add_target_crate_root(
452 env.set("OUT_DIR", out_dir); 460 env.set("OUT_DIR", out_dir);
453 } 461 }
454 } 462 }
455 let proc_macro = pkg 463 let proc_macro =
456 .proc_macro_dylib_path 464 pkg.proc_macro_dylib_path.as_ref().map(|it| proc_macro_loader(&it)).unwrap_or_default();
457 .as_ref()
458 .map(|it| proc_macro_client.by_dylib_path(&it))
459 .unwrap_or_default();
460 465
461 let display_name = CrateDisplayName::from_canonical_name(pkg.name.clone()); 466 let display_name = CrateDisplayName::from_canonical_name(pkg.name.clone());
462 let crate_id = crate_graph.add_crate_root( 467 let crate_id = crate_graph.add_crate_root(