Skip to content

Commit d34e725

Browse files
committed
Better factoring
1 parent d21c84a commit d34e725

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

crates/ra_hir_ty/src/test_db.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ use std::{
88
use hir_def::{db::DefDatabase, AssocItemId, ModuleDefId, ModuleId};
99
use hir_expand::{db::AstDatabase, diagnostics::DiagnosticSink};
1010
use ra_db::{salsa, CrateId, FileId, FileLoader, FileLoaderDelegate, SourceDatabase, Upcast};
11-
use rustc_hash::FxHashSet;
11+
use ra_syntax::TextRange;
12+
use rustc_hash::{FxHashMap, FxHashSet};
1213
use stdx::format_to;
14+
use test_utils::extract_annotations;
1315

1416
use crate::{
1517
db::HirDatabase, diagnostics::Diagnostic, expr::ExprValidator,
@@ -155,17 +157,27 @@ impl TestDB {
155157
(buf, count)
156158
}
157159

158-
pub fn all_files(&self) -> Vec<FileId> {
159-
let mut res = Vec::new();
160+
pub fn extract_annotations(&self) -> FxHashMap<FileId, Vec<(TextRange, String)>> {
161+
let mut files = Vec::new();
160162
let crate_graph = self.crate_graph();
161163
for krate in crate_graph.iter() {
162164
let crate_def_map = self.crate_def_map(krate);
163165
for (module_id, _) in crate_def_map.modules.iter() {
164166
let file_id = crate_def_map[module_id].origin.file_id();
165-
res.extend(file_id)
167+
files.extend(file_id)
166168
}
167169
}
168-
res
170+
files
171+
.into_iter()
172+
.filter_map(|file_id| {
173+
let text = self.file_text(file_id);
174+
let annotations = extract_annotations(&text);
175+
if annotations.is_empty() {
176+
return None;
177+
}
178+
Some((file_id, annotations))
179+
})
180+
.collect()
169181
}
170182
}
171183

crates/ra_hir_ty/src/tests.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ use ra_syntax::{
2828
SyntaxNode,
2929
};
3030
use stdx::format_to;
31-
use test_utils::extract_annotations;
3231

3332
use crate::{
3433
db::HirDatabase, display::HirDisplay, infer::TypeMismatch, test_db::TestDB, InferenceResult, Ty,
@@ -49,9 +48,7 @@ fn check_types_source_code(ra_fixture: &str) {
4948
fn check_types_impl(ra_fixture: &str, display_source: bool) {
5049
let db = TestDB::with_files(ra_fixture);
5150
let mut checked_one = false;
52-
for file_id in db.all_files() {
53-
let text = db.parse(file_id).syntax_node().to_string();
54-
let annotations = extract_annotations(&text);
51+
for (file_id, annotations) in db.extract_annotations() {
5552
for (range, expected) in annotations {
5653
let ty = type_at_range(&db, FileRange { file_id, range });
5754
let actual = if display_source {

0 commit comments

Comments
 (0)