Skip to content

Commit e1541bd

Browse files
bors[bot]Jonas Schievink
and
Jonas Schievink
authored
Merge #11552
11552: fix: properly display `$crate` in hovers r=jonas-schievink a=jonas-schievink We used to print it as `{extern_crate}`, this PR resolves it to the crate's name, or falls back to `$crate` if the crate has no name. bors r+ Co-authored-by: Jonas Schievink <[email protected]>
2 parents ab896e3 + a247fff commit e1541bd

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

crates/hir_ty/src/display.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -1189,7 +1189,18 @@ impl HirDisplay for Path {
11891189
write!(f, "super")?;
11901190
}
11911191
}
1192-
(_, PathKind::DollarCrate(_)) => write!(f, "{{extern_crate}}")?,
1192+
(_, PathKind::DollarCrate(id)) => {
1193+
// Resolve `$crate` to the crate's display name.
1194+
// FIXME: should use the dependency name instead if available, but that depends on
1195+
// the crate invoking `HirDisplay`
1196+
let crate_graph = f.db.crate_graph();
1197+
let name = crate_graph[*id]
1198+
.display_name
1199+
.as_ref()
1200+
.map(|name| name.canonical_name())
1201+
.unwrap_or("$crate");
1202+
write!(f, "{name}")?
1203+
}
11931204
}
11941205

11951206
for (seg_idx, segment) in self.segments().iter().enumerate() {

crates/ide/src/hover/tests.rs

+30
Original file line numberDiff line numberDiff line change
@@ -4583,3 +4583,33 @@ pub struct Foo;
45834583
"##]],
45844584
);
45854585
}
4586+
4587+
#[test]
4588+
fn hover_dollar_crate() {
4589+
// $crate should be resolved to the right crate name.
4590+
4591+
check(
4592+
r#"
4593+
//- /main.rs crate:main deps:dep
4594+
dep::m!(KONST$0);
4595+
//- /dep.rs crate:dep
4596+
#[macro_export]
4597+
macro_rules! m {
4598+
( $name:ident ) => { const $name: $crate::Type = $crate::Type; };
4599+
}
4600+
4601+
pub struct Type;
4602+
"#,
4603+
expect![[r#"
4604+
*KONST*
4605+
4606+
```rust
4607+
main
4608+
```
4609+
4610+
```rust
4611+
const KONST: dep::Type = $crate::Type
4612+
```
4613+
"#]],
4614+
);
4615+
}

0 commit comments

Comments
 (0)