Skip to content

Commit c048390

Browse files
committed
Use correct HirFileId in find_related_test
1 parent 820ecdd commit c048390

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

crates/hir/src/semantics.rs

+4
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,10 @@ impl<'db, DB: HirDatabase> Semantics<'db, DB> {
173173
self.imp.descend_node_at_offset(node, offset).find_map(N::cast)
174174
}
175175

176+
pub fn hir_file_for(&self, syntax_node: &SyntaxNode) -> HirFileId {
177+
self.imp.find_file(syntax_node.clone()).file_id
178+
}
179+
176180
pub fn original_range(&self, node: &SyntaxNode) -> FileRange {
177181
self.imp.original_range(node)
178182
}

crates/hir_expand/src/lib.rs

+4-9
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ use either::Either;
2222
pub use mbe::{ExpandError, ExpandResult};
2323
pub use parser::FragmentKind;
2424

25-
use std::hash::Hash;
2625
use std::sync::Arc;
26+
use std::{hash::Hash, iter};
2727

2828
use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
2929
use syntax::{
@@ -453,7 +453,7 @@ impl InFile<SyntaxNode> {
453453
self,
454454
db: &dyn db::AstDatabase,
455455
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
456-
std::iter::successors(Some(self), move |node| match node.value.parent() {
456+
iter::successors(Some(self), move |node| match node.value.parent() {
457457
Some(parent) => Some(node.with_value(parent)),
458458
None => {
459459
let parent_node = node.file_id.call_node(db)?;
@@ -555,19 +555,14 @@ impl<N: AstNode> InFile<N> {
555555
where
556556
N: 'db,
557557
{
558-
std::iter::successors(Some(self), move |node| {
558+
iter::successors(Some(self), move |node| {
559559
let InFile { file_id, value } = node.file_id.call_node(db)?;
560560
N::cast(value).map(|n| InFile::new(file_id, n))
561561
})
562562
}
563563

564564
pub fn node_with_attributes(self, db: &dyn db::AstDatabase) -> InFile<N> {
565-
std::iter::successors(Some(self), move |node| {
566-
let InFile { file_id, value } = node.file_id.call_node(db)?;
567-
N::cast(value).map(|n| InFile::new(file_id, n))
568-
})
569-
.last()
570-
.unwrap()
565+
self.nodes_with_attributes(db).last().unwrap()
571566
}
572567
}
573568

crates/ide/src/runnables.rs

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use std::fmt;
33
use ast::NameOwner;
44
use cfg::CfgExpr;
55
use either::Either;
6-
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, Semantics};
6+
use hir::{AsAssocItem, HasAttrs, HasSource, HirDisplay, InFile, Semantics};
77
use ide_assists::utils::test_related_attribute;
88
use ide_db::{
99
base_db::{FilePosition, FileRange},
@@ -232,22 +232,21 @@ fn find_related_tests(
232232
let functions = refs.iter().filter_map(|(range, _)| {
233233
let token = file.token_at_offset(range.start()).next()?;
234234
let token = sema.descend_into_macros(token);
235-
// FIXME: This is the wrong file_id
236235
token
237236
.ancestors()
238237
.find_map(ast::Fn::cast)
239-
.map(|f| hir::InFile::new(file_id.into(), f))
238+
.map(|f| hir::InFile::new(sema.hir_file_for(f.syntax()), f))
240239
});
241240

242241
for fn_def in functions {
243242
// #[test/bench] expands to just the item causing us to lose the attribute, so recover them by going out of the attribute
244-
let fn_def = fn_def.node_with_attributes(sema.db);
245-
if let Some(runnable) = as_test_runnable(sema, &fn_def.value) {
243+
let InFile { value: fn_def, .. } = &fn_def.node_with_attributes(sema.db);
244+
if let Some(runnable) = as_test_runnable(sema, fn_def) {
246245
// direct test
247246
tests.insert(runnable);
248-
} else if let Some(module) = parent_test_module(sema, &fn_def.value) {
247+
} else if let Some(module) = parent_test_module(sema, fn_def) {
249248
// indirect test
250-
find_related_tests_in_module(sema, &fn_def.value, &module, tests);
249+
find_related_tests_in_module(sema, fn_def, &module, tests);
251250
}
252251
}
253252
}

0 commit comments

Comments
 (0)