]> git.lizzy.rs Git - rust.git/blob - crates/rust-analyzer/src/cli/load_cargo.rs
Merge #6350
[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 use std::{path::Path, sync::Arc};
4
5 use anyhow::Result;
6 use crossbeam_channel::{unbounded, Receiver};
7 use ide::{AnalysisHost, Change};
8 use ide_db::base_db::CrateGraph;
9 use project_model::{CargoConfig, ProcMacroClient, ProjectManifest, ProjectWorkspace};
10 use vfs::{loader::Handle, AbsPath, AbsPathBuf};
11
12 use crate::reload::{ProjectFolders, SourceRootConfig};
13
14 pub fn load_cargo(
15     root: &Path,
16     load_out_dirs_from_check: bool,
17     with_proc_macro: bool,
18 ) -> Result<(AnalysisHost, vfs::Vfs)> {
19     let root = AbsPathBuf::assert(std::env::current_dir()?.join(root));
20     let root = ProjectManifest::discover_single(&root)?;
21     let ws = ProjectWorkspace::load(
22         root,
23         &CargoConfig { load_out_dirs_from_check, ..Default::default() },
24         true,
25     )?;
26
27     let (sender, receiver) = unbounded();
28     let mut vfs = vfs::Vfs::default();
29     let mut loader = {
30         let loader =
31             vfs_notify::NotifyHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
32         Box::new(loader)
33     };
34
35     let proc_macro_client = if with_proc_macro {
36         let path = std::env::current_exe()?;
37         ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap()
38     } else {
39         ProcMacroClient::dummy()
40     };
41
42     let crate_graph = ws.to_crate_graph(None, &proc_macro_client, &mut |path: &AbsPath| {
43         let contents = loader.load_sync(path);
44         let path = vfs::VfsPath::from(path.to_path_buf());
45         vfs.set_file_contents(path.clone(), contents);
46         vfs.file_id(&path)
47     });
48
49     let project_folders = ProjectFolders::new(&[ws]);
50     loader.set_config(vfs::loader::Config { load: project_folders.load, watch: vec![] });
51
52     log::debug!("crate graph: {:?}", crate_graph);
53     let host = load(crate_graph, project_folders.source_root_config, &mut vfs, &receiver);
54     Ok((host, vfs))
55 }
56
57 fn load(
58     crate_graph: CrateGraph,
59     source_root_config: SourceRootConfig,
60     vfs: &mut vfs::Vfs,
61     receiver: &Receiver<vfs::loader::Message>,
62 ) -> AnalysisHost {
63     let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
64     let mut host = AnalysisHost::new(lru_cap);
65     let mut analysis_change = Change::new();
66
67     // wait until Vfs has loaded all roots
68     for task in receiver {
69         match task {
70             vfs::loader::Message::Progress { n_done, n_total } => {
71                 if n_done == n_total {
72                     break;
73                 }
74             }
75             vfs::loader::Message::Loaded { files } => {
76                 for (path, contents) in files {
77                     vfs.set_file_contents(path.into(), contents)
78                 }
79             }
80         }
81     }
82     let changes = vfs.take_changes();
83     for file in changes {
84         if file.exists() {
85             let contents = vfs.file_contents(file.file_id).to_vec();
86             if let Ok(text) = String::from_utf8(contents) {
87                 analysis_change.change_file(file.file_id, Some(Arc::new(text)))
88             }
89         }
90     }
91     let source_roots = source_root_config.partition(&vfs);
92     analysis_change.set_roots(source_roots);
93
94     analysis_change.set_crate_graph(crate_graph);
95
96     host.apply_change(analysis_change);
97     host
98 }
99
100 #[cfg(test)]
101 mod tests {
102     use super::*;
103
104     use hir::Crate;
105
106     #[test]
107     fn test_loading_rust_analyzer() {
108         let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
109         let (host, _vfs) = load_cargo(path, false, false).unwrap();
110         let n_crates = Crate::all(host.raw_database()).len();
111         // RA has quite a few crates, but the exact count doesn't matter
112         assert!(n_crates > 20);
113     }
114 }