Skip to content

Commit 557df6f

Browse files
committed
Use correct HirFileId in find_related_test
1 parent 7342dcf commit 557df6f

File tree

3 files changed

+14
-16
lines changed

3 files changed

+14
-16
lines changed

crates/hir/src/semantics.rs

Lines changed: 4 additions & 0 deletions
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

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

25-
use std::{hash::Hash, sync::Arc};
25+
use std::{hash::Hash, iter, sync::Arc};
2626

2727
use base_db::{impl_intern_key, salsa, CrateId, FileId, FileRange};
2828
use syntax::{
@@ -454,7 +454,7 @@ impl InFile<SyntaxNode> {
454454
self,
455455
db: &dyn db::AstDatabase,
456456
) -> impl Iterator<Item = InFile<SyntaxNode>> + '_ {
457-
std::iter::successors(Some(self), move |node| match node.value.parent() {
457+
iter::successors(Some(self), move |node| match node.value.parent() {
458458
Some(parent) => Some(node.with_value(parent)),
459459
None => {
460460
let parent_node = node.file_id.call_node(db)?;
@@ -570,19 +570,14 @@ impl<N: AstNode> InFile<N> {
570570
where
571571
N: 'db,
572572
{
573-
std::iter::successors(Some(self), move |node| {
573+
iter::successors(Some(self), move |node| {
574574
let InFile { file_id, value } = node.file_id.call_node(db)?;
575575
N::cast(value).map(|n| InFile::new(file_id, n))
576576
})
577577
}
578578

579579
pub fn node_with_attributes(self, db: &dyn db::AstDatabase) -> InFile<N> {
580-
std::iter::successors(Some(self), move |node| {
581-
let InFile { file_id, value } = node.file_id.call_node(db)?;
582-
N::cast(value).map(|n| InFile::new(file_id, n))
583-
})
584-
.last()
585-
.unwrap()
580+
self.nodes_with_attributes(db).last().unwrap()
586581
}
587582
}
588583

crates/ide/src/runnables.rs

Lines changed: 6 additions & 7 deletions
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)