Encapsulate CrateGraph a bit

This commit is contained in:
Aleksey Kladov 2018-10-25 17:40:24 +03:00
parent 75d9cbd7c2
commit e0eb33605a
4 changed files with 24 additions and 18 deletions

View file

@ -11,7 +11,7 @@ rayon = "1.0.2"
fst = "0.3.1"
ra_syntax = { path = "../ra_syntax" }
ra_editor = { path = "../ra_editor" }
salsa = "0.6.2"
salsa = "0.6.1"
rustc-hash = "1.0"
[dev-dependencies]

View file

@ -15,9 +15,9 @@ mod completion;
use std::{
fmt,
sync::Arc,
collections::BTreeMap,
};
use rustc_hash::FxHashMap;
use ra_syntax::{AtomEdit, File, TextRange, TextUnit};
use relative_path::{RelativePath, RelativePathBuf};
use rayon::prelude::*;
@ -55,9 +55,21 @@ 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, Hash)]
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CrateGraph {
pub crate_roots: BTreeMap<CrateId, FileId>,
crate_roots: FxHashMap<CrateId, FileId>,
}
impl CrateGraph {
pub fn new() -> CrateGraph {
CrateGraph::default()
}
pub fn add_crate_root(&mut self, file_id: FileId) -> CrateId{
let crate_id = CrateId(self.crate_roots.len() as u32);
let prev = self.crate_roots.insert(crate_id, file_id);
assert!(prev.is_none());
crate_id
}
}
pub trait FileResolver: fmt::Debug + Send + Sync + 'static {

View file

@ -7,7 +7,6 @@ extern crate test_utils;
use std::{
sync::Arc,
collections::BTreeMap,
};
use ra_syntax::TextRange;
@ -130,19 +129,17 @@ fn test_resolve_crate_root() {
let snap = host.analysis();
assert!(snap.crate_for(FileId(2)).unwrap().is_empty());
let crate_graph = CrateGraph {
crate_roots: {
let mut m = BTreeMap::default();
m.insert(CrateId(1), FileId(1));
m
},
let crate_graph = {
let mut g = CrateGraph::new();
g.add_crate_root(FileId(1));
g
};
let mut change = AnalysisChange::new();
change.set_crate_graph(crate_graph);
host.apply_change(change);
let snap = host.analysis();
assert_eq!(snap.crate_for(FileId(2)).unwrap(), vec![CrateId(1)],);
assert_eq!(snap.crate_for(FileId(2)).unwrap(), vec![CrateId(0)],);
}
#[test]

View file

@ -2,11 +2,10 @@ use std::{
fs,
path::{Path, PathBuf},
sync::Arc,
collections::BTreeMap,
};
use languageserver_types::Url;
use ra_analysis::{Analysis, AnalysisHost, AnalysisChange, CrateGraph, CrateId, FileId, FileResolver, LibraryData};
use ra_analysis::{Analysis, AnalysisHost, AnalysisChange, CrateGraph, FileId, FileResolver, LibraryData};
use rustc_hash::FxHashMap;
use crate::{
@ -149,7 +148,7 @@ impl ServerWorldState {
Ok(file_id)
}
pub fn set_workspaces(&mut self, ws: Vec<CargoWorkspace>) {
let mut crate_roots = BTreeMap::default();
let mut crate_graph = CrateGraph::new();
ws.iter()
.flat_map(|ws| {
ws.packages()
@ -158,11 +157,9 @@ impl ServerWorldState {
})
.for_each(|root| {
if let Some(file_id) = self.path_map.get_id(root) {
let crate_id = CrateId(crate_roots.len() as u32);
crate_roots.insert(crate_id, file_id);
crate_graph.add_crate_root(file_id);
}
});
let crate_graph = CrateGraph { crate_roots };
self.workspaces = Arc::new(ws);
let mut change = AnalysisChange::new();
change.set_crate_graph(crate_graph);