mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 22:31:43 +00:00
fix hir mock
This commit is contained in:
parent
18aac1df45
commit
26dcc70129
2 changed files with 23 additions and 19 deletions
|
@ -2,7 +2,7 @@ use std::sync::Arc;
|
||||||
|
|
||||||
use parking_lot::Mutex;
|
use parking_lot::Mutex;
|
||||||
use salsa::{self, Database};
|
use salsa::{self, Database};
|
||||||
use ra_db::{LocationIntener, BaseDatabase, FilePosition, mock::FileMap, FileId, WORKSPACE, CrateGraph};
|
use ra_db::{LocationIntener, BaseDatabase, FilePosition, FileId, WORKSPACE, CrateGraph, SourceRoot};
|
||||||
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};
|
||||||
|
|
||||||
|
@ -16,10 +16,10 @@ pub(crate) struct MockDatabase {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl MockDatabase {
|
impl MockDatabase {
|
||||||
pub(crate) fn with_files(fixture: &str) -> (MockDatabase, FileMap) {
|
pub(crate) fn with_files(fixture: &str) -> (MockDatabase, SourceRoot) {
|
||||||
let (db, file_map, position) = MockDatabase::from_fixture(fixture);
|
let (db, source_root, position) = MockDatabase::from_fixture(fixture);
|
||||||
assert!(position.is_none());
|
assert!(position.is_none());
|
||||||
(db, file_map)
|
(db, source_root)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn with_position(fixture: &str) -> (MockDatabase, FilePosition) {
|
pub(crate) fn with_position(fixture: &str) -> (MockDatabase, FilePosition) {
|
||||||
|
@ -33,48 +33,50 @@ impl MockDatabase {
|
||||||
.set((), Arc::new(crate_graph));
|
.set((), Arc::new(crate_graph));
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_fixture(fixture: &str) -> (MockDatabase, FileMap, Option<FilePosition>) {
|
fn from_fixture(fixture: &str) -> (MockDatabase, SourceRoot, Option<FilePosition>) {
|
||||||
let mut db = MockDatabase::default();
|
let mut db = MockDatabase::default();
|
||||||
|
|
||||||
let mut position = None;
|
let mut position = None;
|
||||||
let mut file_map = FileMap::default();
|
let mut source_root = SourceRoot::default();
|
||||||
for entry in parse_fixture(fixture) {
|
for entry in parse_fixture(fixture) {
|
||||||
if entry.text.contains(CURSOR_MARKER) {
|
if entry.text.contains(CURSOR_MARKER) {
|
||||||
assert!(
|
assert!(
|
||||||
position.is_none(),
|
position.is_none(),
|
||||||
"only one marker (<|>) per fixture is allowed"
|
"only one marker (<|>) per fixture is allowed"
|
||||||
);
|
);
|
||||||
position = Some(db.add_file_with_position(&mut file_map, &entry.meta, &entry.text));
|
position =
|
||||||
|
Some(db.add_file_with_position(&mut source_root, &entry.meta, &entry.text));
|
||||||
} else {
|
} else {
|
||||||
db.add_file(&mut file_map, &entry.meta, &entry.text);
|
db.add_file(&mut source_root, &entry.meta, &entry.text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
let source_root = file_map.clone().into_source_root();
|
|
||||||
db.query_mut(ra_db::SourceRootQuery)
|
db.query_mut(ra_db::SourceRootQuery)
|
||||||
.set(WORKSPACE, Arc::new(source_root));
|
.set(WORKSPACE, Arc::new(source_root.clone()));
|
||||||
(db, file_map, position)
|
(db, source_root, position)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_file(&mut self, file_map: &mut FileMap, path: &str, text: &str) -> FileId {
|
fn add_file(&mut self, source_root: &mut SourceRoot, path: &str, text: &str) -> FileId {
|
||||||
assert!(path.starts_with('/'));
|
assert!(path.starts_with('/'));
|
||||||
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
|
let path = RelativePathBuf::from_path(&path[1..]).unwrap();
|
||||||
|
let file_id = FileId(source_root.files.len() as u32);
|
||||||
let file_id = file_map.add(path);
|
|
||||||
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.query_mut(ra_db::FileTextQuery).set(file_id, text);
|
||||||
|
self.query_mut(ra_db::FileRelativePathQuery)
|
||||||
|
.set(file_id, path.clone());
|
||||||
self.query_mut(ra_db::FileSourceRootQuery)
|
self.query_mut(ra_db::FileSourceRootQuery)
|
||||||
.set(file_id, WORKSPACE);
|
.set(file_id, WORKSPACE);
|
||||||
|
source_root.files.insert(path, file_id);
|
||||||
file_id
|
file_id
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add_file_with_position(
|
fn add_file_with_position(
|
||||||
&mut self,
|
&mut self,
|
||||||
file_map: &mut FileMap,
|
source_root: &mut SourceRoot,
|
||||||
path: &str,
|
path: &str,
|
||||||
text: &str,
|
text: &str,
|
||||||
) -> FilePosition {
|
) -> FilePosition {
|
||||||
let (offset, text) = extract_offset(text);
|
let (offset, text) = extract_offset(text);
|
||||||
let file_id = self.add_file(file_map, path, &text);
|
let file_id = self.add_file(source_root, path, &text);
|
||||||
FilePosition { file_id, offset }
|
FilePosition { file_id, offset }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -158,6 +160,7 @@ salsa::database_storage! {
|
||||||
pub(crate) struct MockDatabaseStorage for MockDatabase {
|
pub(crate) struct MockDatabaseStorage for MockDatabase {
|
||||||
impl ra_db::FilesDatabase {
|
impl ra_db::FilesDatabase {
|
||||||
fn file_text() for ra_db::FileTextQuery;
|
fn file_text() for ra_db::FileTextQuery;
|
||||||
|
fn file_relative_path() for ra_db::FileRelativePathQuery;
|
||||||
fn file_source_root() for ra_db::FileSourceRootQuery;
|
fn file_source_root() for ra_db::FileSourceRootQuery;
|
||||||
fn source_root() for ra_db::SourceRootQuery;
|
fn source_root() for ra_db::SourceRootQuery;
|
||||||
fn libraries() for ra_db::LibrariesQuery;
|
fn libraries() for ra_db::LibrariesQuery;
|
||||||
|
|
|
@ -3,6 +3,7 @@ use std::sync::Arc;
|
||||||
use salsa::Database;
|
use salsa::Database;
|
||||||
use ra_db::{FilesDatabase, CrateGraph};
|
use ra_db::{FilesDatabase, CrateGraph};
|
||||||
use ra_syntax::SmolStr;
|
use ra_syntax::SmolStr;
|
||||||
|
use relative_path::RelativePath;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
self as hir,
|
self as hir,
|
||||||
|
@ -44,7 +45,7 @@ fn item_map_smoke_test() {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn item_map_across_crates() {
|
fn item_map_across_crates() {
|
||||||
let (mut db, files) = MockDatabase::with_files(
|
let (mut db, sr) = MockDatabase::with_files(
|
||||||
"
|
"
|
||||||
//- /main.rs
|
//- /main.rs
|
||||||
use test_crate::Baz;
|
use test_crate::Baz;
|
||||||
|
@ -53,8 +54,8 @@ fn item_map_across_crates() {
|
||||||
pub struct Baz;
|
pub struct Baz;
|
||||||
",
|
",
|
||||||
);
|
);
|
||||||
let main_id = files.file_id("/main.rs");
|
let main_id = sr.files[RelativePath::new("/main.rs")];
|
||||||
let lib_id = files.file_id("/lib.rs");
|
let lib_id = sr.files[RelativePath::new("/lib.rs")];
|
||||||
|
|
||||||
let mut crate_graph = CrateGraph::default();
|
let mut crate_graph = CrateGraph::default();
|
||||||
let main_crate = crate_graph.add_crate_root(main_id);
|
let main_crate = crate_graph.add_crate_root(main_id);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue