rust/crates/rust-analyzer/src/cli/load_cargo.rs

174 lines
6.0 KiB
Rust
Raw Normal View History

2020-02-18 11:11:32 +00:00
//! Loads a Cargo project into a static instance of analysis, without support
//! for incorporating changes.
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-16 13:51:44 +00:00
use ra_project_model::{get_rustc_cfg_options, CargoFeatures, PackageRoot, ProjectWorkspace};
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(),
&CargoFeatures { load_out_dirs_from_check, ..Default::default() },
)?;
let mut extern_dirs = FxHashSet::default();
extern_dirs.extend(ws.out_dirs());
let mut project_roots = ws.to_roots();
project_roots
.extend(extern_dirs.iter().map(|path| PackageRoot::new(path.to_path_buf(), false)));
2019-08-25 10:04:56 +00:00
let (sender, receiver) = unbounded();
let sender = Box::new(move |t| sender.send(t).unwrap());
let (mut vfs, roots) = Vfs::new(
project_roots
.iter()
.map(|pkg_root| {
RootEntry::new(
pkg_root.path().clone(),
RustPackageFilterBuilder::default()
.set_member(pkg_root.is_member())
.into_vfs_filter(),
)
})
.collect(),
2019-08-25 10:04:56 +00:00
sender,
2019-09-06 13:25:24 +00:00
Watch(false),
);
2019-10-02 18:02:53 +00:00
let source_roots = roots
.iter()
.map(|&vfs_root| {
let source_root_id = vfs_root_to_id(vfs_root);
let project_root = project_roots
.iter()
.find(|it| it.path() == &vfs.root2path(vfs_root))
.unwrap()
.clone();
(source_root_id, project_root)
})
.collect::<FxHashMap<_, _>>();
2020-03-16 13:51:44 +00:00
let host = load(&source_roots, ws, &mut vfs, receiver, extern_dirs);
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,
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>,
) -> 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);
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);
analysis_change.set_debug_root_path(
source_root_id,
source_roots[&source_root_id].path().display().to_string(),
);
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
}
}
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
};
let crate_graph =
ws.to_crate_graph(&default_cfg_options, &extern_source_roots, &mut |path: &Path| {
let vfs_file = vfs.load(path);
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
vfs_file.map(vfs_file_to_id)
});
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();
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
}
}