Style hover messages a bit differently

This commit is contained in:
Lukas Wirth 2024-10-29 16:59:48 +01:00
parent d8f6538bac
commit 8a51cbf786
3 changed files with 363 additions and 117 deletions

View File

@ -34,7 +34,10 @@
mod display; mod display;
use std::{mem::discriminant, ops::ControlFlow}; use std::{
mem::discriminant,
ops::{ControlFlow, Not},
};
use arrayvec::ArrayVec; use arrayvec::ArrayVec;
use base_db::{CrateDisplayName, CrateId, CrateOrigin}; use base_db::{CrateDisplayName, CrateId, CrateOrigin};
@ -2697,6 +2700,18 @@ pub fn dyn_compatibility(&self, db: &dyn HirDatabase) -> Option<DynCompatibility
hir_ty::dyn_compatibility::dyn_compatibility(db, self.id) hir_ty::dyn_compatibility::dyn_compatibility(db, self.id)
} }
pub fn dyn_compatibility_all_violations(
&self,
db: &dyn HirDatabase,
) -> Option<Vec<DynCompatibilityViolation>> {
let mut violations = vec![];
hir_ty::dyn_compatibility::dyn_compatibility_with_callback(db, self.id, &mut |violation| {
violations.push(violation);
ControlFlow::Continue(())
});
violations.is_empty().not().then_some(violations)
}
fn all_macro_calls(&self, db: &dyn HirDatabase) -> Box<[(AstId<ast::Item>, MacroCallId)]> { fn all_macro_calls(&self, db: &dyn HirDatabase) -> Box<[(AstId<ast::Item>, MacroCallId)]> {
db.trait_data(self.id) db.trait_data(self.id)
.macro_calls .macro_calls

View File

@ -273,7 +273,7 @@ pub(super) fn keyword(
let markup = process_markup( let markup = process_markup(
sema.db, sema.db,
Definition::Module(doc_owner), Definition::Module(doc_owner),
&markup(Some(docs.into()), description, None), &markup(Some(docs.into()), description, None, None),
config, config,
); );
Some(HoverResult { markup, actions }) Some(HoverResult { markup, actions })
@ -539,28 +539,29 @@ pub(super) fn definition(
_ => None, _ => None,
}; };
let mut desc = String::new(); let mut extra = String::new();
if let Some(notable_traits) = render_notable_trait_comment(db, notable_traits, edition) {
desc.push_str(&notable_traits);
desc.push('\n');
}
if hovered_definition { if hovered_definition {
if let Some(notable_traits) = render_notable_trait(db, notable_traits, edition) {
extra.push_str("\n___\n");
extra.push_str(&notable_traits);
}
if let Some(layout_info) = layout_info() { if let Some(layout_info) = layout_info() {
desc.push_str(&layout_info); extra.push_str("\n___\n");
desc.push('\n'); extra.push_str(&layout_info);
} }
if let Some(dyn_compatibility_info) = dyn_compatibility_info() { if let Some(dyn_compatibility_info) = dyn_compatibility_info() {
desc.push_str(&dyn_compatibility_info); extra.push_str("\n___\n");
desc.push('\n'); extra.push_str(&dyn_compatibility_info);
} }
} }
let mut desc = String::new();
desc.push_str(&label); desc.push_str(&label);
if let Some(value) = value() { if let Some(value) = value() {
desc.push_str(" = "); desc.push_str(" = ");
desc.push_str(&value); desc.push_str(&value);
} }
markup(docs.map(Into::into), desc, mod_path) markup(docs.map(Into::into), desc, extra.is_empty().not().then_some(extra), mod_path)
} }
pub(super) fn literal( pub(super) fn literal(
@ -630,7 +631,7 @@ pub(super) fn literal(
Some(s.into()) Some(s.into())
} }
fn render_notable_trait_comment( fn render_notable_trait(
db: &RootDatabase, db: &RootDatabase,
notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)], notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)],
edition: Edition, edition: Edition,
@ -639,7 +640,7 @@ fn render_notable_trait_comment(
let mut needs_impl_header = true; let mut needs_impl_header = true;
for (trait_, assoc_types) in notable_traits { for (trait_, assoc_types) in notable_traits {
desc.push_str(if mem::take(&mut needs_impl_header) { desc.push_str(if mem::take(&mut needs_impl_header) {
"// Implements notable traits: " "Implements notable traits: "
} else { } else {
", " ", "
}); });
@ -732,13 +733,12 @@ fn type_info(
) )
.into() .into()
} else { } else {
let mut desc = let mut desc = format!("```rust\n{}\n```", original.display(db, edition));
match render_notable_trait_comment(db, &notable_traits(db, &original), edition) { if let Some(extra) = render_notable_trait(db, &notable_traits(db, &original), edition) {
Some(desc) => desc + "\n", desc.push_str("\n___\n");
None => String::new(), desc.push_str(&extra);
}; };
format_to!(desc, "{}", original.display(db, edition)); desc.into()
Markup::fenced_block(&desc)
}; };
if let Some(actions) = HoverAction::goto_type_from_targets(db, targets, edition) { if let Some(actions) = HoverAction::goto_type_from_targets(db, targets, edition) {
res.actions.push(actions); res.actions.push(actions);
@ -790,20 +790,16 @@ fn closure_ty(
}; };
let mut markup = format!("```rust\n{}", c.display_with_id(sema.db, edition)); let mut markup = format!("```rust\n{}", c.display_with_id(sema.db, edition));
if let Some(layout) =
render_memory_layout(config.memory_layout, || original.layout(sema.db), |_| None, |_| None)
{
format_to!(markup, " {layout}");
}
if let Some(trait_) = c.fn_trait(sema.db).get_id(sema.db, original.krate(sema.db).into()) { if let Some(trait_) = c.fn_trait(sema.db).get_id(sema.db, original.krate(sema.db).into()) {
push_new_def(hir::Trait::from(trait_).into()) push_new_def(hir::Trait::from(trait_).into())
} }
format_to!( format_to!(markup, "\n{}\n```", c.display_with_impl(sema.db, edition),);
markup, if let Some(layout) =
"\n{}\n```{adjusted}\n\n## Captures\n{}", render_memory_layout(config.memory_layout, || original.layout(sema.db), |_| None, |_| None)
c.display_with_impl(sema.db, edition), {
captures_rendered, format_to!(markup, "\n___\n{layout}");
); }
format_to!(markup, "{adjusted}\n\n## Captures\n{}", captures_rendered,);
let mut res = HoverResult::default(); let mut res = HoverResult::default();
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) { if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets, edition) {
@ -828,7 +824,12 @@ fn definition_mod_path(db: &RootDatabase, def: &Definition, edition: Edition) ->
.map(|module| path(db, module, definition_owner_name(db, def, edition), edition)) .map(|module| path(db, module, definition_owner_name(db, def, edition), edition))
} }
fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Markup { fn markup(
docs: Option<String>,
rust: String,
extra: Option<String>,
mod_path: Option<String>,
) -> Markup {
let mut buf = String::new(); let mut buf = String::new();
if let Some(mod_path) = mod_path { if let Some(mod_path) = mod_path {
@ -836,7 +837,11 @@ fn markup(docs: Option<String>, desc: String, mod_path: Option<String>) -> Marku
format_to!(buf, "```rust\n{}\n```\n\n", mod_path); format_to!(buf, "```rust\n{}\n```\n\n", mod_path);
} }
} }
format_to!(buf, "```rust\n{}\n```", desc); format_to!(buf, "```rust\n{}\n```", rust);
if let Some(extra) = extra {
buf.push_str(&extra);
}
if let Some(doc) = docs { if let Some(doc) = docs {
format_to!(buf, "\n___\n\n{}", doc); format_to!(buf, "\n___\n\n{}", doc);
@ -866,7 +871,7 @@ fn render_memory_layout(
let config = config?; let config = config?;
let layout = layout().ok()?; let layout = layout().ok()?;
let mut label = String::from("// "); let mut label = String::new();
if let Some(render) = config.size { if let Some(render) = config.size {
let size = match tag(&layout) { let size = match tag(&layout) {
@ -998,10 +1003,10 @@ fn render_dyn_compatibility(
safety: Option<DynCompatibilityViolation>, safety: Option<DynCompatibilityViolation>,
) { ) {
let Some(osv) = safety else { let Some(osv) = safety else {
buf.push_str("// Is Dyn compatible"); buf.push_str("Is Dyn compatible");
return; return;
}; };
buf.push_str("// Is not Dyn compatible due to "); buf.push_str("Is not Dyn compatible due to ");
match osv { match osv {
DynCompatibilityViolation::SizedSelf => { DynCompatibilityViolation::SizedSelf => {
buf.push_str("having a `Self: Sized` bound"); buf.push_str("having a `Self: Sized` bound");

File diff suppressed because it is too large Load Diff