Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit d8f6538

Browse files
committed
Do not render meta info when hovering usages
1 parent 25b0846 commit d8f6538

File tree

4 files changed

+124
-83
lines changed

4 files changed

+124
-83
lines changed

src/tools/rust-analyzer/crates/ide/src/hover.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ fn hover_offset(
158158
if let Some(doc_comment) = token_as_doc_comment(&original_token) {
159159
cov_mark::hit!(no_highlight_on_comment_hover);
160160
return doc_comment.get_definition_with_descend_at(sema, offset, |def, node, range| {
161-
let res = hover_for_definition(sema, file_id, def, &node, None, config, edition);
161+
let res = hover_for_definition(sema, file_id, def, &node, None, false, config, edition);
162162
Some(RangeInfo::new(range, res))
163163
});
164164
}
@@ -172,6 +172,7 @@ fn hover_offset(
172172
Definition::from(resolution?),
173173
&original_token.parent()?,
174174
None,
175+
false,
175176
config,
176177
edition,
177178
);
@@ -218,6 +219,7 @@ fn hover_offset(
218219
break 'a vec![(
219220
Definition::Macro(macro_),
220221
sema.resolve_macro_call_arm(&macro_call),
222+
false,
221223
node,
222224
)];
223225
}
@@ -234,19 +236,34 @@ fn hover_offset(
234236
decl,
235237
..
236238
}) => {
237-
vec![(Definition::ExternCrateDecl(decl), None, node)]
239+
vec![(Definition::ExternCrateDecl(decl), None, false, node)]
238240
}
239241

240242
class => {
241-
multizip((class.definitions(), iter::repeat(None), iter::repeat(node)))
242-
.collect::<Vec<_>>()
243+
let is_def = matches!(class, IdentClass::NameClass(_));
244+
multizip((
245+
class.definitions(),
246+
iter::repeat(None),
247+
iter::repeat(is_def),
248+
iter::repeat(node),
249+
))
250+
.collect::<Vec<_>>()
243251
}
244252
}
245253
}
246254
.into_iter()
247-
.unique_by(|&(def, _, _)| def)
248-
.map(|(def, macro_arm, node)| {
249-
hover_for_definition(sema, file_id, def, &node, macro_arm, config, edition)
255+
.unique_by(|&(def, _, _, _)| def)
256+
.map(|(def, macro_arm, hovered_definition, node)| {
257+
hover_for_definition(
258+
sema,
259+
file_id,
260+
def,
261+
&node,
262+
macro_arm,
263+
hovered_definition,
264+
config,
265+
edition,
266+
)
250267
})
251268
.collect::<Vec<_>>(),
252269
)
@@ -366,6 +383,7 @@ pub(crate) fn hover_for_definition(
366383
def: Definition,
367384
scope_node: &SyntaxNode,
368385
macro_arm: Option<u32>,
386+
hovered_definition: bool,
369387
config: &HoverConfig,
370388
edition: Edition,
371389
) -> HoverResult {
@@ -397,6 +415,7 @@ pub(crate) fn hover_for_definition(
397415
famous_defs.as_ref(),
398416
&notable_traits,
399417
macro_arm,
418+
hovered_definition,
400419
config,
401420
edition,
402421
);

src/tools/rust-analyzer/crates/ide/src/hover/render.rs

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ pub(super) fn definition(
419419
famous_defs: Option<&FamousDefs<'_, '_>>,
420420
notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)],
421421
macro_arm: Option<u32>,
422+
hovered_definition: bool,
422423
config: &HoverConfig,
423424
edition: Edition,
424425
) -> Markup {
@@ -456,7 +457,7 @@ pub(super) fn definition(
456457
_ => def.label(db, edition),
457458
};
458459
let docs = def.docs(db, famous_defs, edition);
459-
let value = (|| match def {
460+
let value = || match def {
460461
Definition::Variant(it) => {
461462
if !it.parent_enum(db).is_data_carrying(db) {
462463
match it.eval(db) {
@@ -494,9 +495,9 @@ pub(super) fn definition(
494495
Some(body.to_string())
495496
}
496497
_ => None,
497-
})();
498+
};
498499

499-
let layout_info = match def {
500+
let layout_info = || match def {
500501
Definition::Field(it) => render_memory_layout(
501502
config.memory_layout,
502503
|| it.layout(db),
@@ -529,29 +530,32 @@ pub(super) fn definition(
529530
_ => None,
530531
};
531532

532-
let dyn_compatibility_info = if let Definition::Trait(it) = def {
533-
let mut dyn_compatibility_info = String::new();
534-
render_dyn_compatibility(db, &mut dyn_compatibility_info, it.dyn_compatibility(db));
535-
Some(dyn_compatibility_info)
536-
} else {
537-
None
533+
let dyn_compatibility_info = || match def {
534+
Definition::Trait(it) => {
535+
let mut dyn_compatibility_info = String::new();
536+
render_dyn_compatibility(db, &mut dyn_compatibility_info, it.dyn_compatibility(db));
537+
Some(dyn_compatibility_info)
538+
}
539+
_ => None,
538540
};
539541

540542
let mut desc = String::new();
541543
if let Some(notable_traits) = render_notable_trait_comment(db, notable_traits, edition) {
542544
desc.push_str(&notable_traits);
543545
desc.push('\n');
544546
}
545-
if let Some(layout_info) = layout_info {
546-
desc.push_str(&layout_info);
547-
desc.push('\n');
548-
}
549-
if let Some(dyn_compatibility_info) = dyn_compatibility_info {
550-
desc.push_str(&dyn_compatibility_info);
551-
desc.push('\n');
547+
if hovered_definition {
548+
if let Some(layout_info) = layout_info() {
549+
desc.push_str(&layout_info);
550+
desc.push('\n');
551+
}
552+
if let Some(dyn_compatibility_info) = dyn_compatibility_info() {
553+
desc.push_str(&dyn_compatibility_info);
554+
desc.push('\n');
555+
}
552556
}
553557
desc.push_str(&label);
554-
if let Some(value) = value {
558+
if let Some(value) = value() {
555559
desc.push_str(" = ");
556560
desc.push_str(&value);
557561
}
@@ -994,55 +998,53 @@ fn render_dyn_compatibility(
994998
safety: Option<DynCompatibilityViolation>,
995999
) {
9961000
let Some(osv) = safety else {
997-
buf.push_str("// Dyn Compatible: Yes");
1001+
buf.push_str("// Is Dyn compatible");
9981002
return;
9991003
};
1000-
buf.push_str("// Dyn Compatible: No\n// - Reason: ");
1004+
buf.push_str("// Is not Dyn compatible due to ");
10011005
match osv {
10021006
DynCompatibilityViolation::SizedSelf => {
1003-
buf.push_str("has a `Self: Sized` bound");
1007+
buf.push_str("having a `Self: Sized` bound");
10041008
}
10051009
DynCompatibilityViolation::SelfReferential => {
1006-
buf.push_str("has a bound that references `Self`");
1010+
buf.push_str("having a bound that references `Self`");
10071011
}
10081012
DynCompatibilityViolation::Method(func, mvc) => {
10091013
let name = hir::Function::from(func).name(db);
1010-
format_to!(
1011-
buf,
1012-
"has a method `{}` that is non dispatchable because of:\n// - ",
1013-
name.as_str()
1014-
);
1014+
format_to!(buf, "having a method `{}` that is not dispatchable due to ", name.as_str());
10151015
let desc = match mvc {
10161016
MethodViolationCode::StaticMethod => "missing a receiver",
1017-
MethodViolationCode::ReferencesSelfInput => "a parameter references `Self`",
1018-
MethodViolationCode::ReferencesSelfOutput => "the return type references `Self`",
1017+
MethodViolationCode::ReferencesSelfInput => "having a parameter referencing `Self`",
1018+
MethodViolationCode::ReferencesSelfOutput => "the return type referencing `Self`",
10191019
MethodViolationCode::ReferencesImplTraitInTrait => {
1020-
"the return type contains `impl Trait`"
1020+
"the return type containing `impl Trait`"
10211021
}
10221022
MethodViolationCode::AsyncFn => "being async",
10231023
MethodViolationCode::WhereClauseReferencesSelf => {
1024-
"a where clause references `Self`"
1024+
"a where clause referencing `Self`"
1025+
}
1026+
MethodViolationCode::Generic => "having a const or type generic parameter",
1027+
MethodViolationCode::UndispatchableReceiver => {
1028+
"having a non-dispatchable receiver type"
10251029
}
1026-
MethodViolationCode::Generic => "a non-lifetime generic parameter",
1027-
MethodViolationCode::UndispatchableReceiver => "a non-dispatchable receiver type",
10281030
};
10291031
buf.push_str(desc);
10301032
}
10311033
DynCompatibilityViolation::AssocConst(const_) => {
10321034
let name = hir::Const::from(const_).name(db);
10331035
if let Some(name) = name {
1034-
format_to!(buf, "has an associated constant `{}`", name.as_str());
1036+
format_to!(buf, "having an associated constant `{}`", name.as_str());
10351037
} else {
1036-
buf.push_str("has an associated constant");
1038+
buf.push_str("having an associated constant");
10371039
}
10381040
}
10391041
DynCompatibilityViolation::GAT(alias) => {
10401042
let name = hir::TypeAlias::from(alias).name(db);
1041-
format_to!(buf, "has a generic associated type `{}`", name.as_str());
1043+
format_to!(buf, "having a generic associated type `{}`", name.as_str());
10421044
}
10431045
DynCompatibilityViolation::HasNonCompatibleSuperTrait(super_trait) => {
10441046
let name = hir::Trait::from(super_trait).name(db);
1045-
format_to!(buf, "has a dyn incompatible supertrait `{}`", name.as_str());
1047+
format_to!(buf, "having a dyn incompatible supertrait `{}`", name.as_str());
10461048
}
10471049
}
10481050
}

0 commit comments

Comments
 (0)