]> git.lizzy.rs Git - rust.git/blobdiff - crates/rust-analyzer/src/cli/load_cargo.rs
Merge #7482
[rust.git] / crates / rust-analyzer / src / cli / load_cargo.rs
index e910db6eb5341f9b60d0a2839de65868287c5ea4..e12e871806b2a1576d85872127f5b3a45ee894df 100644 (file)
@@ -1,15 +1,17 @@
 //! Loads a Cargo project into a static instance of analysis, without support
 //! for incorporating changes.
-use std::{convert::TryFrom, path::Path, sync::Arc};
+use std::{path::Path, sync::Arc};
 
 use anyhow::Result;
 use crossbeam_channel::{unbounded, Receiver};
-use ra_db::{AbsPathBuf, CrateGraph};
-use ra_ide::{AnalysisChange, AnalysisHost};
-use ra_project_model::{CargoConfig, ProcMacroClient, ProjectManifest, ProjectWorkspace};
-use vfs::loader::Handle;
+use ide::{AnalysisHost, Change};
+use ide_db::base_db::CrateGraph;
+use project_model::{
+    BuildDataCollector, CargoConfig, ProcMacroClient, ProjectManifest, ProjectWorkspace,
+};
+use vfs::{loader::Handle, AbsPath, AbsPathBuf};
 
-use crate::global_state::{ProjectFolders, SourceRootConfig};
+use crate::reload::{ProjectFolders, SourceRootConfig};
 
 pub fn load_cargo(
     root: &Path,
@@ -18,36 +20,43 @@ pub fn load_cargo(
 ) -> Result<(AnalysisHost, vfs::Vfs)> {
     let root = AbsPathBuf::assert(std::env::current_dir()?.join(root));
     let root = ProjectManifest::discover_single(&root)?;
-    let ws = ProjectWorkspace::load(
-        root,
-        &CargoConfig { load_out_dirs_from_check, ..Default::default() },
-        true,
-    )?;
+    let ws = ProjectWorkspace::load(root, &CargoConfig::default(), &|_| {})?;
 
     let (sender, receiver) = unbounded();
     let mut vfs = vfs::Vfs::default();
     let mut loader = {
         let loader =
-            vfs_notify::LoaderHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
+            vfs_notify::NotifyHandle::spawn(Box::new(move |msg| sender.send(msg).unwrap()));
         Box::new(loader)
     };
 
     let proc_macro_client = if with_proc_macro {
         let path = std::env::current_exe()?;
-        ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap()
+        Some(ProcMacroClient::extern_process(path, &["proc-macro"]).unwrap())
     } else {
-        ProcMacroClient::dummy()
+        None
     };
 
-    let crate_graph = ws.to_crate_graph(None, &proc_macro_client, &mut |path: &Path| {
-        let path = AbsPathBuf::try_from(path.to_path_buf()).unwrap();
-        let contents = loader.load_sync(&path);
-        let path = vfs::VfsPath::from(path);
-        vfs.set_file_contents(path.clone(), contents);
-        vfs.file_id(&path)
-    });
+    let build_data = if load_out_dirs_from_check {
+        let mut collector = BuildDataCollector::default();
+        ws.collect_build_data_configs(&mut collector);
+        Some(collector.collect(&|_| {})?)
+    } else {
+        None
+    };
+
+    let crate_graph = ws.to_crate_graph(
+        build_data.as_ref(),
+        proc_macro_client.as_ref(),
+        &mut |path: &AbsPath| {
+            let contents = loader.load_sync(path);
+            let path = vfs::VfsPath::from(path.to_path_buf());
+            vfs.set_file_contents(path.clone(), contents);
+            vfs.file_id(&path)
+        },
+    );
 
-    let project_folders = ProjectFolders::new(&[ws]);
+    let project_folders = ProjectFolders::new(&[ws], &[], build_data.as_ref());
     loader.set_config(vfs::loader::Config { load: project_folders.load, watch: vec![] });
 
     log::debug!("crate graph: {:?}", crate_graph);
@@ -55,7 +64,7 @@ pub fn load_cargo(
     Ok((host, vfs))
 }
 
-pub(crate) fn load(
+fn load(
     crate_graph: CrateGraph,
     source_root_config: SourceRootConfig,
     vfs: &mut vfs::Vfs,
@@ -63,19 +72,19 @@ pub(crate) fn load(
 ) -> AnalysisHost {
     let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
     let mut host = AnalysisHost::new(lru_cap);
-    let mut analysis_change = AnalysisChange::new();
+    let mut analysis_change = Change::new();
 
     // wait until Vfs has loaded all roots
     for task in receiver {
         match task {
-            vfs::loader::Message::Progress { n_entries_done, n_entries_total } => {
-                if n_entries_done == n_entries_total {
+            vfs::loader::Message::Progress { n_done, n_total } => {
+                if n_done == n_total {
                     break;
                 }
             }
             vfs::loader::Message::Loaded { files } => {
                 for (path, contents) in files {
-                    vfs.set_file_contents(path.into(), contents)
+                    vfs.set_file_contents(path.into(), contents);
                 }
             }
         }