Skip to content

Commit 6a73d54

Browse files
bors[bot]matklad
andauthored
Merge #5137
5137: Make gotodef tests more data-driven r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
2 parents 5a0fb3c + af7e300 commit 6a73d54

File tree

10 files changed

+639
-737
lines changed

10 files changed

+639
-737
lines changed

crates/ra_db/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ pub struct FilePosition {
8080
pub offset: TextSize,
8181
}
8282

83-
#[derive(Clone, Copy, Debug)]
83+
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
8484
pub struct FileRange {
8585
pub file_id: FileId,
8686
pub range: TextRange,

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 {

crates/ra_ide/src/display/navigation_target.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use ra_syntax::{
1111
TextRange,
1212
};
1313

14-
use crate::{FileRange, FileSymbol};
14+
use crate::FileSymbol;
1515

1616
use super::short_label::ShortLabel;
1717

@@ -47,6 +47,19 @@ impl NavigationTarget {
4747
pub fn range(&self) -> TextRange {
4848
self.focus_range.unwrap_or(self.full_range)
4949
}
50+
/// A "most interesting" range withing the `full_range`.
51+
///
52+
/// Typically, `full_range` is the whole syntax node,
53+
/// including doc comments, and `focus_range` is the range of the identifier.
54+
pub fn focus_range(&self) -> Option<TextRange> {
55+
self.focus_range
56+
}
57+
pub fn full_range(&self) -> TextRange {
58+
self.full_range
59+
}
60+
pub fn file_id(&self) -> FileId {
61+
self.file_id
62+
}
5063

5164
pub fn name(&self) -> &SmolStr {
5265
&self.name
@@ -60,18 +73,6 @@ impl NavigationTarget {
6073
self.kind
6174
}
6275

63-
pub fn file_id(&self) -> FileId {
64-
self.file_id
65-
}
66-
67-
pub fn file_range(&self) -> FileRange {
68-
FileRange { file_id: self.file_id, range: self.full_range }
69-
}
70-
71-
pub fn full_range(&self) -> TextRange {
72-
self.full_range
73-
}
74-
7576
pub fn docs(&self) -> Option<&str> {
7677
self.docs.as_deref()
7778
}
@@ -80,14 +81,6 @@ impl NavigationTarget {
8081
self.description.as_deref()
8182
}
8283

83-
/// A "most interesting" range withing the `full_range`.
84-
///
85-
/// Typically, `full_range` is the whole syntax node,
86-
/// including doc comments, and `focus_range` is the range of the identifier.
87-
pub fn focus_range(&self) -> Option<TextRange> {
88-
self.focus_range
89-
}
90-
9184
pub(crate) fn from_module_to_decl(db: &RootDatabase, module: hir::Module) -> NavigationTarget {
9285
let name = module.name(db).map(|it| it.to_string().into()).unwrap_or_default();
9386
if let Some(src) = module.declaration_source(db) {
@@ -278,16 +271,22 @@ impl ToNav for hir::Module {
278271
impl ToNav for hir::ImplDef {
279272
fn to_nav(&self, db: &RootDatabase) -> NavigationTarget {
280273
let src = self.source(db);
281-
let frange = if let Some(item) = self.is_builtin_derive(db) {
274+
let derive_attr = self.is_builtin_derive(db);
275+
let frange = if let Some(item) = &derive_attr {
282276
original_range(db, item.syntax())
283277
} else {
284278
original_range(db, src.as_ref().map(|it| it.syntax()))
285279
};
280+
let focus_range = if derive_attr.is_some() {
281+
None
282+
} else {
283+
src.value.target_type().map(|ty| original_range(db, src.with_value(ty.syntax())).range)
284+
};
286285

287286
NavigationTarget::from_syntax(
288287
frange.file_id,
289288
"impl".into(),
290-
None,
289+
focus_range,
291290
frange.range,
292291
src.value.syntax().kind(),
293292
)

0 commit comments

Comments
 (0)