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

Commit 8a51cbf

Browse files
committed
Style hover messages a bit differently
1 parent d8f6538 commit 8a51cbf

File tree

3 files changed

+362
-116
lines changed

3 files changed

+362
-116
lines changed

src/tools/rust-analyzer/crates/hir/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ pub mod term_search;
3434

3535
mod display;
3636

37-
use std::{mem::discriminant, ops::ControlFlow};
37+
use std::{
38+
mem::discriminant,
39+
ops::{ControlFlow, Not},
40+
};
3841

3942
use arrayvec::ArrayVec;
4043
use base_db::{CrateDisplayName, CrateId, CrateOrigin};
@@ -2697,6 +2700,18 @@ impl Trait {
26972700
hir_ty::dyn_compatibility::dyn_compatibility(db, self.id)
26982701
}
26992702

2703+
pub fn dyn_compatibility_all_violations(
2704+
&self,
2705+
db: &dyn HirDatabase,
2706+
) -> Option<Vec<DynCompatibilityViolation>> {
2707+
let mut violations = vec![];
2708+
hir_ty::dyn_compatibility::dyn_compatibility_with_callback(db, self.id, &mut |violation| {
2709+
violations.push(violation);
2710+
ControlFlow::Continue(())
2711+
});
2712+
violations.is_empty().not().then_some(violations)
2713+
}
2714+
27002715
fn all_macro_calls(&self, db: &dyn HirDatabase) -> Box<[(AstId<ast::Item>, MacroCallId)]> {
27012716
db.trait_data(self.id)
27022717
.macro_calls

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

Lines changed: 40 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ pub(super) fn keyword(
273273
let markup = process_markup(
274274
sema.db,
275275
Definition::Module(doc_owner),
276-
&markup(Some(docs.into()), description, None),
276+
&markup(Some(docs.into()), description, None, None),
277277
config,
278278
);
279279
Some(HoverResult { markup, actions })
@@ -539,28 +539,29 @@ pub(super) fn definition(
539539
_ => None,
540540
};
541541

542-
let mut desc = String::new();
543-
if let Some(notable_traits) = render_notable_trait_comment(db, notable_traits, edition) {
544-
desc.push_str(&notable_traits);
545-
desc.push('\n');
546-
}
542+
let mut extra = String::new();
547543
if hovered_definition {
544+
if let Some(notable_traits) = render_notable_trait(db, notable_traits, edition) {
545+
extra.push_str("\n___\n");
546+
extra.push_str(&notable_traits);
547+
}
548548
if let Some(layout_info) = layout_info() {
549-
desc.push_str(&layout_info);
550-
desc.push('\n');
549+
extra.push_str("\n___\n");
550+
extra.push_str(&layout_info);
551551
}
552552
if let Some(dyn_compatibility_info) = dyn_compatibility_info() {
553-
desc.push_str(&dyn_compatibility_info);
554-
desc.push('\n');
553+
extra.push_str("\n___\n");
554+
extra.push_str(&dyn_compatibility_info);
555555
}
556556
}
557+
let mut desc = String::new();
557558
desc.push_str(&label);
558559
if let Some(value) = value() {
559560
desc.push_str(" = ");
560561
desc.push_str(&value);
561562
}
562563

563-
markup(docs.map(Into::into), desc, mod_path)
564+
markup(docs.map(Into::into), desc, extra.is_empty().not().then_some(extra), mod_path)
564565
}
565566

566567
pub(super) fn literal(
@@ -630,7 +631,7 @@ pub(super) fn literal(
630631
Some(s.into())
631632
}
632633

633-
fn render_notable_trait_comment(
634+
fn render_notable_trait(
634635
db: &RootDatabase,
635636
notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)],
636637
edition: Edition,
@@ -639,7 +640,7 @@ fn render_notable_trait_comment(
639640
let mut needs_impl_header = true;
640641
for (trait_, assoc_types) in notable_traits {
641642
desc.push_str(if mem::take(&mut needs_impl_header) {
642-
"// Implements notable traits: "
643+
"Implements notable traits: "
643644
} else {
644645
", "
645646
});
@@ -732,13 +733,12 @@ fn type_info(
732733
)
733734
.into()
734735
} else {
735-
let mut desc =
736-
match render_notable_trait_comment(db, &notable_traits(db, &original), edition) {
737-
Some(desc) => desc + "\n",
738-
None => String::new(),
739-
};
740-
format_to!(desc, "{}", original.display(db, edition));
741-
Markup::fenced_block(&desc)
736+
let mut desc = format!("```rust\n{}\n```", original.display(db, edition));
737+
if let Some(extra) = render_notable_trait(db, &notable_traits(db, &original), edition) {
738+
desc.push_str("\n___\n");
739+
desc.push_str(&extra);
740+
};
741+
desc.into()
742742
};
743743
if let Some(actions) = HoverAction::goto_type_from_targets(db, targets, edition) {
744744
res.actions.push(actions);
@@ -790,20 +790,16 @@ fn closure_ty(
790790
};
791791
let mut markup = format!("```rust\n{}", c.display_with_id(sema.db, edition));
792792

793+
if let Some(trait_) = c.fn_trait(sema.db).get_id(sema.db, original.krate(sema.db).into()) {
794+
push_new_def(hir::Trait::from(trait_).into())
795+
}
796+
format_to!(markup, "\n{}\n```", c.display_with_impl(sema.db, edition),);
793797
if let Some(layout) =
794798
render_memory_layout(config.memory_layout, || original.layout(sema.db), |_| None, |_| None)
795799
{
796-
format_to!(markup, " {layout}");
797-
}
798-
if let Some(trait_) = c.fn_trait(sema.db).get_id(sema.db, original.krate(sema.db).into()) {
799-
push_new_def(hir::Trait::from(trait_).into())
800+
format_to!(markup, "\n___\n{layout}");
800801
}
801-
format_to!(
802-
markup,
803-
"\n{}\n```{adjusted}\n\n## Captures\n{}",
804-
c.display_with_impl(sema.db, edition),
805-
captures_rendered,
806-
);
802+
format_to!(markup, "{adjusted}\n\n## Captures\n{}", captures_rendered,);
807803

808804
let mut res = HoverResult::default();
809805
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
@@ -828,15 +824,24 @@ fn definition_mod_path(db: &RootDatabase, def: &Definition, edition: Edition) ->
828824
.map(|module| path(db, module, definition_owner_name(db, def, edition), edition))
829825
}
830826

831-
fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Markup {
827+
fn markup(
828+
docs: Option<String>,
829+
rust: String,
830+
extra: Option<String>,
831+
mod_path: Option<String>,
832+
) -> Markup {
832833
let mut buf = String::new();
833834

834835
if let Some(mod_path) = mod_path {
835836
if !mod_path.is_empty() {
836837
format_to!(buf, "```rust\n{}\n```\n\n", mod_path);
837838
}
838839
}
839-
format_to!(buf, "```rust\n{}\n```", desc);
840+
format_to!(buf, "```rust\n{}\n```", rust);
841+
842+
if let Some(extra) = extra {
843+
buf.push_str(&extra);
844+
}
840845

841846
if let Some(doc) = docs {
842847
format_to!(buf, "\n___\n\n{}", doc);
@@ -866,7 +871,7 @@ fn render_memory_layout(
866871
let config = config?;
867872
let layout = layout().ok()?;
868873

869-
let mut label = String::from("// ");
874+
let mut label = String::new();
870875

871876
if let Some(render) = config.size {
872877
let size = match tag(&layout) {
@@ -998,10 +1003,10 @@ fn render_dyn_compatibility(
9981003
safety: Option<DynCompatibilityViolation>,
9991004
) {
10001005
let Some(osv) = safety else {
1001-
buf.push_str("// Is Dyn compatible");
1006+
buf.push_str("Is Dyn compatible");
10021007
return;
10031008
};
1004-
buf.push_str("// Is not Dyn compatible due to ");
1009+
buf.push_str("Is not Dyn compatible due to ");
10051010
match osv {
10061011
DynCompatibilityViolation::SizedSelf => {
10071012
buf.push_str("having a `Self: Sized` bound");

0 commit comments

Comments
 (0)