139 lines
4.5 KiB
Rust
Raw Normal View History

mod vfs_filter;
2019-02-05 22:54:17 +01:00
use std::sync::Arc;
use std::path::Path;
2019-02-09 13:06:12 +01:00
use std::collections::HashSet;
use rustc_hash::FxHashMap;
2019-02-05 22:54:17 +01:00
use ra_db::{
2019-02-09 13:06:12 +01:00
CrateGraph, FileId, SourceRoot, SourceRootId, SourceDatabase, salsa,
2019-02-05 22:54:17 +01:00
};
2019-04-09 22:51:22 +03:00
use ra_hir::db;
2019-02-09 13:06:12 +01:00
use ra_project_model::ProjectWorkspace;
use ra_vfs::{Vfs, VfsChange};
use vfs_filter::IncludeRustFiles;
2019-02-09 13:06:12 +01:00
type Result<T> = std::result::Result<T, failure::Error>;
2019-02-05 22:54:17 +01:00
2019-06-01 21:17:57 +03:00
#[salsa::database(
ra_db::SourceDatabaseStorage,
db::AstDatabaseStorage,
db::DefDatabaseStorage,
db::HirDatabaseStorage
)]
2019-02-05 22:54:17 +01:00
#[derive(Debug)]
2019-02-09 13:06:12 +01:00
pub struct BatchDatabase {
2019-02-05 22:54:17 +01:00
runtime: salsa::Runtime<BatchDatabase>,
}
impl salsa::Database for BatchDatabase {
fn salsa_runtime(&self) -> &salsa::Runtime<BatchDatabase> {
&self.runtime
}
}
2019-02-09 13:06:12 +01:00
fn vfs_file_to_id(f: ra_vfs::VfsFile) -> FileId {
FileId(f.0.into())
}
fn vfs_root_to_id(r: ra_vfs::VfsRoot) -> SourceRootId {
SourceRootId(r.0.into())
}
impl BatchDatabase {
pub fn load(crate_graph: CrateGraph, vfs: &mut Vfs) -> BatchDatabase {
2019-04-09 22:51:22 +03:00
let mut db = BatchDatabase { runtime: salsa::Runtime::default() };
2019-02-09 13:06:12 +01:00
db.set_crate_graph(Arc::new(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);
log::debug!(
"loaded source root {:?} with path {:?}",
source_root_id,
vfs.root2path(root)
);
2019-02-09 13:06:12 +01:00
let mut file_map = FxHashMap::default();
for (vfs_file, path, text) in files {
let file_id = vfs_file_to_id(vfs_file);
db.set_file_text(file_id, text);
db.set_file_relative_path(file_id, path.clone());
db.set_file_source_root(file_id, source_root_id);
file_map.insert(path, file_id);
}
let source_root = SourceRoot { files: file_map };
db.set_source_root(source_root_id, Arc::new(source_root));
roots_loaded.insert(source_root_id);
2019-02-17 21:05:33 +03:00
if roots_loaded.len() == vfs.n_roots() {
2019-02-09 13:06:12 +01:00
done = true;
}
}
VfsChange::AddFile { .. }
| VfsChange::RemoveFile { .. }
| VfsChange::ChangeFile { .. } => {
2019-02-10 11:48:59 +01:00
// We just need the first scan, so just ignore these
2019-02-09 13:06:12 +01:00
}
}
}
if done {
break;
}
}
db
}
pub fn load_cargo(root: impl AsRef<Path>) -> Result<(BatchDatabase, Vec<SourceRootId>)> {
let root = std::env::current_dir()?.join(root);
2019-02-09 13:06:12 +01:00
let ws = ProjectWorkspace::discover(root.as_ref())?;
let mut roots = Vec::new();
roots.push(IncludeRustFiles::member(root.clone()));
roots.extend(IncludeRustFiles::from_roots(ws.to_roots()));
2019-02-09 13:06:12 +01:00
let (mut vfs, roots) = Vfs::new(roots);
let mut load = |path: &Path| {
let vfs_file = vfs.load(path);
log::debug!("vfs file {:?} -> {:?}", path, vfs_file);
vfs_file.map(vfs_file_to_id)
};
let crate_graph = ws.to_crate_graph(&mut load);
log::debug!("crate graph: {:?}", crate_graph);
let local_roots = roots
.into_iter()
2019-02-09 13:06:12 +01:00
.filter(|r| vfs.root2path(*r).starts_with(&root))
.map(vfs_root_to_id)
.collect();
let db = BatchDatabase::load(crate_graph, &mut vfs);
Ok((db, local_roots))
}
}
2019-02-10 11:44:53 +01:00
#[cfg(test)]
mod tests {
use ra_hir::Crate;
use super::*;
#[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-02-10 11:44:53 +01:00
let (db, roots) = BatchDatabase::load_cargo(path).unwrap();
2019-02-17 21:05:33 +03:00
let mut n_crates = 0;
2019-02-10 11:44:53 +01:00
for root in roots {
for _krate in Crate::source_root_crates(&db, 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
}
}