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" fst = "0.3.1"
ra_syntax = { path = "../ra_syntax" } ra_syntax = { path = "../ra_syntax" }
ra_editor = { path = "../ra_editor" } ra_editor = { path = "../ra_editor" }
salsa = "0.6.2" salsa = "0.6.1"
rustc-hash = "1.0" rustc-hash = "1.0"
[dev-dependencies] [dev-dependencies]

View file

@ -15,9 +15,9 @@ mod completion;
use std::{ use std::{
fmt, fmt,
sync::Arc, sync::Arc,
collections::BTreeMap,
}; };
use rustc_hash::FxHashMap;
use ra_syntax::{AtomEdit, File, TextRange, TextUnit}; use ra_syntax::{AtomEdit, File, TextRange, TextUnit};
use relative_path::{RelativePath, RelativePathBuf}; use relative_path::{RelativePath, RelativePathBuf};
use rayon::prelude::*; use rayon::prelude::*;
@ -55,9 +55,21 @@ pub struct FileId(pub u32);
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct CrateId(pub u32); pub struct CrateId(pub u32);
#[derive(Debug, Clone, Default, PartialEq, Eq, Hash)] #[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct CrateGraph { 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 { pub trait FileResolver: fmt::Debug + Send + Sync + 'static {

View file

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

View file

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