Skip to content

Commit a822291

Browse files
committed
Infallible definition hovers
1 parent 2223b4f commit a822291

File tree

5 files changed

+135
-38
lines changed

5 files changed

+135
-38
lines changed

crates/ide-db/src/defs.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ impl Definition {
213213
})
214214
}
215215

216-
pub fn label(&self, db: &RootDatabase) -> Option<String> {
217-
let label = match *self {
216+
pub fn label(&self, db: &RootDatabase) -> String {
217+
match *self {
218218
Definition::Macro(it) => it.display(db).to_string(),
219219
Definition::Field(it) => it.display(db).to_string(),
220220
Definition::TupleField(it) => it.display(db).to_string(),
@@ -241,16 +241,19 @@ impl Definition {
241241
}
242242
}
243243
Definition::SelfType(impl_def) => {
244-
impl_def.self_ty(db).as_adt().and_then(|adt| Definition::Adt(adt).label(db))?
244+
let self_ty = &impl_def.self_ty(db);
245+
match self_ty.as_adt() {
246+
Some(it) => it.display(db).to_string(),
247+
None => self_ty.display(db).to_string(),
248+
}
245249
}
246250
Definition::GenericParam(it) => it.display(db).to_string(),
247251
Definition::Label(it) => it.name(db).display(db).to_string(),
248252
Definition::ExternCrateDecl(it) => it.display(db).to_string(),
249253
Definition::BuiltinAttr(it) => format!("#[{}]", it.name(db)),
250254
Definition::ToolModule(it) => it.name(db).to_string(),
251255
Definition::DeriveHelper(it) => format!("derive_helper {}", it.name(db).display(db)),
252-
};
253-
Some(label)
256+
}
254257
}
255258
}
256259

crates/ide/src/hover.rs

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ fn hover_simple(
147147
if let Some(doc_comment) = token_as_doc_comment(&original_token) {
148148
cov_mark::hit!(no_highlight_on_comment_hover);
149149
return doc_comment.get_definition_with_descend_at(sema, offset, |def, node, range| {
150-
let res = hover_for_definition(sema, file_id, def, &node, config)?;
150+
let res = hover_for_definition(sema, file_id, def, &node, config);
151151
Some(RangeInfo::new(range, res))
152152
});
153153
}
@@ -161,7 +161,7 @@ fn hover_simple(
161161
Definition::from(resolution?),
162162
&original_token.parent()?,
163163
config,
164-
)?;
164+
);
165165
return Some(RangeInfo::new(range, res));
166166
}
167167

@@ -215,7 +215,7 @@ fn hover_simple(
215215
})
216216
.flatten()
217217
.unique_by(|&(def, _)| def)
218-
.filter_map(|(def, node)| hover_for_definition(sema, file_id, def, &node, config))
218+
.map(|(def, node)| hover_for_definition(sema, file_id, def, &node, config))
219219
.reduce(|mut acc: HoverResult, HoverResult { markup, actions }| {
220220
acc.actions.extend(actions);
221221
acc.markup = Markup::from(format!("{}\n---\n{markup}", acc.markup));
@@ -373,9 +373,9 @@ pub(crate) fn hover_for_definition(
373373
def: Definition,
374374
scope_node: &SyntaxNode,
375375
config: &HoverConfig,
376-
) -> Option<HoverResult> {
376+
) -> HoverResult {
377377
let famous_defs = match &def {
378-
Definition::BuiltinType(_) => Some(FamousDefs(sema, sema.scope(scope_node)?.krate())),
378+
Definition::BuiltinType(_) => sema.scope(scope_node).map(|it| FamousDefs(sema, it.krate())),
379379
_ => None,
380380
};
381381

@@ -396,20 +396,19 @@ pub(crate) fn hover_for_definition(
396396
};
397397
let notable_traits = def_ty.map(|ty| notable_traits(db, &ty)).unwrap_or_default();
398398

399-
render::definition(sema.db, def, famous_defs.as_ref(), &notable_traits, config).map(|markup| {
400-
HoverResult {
401-
markup: render::process_markup(sema.db, def, &markup, config),
402-
actions: [
403-
show_implementations_action(sema.db, def),
404-
show_fn_references_action(sema.db, def),
405-
runnable_action(sema, def, file_id),
406-
goto_type_action_for_def(sema.db, def, &notable_traits),
407-
]
408-
.into_iter()
409-
.flatten()
410-
.collect(),
411-
}
412-
})
399+
let markup = render::definition(sema.db, def, famous_defs.as_ref(), &notable_traits, config);
400+
HoverResult {
401+
markup: render::process_markup(sema.db, def, &markup, config),
402+
actions: [
403+
show_implementations_action(sema.db, def),
404+
show_fn_references_action(sema.db, def),
405+
runnable_action(sema, def, file_id),
406+
goto_type_action_for_def(sema.db, def, &notable_traits),
407+
]
408+
.into_iter()
409+
.flatten()
410+
.collect(),
411+
}
413412
}
414413

415414
fn notable_traits(

crates/ide/src/hover/render.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ pub(super) fn keyword(
264264
let markup = process_markup(
265265
sema.db,
266266
Definition::Module(doc_owner),
267-
&markup(Some(docs.into()), description, None)?,
267+
&markup(Some(docs.into()), description, None),
268268
config,
269269
);
270270
Some(HoverResult { markup, actions })
@@ -396,11 +396,11 @@ pub(super) fn definition(
396396
famous_defs: Option<&FamousDefs<'_, '_>>,
397397
notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)],
398398
config: &HoverConfig,
399-
) -> Option<Markup> {
399+
) -> Markup {
400400
let mod_path = definition_mod_path(db, &def);
401-
let label = def.label(db)?;
401+
let label = def.label(db);
402402
let docs = def.docs(db, famous_defs);
403-
let value = match def {
403+
let value = (|| match def {
404404
Definition::Variant(it) => {
405405
if !it.parent_enum(db).is_data_carrying(db) {
406406
match it.eval(db) {
@@ -436,7 +436,7 @@ pub(super) fn definition(
436436
Some(body.to_string())
437437
}
438438
_ => None,
439-
};
439+
})();
440440

441441
let layout_info = match def {
442442
Definition::Field(it) => render_memory_layout(
@@ -683,7 +683,7 @@ fn definition_mod_path(db: &RootDatabase, def: &Definition) -> Option<String> {
683683
def.module(db).map(|module| path(db, module, definition_owner_name(db, def)))
684684
}
685685

686-
fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Option<Markup> {
686+
fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Markup {
687687
let mut buf = String::new();
688688

689689
if let Some(mod_path) = mod_path {
@@ -696,7 +696,7 @@ fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Optio
696696
if let Some(doc) = docs {
697697
format_to!(buf, "\n___\n\n{}", doc);
698698
}
699-
Some(buf.into())
699+
buf.into()
700700
}
701701

702702
fn find_std_module(famous_defs: &FamousDefs<'_, '_>, name: &str) -> Option<hir::Module> {

crates/ide/src/hover/tests.rs

Lines changed: 100 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,11 +1279,11 @@ impl Thing {
12791279
);
12801280
check(
12811281
r#"
1282-
enum Thing { A }
1283-
impl Thing {
1284-
pub fn thing(a: Self$0) {}
1285-
}
1286-
"#,
1282+
enum Thing { A }
1283+
impl Thing {
1284+
pub fn thing(a: Self$0) {}
1285+
}
1286+
"#,
12871287
expect![[r#"
12881288
*Self*
12891289
@@ -1298,6 +1298,42 @@ impl Thing {
12981298
```
12991299
"#]],
13001300
);
1301+
check(
1302+
r#"
1303+
impl usize {
1304+
pub fn thing(a: Self$0) {}
1305+
}
1306+
"#,
1307+
expect![[r#"
1308+
*Self*
1309+
1310+
```rust
1311+
test
1312+
```
1313+
1314+
```rust
1315+
usize
1316+
```
1317+
"#]],
1318+
);
1319+
check(
1320+
r#"
1321+
impl fn() -> usize {
1322+
pub fn thing(a: Self$0) {}
1323+
}
1324+
"#,
1325+
expect![[r#"
1326+
*Self*
1327+
1328+
```rust
1329+
test
1330+
```
1331+
1332+
```rust
1333+
fn() -> usize
1334+
```
1335+
"#]],
1336+
);
13011337
}
13021338

13031339
#[test]
@@ -7201,6 +7237,65 @@ impl Iterator for S {
72017237
);
72027238
}
72037239

7240+
#[test]
7241+
fn extern_items() {
7242+
check(
7243+
r#"
7244+
extern "C" {
7245+
static STATIC$0: ();
7246+
}
7247+
"#,
7248+
expect![[r#"
7249+
*STATIC*
7250+
7251+
```rust
7252+
test
7253+
```
7254+
7255+
```rust
7256+
static STATIC: ()
7257+
```
7258+
"#]],
7259+
);
7260+
check(
7261+
r#"
7262+
extern "C" {
7263+
fn fun$0();
7264+
}
7265+
"#,
7266+
expect![[r#"
7267+
*fun*
7268+
7269+
```rust
7270+
test
7271+
```
7272+
7273+
```rust
7274+
unsafe fn fun()
7275+
```
7276+
"#]],
7277+
);
7278+
check(
7279+
r#"
7280+
extern "C" {
7281+
type Ty$0;
7282+
}
7283+
"#,
7284+
expect![[r#"
7285+
*Ty*
7286+
7287+
```rust
7288+
test
7289+
```
7290+
7291+
```rust
7292+
// size = 0, align = 1
7293+
type Ty
7294+
```
7295+
"#]],
7296+
);
7297+
}
7298+
72047299
#[test]
72057300
fn notable_ranged() {
72067301
check_hover_range(

crates/ide/src/static_index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ impl StaticIndex<'_> {
186186
} else {
187187
let it = self.tokens.insert(TokenStaticData {
188188
documentation: documentation_for_definition(&sema, def, &node),
189-
hover: hover_for_definition(&sema, file_id, def, &node, &hover_config),
189+
hover: Some(hover_for_definition(&sema, file_id, def, &node, &hover_config)),
190190
definition: def.try_to_nav(self.db).map(UpmappingResult::call_site).map(|it| {
191191
FileRange { file_id: it.file_id, range: it.focus_or_full_range() }
192192
}),
@@ -196,7 +196,7 @@ impl StaticIndex<'_> {
196196
enclosing_moniker: current_crate
197197
.zip(def.enclosing_definition(self.db))
198198
.and_then(|(cc, enclosing_def)| def_to_moniker(self.db, enclosing_def, cc)),
199-
signature: def.label(self.db),
199+
signature: Some(def.label(self.db)),
200200
kind: def_to_kind(self.db, def),
201201
});
202202
self.def_map.insert(def, it);

0 commit comments

Comments
 (0)