use set methods

This commit is contained in:
Aleksey Kladov 2019-01-25 15:25:01 +03:00
parent 8cf092d5de
commit 08c12e424d
3 changed files with 29 additions and 54 deletions

View File

@ -2,8 +2,7 @@ use std::{sync::Arc, panic};
use parking_lot::Mutex; use parking_lot::Mutex;
use ra_db::{ use ra_db::{
BaseDatabase, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, BaseDatabase, FilePosition, FileId, CrateGraph, SourceRoot, SourceRootId, FilesDatabase,
salsa::{self, Database},
}; };
use relative_path::RelativePathBuf; use relative_path::RelativePathBuf;
use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset}; use test_utils::{parse_fixture, CURSOR_MARKER, extract_offset};
@ -34,8 +33,7 @@ impl MockDatabase {
let mut db = MockDatabase::default(); let mut db = MockDatabase::default();
let mut source_root = SourceRoot::default(); let mut source_root = SourceRoot::default();
let file_id = db.add_file(WORKSPACE, &mut source_root, "/main.rs", text); let file_id = db.add_file(WORKSPACE, &mut source_root, "/main.rs", text);
db.query_mut(ra_db::SourceRootQuery) db.set_source_root(WORKSPACE, Arc::new(source_root.clone()));
.set(WORKSPACE, Arc::new(source_root.clone()));
(db, source_root, file_id) (db, source_root, file_id)
} }
@ -45,11 +43,6 @@ impl MockDatabase {
(db, position) (db, position)
} }
pub(crate) fn set_crate_graph(&mut self, crate_graph: CrateGraph) {
self.query_mut(ra_db::CrateGraphQuery)
.set((), Arc::new(crate_graph));
}
fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) { fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) {
let mut db = MockDatabase::default(); let mut db = MockDatabase::default();
@ -81,8 +74,7 @@ impl MockDatabase {
self.add_file(source_root_id, &mut source_root, &entry.meta, &entry.text); self.add_file(source_root_id, &mut source_root, &entry.meta, &entry.text);
} }
} }
self.query_mut(ra_db::SourceRootQuery) self.set_source_root(source_root_id, Arc::new(source_root.clone()));
.set(source_root_id, Arc::new(source_root.clone()));
(source_root, position) (source_root, position)
} }
@ -100,17 +92,15 @@ impl MockDatabase {
let file_id = FileId(self.file_counter); let file_id = FileId(self.file_counter);
self.file_counter += 1; self.file_counter += 1;
let text = Arc::new(text.to_string()); let text = Arc::new(text.to_string());
self.query_mut(ra_db::FileTextQuery).set(file_id, text); self.set_file_text(file_id, text);
self.query_mut(ra_db::FileRelativePathQuery) self.set_file_relative_path(file_id, path.clone());
.set(file_id, path.clone()); self.set_file_source_root(file_id, source_root_id);
self.query_mut(ra_db::FileSourceRootQuery)
.set(file_id, source_root_id);
source_root.files.insert(path, file_id); source_root.files.insert(path, file_id);
if is_crate_root { if is_crate_root {
let mut crate_graph = CrateGraph::default(); let mut crate_graph = CrateGraph::default();
crate_graph.add_crate_root(file_id); crate_graph.add_crate_root(file_id);
self.set_crate_graph(crate_graph); self.set_crate_graph(Arc::new(crate_graph));
} }
file_id file_id
} }
@ -149,12 +139,9 @@ impl Default for MockDatabase {
interner: Default::default(), interner: Default::default(),
file_counter: 0, file_counter: 0,
}; };
db.query_mut(ra_db::CrateGraphQuery) db.set_crate_graph(Default::default());
.set((), Default::default()); db.set_local_roots(Default::default());
db.query_mut(ra_db::LocalRootsQuery) db.set_library_roots(Default::default());
.set((), Default::default());
db.query_mut(ra_db::LibraryRootsQuery)
.set((), Default::default());
db db
} }
} }

View File

@ -1,6 +1,6 @@
use std::sync::Arc; use std::sync::Arc;
use ra_db::{CrateGraph, SourceRootId, salsa::Database}; use ra_db::{CrateGraph, SourceRootId, FilesDatabase};
use relative_path::RelativePath; use relative_path::RelativePath;
use test_utils::{assert_eq_text, covers}; use test_utils::{assert_eq_text, covers};
@ -257,7 +257,7 @@ fn item_map_across_crates() {
.add_dep(main_crate, "test_crate".into(), lib_crate) .add_dep(main_crate, "test_crate".into(), lib_crate)
.unwrap(); .unwrap();
db.set_crate_graph(crate_graph); db.set_crate_graph(Arc::new(crate_graph));
let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
let krate = module.krate(&db).unwrap(); let krate = module.krate(&db).unwrap();
@ -309,7 +309,7 @@ fn import_across_source_roots() {
.add_dep(main_crate, "test_crate".into(), lib_crate) .add_dep(main_crate, "test_crate".into(), lib_crate)
.unwrap(); .unwrap();
db.set_crate_graph(crate_graph); db.set_crate_graph(Arc::new(crate_graph));
let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
let krate = module.krate(&db).unwrap(); let krate = module.krate(&db).unwrap();
@ -351,7 +351,7 @@ fn reexport_across_crates() {
.add_dep(main_crate, "test_crate".into(), lib_crate) .add_dep(main_crate, "test_crate".into(), lib_crate)
.unwrap(); .unwrap();
db.set_crate_graph(crate_graph); db.set_crate_graph(Arc::new(crate_graph));
let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap(); let module = crate::source_binder::module_from_file_id(&db, main_id).unwrap();
let krate = module.krate(&db).unwrap(); let krate = module.krate(&db).unwrap();
@ -377,8 +377,7 @@ fn check_item_map_is_not_recomputed(initial: &str, file_change: &str) {
}); });
assert!(format!("{:?}", events).contains("item_map")) assert!(format!("{:?}", events).contains("item_map"))
} }
db.query_mut(ra_db::FileTextQuery) db.set_file_text(pos.file_id, Arc::new(file_change.to_string()));
.set(pos.file_id, Arc::new(file_change.to_string()));
{ {
let events = db.log_executed(|| { let events = db.log_executed(|| {

View File

@ -5,7 +5,7 @@ use hir::{
}; };
use ra_db::{ use ra_db::{
FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase, FilesDatabase, SourceRoot, SourceRootId, SyntaxDatabase,
salsa::{self, Database}, salsa::Database,
}; };
use ra_ide_api_light::{self, assists, LocalEdit, Severity}; use ra_ide_api_light::{self, assists, LocalEdit, Severity};
use ra_syntax::{ use ra_syntax::{
@ -18,7 +18,7 @@ use crate::{
AnalysisChange, AnalysisChange,
CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit, CrateId, db, Diagnostic, FileId, FilePosition, FileRange, FileSystemEdit,
Query, RootChange, SourceChange, SourceFileEdit, Query, RootChange, SourceChange, SourceFileEdit,
symbol_index::{FileSymbol, LibrarySymbolsQuery}, symbol_index::{FileSymbol, SymbolsDatabase},
}; };
impl db::RootDatabase { impl db::RootDatabase {
@ -28,59 +28,48 @@ impl db::RootDatabase {
if !change.new_roots.is_empty() { if !change.new_roots.is_empty() {
let mut local_roots = Vec::clone(&self.local_roots()); let mut local_roots = Vec::clone(&self.local_roots());
for (root_id, is_local) in change.new_roots { for (root_id, is_local) in change.new_roots {
self.query_mut(ra_db::SourceRootQuery) self.set_source_root(root_id, Default::default());
.set(root_id, Default::default());
if is_local { if is_local {
local_roots.push(root_id); local_roots.push(root_id);
} }
} }
self.query_mut(ra_db::LocalRootsQuery) self.set_local_roots(Arc::new(local_roots));
.set((), Arc::new(local_roots));
} }
for (root_id, root_change) in change.roots_changed { for (root_id, root_change) in change.roots_changed {
self.apply_root_change(root_id, root_change); self.apply_root_change(root_id, root_change);
} }
for (file_id, text) in change.files_changed { for (file_id, text) in change.files_changed {
self.query_mut(ra_db::FileTextQuery).set(file_id, text) self.set_file_text(file_id, text)
} }
if !change.libraries_added.is_empty() { if !change.libraries_added.is_empty() {
let mut libraries = Vec::clone(&self.library_roots()); let mut libraries = Vec::clone(&self.library_roots());
for library in change.libraries_added { for library in change.libraries_added {
libraries.push(library.root_id); libraries.push(library.root_id);
self.query_mut(ra_db::SourceRootQuery) self.set_source_root(library.root_id, Default::default());
.set(library.root_id, Default::default()); self.set_constant_library_symbols(library.root_id, Arc::new(library.symbol_index));
self.query_mut(LibrarySymbolsQuery)
.set_constant(library.root_id, Arc::new(library.symbol_index));
self.apply_root_change(library.root_id, library.root_change); self.apply_root_change(library.root_id, library.root_change);
} }
self.query_mut(ra_db::LibraryRootsQuery) self.set_library_roots(Arc::new(libraries));
.set((), Arc::new(libraries));
} }
if let Some(crate_graph) = change.crate_graph { if let Some(crate_graph) = change.crate_graph {
self.query_mut(ra_db::CrateGraphQuery) self.set_crate_graph(Arc::new(crate_graph))
.set((), Arc::new(crate_graph))
} }
} }
fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) { fn apply_root_change(&mut self, root_id: SourceRootId, root_change: RootChange) {
let mut source_root = SourceRoot::clone(&self.source_root(root_id)); let mut source_root = SourceRoot::clone(&self.source_root(root_id));
for add_file in root_change.added { for add_file in root_change.added {
self.query_mut(ra_db::FileTextQuery) self.set_file_text(add_file.file_id, add_file.text);
.set(add_file.file_id, add_file.text); self.set_file_relative_path(add_file.file_id, add_file.path.clone());
self.query_mut(ra_db::FileRelativePathQuery) self.set_file_source_root(add_file.file_id, root_id);
.set(add_file.file_id, add_file.path.clone());
self.query_mut(ra_db::FileSourceRootQuery)
.set(add_file.file_id, root_id);
source_root.files.insert(add_file.path, add_file.file_id); source_root.files.insert(add_file.path, add_file.file_id);
} }
for remove_file in root_change.removed { for remove_file in root_change.removed {
self.query_mut(ra_db::FileTextQuery) self.set_file_text(remove_file.file_id, Default::default());
.set(remove_file.file_id, Default::default());
source_root.files.remove(&remove_file.path); source_root.files.remove(&remove_file.path);
} }
self.query_mut(ra_db::SourceRootQuery) self.set_source_root(root_id, Arc::new(source_root));
.set(root_id, Arc::new(source_root));
} }
#[allow(unused)] #[allow(unused)]