2020-02-18 11:11:32 +00:00
|
|
|
//! Loads a Cargo project into a static instance of analysis, without support
|
|
|
|
//! for incorporating changes.
|
2019-09-30 08:58:53 +00:00
|
|
|
|
2020-03-16 13:51:44 +00:00
|
|
|
use std::path::{Path, PathBuf};
|
2019-02-05 21:54:17 +00:00
|
|
|
|
2020-02-18 11:11:32 +00:00
|
|
|
use anyhow::Result;
|
2019-08-25 10:04:56 +00:00
|
|
|
use crossbeam_channel::{unbounded, Receiver};
|
2020-03-16 13:51:44 +00:00
|
|
|
use ra_db::{ExternSourceId, FileId, SourceRootId};
|
2020-03-10 17:56:15 +00:00
|
|
|
use ra_ide::{AnalysisChange, AnalysisHost};
|
2020-03-18 12:56:46 +00:00
|
|
|
use ra_project_model::{
|
2020-04-01 16:51:16 +00:00
|
|
|
get_rustc_cfg_options, CargoConfig, PackageRoot, ProcMacroClient, ProjectWorkspace,
|
2020-03-18 12:56:46 +00:00
|
|
|
};
|
2019-09-06 13:25:24 +00:00
|
|
|
use ra_vfs::{RootEntry, Vfs, VfsChange, VfsTask, Watch};
|
2020-02-18 11:11:32 +00:00
|
|
|
use rustc_hash::{FxHashMap, FxHashSet};
|
2019-02-09 12:06:12 +00:00
|
|
|
|
2020-02-17 18:07:30 +00:00
|
|
|
use crate::vfs_glob::RustPackageFilterBuilder;
|
|
|
|
|
2019-02-09 12:06:12 +00:00
|
|
|
fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
|
2019-06-03 14:21:08 +00:00
|
|
|
FileId(f.0)
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
|
|
|
fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
|
2019-06-03 14:21:08 +00:00
|
|
|
SourceRootId(r.0)
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
|
|
|
|
2020-02-18 11:11:32 +00:00
|
|
|
pub(crate) fn load_cargo(
|
|
|
|
root: &Path,
|
2020-03-16 13:51:44 +00:00
|
|
|
load_out_dirs_from_check: bool,
|
2020-02-18 11:11:32 +00:00
|
|
|
) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> {
|
2019-06-15 13:29:23 +00:00
|
|
|
let root = std::env::current_dir()?.join(root);
|
2020-03-16 13:51:44 +00:00
|
|
|
let ws = ProjectWorkspace::discover(
|
|
|
|
root.as_ref(),
|
2020-04-01 16:51:16 +00:00
|
|
|
&CargoConfig { load_out_dirs_from_check, ..Default::default() },
|
2020-03-16 13:51:44 +00:00
|
|
|
)?;
|
|
|
|
|
|
|
|
let mut extern_dirs = FxHashSet::default();
|
|
|
|
extern_dirs.extend(ws.out_dirs());
|
|
|
|
|
|
|
|
let mut project_roots = ws.to_roots();
|
2020-03-31 23:15:20 +00:00
|
|
|
project_roots.extend(extern_dirs.iter().cloned().map(PackageRoot::new_non_member));
|
2020-03-16 13:51:44 +00:00
|
|
|
|
2019-08-25 10:04:56 +00:00
|
|
|
let (sender, receiver) = unbounded();
|
|
|
|
let sender = Box::new(move |t| sender.send(t).unwrap());
|
2019-08-06 11:00:37 +00:00
|
|
|
let (mut vfs, roots) = Vfs::new(
|
|
|
|
project_roots
|
|
|
|
.iter()
|
|
|
|
.map(|pkg_root| {
|
|
|
|
RootEntry::new(
|
2020-04-01 10:40:40 +00:00
|
|
|
pkg_root.path().to_owned(),
|
2019-08-06 11:00:37 +00:00
|
|
|
RustPackageFilterBuilder::default()
|
2020-04-01 10:40:40 +00:00
|
|
|
.set_member(pkg_root.is_member())
|
2019-08-06 11:00:37 +00:00
|
|
|
.into_vfs_filter(),
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect(),
|
2019-08-25 10:04:56 +00:00
|
|
|
sender,
|
2019-09-06 13:25:24 +00:00
|
|
|
Watch(false),
|
2019-08-06 11:00:37 +00:00
|
|
|
);
|
2019-10-02 18:02:53 +00:00
|
|
|
|
2019-06-16 16:19:38 +00:00
|
|
|
let source_roots = roots
|
2020-03-31 23:15:20 +00:00
|
|
|
.into_iter()
|
|
|
|
.map(|vfs_root| {
|
2019-06-16 16:19:38 +00:00
|
|
|
let source_root_id = vfs_root_to_id(vfs_root);
|
2020-04-01 10:40:40 +00:00
|
|
|
let project_root = project_roots
|
|
|
|
.iter()
|
|
|
|
.find(|it| it.path() == vfs.root2path(vfs_root))
|
|
|
|
.unwrap()
|
|
|
|
.clone();
|
2019-06-16 16:19:38 +00:00
|
|
|
(source_root_id, project_root)
|
|
|
|
})
|
|
|
|
.collect::<FxHashMap<_, _>>();
|
2020-03-18 12:56:46 +00:00
|
|
|
|
|
|
|
let proc_macro_client = ProcMacroClient::dummy();
|
|
|
|
let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs, &proc_macro_client);
|
2019-06-16 16:19:38 +00:00
|
|
|
Ok((host, source_roots))
|
2019-06-15 13:29:23 +00:00
|
|
|
}
|
2019-02-09 12:06:12 +00:00
|
|
|
|
2020-02-18 11:11:32 +00:00
|
|
|
pub(crate) fn load(
|
2019-08-06 08:50:32 +00:00
|
|
|
source_roots: &FxHashMap<SourceRootId, PackageRoot>,
|
2020-03-16 13:51:44 +00:00
|
|
|
ws: ProjectWorkspace,
|
2019-06-16 16:19:38 +00:00
|
|
|
vfs: &mut Vfs,
|
2019-08-25 10:04:56 +00:00
|
|
|
receiver: Receiver<VfsTask>,
|
2020-03-16 13:51:44 +00:00
|
|
|
extern_dirs: FxHashSet<PathBuf>,
|
2020-03-18 12:56:46 +00:00
|
|
|
proc_macro_client: &ProcMacroClient,
|
2019-06-16 16:19:38 +00:00
|
|
|
) -> AnalysisHost {
|
2019-06-15 13:29:23 +00:00
|
|
|
let lru_cap = std::env::var("RA_LRU_CAP").ok().and_then(|it| it.parse::<usize>().ok());
|
2020-03-10 17:56:15 +00:00
|
|
|
let mut host = AnalysisHost::new(lru_cap);
|
2019-06-15 13:29:23 +00:00
|
|
|
let mut analysis_change = AnalysisChange::new();
|
|
|
|
|
|
|
|
// wait until Vfs has loaded all roots
|
2020-02-18 11:11:32 +00:00
|
|
|
let mut roots_loaded = FxHashSet::default();
|
2020-03-16 13:51:44 +00:00
|
|
|
let mut extern_source_roots = FxHashMap::default();
|
2019-06-15 13:29:23 +00:00
|
|
|
for task in receiver {
|
|
|
|
vfs.handle_task(task);
|
|
|
|
let mut done = false;
|
|
|
|
for change in vfs.commit_changes() {
|
|
|
|
match change {
|
|
|
|
VfsChange::AddRoot { root, files } => {
|
|
|
|
let source_root_id = vfs_root_to_id(root);
|
2020-04-01 10:40:40 +00:00
|
|
|
let is_local = source_roots[&source_root_id].is_member();
|
2019-06-15 13:29:23 +00:00
|
|
|
log::debug!(
|
|
|
|
"loaded source root {:?} with path {:?}",
|
|
|
|
source_root_id,
|
|
|
|
vfs.root2path(root)
|
|
|
|
);
|
|
|
|
analysis_change.add_root(source_root_id, is_local);
|
2019-09-12 09:38:07 +00:00
|
|
|
analysis_change.set_debug_root_path(
|
|
|
|
source_root_id,
|
2020-04-01 10:40:40 +00:00
|
|
|
source_roots[&source_root_id].path().display().to_string(),
|
2019-09-12 09:38:07 +00:00
|
|
|
);
|
2019-06-15 13:29:23 +00:00
|
|
|
|
2020-03-16 13:51:44 +00:00
|
|
|
let vfs_root_path = vfs.root2path(root);
|
|
|
|
if extern_dirs.contains(&vfs_root_path) {
|
|
|
|
extern_source_roots.insert(vfs_root_path, ExternSourceId(root.0));
|
|
|
|
}
|
|
|
|
|
2019-06-15 13:29:23 +00:00
|
|
|
let mut file_map = FxHashMap::default();
|
|
|
|
for (vfs_file, path, text) in files {
|
|
|
|
let file_id = vfs_file_to_id(vfs_file);
|
|
|
|
analysis_change.add_file(source_root_id, file_id, path.clone(), text);
|
|
|
|
file_map.insert(path, file_id);
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
2019-06-15 13:29:23 +00:00
|
|
|
roots_loaded.insert(source_root_id);
|
|
|
|
if roots_loaded.len() == vfs.n_roots() {
|
|
|
|
done = true;
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-24 09:06:00 +00:00
|
|
|
VfsChange::AddFile { root, file, path, text } => {
|
|
|
|
let source_root_id = vfs_root_to_id(root);
|
|
|
|
let file_id = vfs_file_to_id(file);
|
|
|
|
analysis_change.add_file(source_root_id, file_id, path, text);
|
|
|
|
}
|
|
|
|
VfsChange::RemoveFile { .. } | VfsChange::ChangeFile { .. } => {
|
2019-06-15 13:29:23 +00:00
|
|
|
// We just need the first scan, so just ignore these
|
|
|
|
}
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-15 13:29:23 +00:00
|
|
|
if done {
|
|
|
|
break;
|
|
|
|
}
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
|
|
|
|
2020-03-16 13:51:44 +00:00
|
|
|
// FIXME: cfg options?
|
|
|
|
let default_cfg_options = {
|
|
|
|
let mut opts = get_rustc_cfg_options();
|
|
|
|
opts.insert_atom("test".into());
|
|
|
|
opts.insert_atom("debug_assertion".into());
|
|
|
|
opts
|
|
|
|
};
|
|
|
|
|
2020-03-18 12:56:46 +00:00
|
|
|
let crate_graph = ws.to_crate_graph(
|
|
|
|
&default_cfg_options,
|
|
|
|
&extern_source_roots,
|
|
|
|
proc_macro_client,
|
|
|
|
&mut |path: &Path| {
|
2020-04-11 10:12:50 +00:00
|
|
|
// Some path from metadata will be non canonicalized, e.g. /foo/../bar/lib.rs
|
|
|
|
let path = path.canonicalize().ok()?;
|
|
|
|
let vfs_file = vfs.load(&path);
|
2020-03-16 13:51:44 +00:00
|
|
|
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
|
|
|
|
vfs_file.map(vfs_file_to_id)
|
2020-03-18 12:56:46 +00:00
|
|
|
},
|
|
|
|
);
|
2020-03-16 13:51:44 +00:00
|
|
|
log::debug!("crate graph: {:?}", crate_graph);
|
|
|
|
analysis_change.set_crate_graph(crate_graph);
|
|
|
|
|
2019-06-15 13:29:23 +00:00
|
|
|
host.apply_change(analysis_change);
|
|
|
|
host
|
2019-02-09 12:06:12 +00:00
|
|
|
}
|
2019-02-10 10:44:53 +00:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-02-17 16:31:09 +00:00
|
|
|
|
|
|
|
use hir::Crate;
|
2019-02-10 10:44:53 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_loading_rust_analyzer() {
|
2019-04-04 09:57:10 +00:00
|
|
|
let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
|
2020-03-16 13:51:44 +00:00
|
|
|
let (host, _roots) = load_cargo(path, false).unwrap();
|
2019-10-14 12:15:47 +00:00
|
|
|
let n_crates = Crate::all(host.raw_database()).len();
|
2019-02-10 10:44:53 +00:00
|
|
|
// RA has quite a few crates, but the exact count doesn't matter
|
2019-02-17 18:05:33 +00:00
|
|
|
assert!(n_crates > 20);
|
2019-02-10 10:44:53 +00:00
|
|
|
}
|
|
|
|
}
|