remove depedency on ra_arena

This commit is contained in:
Aleksey Kladov 2019-02-18 14:20:54 +03:00
parent 4c154c289e
commit 74288ae272
2 changed files with 23 additions and 18 deletions

View file

@ -15,7 +15,6 @@ drop_bomb = "0.1.0"
parking_lot = "0.7.0" parking_lot = "0.7.0"
thread_worker = { path = "../thread_worker" } thread_worker = { path = "../thread_worker" }
ra_arena = { path = "../ra_arena" }
[dev-dependencies] [dev-dependencies]
tempfile = "3" tempfile = "3"

View file

@ -25,7 +25,6 @@ use std::{
}; };
use crossbeam_channel::Receiver; use crossbeam_channel::Receiver;
use ra_arena::{impl_arena_id, Arena, RawId};
use relative_path::{RelativePath, RelativePathBuf}; use relative_path::{RelativePath, RelativePathBuf};
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
@ -40,8 +39,7 @@ pub use crate::{
}; };
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct VfsFile(pub RawId); pub struct VfsFile(pub u32);
impl_arena_id!(VfsFile);
struct VfsFileData { struct VfsFileData {
root: VfsRoot, root: VfsRoot,
@ -52,7 +50,7 @@ struct VfsFileData {
pub struct Vfs { pub struct Vfs {
roots: Arc<Roots>, roots: Arc<Roots>,
files: Arena<VfsFile, VfsFileData>, files: Vec<VfsFileData>,
root2files: FxHashMap<VfsRoot, FxHashSet<VfsFile>>, root2files: FxHashMap<VfsRoot, FxHashSet<VfsFile>>,
pending_changes: Vec<VfsChange>, pending_changes: Vec<VfsChange>,
worker: Worker, worker: Worker,
@ -78,8 +76,7 @@ impl Vfs {
root2files.insert(root, Default::default()); root2files.insert(root, Default::default());
worker.sender().send(io::Task::AddRoot { root }).unwrap(); worker.sender().send(io::Task::AddRoot { root }).unwrap();
} }
let res = let res = Vfs { roots, files: Vec::new(), root2files, worker, pending_changes: Vec::new() };
Vfs { roots, files: Arena::default(), root2files, worker, pending_changes: Vec::new() };
let vfs_roots = res.roots.iter().collect(); let vfs_roots = res.roots.iter().collect();
(res, vfs_roots) (res, vfs_roots)
} }
@ -96,8 +93,8 @@ impl Vfs {
} }
pub fn file2path(&self, file: VfsFile) -> PathBuf { pub fn file2path(&self, file: VfsFile) -> PathBuf {
let rel_path = &self.files[file].path; let rel_path = &self.file(file).path;
let root_path = &self.roots.path(self.files[file].root); let root_path = &self.roots.path(self.file(file).root);
rel_path.to_path(root_path) rel_path.to_path(root_path)
} }
@ -166,11 +163,11 @@ impl Vfs {
// been open in the editor, so we need to account for that. // been open in the editor, so we need to account for that.
let existing = self.root2files[&root] let existing = self.root2files[&root]
.iter() .iter()
.map(|&file| (self.files[file].path.clone(), file)) .map(|&file| (self.file(file).path.clone(), file))
.collect::<FxHashMap<_, _>>(); .collect::<FxHashMap<_, _>>();
for (path, text) in files { for (path, text) in files {
if let Some(&file) = existing.get(&path) { if let Some(&file) = existing.get(&path) {
let text = Arc::clone(&self.files[file].text); let text = Arc::clone(&self.file(file).text);
cur_files.push((file, path, text)); cur_files.push((file, path, text));
continue; continue;
} }
@ -184,7 +181,7 @@ impl Vfs {
} }
TaskResult::SingleFile { root, path, text } => { TaskResult::SingleFile { root, path, text } => {
let existing_file = self.find_file(root, &path); let existing_file = self.find_file(root, &path);
if existing_file.map(|file| self.files[file].is_overlayed) == Some(true) { if existing_file.map(|file| self.file(file).is_overlayed) == Some(true) {
return; return;
} }
match (existing_file, text) { match (existing_file, text) {
@ -240,22 +237,23 @@ impl Vfs {
is_overlayed: bool, is_overlayed: bool,
) -> VfsFile { ) -> VfsFile {
let data = VfsFileData { root, path, text, is_overlayed }; let data = VfsFileData { root, path, text, is_overlayed };
let file = self.files.alloc(data); let file = VfsFile(self.files.len() as u32);
self.files.push(data);
self.root2files.get_mut(&root).unwrap().insert(file); self.root2files.get_mut(&root).unwrap().insert(file);
file file
} }
fn raw_change_file(&mut self, file: VfsFile, new_text: Arc<String>, is_overlayed: bool) { fn raw_change_file(&mut self, file: VfsFile, new_text: Arc<String>, is_overlayed: bool) {
let mut file_data = &mut self.files[file]; let mut file_data = &mut self.file_mut(file);
file_data.text = new_text; file_data.text = new_text;
file_data.is_overlayed = is_overlayed; file_data.is_overlayed = is_overlayed;
} }
fn raw_remove_file(&mut self, file: VfsFile) { fn raw_remove_file(&mut self, file: VfsFile) {
// FIXME: use arena with removal // FIXME: use arena with removal
self.files[file].text = Default::default(); self.file_mut(file).text = Default::default();
self.files[file].path = Default::default(); self.file_mut(file).path = Default::default();
let root = self.files[file].root; let root = self.file(file).root;
let removed = self.root2files.get_mut(&root).unwrap().remove(&file); let removed = self.root2files.get_mut(&root).unwrap().remove(&file);
assert!(removed); assert!(removed);
} }
@ -267,7 +265,15 @@ impl Vfs {
} }
fn find_file(&self, root: VfsRoot, path: &RelativePath) -> Option<VfsFile> { fn find_file(&self, root: VfsRoot, path: &RelativePath) -> Option<VfsFile> {
self.root2files[&root].iter().map(|&it| it).find(|&file| self.files[file].path == path) self.root2files[&root].iter().map(|&it| it).find(|&file| self.file(file).path == path)
}
fn file(&self, file: VfsFile) -> &VfsFileData {
&self.files[file.0 as usize]
}
fn file_mut(&mut self, file: VfsFile) -> &mut VfsFileData {
&mut self.files[file.0 as usize]
} }
} }