Skip to content

Commit e33e819

Browse files
committed
Auto merge of #18246 - ChayimFriedman2:fix-18238, r=Veykril
fix: Fix `prettify_macro_expansion()` when the node isn't the whole file Fixes #18238.
2 parents 9aa4293 + c8540e7 commit e33e819

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

crates/hir-expand/src/prettify_macro_expansion_.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,14 @@ pub fn prettify_macro_expansion(
1515
span_map: &ExpansionSpanMap,
1616
target_crate_id: CrateId,
1717
) -> SyntaxNode {
18+
// Because `syntax_bridge::prettify_macro_expansion::prettify_macro_expansion()` clones subtree for `syn`,
19+
// that means it will be offsetted to the beginning.
20+
let span_offset = syn.text_range().start();
1821
let crate_graph = db.crate_graph();
1922
let target_crate = &crate_graph[target_crate_id];
2023
let mut syntax_ctx_id_to_dollar_crate_replacement = FxHashMap::default();
2124
syntax_bridge::prettify_macro_expansion::prettify_macro_expansion(syn, &mut |dollar_crate| {
22-
let ctx = span_map.span_at(dollar_crate.text_range().start()).ctx;
25+
let ctx = span_map.span_at(dollar_crate.text_range().start() + span_offset).ctx;
2326
let replacement =
2427
syntax_ctx_id_to_dollar_crate_replacement.entry(ctx).or_insert_with(|| {
2528
let ctx_data = db.lookup_intern_syntax_context(ctx);

crates/ide/src/hover/tests.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8988,3 +8988,33 @@ mod m {
89888988
"#]],
89898989
);
89908990
}
8991+
8992+
#[test]
8993+
fn regression_18238() {
8994+
check(
8995+
r#"
8996+
macro_rules! foo {
8997+
($name:ident) => {
8998+
pub static $name = Foo::new(|| {
8999+
$crate;
9000+
});
9001+
};
9002+
}
9003+
9004+
foo!(BAR_$0);
9005+
"#,
9006+
expect![[r#"
9007+
*BAR_*
9008+
9009+
```rust
9010+
test
9011+
```
9012+
9013+
```rust
9014+
pub static BAR_: {error} = Foo::new(||{
9015+
crate;
9016+
})
9017+
```
9018+
"#]],
9019+
);
9020+
}

0 commit comments

Comments
 (0)