Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/ide/src/hover/render.rs')
| -rw-r--r-- | crates/ide/src/hover/render.rs | 151 |
1 files changed, 124 insertions, 27 deletions
diff --git a/crates/ide/src/hover/render.rs b/crates/ide/src/hover/render.rs index ee2f15c5a6..45386df2b2 100644 --- a/crates/ide/src/hover/render.rs +++ b/crates/ide/src/hover/render.rs @@ -1,7 +1,10 @@ //! Logic for rendering the different hover messages +use std::{mem, ops::Not}; + use either::Either; use hir::{ - Adt, AsAssocItem, CaptureKind, HasSource, HirDisplay, Layout, LayoutError, Semantics, TypeInfo, + Adt, AsAssocItem, CaptureKind, HasCrate, HasSource, HirDisplay, Layout, LayoutError, Name, + Semantics, Trait, Type, TypeInfo, }; use ide_db::{ base_db::SourceDatabase, @@ -22,7 +25,7 @@ use syntax::{ use crate::{ doc_links::{remove_links, rewrite_links}, - hover::walk_and_push_ty, + hover::{notable_traits, walk_and_push_ty}, HoverAction, HoverConfig, HoverResult, Markup, MemoryLayoutHoverConfig, MemoryLayoutHoverRenderKind, }; @@ -114,7 +117,9 @@ pub(super) fn try_expr( }; walk_and_push_ty(sema.db, &inner_ty, &mut push_new_def); walk_and_push_ty(sema.db, &body_ty, &mut push_new_def); - res.actions.push(HoverAction::goto_type_from_targets(sema.db, targets)); + if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets) { + res.actions.push(actions); + } let inner_ty = inner_ty.display(sema.db).to_string(); let body_ty = body_ty.display(sema.db).to_string(); @@ -123,7 +128,7 @@ pub(super) fn try_expr( let l = "Propagated as: ".len() - " Type: ".len(); let static_text_len_diff = l as isize - s.len() as isize; let tpad = static_text_len_diff.max(0) as usize; - let ppad = static_text_len_diff.min(0).abs() as usize; + let ppad = static_text_len_diff.min(0).unsigned_abs(); res.markup = format!( "```text\n{} Type: {:>pad0$}\nPropagated as: {:>pad1$}\n```\n", @@ -192,7 +197,9 @@ pub(super) fn deref_expr( ) .into() }; - res.actions.push(HoverAction::goto_type_from_targets(sema.db, targets)); + if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets) { + res.actions.push(actions); + } Some(res) } @@ -299,7 +306,9 @@ pub(super) fn struct_rest_pat( Markup::fenced_block(&s) }; - res.actions.push(HoverAction::goto_type_from_targets(sema.db, targets)); + if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets) { + res.actions.push(actions); + } res } @@ -333,7 +342,7 @@ pub(super) fn try_for_lint(attr: &ast::Attr, token: &SyntaxToken) -> Option<Hove tmp = format!("clippy::{}", token.text()); &tmp } else { - &*token.text() + token.text() }; let lint = @@ -385,12 +394,12 @@ pub(super) fn definition( db: &RootDatabase, def: Definition, famous_defs: Option<&FamousDefs<'_, '_>>, + notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)], config: &HoverConfig, ) -> Option<Markup> { let mod_path = definition_mod_path(db, &def); let label = def.label(db)?; let docs = def.docs(db, famous_defs); - let value = match def { Definition::Variant(it) => { if !it.parent_enum(db).is_data_carrying(db) { @@ -462,14 +471,55 @@ pub(super) fn definition( _ => None, }; - let label = match (value, layout_info) { - (Some(value), Some(layout_info)) => format!("{label} = {value}{layout_info}"), - (Some(value), None) => format!("{label} = {value}"), - (None, Some(layout_info)) => format!("{label}{layout_info}"), - (None, None) => label, - }; + let mut desc = String::new(); + if let Some(notable_traits) = render_notable_trait_comment(db, notable_traits) { + desc.push_str(¬able_traits); + desc.push('\n'); + } + if let Some(layout_info) = layout_info { + desc.push_str(&layout_info); + desc.push('\n'); + } + desc.push_str(&label); + if let Some(value) = value { + desc.push_str(" = "); + desc.push_str(&value); + } - markup(docs.map(Into::into), label, mod_path) + markup(docs.map(Into::into), desc, mod_path) +} + +fn render_notable_trait_comment( + db: &RootDatabase, + notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)], +) -> Option<String> { + let mut desc = String::new(); + let mut needs_impl_header = true; + for (trait_, assoc_types) in notable_traits { + desc.push_str(if mem::take(&mut needs_impl_header) { + " // Implements notable traits: " + } else { + ", " + }); + format_to!(desc, "{}", trait_.name(db).display(db),); + if !assoc_types.is_empty() { + desc.push('<'); + format_to!( + desc, + "{}", + assoc_types.iter().format_with(", ", |(ty, name), f| { + f(&name.display(db))?; + f(&" = ")?; + match ty { + Some(ty) => f(&ty.display(db)), + None => f(&"?"), + } + }) + ); + desc.push('>'); + } + } + desc.is_empty().not().then_some(desc) } fn type_info( @@ -480,6 +530,7 @@ fn type_info( if let Some(res) = closure_ty(sema, config, &ty) { return Some(res); }; + let db = sema.db; let TypeInfo { original, adjusted } = ty; let mut res = HoverResult::default(); let mut targets: Vec<hir::ModuleDef> = Vec::new(); @@ -488,15 +539,49 @@ fn type_info( targets.push(item); } }; - walk_and_push_ty(sema.db, &original, &mut push_new_def); + walk_and_push_ty(db, &original, &mut push_new_def); res.markup = if let Some(adjusted_ty) = adjusted { - walk_and_push_ty(sema.db, &adjusted_ty, &mut push_new_def); - let original = original.display(sema.db).to_string(); - let adjusted = adjusted_ty.display(sema.db).to_string(); + walk_and_push_ty(db, &adjusted_ty, &mut push_new_def); + + let notable = { + let mut desc = String::new(); + let mut needs_impl_header = true; + for (trait_, assoc_types) in notable_traits(db, &original) { + desc.push_str(if mem::take(&mut needs_impl_header) { + "Implements Notable Traits: " + } else { + ", " + }); + format_to!(desc, "{}", trait_.name(db).display(db),); + if !assoc_types.is_empty() { + desc.push('<'); + format_to!( + desc, + "{}", + assoc_types.into_iter().format_with(", ", |(ty, name), f| { + f(&name.display(db))?; + f(&" = ")?; + match ty { + Some(ty) => f(&ty.display(db)), + None => f(&"?"), + } + }) + ); + desc.push('>'); + } + } + if !desc.is_empty() { + desc.push('\n'); + } + desc + }; + + let original = original.display(db).to_string(); + let adjusted = adjusted_ty.display(db).to_string(); let static_text_diff_len = "Coerced to: ".len() - "Type: ".len(); format!( - "```text\nType: {:>apad$}\nCoerced to: {:>opad$}\n```\n", + "```text\nType: {:>apad$}\nCoerced to: {:>opad$}\n{notable}```\n", original, adjusted, apad = static_text_diff_len + adjusted.len().max(original.len()), @@ -504,9 +589,16 @@ fn type_info( ) .into() } else { - Markup::fenced_block(&original.display(sema.db)) + let mut desc = match render_notable_trait_comment(db, ¬able_traits(db, &original)) { + Some(desc) => desc + "\n", + None => String::new(), + }; + format_to!(desc, "{}", original.display(db)); + Markup::fenced_block(&desc) }; - res.actions.push(HoverAction::goto_type_from_targets(sema.db, targets)); + if let Some(actions) = HoverAction::goto_type_from_targets(db, targets) { + res.actions.push(actions); + } Some(res) } @@ -543,7 +635,7 @@ fn closure_ty( }); let adjusted = if let Some(adjusted_ty) = adjusted { - walk_and_push_ty(sema.db, &adjusted_ty, &mut push_new_def); + walk_and_push_ty(sema.db, adjusted_ty, &mut push_new_def); format!( "\nCoerced to: {}", adjusted_ty.display(sema.db).with_closure_style(hir::ClosureStyle::ImplFn) @@ -558,6 +650,9 @@ fn closure_ty( { format_to!(markup, "{layout}"); } + 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()) + } format_to!( markup, "\n{}\n```{adjusted}\n\n## Captures\n{}", @@ -566,7 +661,9 @@ fn closure_ty( ); let mut res = HoverResult::default(); - res.actions.push(HoverAction::goto_type_from_targets(sema.db, targets)); + if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets) { + res.actions.push(actions); + } res.markup = markup.into(); Some(res) } @@ -617,8 +714,6 @@ fn render_memory_layout( offset: impl FnOnce(&Layout) -> Option<u64>, tag: impl FnOnce(&Layout) -> Option<usize>, ) -> Option<String> { - // field - let config = config?; let layout = layout().ok()?; @@ -722,7 +817,9 @@ fn keyword_hints( KeywordHint { description, keyword_mod, - actions: vec![HoverAction::goto_type_from_targets(sema.db, targets)], + actions: HoverAction::goto_type_from_targets(sema.db, targets) + .into_iter() + .collect(), } } _ => KeywordHint { |