]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/cli/load_cargo.rs
Merge #4821
[rust.git] / crates / rust-analyzer / src / cli / load_cargo.rs
1 //! Loads a Cargo project into a static instance of analysis, without support
2 //! for incorporating changes.
3
4 use std::path::{Path, PathBuf};
5
6 use anyhow::Result;
7 use crossbeam_channel::{unbounded, Receiver};
8 use ra_db::{ExternSourceId, FileId, SourceRootId};
9 use ra_ide::{AnalysisChange, AnalysisHost};
10 use ra_project_model::{
11     CargoConfig, PackageRoot, ProcMacroClient, ProjectManifest, ProjectWorkspace,
12 };
13 use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
14 use rustc_hash::{FxHashMap, FxHashSet};
15
16 use crate::vfs_glob::RustPackageFilterBuilder;
17
18 fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
19     FileId(f.0)
20 }
21 fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
22     SourceRootId(r.0)
23 }
24
25 pub fn load_cargo(
26     root: &Path,
27     load_out_dirs_from_check: bool,
28     with_proc_macro: bool,
29 ) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> {
30     let root = std::env::current_dir()?.join(root);
31     let root = ProjectManifest::discover_single(&root)?;
32     let ws = ProjectWorkspace::load(
33         root,
34         &CargoConfig { load_out_dirs_from_check, ..Default::default() },
35         true,
36     )?;
37
38     let mut extern_dirs = FxHashSet::default();
39
40     let (sender, receiver) = unbounded();
41     let sender = Box::new(move |t| sender.send(t).unwrap());
42
43     let mut roots = Vec::new();
44     let project_roots = ws.to_roots();
45     for root in &project_roots {
46         roots.push(RootEntry::new(
47             root.path().to_owned(),
48             RustPackageFilterBuilder::default().set_member(root.is_member()).into_vfs_filter(),
49         ));
50
51         if let Some(out_dir) = root.out_dir() {
52             extern_dirs.insert(out_dir.to_path_buf());
53             roots.push(RootEntry::new(
54                 out_dir.to_owned(),
55                 RustPackageFilterBuilder::default().set_member(root.is_member()).into_vfs_filter(),
56             ))
57         }
58     }
59
60     let (mut vfs, roots) = Vfs::new(roots, sender, Watch(false));
61
62     let source_roots = roots
63         .into_iter()
64         .map(|vfs_root| {
65             let source_root_id = vfs_root_to_id(vfs_root);
66             let project_root = project_roots
67                 .iter()
68                 .find(|it| it.path() == vfs.root2path(vfs_root))
69                 .unwrap()
70                 .clone();
71             (source_root_id, project_root)
72         })
73         .collect::<FxHashMap<_, _>>();
74
75     let proc_macro_client = if !with_proc_macro {
76         ProcMacroClient::dummy()
77     } else {
78         let path = std::env::current_exe()?;
79         ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap()
80     };
81     let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
82     Ok((host, source_roots))
83 }
84
85 pub(crate) fn load(
86     source_roots: &FxHashMap<SourceRootId, PackageRoot>,
87     ws: ProjectWorkspace,
88     vfs: &mut Vfs,
89     receiver: Receiver<VfsTask>,
90     extern_dirs: FxHashSet<PathBuf>,
91     proc_macro_client: &ProcMacroClient,
92 ) -> AnalysisHost {
93     let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
94     let mut host = AnalysisHost::new(lru_cap);
95     let mut analysis_change = AnalysisChange::new();
96
97     // wait until Vfs has loaded all roots
98     let mut roots_loaded = FxHashSet::default();
99     let mut extern_source_roots = FxHashMap::default();
100     for task in receiver {
101         vfs.handle_task(task);
102         let mut done = false;
103         for change in vfs.commit_changes() {
104             match change {
105                 VfsChange::AddRoot { root, files } => {
106                     let source_root_id = vfs_root_to_id(root);
107                     let is_local = source_roots[&source_root_id].is_member();
108                     log::debug!(
109                         "loaded source root {:?} with path {:?}",
110                         source_root_id,
111                         vfs.root2path(root)
112                     );
113                     analysis_change.add_root(source_root_id, is_local);
114
115                     let vfs_root_path = vfs.root2path(root);
116                     if extern_dirs.contains(&vfs_root_path) {
117                         extern_source_roots.insert(vfs_root_path, ExternSourceId(root.0));
118                     }
119
120                     let mut file_map = FxHashMap::default();
121                     for (vfs_file, path, text) in files {
122                         let file_id = vfs_file_to_id(vfs_file);
123                         analysis_change.add_file(source_root_id, file_id, path.clone(), text);
124                         file_map.insert(path, file_id);
125                     }
126                     roots_loaded.insert(source_root_id);
127                     if roots_loaded.len() == vfs.n_roots() {
128                         done = true;
129                     }
130                 }
131                 VfsChange::AddFile { root, file, path, text } => {
132                     let source_root_id = vfs_root_to_id(root);
133                     let file_id = vfs_file_to_id(file);
134                     analysis_change.add_file(source_root_id, file_id, path, text);
135                 }
136                 VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => {
137                     // We just need the first scan, so just ignore these
138                 }
139             }
140         }
141         if done {
142             break;
143         }
144     }
145
146     let crate_graph =
147         ws.to_crate_graph(None, &extern_source_roots, proc_macro_client, &mut |path: &Path| {
148             // Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
149             let path = path.canonicalize().ok()?;
150             let vfs_file = vfs.load(&path);
151             log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
152             vfs_file.map(vfs_file_to_id)
153         });
154     log::debug!("crate graph: {:?}", crate_graph);
155     analysis_change.set_crate_graph(crate_graph);
156
157     host.apply_change(analysis_change);
158     host
159 }
160
161 #[cfg(test)]
162 mod tests {
163     use super::*;
164
165     use hir::Crate;
166
167     #[test]
168     fn test_loading_rust_analyzer() {
169         let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
170         let (host, _roots) = load_cargo(path, false, false).unwrap();
171         let n_crates = Crate::all(host.raw_database()).len();
172         // RA has quite a few crates, but the exact count doesn't matter
173         assert!(n_crates > 20);
174     }
175 }