aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2020-03-10 14:00:58 +0000
committerEdwin Cheng <[email protected]>2020-03-10 17:06:01 +0000
commite00a1e0b79e2b2c0c20a96e5341e3a35f46f99b7 (patch)
treeb363f9ff36e14e7f90808e79ac36a35f96b50438 /crates
parent22f064cca7651eaf2980fcfa27618d99c633a589 (diff)
Setup Env in world
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_project_model/src/lib.rs18
-rw-r--r--crates/rust-analyzer/src/cli/load_cargo.rs5
-rw-r--r--crates/rust-analyzer/src/world.rs37
3 files changed, 52 insertions, 8 deletions
diff --git a/crates/ra_project_model/src/lib.rs b/crates/ra_project_model/src/lib.rs
index 37845ca56..b46320304 100644
--- a/crates/ra_project_model/src/lib.rs
+++ b/crates/ra_project_model/src/lib.rs
@@ -14,7 +14,7 @@ use std::{
14 14
15use anyhow::{bail, Context, Result}; 15use anyhow::{bail, Context, Result};
16use ra_cfg::CfgOptions; 16use ra_cfg::CfgOptions;
17use ra_db::{CrateGraph, CrateName, Edition, Env, FileId}; 17use ra_db::{CrateGraph, CrateName, Edition, Env, ExternSourceId, FileId};
18use rustc_hash::FxHashMap; 18use rustc_hash::FxHashMap;
19use serde_json::from_reader; 19use serde_json::from_reader;
20 20
@@ -162,6 +162,7 @@ impl ProjectWorkspace {
162 pub fn to_crate_graph( 162 pub fn to_crate_graph(
163 &self, 163 &self,
164 default_cfg_options: &CfgOptions, 164 default_cfg_options: &CfgOptions,
165 outdirs: &FxHashMap<String, (ExternSourceId, String)>,
165 load: &mut dyn FnMut(&Path) -> Option<FileId>, 166 load: &mut dyn FnMut(&Path) -> Option<FileId>,
166 ) -> CrateGraph { 167 ) -> CrateGraph {
167 let mut crate_graph = CrateGraph::default(); 168 let mut crate_graph = CrateGraph::default();
@@ -185,6 +186,8 @@ impl ProjectWorkspace {
185 } 186 }
186 opts 187 opts
187 }; 188 };
189
190 // FIXME: No crate name in json definition such that we cannot add OUT_DIR to env
188 crates.insert( 191 crates.insert(
189 crate_id, 192 crate_id,
190 crate_graph.add_crate_root( 193 crate_graph.add_crate_root(
@@ -231,12 +234,17 @@ impl ProjectWorkspace {
231 opts 234 opts
232 }; 235 };
233 236
237 let mut env = Env::default();
238 if let Some((id, path)) = outdirs.get(krate.name(&sysroot)) {
239 env.set_extern_path("OUT_DIR", &path, *id);
240 }
241
234 let crate_id = crate_graph.add_crate_root( 242 let crate_id = crate_graph.add_crate_root(
235 file_id, 243 file_id,
236 Edition::Edition2018, 244 Edition::Edition2018,
237 Some(krate.name(&sysroot).to_string()), 245 Some(krate.name(&sysroot).to_string()),
238 cfg_options, 246 cfg_options,
239 Env::default(), 247 env,
240 ); 248 );
241 sysroot_crates.insert(krate, crate_id); 249 sysroot_crates.insert(krate, crate_id);
242 } 250 }
@@ -275,12 +283,16 @@ impl ProjectWorkspace {
275 opts.insert_features(pkg.features(&cargo).iter().map(Into::into)); 283 opts.insert_features(pkg.features(&cargo).iter().map(Into::into));
276 opts 284 opts
277 }; 285 };
286 let mut env = Env::default();
287 if let Some((id, path)) = outdirs.get(pkg.name(&cargo)) {
288 env.set_extern_path("OUT_DIR", &path, *id);
289 }
278 let crate_id = crate_graph.add_crate_root( 290 let crate_id = crate_graph.add_crate_root(
279 file_id, 291 file_id,
280 edition, 292 edition,
281 Some(pkg.name(&cargo).to_string()), 293 Some(pkg.name(&cargo).to_string()),
282 cfg_options, 294 cfg_options,
283 Env::default(), 295 env,
284 ); 296 );
285 if tgt.kind(&cargo) == TargetKind::Lib { 297 if tgt.kind(&cargo) == TargetKind::Lib {
286 lib_tgt = Some(crate_id); 298 lib_tgt = Some(crate_id);
diff --git a/crates/rust-analyzer/src/cli/load_cargo.rs b/crates/rust-analyzer/src/cli/load_cargo.rs
index 4be987860..403f353f1 100644
--- a/crates/rust-analyzer/src/cli/load_cargo.rs
+++ b/crates/rust-analyzer/src/cli/load_cargo.rs
@@ -52,7 +52,10 @@ pub(crate) fn load_cargo(
52 opts 52 opts
53 }; 53 };
54 54
55 let crate_graph = ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| { 55 // FIXME: outdirs?
56 let outdirs = FxHashMap::default();
57
58 let crate_graph = ws.to_crate_graph(&default_cfg_options, &outdirs, &mut |path: &Path| {
56 let vfs_file = vfs.load(path); 59 let vfs_file = vfs.load(path);
57 log::debug!("vfs file {:?} -> {:?}", path, vfs_file); 60 log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
58 vfs_file.map(vfs_file_to_id) 61 vfs_file.map(vfs_file_to_id)
diff --git a/crates/rust-analyzer/src/world.rs b/crates/rust-analyzer/src/world.rs
index ac4395617..b64140b74 100644
--- a/crates/rust-analyzer/src/world.rs
+++ b/crates/rust-analyzer/src/world.rs
@@ -26,6 +26,8 @@ use crate::{
26 vfs_glob::{Glob, RustPackageFilterBuilder}, 26 vfs_glob::{Glob, RustPackageFilterBuilder},
27 LspError, Result, 27 LspError, Result,
28}; 28};
29use ra_db::ExternSourceId;
30use rustc_hash::{FxHashMap, FxHashSet};
29 31
30#[derive(Debug, Clone)] 32#[derive(Debug, Clone)]
31pub struct Options { 33pub struct Options {
@@ -98,6 +100,19 @@ impl WorldState {
98 RootEntry::new(pkg_root.path().clone(), filter.into_vfs_filter()) 100 RootEntry::new(pkg_root.path().clone(), filter.into_vfs_filter())
99 })); 101 }));
100 } 102 }
103
104 let extern_dirs: FxHashSet<_> =
105 additional_out_dirs.iter().map(|(_, path)| (PathBuf::from(path))).collect();
106 let mut extern_source_roots = FxHashMap::default();
107
108 roots.extend(additional_out_dirs.iter().map(|(_, path)| {
109 let mut filter = RustPackageFilterBuilder::default().set_member(false);
110 for glob in exclude_globs.iter() {
111 filter = filter.exclude(glob.clone());
112 }
113 RootEntry::new(PathBuf::from(&path), filter.into_vfs_filter())
114 }));
115
101 let (task_sender, task_receiver) = unbounded(); 116 let (task_sender, task_receiver) = unbounded();
102 let task_sender = Box::new(move |t| task_sender.send(t).unwrap()); 117 let task_sender = Box::new(move |t| task_sender.send(t).unwrap());
103 let (mut vfs, vfs_roots) = Vfs::new(roots, task_sender, watch); 118 let (mut vfs, vfs_roots) = Vfs::new(roots, task_sender, watch);
@@ -107,6 +122,11 @@ impl WorldState {
107 let is_local = folder_roots.iter().any(|it| vfs_root_path.starts_with(it)); 122 let is_local = folder_roots.iter().any(|it| vfs_root_path.starts_with(it));
108 change.add_root(SourceRootId(r.0), is_local); 123 change.add_root(SourceRootId(r.0), is_local);
109 change.set_debug_root_path(SourceRootId(r.0), vfs_root_path.display().to_string()); 124 change.set_debug_root_path(SourceRootId(r.0), vfs_root_path.display().to_string());
125
126 // FIXME: add path2root in vfs to simpily this logic
127 if extern_dirs.contains(&vfs_root_path) {
128 extern_source_roots.insert(vfs_root_path, ExternSourceId(r.0));
129 }
110 } 130 }
111 131
112 // FIXME: Read default cfgs from config 132 // FIXME: Read default cfgs from config
@@ -124,11 +144,20 @@ impl WorldState {
124 vfs_file.map(|f| FileId(f.0)) 144 vfs_file.map(|f| FileId(f.0))
125 }; 145 };
126 146
127 workspaces.iter().map(|ws| ws.to_crate_graph(&default_cfg_options, &mut load)).for_each( 147 let mut outdirs = FxHashMap::default();
128 |graph| { 148 for (name, path) in additional_out_dirs {
149 let path = PathBuf::from(&path);
150 if let Some(id) = extern_source_roots.get(&path) {
151 outdirs.insert(name, (id.clone(), path.to_string_lossy().replace("\\", "/")));
152 }
153 }
154
155 workspaces
156 .iter()
157 .map(|ws| ws.to_crate_graph(&default_cfg_options, &outdirs, &mut load))
158 .for_each(|graph| {
129 crate_graph.extend(graph); 159 crate_graph.extend(graph);
130 }, 160 });
131 );
132 change.set_crate_graph(crate_graph); 161 change.set_crate_graph(crate_graph);
133 162
134 // FIXME: Figure out the multi-workspace situation 163 // FIXME: Figure out the multi-workspace situation