aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_cli/src/load_cargo.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_cli/src/load_cargo.rs')
-rw-r--r--crates/ra_cli/src/load_cargo.rs152
1 files changed, 0 insertions, 152 deletions
diff --git a/crates/ra_cli/src/load_cargo.rs b/crates/ra_cli/src/load_cargo.rs
deleted file mode 100644
index b9a4e6aba..000000000
--- a/crates/ra_cli/src/load_cargo.rs
+++ /dev/null
@@ -1,152 +0,0 @@
1//! FIXME: write short doc here
2
3use std::{collections::HashSet, path::Path};
4
5use crossbeam_channel::{unbounded, Receiver};
6use ra_db::{CrateGraph, FileId, SourceRootId};
7use ra_ide::{AnalysisChange, AnalysisHost, FeatureFlags};
8use ra_project_model::{get_rustc_cfg_options, PackageRoot, ProjectWorkspace};
9use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
10use ra_vfs_glob::RustPackageFilterBuilder;
11use rustc_hash::FxHashMap;
12
13use anyhow::Result;
14
15fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
16 FileId(f.0)
17}
18fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
19 SourceRootId(r.0)
20}
21
22pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> {
23 let root = std::env::current_dir()?.join(root);
24 let ws = ProjectWorkspace::discover(root.as_ref(), &Default::default())?;
25 let project_roots = ws.to_roots();
26 let (sender, receiver) = unbounded();
27 let sender = Box::new(move |t| sender.send(t).unwrap());
28 let (mut vfs, roots) = Vfs::new(
29 project_roots
30 .iter()
31 .map(|pkg_root| {
32 RootEntry::new(
33 pkg_root.path().clone(),
34 RustPackageFilterBuilder::default()
35 .set_member(pkg_root.is_member())
36 .into_vfs_filter(),
37 )
38 })
39 .collect(),
40 sender,
41 Watch(false),
42 );
43
44 // FIXME: cfg options?
45 let default_cfg_options = {
46 let mut opts = get_rustc_cfg_options();
47 opts.insert_atom("test".into());
48 opts.insert_atom("debug_assertion".into());
49 opts
50 };
51
52 let (crate_graph, _crate_names) =
53 ws.to_crate_graph(&default_cfg_options, &mut |path: &Path| {
54 let vfs_file = vfs.load(path);
55 log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
56 vfs_file.map(vfs_file_to_id)
57 });
58 log::debug!("crate graph: {:?}", crate_graph);
59
60 let source_roots = roots
61 .iter()
62 .map(|&vfs_root| {
63 let source_root_id = vfs_root_to_id(vfs_root);
64 let project_root = project_roots
65 .iter()
66 .find(|it| it.path() == &vfs.root2path(vfs_root))
67 .unwrap()
68 .clone();
69 (source_root_id, project_root)
70 })
71 .collect::<FxHashMap<_, _>>();
72 let host = load(&source_roots, crate_graph, &mut vfs, receiver);
73 Ok((host, source_roots))
74}
75
76pub fn load(
77 source_roots: &FxHashMap<SourceRootId, PackageRoot>,
78 crate_graph: CrateGraph,
79 vfs: &mut Vfs,
80 receiver: Receiver<VfsTask>,
81) -> AnalysisHost {
82 let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
83 let mut host = AnalysisHost::new(lru_cap, FeatureFlags::default());
84 let mut analysis_change = AnalysisChange::new();
85 analysis_change.set_crate_graph(crate_graph);
86
87 // wait until Vfs has loaded all roots
88 let mut roots_loaded = HashSet::new();
89 for task in receiver {
90 vfs.handle_task(task);
91 let mut done = false;
92 for change in vfs.commit_changes() {
93 match change {
94 VfsChange::AddRoot { root, files } => {
95 let source_root_id = vfs_root_to_id(root);
96 let is_local = source_roots[&source_root_id].is_member();
97 log::debug!(
98 "loaded source root {:?} with path {:?}",
99 source_root_id,
100 vfs.root2path(root)
101 );
102 analysis_change.add_root(source_root_id, is_local);
103 analysis_change.set_debug_root_path(
104 source_root_id,
105 source_roots[&source_root_id].path().display().to_string(),
106 );
107
108 let mut file_map = FxHashMap::default();
109 for (vfs_file, path, text) in files {
110 let file_id = vfs_file_to_id(vfs_file);
111 analysis_change.add_file(source_root_id, file_id, path.clone(), text);
112 file_map.insert(path, file_id);
113 }
114 roots_loaded.insert(source_root_id);
115 if roots_loaded.len() == vfs.n_roots() {
116 done = true;
117 }
118 }
119 VfsChange::AddFile { root, file, path, text } => {
120 let source_root_id = vfs_root_to_id(root);
121 let file_id = vfs_file_to_id(file);
122 analysis_change.add_file(source_root_id, file_id, path, text);
123 }
124 VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => {
125 // We just need the first scan, so just ignore these
126 }
127 }
128 }
129 if done {
130 break;
131 }
132 }
133
134 host.apply_change(analysis_change);
135 host
136}
137
138#[cfg(test)]
139mod tests {
140 use super::*;
141
142 use hir::Crate;
143
144 #[test]
145 fn test_loading_rust_analyzer() {
146 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
147 let (host, _roots) = load_cargo(path).unwrap();
148 let n_crates = Crate::all(host.raw_database()).len();
149 // RA has quite a few crates, but the exact count doesn't matter
150 assert!(n_crates > 20);
151 }
152}