135 lines
4.5 KiB
Rust
Raw Normal View History

use std::{collections::HashSet, error::Error, path::Path};
2019-06-15 13:24:02 +06:00
2019-02-09 13:06:12 +01:00
use rustc_hash::FxHashMap;
2019-02-05 22:54:17 +01:00
use ra_db::{CrateGraph, FileId, SourceRootId};
use ra_ide_api::{AnalysisChange, AnalysisHost};
2019-08-06 10:50:32 +02:00
use ra_project_model::{PackageRoot, ProjectWorkspace};
use ra_vfs::{RootEntry, Vfs, VfsChange};
use ra_vfs_glob::RustPackageFilterBuilder;
2019-02-09 13:06:12 +01:00
2019-06-15 13:24:02 +06:00
type Result<T> = std::result::Result<T, Box<dyn Error + Send + Sync>>;
2019-02-05 22:54:17 +01:00
2019-02-09 13:06:12 +01:00
fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
2019-06-03 10:21:08 -04:00
FileId(f.0)
2019-02-09 13:06:12 +01:00
}
fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
2019-06-03 10:21:08 -04:00
SourceRootId(r.0)
2019-02-09 13:06:12 +01:00
}
2019-08-06 10:50:32 +02:00
pub fn load_cargo(root: &Path) -> Result<(AnalysisHost, FxHashMap<SourceRootId, PackageRoot>)> {
2019-06-15 16:29:23 +03:00
let root = std::env::current_dir()?.join(root);
let ws = ProjectWorkspace::discover(root.as_ref())?;
let project_roots = ws.to_roots();
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-06-15 16:29:23 +03:00
let crate_graph = ws.to_crate_graph(&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);
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<_, _>>();
let host = load(&source_roots, crate_graph, &mut vfs);
Ok((host, source_roots))
2019-06-15 16:29:23 +03:00
}
2019-02-09 13:06:12 +01:00
pub fn load(
2019-08-06 10:50:32 +02:00
source_roots: &FxHashMap<SourceRootId, PackageRoot>,
crate_graph: CrateGraph,
vfs: &mut Vfs,
) -> AnalysisHost {
2019-06-15 16:29:23 +03:00
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();
analysis_change.set_crate_graph(crate_graph);
// wait until Vfs has loaded all roots
let receiver = vfs.task_receiver().clone();
let mut roots_loaded = HashSet::new();
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 16:29:23 +03:00
log::debug!(
"loaded source root {:?} with path {:?}",
source_root_id,
vfs.root2path(root)
);
analysis_change.add_root(source_root_id, is_local);
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 13:06:12 +01:00
}
2019-06-15 16:29:23 +03:00
roots_loaded.insert(source_root_id);
if roots_loaded.len() == vfs.n_roots() {
done = true;
2019-02-09 13:06:12 +01:00
}
}
2019-06-15 16:29:23 +03:00
VfsChange::AddFile { .. }
| VfsChange::RemoveFile { .. }
| VfsChange::ChangeFile { .. } => {
// We just need the first scan, so just ignore these
}
2019-02-09 13:06:12 +01:00
}
}
2019-06-15 16:29:23 +03:00
if done {
break;
}
2019-02-09 13:06:12 +01:00
}
2019-06-15 16:29:23 +03:00
host.apply_change(analysis_change);
host
2019-02-09 13:06:12 +01:00
}
2019-02-10 11:44:53 +01:00
#[cfg(test)]
mod tests {
use super::*;
use ra_hir::Crate;
2019-02-10 11:44:53 +01:00
#[test]
fn test_loading_rust_analyzer() {
2019-04-04 18:57:10 +09:00
let path = Path::new(env!("CARGO_MANIFEST_DIR")).parent().unwrap().parent().unwrap();
2019-06-15 16:29:23 +03:00
let (host, roots) = load_cargo(path).unwrap();
2019-02-17 21:05:33 +03:00
let mut n_crates = 0;
for (root, _) in roots {
2019-06-15 16:29:23 +03:00
for _krate in Crate::source_root_crates(host.raw_database(), root) {
2019-02-17 21:05:33 +03:00
n_crates += 1;
2019-02-10 11:44:53 +01:00
}
}
// RA has quite a few crates, but the exact count doesn't matter
2019-02-17 21:05:33 +03:00
assert!(n_crates > 20);
2019-02-10 11:44:53 +01:00
}
}