diff --git a/crates/libanalysis/Cargo.toml b/crates/libanalysis/Cargo.toml index 9c7977e929..4d565e95fc 100644 --- a/crates/libanalysis/Cargo.toml +++ b/crates/libanalysis/Cargo.toml @@ -8,7 +8,7 @@ relative-path = "0.3.7" log = "0.4.2" crossbeam-channel = "0.2.4" parking_lot = "0.6.3" -once_cell = "0.1.4" +once_cell = "0.1.5" rayon = "1.0.2" fst = "0.3.1" libsyntax2 = { path = "../libsyntax2" } diff --git a/crates/libanalysis/src/roots.rs b/crates/libanalysis/src/roots.rs index 675bce54ca..eb14e75678 100644 --- a/crates/libanalysis/src/roots.rs +++ b/crates/libanalysis/src/roots.rs @@ -18,7 +18,7 @@ use { #[derive(Clone, Default, Debug)] pub(crate) struct SourceRoot { - file_map: HashMap>, + file_map: HashMap)>>, module_map: ModuleMap, } @@ -37,9 +37,7 @@ impl SourceRoot { self.file_map.remove(&file_id); if let Some(text) = text { let file_data = FileData::new(text); - self.file_map.insert(file_id, Arc::new(file_data)); - } else { - self.file_map.remove(&file_id); + self.file_map.insert(file_id, Arc::new((file_data, Default::default()))); } } pub fn module_map(&self) -> &ModuleMap { @@ -64,7 +62,7 @@ impl SourceRoot { pub(crate) fn symbols(&self) -> Vec<(FileId, &FileSymbols)> { self.file_map .iter() - .map(|(&file_id, data)| (file_id, data.symbols())) + .map(|(&file_id, data)| (file_id, symbols(data))) .collect() } pub fn reindex(&self) { @@ -72,32 +70,35 @@ impl SourceRoot { self.file_map .par_iter() .for_each(|(_, data)| { - data.symbols(); + symbols(data); }); info!("parallel indexing took {:?}", now.elapsed()); } fn data(&self, file_id: FileId) -> &FileData { match self.file_map.get(&file_id) { - Some(data) => data, + Some(data) => &data.0, None => panic!("unknown file: {:?}", file_id), } } } +fn symbols((data, symbols): &(FileData, OnceCell)) -> &FileSymbols { + let syntax = data.syntax_transient(); + symbols.get_or_init(|| FileSymbols::new(&syntax)) +} + #[derive(Debug)] struct FileData { text: String, lines: OnceCell, syntax: OnceCell, - symbols: OnceCell, } impl FileData { fn new(text: String) -> FileData { FileData { text, - symbols: OnceCell::new(), syntax: OnceCell::new(), lines: OnceCell::new(), } @@ -106,8 +107,4 @@ impl FileData { self.syntax.get().map(|s| s.clone()) .unwrap_or_else(|| File::parse(&self.text)) } - fn symbols(&self) -> &FileSymbols { - let syntax = self.syntax_transient(); - self.symbols.get_or_init(|| FileSymbols::new(&syntax)) - } }