Skip to content

Commit eddab6e

Browse files
committed
Auto merge of #18349 - dqkqd:issue-18344, r=Veykril
feat: render docs from aliased type when type has no docs Trying to close #18344 - [x] ~Find the docs by traversing upwards if the type itself has none but aliasing for another type that might have.~ - [x] Show docs from aliased type. - [x] Showing description that we are displaying documentation for different definition in hover box. ![image](https://github.com/user-attachments/assets/820d6f97-aa2c-4dc4-8a25-75746e32d950)
2 parents c58427f + 44e48d7 commit eddab6e

File tree

2 files changed

+166
-1
lines changed

2 files changed

+166
-1
lines changed

crates/ide-db/src/defs.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,19 @@ impl Definition {
178178
Definition::Static(it) => it.docs(db),
179179
Definition::Trait(it) => it.docs(db),
180180
Definition::TraitAlias(it) => it.docs(db),
181-
Definition::TypeAlias(it) => it.docs(db),
181+
Definition::TypeAlias(it) => {
182+
it.docs(db).or_else(|| {
183+
// docs are missing, try to fall back to the docs of the aliased item.
184+
let adt = it.ty(db).as_adt()?;
185+
let docs = adt.docs(db)?;
186+
let docs = format!(
187+
"*This is the documentation for* `{}`\n\n{}",
188+
adt.display(db, edition),
189+
docs.as_str()
190+
);
191+
Some(Documentation::new(docs))
192+
})
193+
}
182194
Definition::BuiltinType(it) => {
183195
famous_defs.and_then(|fd| {
184196
// std exposes prim_{} modules with docstrings on the root to document the builtins

crates/ide/src/hover/tests.rs

+153
Original file line numberDiff line numberDiff line change
@@ -9018,3 +9018,156 @@ foo!(BAR_$0);
90189018
"#]],
90199019
);
90209020
}
9021+
9022+
#[test]
9023+
fn type_alias_without_docs() {
9024+
// Simple.
9025+
check(
9026+
r#"
9027+
/// Docs for B
9028+
struct B;
9029+
9030+
type A$0 = B;
9031+
"#,
9032+
expect![[r#"
9033+
*A*
9034+
9035+
```rust
9036+
test
9037+
```
9038+
9039+
```rust
9040+
// size = 0, align = 1
9041+
type A = B
9042+
```
9043+
9044+
---
9045+
9046+
*This is the documentation for* `struct B`
9047+
9048+
Docs for B
9049+
"#]],
9050+
);
9051+
9052+
// Nested.
9053+
check(
9054+
r#"
9055+
/// Docs for C
9056+
struct C;
9057+
9058+
type B = C;
9059+
9060+
type A$0 = B;
9061+
"#,
9062+
expect![[r#"
9063+
*A*
9064+
9065+
```rust
9066+
test
9067+
```
9068+
9069+
```rust
9070+
// size = 0, align = 1
9071+
type A = B
9072+
```
9073+
9074+
---
9075+
9076+
*This is the documentation for* `struct C`
9077+
9078+
Docs for C
9079+
"#]],
9080+
);
9081+
9082+
// Showing the docs for aliased struct instead of intermediate type.
9083+
check(
9084+
r#"
9085+
/// Docs for C
9086+
struct C;
9087+
9088+
/// Docs for B
9089+
type B = C;
9090+
9091+
type A$0 = B;
9092+
"#,
9093+
expect![[r#"
9094+
*A*
9095+
9096+
```rust
9097+
test
9098+
```
9099+
9100+
```rust
9101+
// size = 0, align = 1
9102+
type A = B
9103+
```
9104+
9105+
---
9106+
9107+
*This is the documentation for* `struct C`
9108+
9109+
Docs for C
9110+
"#]],
9111+
);
9112+
9113+
// No docs found.
9114+
check(
9115+
r#"
9116+
struct C;
9117+
9118+
type B = C;
9119+
9120+
type A$0 = B;
9121+
"#,
9122+
expect![[r#"
9123+
*A*
9124+
9125+
```rust
9126+
test
9127+
```
9128+
9129+
```rust
9130+
// size = 0, align = 1
9131+
type A = B
9132+
```
9133+
"#]],
9134+
);
9135+
9136+
// Multiple nested crate.
9137+
check(
9138+
r#"
9139+
//- /lib.rs crate:c
9140+
/// Docs for C
9141+
pub struct C;
9142+
9143+
//- /lib.rs crate:b deps:c
9144+
pub use c::C;
9145+
pub type B = C;
9146+
9147+
//- /lib.rs crate:a deps:b
9148+
pub use b::B;
9149+
pub type A = B;
9150+
9151+
//- /main.rs crate:main deps:a
9152+
use a::A$0;
9153+
"#,
9154+
expect![[r#"
9155+
*A*
9156+
9157+
```rust
9158+
a
9159+
```
9160+
9161+
```rust
9162+
// size = 0, align = 1
9163+
pub type A = B
9164+
```
9165+
9166+
---
9167+
9168+
*This is the documentation for* `pub struct C`
9169+
9170+
Docs for C
9171+
"#]],
9172+
);
9173+
}

0 commit comments

Comments
 (0)