Skip to content

Commit eeb27f9

Browse files
committed
Rewrite fixtures on top of Change
1 parent 8716c4c commit eeb27f9

File tree

1 file changed

+90
-83
lines changed

1 file changed

+90
-83
lines changed

crates/base_db/src/fixture.rs

Lines changed: 90 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -65,24 +65,26 @@ use test_utils::{extract_range_or_offset, Fixture, RangeOrOffset, CURSOR_MARKER}
6565
use vfs::{file_set::FileSet, VfsPath};
6666

6767
use crate::{
68-
input::CrateName, CrateGraph, CrateId, Edition, Env, FileId, FilePosition, SourceDatabaseExt,
69-
SourceRoot, SourceRootId,
68+
input::CrateName, Change, CrateGraph, CrateId, Edition, Env, FileId, FilePosition,
69+
SourceDatabaseExt, SourceRoot, SourceRootId,
7070
};
7171

7272
pub const WORKSPACE: SourceRootId = SourceRootId(0);
7373

7474
pub trait WithFixture: Default + SourceDatabaseExt + 'static {
7575
fn with_single_file(text: &str) -> (Self, FileId) {
76+
let fixture = ChangeFixture::parse(text);
7677
let mut db = Self::default();
77-
let (_, files) = with_files(&mut db, text);
78-
assert_eq!(files.len(), 1);
79-
(db, files[0])
78+
fixture.change.apply(&mut db);
79+
assert_eq!(fixture.files.len(), 1);
80+
(db, fixture.files[0])
8081
}
8182

8283
fn with_files(ra_fixture: &str) -> Self {
84+
let fixture = ChangeFixture::parse(ra_fixture);
8385
let mut db = Self::default();
84-
let (pos, _) = with_files(&mut db, ra_fixture);
85-
assert!(pos.is_none());
86+
fixture.change.apply(&mut db);
87+
assert!(fixture.file_position.is_none());
8688
db
8789
}
8890

@@ -96,9 +98,10 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
9698
}
9799

98100
fn with_range_or_offset(ra_fixture: &str) -> (Self, FileId, RangeOrOffset) {
101+
let fixture = ChangeFixture::parse(ra_fixture);
99102
let mut db = Self::default();
100-
let (pos, _) = with_files(&mut db, ra_fixture);
101-
let (file_id, range_or_offset) = pos.unwrap();
103+
fixture.change.apply(&mut db);
104+
let (file_id, range_or_offset) = fixture.file_position.unwrap();
102105
(db, file_id, range_or_offset)
103106
}
104107

@@ -113,89 +116,93 @@ pub trait WithFixture: Default + SourceDatabaseExt + 'static {
113116

114117
impl<DB: SourceDatabaseExt + Default + 'static> WithFixture for DB {}
115118

116-
fn with_files(
117-
db: &mut dyn SourceDatabaseExt,
118-
fixture: &str,
119-
) -> (Option<(FileId, RangeOrOffset)>, Vec<FileId>) {
120-
let fixture = Fixture::parse(fixture);
121-
122-
let mut files = Vec::new();
123-
let mut crate_graph = CrateGraph::default();
124-
let mut crates = FxHashMap::default();
125-
let mut crate_deps = Vec::new();
126-
let mut default_crate_root: Option<FileId> = None;
127-
128-
let mut file_set = FileSet::default();
129-
let source_root_id = WORKSPACE;
130-
let source_root_prefix = "/".to_string();
131-
let mut file_id = FileId(0);
132-
133-
let mut file_position = None;
134-
135-
for entry in fixture {
136-
let text = if entry.text.contains(CURSOR_MARKER) {
137-
let (range_or_offset, text) = extract_range_or_offset(&entry.text);
138-
assert!(file_position.is_none());
139-
file_position = Some((file_id, range_or_offset));
140-
text.to_string()
141-
} else {
142-
entry.text.clone()
143-
};
119+
pub struct ChangeFixture {
120+
file_position: Option<(FileId, RangeOrOffset)>,
121+
files: Vec<FileId>,
122+
change: Change,
123+
}
144124

145-
let meta = FileMeta::from(entry);
146-
assert!(meta.path.starts_with(&source_root_prefix));
125+
impl ChangeFixture {
126+
fn parse(ra_fixture: &str) -> ChangeFixture {
127+
let fixture = Fixture::parse(ra_fixture);
128+
let mut change = Change::new();
129+
130+
let mut files = Vec::new();
131+
let mut crate_graph = CrateGraph::default();
132+
let mut crates = FxHashMap::default();
133+
let mut crate_deps = Vec::new();
134+
let mut default_crate_root: Option<FileId> = None;
135+
136+
let mut file_set = FileSet::default();
137+
let source_root_prefix = "/".to_string();
138+
let mut file_id = FileId(0);
139+
140+
let mut file_position = None;
141+
142+
for entry in fixture {
143+
let text = if entry.text.contains(CURSOR_MARKER) {
144+
let (range_or_offset, text) = extract_range_or_offset(&entry.text);
145+
assert!(file_position.is_none());
146+
file_position = Some((file_id, range_or_offset));
147+
text.to_string()
148+
} else {
149+
entry.text.clone()
150+
};
151+
152+
let meta = FileMeta::from(entry);
153+
assert!(meta.path.starts_with(&source_root_prefix));
154+
155+
if let Some(krate) = meta.krate {
156+
let crate_id = crate_graph.add_crate_root(
157+
file_id,
158+
meta.edition,
159+
Some(krate.clone()),
160+
meta.cfg,
161+
meta.env,
162+
Default::default(),
163+
);
164+
let crate_name = CrateName::new(&krate).unwrap();
165+
let prev = crates.insert(crate_name.clone(), crate_id);
166+
assert!(prev.is_none());
167+
for dep in meta.deps {
168+
let dep = CrateName::new(&dep).unwrap();
169+
crate_deps.push((crate_name.clone(), dep))
170+
}
171+
} else if meta.path == "/main.rs" || meta.path == "/lib.rs" {
172+
assert!(default_crate_root.is_none());
173+
default_crate_root = Some(file_id);
174+
}
175+
176+
change.change_file(file_id, Some(Arc::new(text)));
177+
let path = VfsPath::new_virtual_path(meta.path);
178+
file_set.insert(file_id, path.into());
179+
files.push(file_id);
180+
file_id.0 += 1;
181+
}
147182

148-
if let Some(krate) = meta.krate {
149-
let crate_id = crate_graph.add_crate_root(
150-
file_id,
151-
meta.edition,
152-
Some(krate.clone()),
153-
meta.cfg,
154-
meta.env,
183+
if crates.is_empty() {
184+
let crate_root = default_crate_root.unwrap();
185+
crate_graph.add_crate_root(
186+
crate_root,
187+
Edition::Edition2018,
188+
None,
189+
CfgOptions::default(),
190+
Env::default(),
155191
Default::default(),
156192
);
157-
let crate_name = CrateName::new(&krate).unwrap();
158-
let prev = crates.insert(crate_name.clone(), crate_id);
159-
assert!(prev.is_none());
160-
for dep in meta.deps {
161-
let dep = CrateName::new(&dep).unwrap();
162-
crate_deps.push((crate_name.clone(), dep))
193+
} else {
194+
for (from, to) in crate_deps {
195+
let from_id = crates[&from];
196+
let to_id = crates[&to];
197+
crate_graph.add_dep(from_id, CrateName::new(&to).unwrap(), to_id).unwrap();
163198
}
164-
} else if meta.path == "/main.rs" || meta.path == "/lib.rs" {
165-
assert!(default_crate_root.is_none());
166-
default_crate_root = Some(file_id);
167199
}
168200

169-
db.set_file_text(file_id, Arc::new(text));
170-
db.set_file_source_root(file_id, source_root_id);
171-
let path = VfsPath::new_virtual_path(meta.path);
172-
file_set.insert(file_id, path.into());
173-
files.push(file_id);
174-
file_id.0 += 1;
175-
}
201+
change.set_roots(vec![SourceRoot::new_local(file_set)]);
202+
change.set_crate_graph(crate_graph);
176203

177-
if crates.is_empty() {
178-
let crate_root = default_crate_root.unwrap();
179-
crate_graph.add_crate_root(
180-
crate_root,
181-
Edition::Edition2018,
182-
None,
183-
CfgOptions::default(),
184-
Env::default(),
185-
Default::default(),
186-
);
187-
} else {
188-
for (from, to) in crate_deps {
189-
let from_id = crates[&from];
190-
let to_id = crates[&to];
191-
crate_graph.add_dep(from_id, CrateName::new(&to).unwrap(), to_id).unwrap();
192-
}
204+
ChangeFixture { file_position, files, change }
193205
}
194-
195-
db.set_source_root(source_root_id, Arc::new(SourceRoot::new_local(file_set)));
196-
db.set_crate_graph(Arc::new(crate_graph));
197-
198-
(file_position, files)
199206
}
200207

201208
struct FileMeta {

0 commit comments

Comments
 (0)