1 //! Loads a Cargo project into a static instance of analysis, without support
2 //! for incorporating changes.
3 use std::{path::Path, sync::Arc};
6 use crossbeam_channel::{unbounded, Receiver};
7 use ide::{AnalysisHost, Change};
8 use ide_db::base_db::CrateGraph;
10 BuildDataCollector, CargoConfig, ProcMacroClient, ProjectManifest, ProjectWorkspace,
12 use vfs::{loader::Handle, AbsPath, AbsPathBuf};
14 use crate::reload::{ProjectFolders, SourceRootConfig};
16 pub struct LoadCargoConfig {
17 pub load_out_dirs_from_check: bool,
18 pub with_proc_macro: bool,
21 pub fn load_workspace_at(
23 cargo_config: &CargoConfig,
24 load_config: &LoadCargoConfig,
25 progress: &dyn Fn(String),
26 ) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroClient>)> {
27 let root = AbsPathBuf::assert(std::env::current_dir()?.join(root));
28 let root = ProjectManifest::discover_single(&root)?;
29 let workspace = ProjectWorkspace::load(root, cargo_config, progress)?;
31 load_workspace(workspace, load_config, progress)
34 pub fn load_workspace(
36 config: &LoadCargoConfig,
37 progress: &dyn Fn(String),
38 ) -> Result<(AnalysisHost, vfs::Vfs, Option<ProcMacroClient>)> {
39 let (sender, receiver) = unbounded();
40 let mut vfs = vfs::Vfs::default();
43 vfs_notify::NotifyHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
47 let proc_macro_client = if config.with_proc_macro {
48 let path = std::env::current_exe()?;
49 Some(ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap())
54 let build_data = if config.load_out_dirs_from_check {
55 let mut collector = BuildDataCollector::default();
56 ws.collect_build_data_configs(&mut collector);
57 Some(collector.collect(progress)?)
62 let crate_graph = ws.to_crate_graph(
64 proc_macro_client.as_ref(),
65 &mut |path: &AbsPath| {
66 let contents = loader.load_sync(path);
67 let path = vfs::VfsPath::from(path.to_path_buf());
68 vfs.set_file_contents(path.clone(), contents);
73 let project_folders = ProjectFolders::new(&[ws], &[], build_data.as_ref());
74 loader.set_config(vfs::loader::Config {
75 load: project_folders.load,
80 log::debug!("crate graph: {:?}", crate_graph);
82 load_crate_graph(crate_graph, project_folders.source_root_config, &mut vfs, &receiver);
83 Ok((host, vfs, proc_macro_client))
87 crate_graph: CrateGraph,
88 source_root_config: SourceRootConfig,
90 receiver: &Receiver<vfs::loader::Message>,
92 let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
93 let mut host = AnalysisHost::new(lru_cap);
94 let mut analysis_change = Change::new();
96 // wait until Vfs has loaded all roots
97 for task in receiver {
99 vfs::loader::Message::Progress { n_done, n_total, config_version: _ } => {
100 if n_done == n_total {
104 vfs::loader::Message::Loaded { files } => {
105 for (path, contents) in files {
106 vfs.set_file_contents(path.into(), contents);
111 let changes = vfs.take_changes();
112 for file in changes {
114 let contents = vfs.file_contents(file.file_id).to_vec();
115 if let Ok(text) = String::from_utf8(contents) {
116 analysis_change.change_file(file.file_id, Some(Arc::new(text)))
120 let source_roots = source_root_config.partition(&vfs);
121 analysis_change.set_roots(source_roots);
123 analysis_change.set_crate_graph(crate_graph);
125 host.apply_change(analysis_change);
136 fn test_loading_rust_analyzer() -> Result<()> {
137 let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
138 let cargo_config = Default::default();
139 let load_cargo_config =
140 LoadCargoConfig { load_out_dirs_from_check: false, with_proc_macro: false };
141 let (host, _vfs, _proc_macro) =
142 load_workspace_at(path, &cargo_config, &load_cargo_config, &|_| {})?;
144 let n_crates = Crate::all(host.raw_database()).len();
145 // RA has quite a few crates, but the exact count doesn't matter
146 assert!(n_crates > 20);