rust-analyzer/crates/ra_db/src/input.rs

104 lines
2.7 KiB
Rust
Raw Normal View History

use std::sync::Arc;
2018-10-25 14:52:50 +00:00
use rustc_hash::FxHashMap;
2018-10-31 20:41:43 +00:00
use rustc_hash::FxHashSet;
use salsa;
use crate::file_resolver::FileResolverImp;
2018-10-25 14:52:50 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct FileId(pub u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CrateId(pub u32);
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CrateGraph {
2018-12-05 13:01:18 +00:00
arena: FxHashMap<CrateId, CrateData>,
2018-10-25 14:52:50 +00:00
}
2018-12-05 13:01:18 +00:00
#[derive(Debug, Clone, PartialEq, Eq)]
struct CrateData {
file_id: FileId,
deps: Vec<Dependency>,
}
impl CrateData {
fn new(file_id: FileId) -> CrateData {
CrateData {
file_id,
deps: Vec::new(),
}
2018-10-25 14:52:50 +00:00
}
2018-12-05 13:01:18 +00:00
fn add_dep(&mut self, dep: CrateId) {
self.deps.push(Dependency { crate_: dep })
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Dependency {
crate_: CrateId,
}
impl CrateGraph {
pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId {
2018-12-05 13:01:18 +00:00
let crate_id = CrateId(self.arena.len() as u32);
let prev = self.arena.insert(crate_id, CrateData::new(file_id));
2018-10-25 14:52:50 +00:00
assert!(prev.is_none());
crate_id
}
2018-12-08 20:16:11 +00:00
//FIXME: check that we don't have cycles here.
// Just a simple depth first search from `to` should work,
// the graph is small.
2018-12-05 13:01:18 +00:00
pub fn add_dep(&mut self, from: CrateId, to: CrateId) {
self.arena.get_mut(&from).unwrap().add_dep(to)
}
pub fn crate_root(&self, crate_id: CrateId) -> FileId {
self.arena[&crate_id].file_id
}
pub fn crate_id_for_crate_root(&self, file_id: FileId) -> Option<CrateId> {
let (&crate_id, _) = self
2018-12-05 13:01:18 +00:00
.arena
.iter()
2018-12-05 13:01:18 +00:00
.find(|(_crate_id, data)| data.file_id == file_id)?;
Some(crate_id)
2018-11-26 21:12:43 +00:00
}
2018-10-25 14:52:50 +00:00
}
salsa::query_group! {
pub trait FilesDatabase: salsa::Database {
fn file_text(file_id: FileId) -> Arc<String> {
type FileTextQuery;
storage input;
}
fn file_source_root(file_id: FileId) -> SourceRootId {
type FileSourceRootQuery;
storage input;
}
fn source_root(id: SourceRootId) -> Arc<SourceRoot> {
type SourceRootQuery;
storage input;
}
fn libraries() -> Arc<Vec<SourceRootId>> {
2018-10-31 18:05:14 +00:00
type LibrariesQuery;
storage input;
}
fn crate_graph() -> Arc<CrateGraph> {
type CrateGraphQuery;
storage input;
}
}
}
2018-10-30 21:59:43 +00:00
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct SourceRootId(pub u32);
2018-10-30 21:59:43 +00:00
#[derive(Default, Clone, Debug, PartialEq, Eq)]
pub struct SourceRoot {
pub file_resolver: FileResolverImp,
pub files: FxHashSet<FileId>,
}
pub const WORKSPACE: SourceRootId = SourceRootId(0);