mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-14 14:13:58 +00:00
Auto merge of #16375 - Veykril:hover-notable, r=Veykril
feat: Goto type actions for notable trait hovers Follow up to https://github.com/rust-lang/rust-analyzer/pull/16374
This commit is contained in:
commit
03336460fc
3 changed files with 527 additions and 338 deletions
|
@ -3,10 +3,10 @@ mod render;
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests;
|
mod tests;
|
||||||
|
|
||||||
use std::iter;
|
use std::{iter, ops::Not};
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{db::DefDatabase, DescendPreference, HasSource, LangItem, Semantics};
|
use hir::{db::DefDatabase, DescendPreference, HasCrate, HasSource, LangItem, Semantics};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::FileRange,
|
base_db::FileRange,
|
||||||
defs::{Definition, IdentClass, NameRefClass, OperatorClass},
|
defs::{Definition, IdentClass, NameRefClass, OperatorClass},
|
||||||
|
@ -64,7 +64,7 @@ pub enum HoverAction {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl HoverAction {
|
impl HoverAction {
|
||||||
fn goto_type_from_targets(db: &RootDatabase, targets: Vec<hir::ModuleDef>) -> Self {
|
fn goto_type_from_targets(db: &RootDatabase, targets: Vec<hir::ModuleDef>) -> Option<Self> {
|
||||||
let targets = targets
|
let targets = targets
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter_map(|it| {
|
.filter_map(|it| {
|
||||||
|
@ -77,8 +77,8 @@ impl HoverAction {
|
||||||
nav: it.try_to_nav(db)?.call_site(),
|
nav: it.try_to_nav(db)?.call_site(),
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
.collect();
|
.collect::<Vec<_>>();
|
||||||
HoverAction::GoToType(targets)
|
targets.is_empty().not().then_some(HoverAction::GoToType(targets))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -315,7 +315,7 @@ fn hover_simple(
|
||||||
ast::IntNumber(num) => {
|
ast::IntNumber(num) => {
|
||||||
res.markup = match num.value() {
|
res.markup = match num.value() {
|
||||||
Ok(num) => {
|
Ok(num) => {
|
||||||
Markup::fenced_block_text(format_args!("{num} (0x{num:X}|0x{num:b})"))
|
Markup::fenced_block_text(format_args!("{num} (0x{num:X}|0b{num:b})"))
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
Markup::fenced_block_text(format_args!("{e}"))
|
Markup::fenced_block_text(format_args!("{e}"))
|
||||||
|
@ -365,25 +365,44 @@ fn hover_ranged(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// FIXME: Why is this pub(crate)?
|
||||||
pub(crate) fn hover_for_definition(
|
pub(crate) fn hover_for_definition(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
file_id: FileId,
|
file_id: FileId,
|
||||||
definition: Definition,
|
def: Definition,
|
||||||
scope_node: &SyntaxNode,
|
scope_node: &SyntaxNode,
|
||||||
config: &HoverConfig,
|
config: &HoverConfig,
|
||||||
) -> Option<HoverResult> {
|
) -> Option<HoverResult> {
|
||||||
let famous_defs = match &definition {
|
let famous_defs = match &def {
|
||||||
Definition::BuiltinType(_) => Some(FamousDefs(sema, sema.scope(scope_node)?.krate())),
|
Definition::BuiltinType(_) => Some(FamousDefs(sema, sema.scope(scope_node)?.krate())),
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
render::definition(sema.db, definition, famous_defs.as_ref(), config).map(|markup| {
|
|
||||||
|
let db = sema.db;
|
||||||
|
let def_ty = match def {
|
||||||
|
Definition::Local(it) => Some(it.ty(db)),
|
||||||
|
Definition::GenericParam(hir::GenericParam::ConstParam(it)) => Some(it.ty(db)),
|
||||||
|
Definition::GenericParam(hir::GenericParam::TypeParam(it)) => Some(it.ty(db)),
|
||||||
|
Definition::Field(field) => Some(field.ty(db)),
|
||||||
|
Definition::TupleField(it) => Some(it.ty(db)),
|
||||||
|
Definition::Function(it) => Some(it.ty(db)),
|
||||||
|
Definition::Adt(it) => Some(it.ty(db)),
|
||||||
|
Definition::Const(it) => Some(it.ty(db)),
|
||||||
|
Definition::Static(it) => Some(it.ty(db)),
|
||||||
|
Definition::TypeAlias(it) => Some(it.ty(db)),
|
||||||
|
Definition::BuiltinType(it) => Some(it.ty(db)),
|
||||||
|
_ => None,
|
||||||
|
};
|
||||||
|
let notable_traits = def_ty.map(|ty| notable_traits(db, &ty)).unwrap_or_default();
|
||||||
|
|
||||||
|
render::definition(sema.db, def, famous_defs.as_ref(), ¬able_traits, config).map(|markup| {
|
||||||
HoverResult {
|
HoverResult {
|
||||||
markup: render::process_markup(sema.db, definition, &markup, config),
|
markup: render::process_markup(sema.db, def, &markup, config),
|
||||||
actions: [
|
actions: [
|
||||||
show_implementations_action(sema.db, definition),
|
show_implementations_action(sema.db, def),
|
||||||
show_fn_references_action(sema.db, definition),
|
show_fn_references_action(sema.db, def),
|
||||||
runnable_action(sema, definition, file_id),
|
runnable_action(sema, def, file_id),
|
||||||
goto_type_action_for_def(sema.db, definition),
|
goto_type_action_for_def(sema.db, def, ¬able_traits),
|
||||||
]
|
]
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.flatten()
|
.flatten()
|
||||||
|
@ -392,6 +411,32 @@ pub(crate) fn hover_for_definition(
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn notable_traits(
|
||||||
|
db: &RootDatabase,
|
||||||
|
ty: &hir::Type,
|
||||||
|
) -> Vec<(hir::Trait, Vec<(Option<hir::Type>, hir::Name)>)> {
|
||||||
|
db.notable_traits_in_deps(ty.krate(db).into())
|
||||||
|
.iter()
|
||||||
|
.flat_map(|it| &**it)
|
||||||
|
.filter_map(move |&trait_| {
|
||||||
|
let trait_ = trait_.into();
|
||||||
|
ty.impls_trait(db, trait_, &[]).then(|| {
|
||||||
|
(
|
||||||
|
trait_,
|
||||||
|
trait_
|
||||||
|
.items(db)
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(hir::AssocItem::as_type_alias)
|
||||||
|
.map(|alias| {
|
||||||
|
(ty.normalize_trait_assoc_type(db, &[], alias), alias.name(db))
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
}
|
||||||
|
|
||||||
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
|
fn show_implementations_action(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
|
||||||
fn to_action(nav_target: NavigationTarget) -> HoverAction {
|
fn to_action(nav_target: NavigationTarget) -> HoverAction {
|
||||||
HoverAction::Implementation(FilePosition {
|
HoverAction::Implementation(FilePosition {
|
||||||
|
@ -446,7 +491,11 @@ fn runnable_action(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn goto_type_action_for_def(db: &RootDatabase, def: Definition) -> Option<HoverAction> {
|
fn goto_type_action_for_def(
|
||||||
|
db: &RootDatabase,
|
||||||
|
def: Definition,
|
||||||
|
notable_traits: &[(hir::Trait, Vec<(Option<hir::Type>, hir::Name)>)],
|
||||||
|
) -> Option<HoverAction> {
|
||||||
let mut targets: Vec<hir::ModuleDef> = Vec::new();
|
let mut targets: Vec<hir::ModuleDef> = Vec::new();
|
||||||
let mut push_new_def = |item: hir::ModuleDef| {
|
let mut push_new_def = |item: hir::ModuleDef| {
|
||||||
if !targets.contains(&item) {
|
if !targets.contains(&item) {
|
||||||
|
@ -454,6 +503,13 @@ fn goto_type_action_for_def(db: &RootDatabase, def: Definition) -> Option<HoverA
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
for &(trait_, ref assocs) in notable_traits {
|
||||||
|
push_new_def(trait_.into());
|
||||||
|
assocs.iter().filter_map(|(ty, _)| ty.as_ref()).for_each(|ty| {
|
||||||
|
walk_and_push_ty(db, ty, &mut push_new_def);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if let Definition::GenericParam(hir::GenericParam::TypeParam(it)) = def {
|
if let Definition::GenericParam(hir::GenericParam::TypeParam(it)) = def {
|
||||||
let krate = it.module(db).krate();
|
let krate = it.module(db).krate();
|
||||||
let sized_trait =
|
let sized_trait =
|
||||||
|
@ -469,13 +525,13 @@ fn goto_type_action_for_def(db: &RootDatabase, def: Definition) -> Option<HoverA
|
||||||
Definition::GenericParam(hir::GenericParam::ConstParam(it)) => it.ty(db),
|
Definition::GenericParam(hir::GenericParam::ConstParam(it)) => it.ty(db),
|
||||||
Definition::Field(field) => field.ty(db),
|
Definition::Field(field) => field.ty(db),
|
||||||
Definition::Function(function) => function.ret_type(db),
|
Definition::Function(function) => function.ret_type(db),
|
||||||
_ => return None,
|
_ => return HoverAction::goto_type_from_targets(db, targets),
|
||||||
};
|
};
|
||||||
|
|
||||||
walk_and_push_ty(db, &ty, &mut push_new_def);
|
walk_and_push_ty(db, &ty, &mut push_new_def);
|
||||||
}
|
}
|
||||||
|
|
||||||
Some(HoverAction::goto_type_from_targets(db, targets))
|
HoverAction::goto_type_from_targets(db, targets)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn walk_and_push_ty(
|
fn walk_and_push_ty(
|
||||||
|
@ -530,7 +586,9 @@ fn dedupe_or_merge_hover_actions(actions: Vec<HoverAction>) -> Vec<HoverAction>
|
||||||
}
|
}
|
||||||
|
|
||||||
if !go_to_type_targets.is_empty() {
|
if !go_to_type_targets.is_empty() {
|
||||||
deduped_actions.push(HoverAction::GoToType(go_to_type_targets.into_iter().collect()));
|
deduped_actions.push(HoverAction::GoToType(
|
||||||
|
go_to_type_targets.into_iter().sorted_by(|a, b| a.mod_path.cmp(&b.mod_path)).collect(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
deduped_actions
|
deduped_actions
|
||||||
|
|
|
@ -3,8 +3,8 @@ use std::{mem, ops::Not};
|
||||||
|
|
||||||
use either::Either;
|
use either::Either;
|
||||||
use hir::{
|
use hir::{
|
||||||
db::DefDatabase, Adt, AsAssocItem, AssocItem, CaptureKind, HasCrate, HasSource, HirDisplay,
|
Adt, AsAssocItem, CaptureKind, HasCrate, HasSource, HirDisplay, Layout, LayoutError, Name,
|
||||||
Layout, LayoutError, Semantics, TypeInfo,
|
Semantics, Trait, Type, TypeInfo,
|
||||||
};
|
};
|
||||||
use ide_db::{
|
use ide_db::{
|
||||||
base_db::SourceDatabase,
|
base_db::SourceDatabase,
|
||||||
|
@ -25,7 +25,7 @@ use syntax::{
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
doc_links::{remove_links, rewrite_links},
|
doc_links::{remove_links, rewrite_links},
|
||||||
hover::walk_and_push_ty,
|
hover::{notable_traits, walk_and_push_ty},
|
||||||
HoverAction, HoverConfig, HoverResult, Markup, MemoryLayoutHoverConfig,
|
HoverAction, HoverConfig, HoverResult, Markup, MemoryLayoutHoverConfig,
|
||||||
MemoryLayoutHoverRenderKind,
|
MemoryLayoutHoverRenderKind,
|
||||||
};
|
};
|
||||||
|
@ -117,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, &inner_ty, &mut push_new_def);
|
||||||
walk_and_push_ty(sema.db, &body_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 inner_ty = inner_ty.display(sema.db).to_string();
|
||||||
let body_ty = body_ty.display(sema.db).to_string();
|
let body_ty = body_ty.display(sema.db).to_string();
|
||||||
|
@ -195,7 +197,9 @@ pub(super) fn deref_expr(
|
||||||
)
|
)
|
||||||
.into()
|
.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)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
@ -302,7 +306,9 @@ pub(super) fn struct_rest_pat(
|
||||||
|
|
||||||
Markup::fenced_block(&s)
|
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
|
res
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -388,6 +394,7 @@ pub(super) fn definition(
|
||||||
db: &RootDatabase,
|
db: &RootDatabase,
|
||||||
def: Definition,
|
def: Definition,
|
||||||
famous_defs: Option<&FamousDefs<'_, '_>>,
|
famous_defs: Option<&FamousDefs<'_, '_>>,
|
||||||
|
notable_traits: &[(Trait, Vec<(Option<Type>, Name)>)],
|
||||||
config: &HoverConfig,
|
config: &HoverConfig,
|
||||||
) -> Option<Markup> {
|
) -> Option<Markup> {
|
||||||
let mod_path = definition_mod_path(db, &def);
|
let mod_path = definition_mod_path(db, &def);
|
||||||
|
@ -464,61 +471,8 @@ pub(super) fn definition(
|
||||||
_ => None,
|
_ => None,
|
||||||
};
|
};
|
||||||
|
|
||||||
let def_ty = match def {
|
|
||||||
Definition::Local(it) => Some(it.ty(db)),
|
|
||||||
Definition::GenericParam(hir::GenericParam::ConstParam(it)) => Some(it.ty(db)),
|
|
||||||
Definition::GenericParam(hir::GenericParam::TypeParam(it)) => Some(it.ty(db)),
|
|
||||||
Definition::Field(field) => Some(field.ty(db)),
|
|
||||||
Definition::TupleField(it) => Some(it.ty(db)),
|
|
||||||
Definition::Function(it) => Some(it.ty(db)),
|
|
||||||
Definition::Adt(it) => Some(it.ty(db)),
|
|
||||||
Definition::Const(it) => Some(it.ty(db)),
|
|
||||||
Definition::Static(it) => Some(it.ty(db)),
|
|
||||||
Definition::TypeAlias(it) => Some(it.ty(db)),
|
|
||||||
Definition::BuiltinType(it) => Some(it.ty(db)),
|
|
||||||
_ => None,
|
|
||||||
};
|
|
||||||
let notable_traits = def_ty.and_then(|ty| {
|
|
||||||
let mut desc = String::new();
|
let mut desc = String::new();
|
||||||
let mut needs_impl_header = true;
|
if let Some(notable_traits) = render_notable_trait_comment(db, notable_traits) {
|
||||||
for &trait_ in db.notable_traits_in_deps(ty.krate(db).into()).iter().flat_map(|it| &**it) {
|
|
||||||
let trait_ = trait_.into();
|
|
||||||
if ty.impls_trait(db, trait_, &[]) {
|
|
||||||
let aliases: Vec<_> = trait_
|
|
||||||
.items(db)
|
|
||||||
.into_iter()
|
|
||||||
.filter_map(AssocItem::as_type_alias)
|
|
||||||
.map(|alias| (ty.normalize_trait_assoc_type(db, &[], alias), alias.name(db)))
|
|
||||||
.collect();
|
|
||||||
desc.push_str(if mem::take(&mut needs_impl_header) {
|
|
||||||
" // notable traits impls: "
|
|
||||||
} else {
|
|
||||||
", "
|
|
||||||
});
|
|
||||||
format_to!(desc, "{}", trait_.name(db).display(db),);
|
|
||||||
if !aliases.is_empty() {
|
|
||||||
desc.push('<');
|
|
||||||
format_to!(
|
|
||||||
desc,
|
|
||||||
"{}",
|
|
||||||
aliases.into_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(|| desc)
|
|
||||||
});
|
|
||||||
|
|
||||||
let mut desc = String::new();
|
|
||||||
if let Some(notable_traits) = notable_traits {
|
|
||||||
desc.push_str(¬able_traits);
|
desc.push_str(¬able_traits);
|
||||||
desc.push('\n');
|
desc.push('\n');
|
||||||
}
|
}
|
||||||
|
@ -535,6 +489,39 @@ pub(super) fn definition(
|
||||||
markup(docs.map(Into::into), desc, 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) {
|
||||||
|
" // notable traits implemented: "
|
||||||
|
} 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('>');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
desc.is_empty().not().then(|| desc)
|
||||||
|
}
|
||||||
|
|
||||||
fn type_info(
|
fn type_info(
|
||||||
sema: &Semantics<'_, RootDatabase>,
|
sema: &Semantics<'_, RootDatabase>,
|
||||||
config: &HoverConfig,
|
config: &HoverConfig,
|
||||||
|
@ -552,8 +539,12 @@ fn type_info(
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
walk_and_push_ty(sema.db, &original, &mut push_new_def);
|
walk_and_push_ty(sema.db, &original, &mut push_new_def);
|
||||||
|
let mut desc = match render_notable_trait_comment(sema.db, ¬able_traits(sema.db, &original))
|
||||||
res.markup = if let Some(adjusted_ty) = adjusted {
|
{
|
||||||
|
Some(desc) => desc + "\n",
|
||||||
|
None => String::new(),
|
||||||
|
};
|
||||||
|
desc += &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);
|
||||||
let original = original.display(sema.db).to_string();
|
let original = original.display(sema.db).to_string();
|
||||||
let adjusted = adjusted_ty.display(sema.db).to_string();
|
let adjusted = adjusted_ty.display(sema.db).to_string();
|
||||||
|
@ -565,11 +556,13 @@ fn type_info(
|
||||||
apad = static_text_diff_len + adjusted.len().max(original.len()),
|
apad = static_text_diff_len + adjusted.len().max(original.len()),
|
||||||
opad = original.len(),
|
opad = original.len(),
|
||||||
)
|
)
|
||||||
.into()
|
|
||||||
} else {
|
} else {
|
||||||
Markup::fenced_block(&original.display(sema.db))
|
Markup::fenced_block(&original.display(sema.db)).into()
|
||||||
};
|
};
|
||||||
res.actions.push(HoverAction::goto_type_from_targets(sema.db, targets));
|
res.markup = desc.into();
|
||||||
|
if let Some(actions) = HoverAction::goto_type_from_targets(sema.db, targets) {
|
||||||
|
res.actions.push(actions);
|
||||||
|
}
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -621,6 +614,9 @@ fn closure_ty(
|
||||||
{
|
{
|
||||||
format_to!(markup, "{layout}");
|
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!(
|
format_to!(
|
||||||
markup,
|
markup,
|
||||||
"\n{}\n```{adjusted}\n\n## Captures\n{}",
|
"\n{}\n```{adjusted}\n\n## Captures\n{}",
|
||||||
|
@ -629,7 +625,9 @@ fn closure_ty(
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut res = HoverResult::default();
|
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();
|
res.markup = markup.into();
|
||||||
Some(res)
|
Some(res)
|
||||||
}
|
}
|
||||||
|
@ -783,7 +781,9 @@ fn keyword_hints(
|
||||||
KeywordHint {
|
KeywordHint {
|
||||||
description,
|
description,
|
||||||
keyword_mod,
|
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 {
|
_ => KeywordHint {
|
||||||
|
|
|
@ -400,6 +400,20 @@ fn main() {
|
||||||
description: "struct S",
|
description: "struct S",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "core::ops::function::FnOnce",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
full_range: 631..866,
|
||||||
|
focus_range: 692..698,
|
||||||
|
name: "FnOnce",
|
||||||
|
kind: Trait,
|
||||||
|
container_name: "function",
|
||||||
|
description: "pub trait FnOnce<Args>\nwhere\n Args: Tuple,",
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
@ -2390,19 +2404,6 @@ fn main() { let s$0t = S{ f1:Arg(0) }; }
|
||||||
[
|
[
|
||||||
GoToType(
|
GoToType(
|
||||||
[
|
[
|
||||||
HoverGotoTypeData {
|
|
||||||
mod_path: "test::S",
|
|
||||||
nav: NavigationTarget {
|
|
||||||
file_id: FileId(
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
full_range: 17..37,
|
|
||||||
focus_range: 24..25,
|
|
||||||
name: "S",
|
|
||||||
kind: Struct,
|
|
||||||
description: "struct S<T> {\n f1: T,\n}",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
HoverGotoTypeData {
|
HoverGotoTypeData {
|
||||||
mod_path: "test::Arg",
|
mod_path: "test::Arg",
|
||||||
nav: NavigationTarget {
|
nav: NavigationTarget {
|
||||||
|
@ -2416,6 +2417,19 @@ fn main() { let s$0t = S{ f1:Arg(0) }; }
|
||||||
description: "struct Arg(u32);",
|
description: "struct Arg(u32);",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "test::S",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
full_range: 17..37,
|
||||||
|
focus_range: 24..25,
|
||||||
|
name: "S",
|
||||||
|
kind: Struct,
|
||||||
|
description: "struct S<T> {\n f1: T,\n}",
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
@ -2449,19 +2463,6 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
|
||||||
[
|
[
|
||||||
GoToType(
|
GoToType(
|
||||||
[
|
[
|
||||||
HoverGotoTypeData {
|
|
||||||
mod_path: "test::S",
|
|
||||||
nav: NavigationTarget {
|
|
||||||
file_id: FileId(
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
full_range: 17..37,
|
|
||||||
focus_range: 24..25,
|
|
||||||
name: "S",
|
|
||||||
kind: Struct,
|
|
||||||
description: "struct S<T> {\n f1: T,\n}",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
HoverGotoTypeData {
|
HoverGotoTypeData {
|
||||||
mod_path: "test::Arg",
|
mod_path: "test::Arg",
|
||||||
nav: NavigationTarget {
|
nav: NavigationTarget {
|
||||||
|
@ -2475,6 +2476,19 @@ fn main() { let s$0t = S{ f1: S{ f1: Arg(0) } }; }
|
||||||
description: "struct Arg(u32);",
|
description: "struct Arg(u32);",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "test::S",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
full_range: 17..37,
|
||||||
|
focus_range: 24..25,
|
||||||
|
name: "S",
|
||||||
|
kind: Struct,
|
||||||
|
description: "struct S<T> {\n f1: T,\n}",
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
@ -2639,19 +2653,6 @@ fn main() { let s$0t = foo(); }
|
||||||
[
|
[
|
||||||
GoToType(
|
GoToType(
|
||||||
[
|
[
|
||||||
HoverGotoTypeData {
|
|
||||||
mod_path: "test::Foo",
|
|
||||||
nav: NavigationTarget {
|
|
||||||
file_id: FileId(
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
full_range: 0..12,
|
|
||||||
focus_range: 6..9,
|
|
||||||
name: "Foo",
|
|
||||||
kind: Trait,
|
|
||||||
description: "trait Foo",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
HoverGotoTypeData {
|
HoverGotoTypeData {
|
||||||
mod_path: "test::Bar",
|
mod_path: "test::Bar",
|
||||||
nav: NavigationTarget {
|
nav: NavigationTarget {
|
||||||
|
@ -2665,6 +2666,19 @@ fn main() { let s$0t = foo(); }
|
||||||
description: "trait Bar",
|
description: "trait Bar",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "test::Foo",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
full_range: 0..12,
|
||||||
|
focus_range: 6..9,
|
||||||
|
name: "Foo",
|
||||||
|
kind: Trait,
|
||||||
|
description: "trait Foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
]
|
]
|
||||||
|
@ -2689,19 +2703,6 @@ fn main() { let s$0t = foo(); }
|
||||||
[
|
[
|
||||||
GoToType(
|
GoToType(
|
||||||
[
|
[
|
||||||
HoverGotoTypeData {
|
|
||||||
mod_path: "test::Foo",
|
|
||||||
nav: NavigationTarget {
|
|
||||||
file_id: FileId(
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
full_range: 0..15,
|
|
||||||
focus_range: 6..9,
|
|
||||||
name: "Foo",
|
|
||||||
kind: Trait,
|
|
||||||
description: "trait Foo<T>",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
HoverGotoTypeData {
|
HoverGotoTypeData {
|
||||||
mod_path: "test::Bar",
|
mod_path: "test::Bar",
|
||||||
nav: NavigationTarget {
|
nav: NavigationTarget {
|
||||||
|
@ -2715,6 +2716,19 @@ fn main() { let s$0t = foo(); }
|
||||||
description: "trait Bar<T>",
|
description: "trait Bar<T>",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "test::Foo",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
full_range: 0..15,
|
||||||
|
focus_range: 6..9,
|
||||||
|
name: "Foo",
|
||||||
|
kind: Trait,
|
||||||
|
description: "trait Foo<T>",
|
||||||
|
},
|
||||||
|
},
|
||||||
HoverGotoTypeData {
|
HoverGotoTypeData {
|
||||||
mod_path: "test::S1",
|
mod_path: "test::S1",
|
||||||
nav: NavigationTarget {
|
nav: NavigationTarget {
|
||||||
|
@ -2793,19 +2807,6 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
|
||||||
[
|
[
|
||||||
GoToType(
|
GoToType(
|
||||||
[
|
[
|
||||||
HoverGotoTypeData {
|
|
||||||
mod_path: "test::Foo",
|
|
||||||
nav: NavigationTarget {
|
|
||||||
file_id: FileId(
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
full_range: 0..12,
|
|
||||||
focus_range: 6..9,
|
|
||||||
name: "Foo",
|
|
||||||
kind: Trait,
|
|
||||||
description: "trait Foo",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
HoverGotoTypeData {
|
HoverGotoTypeData {
|
||||||
mod_path: "test::Bar",
|
mod_path: "test::Bar",
|
||||||
nav: NavigationTarget {
|
nav: NavigationTarget {
|
||||||
|
@ -2819,6 +2820,19 @@ fn foo(ar$0g: &impl Foo + Bar<S>) {}
|
||||||
description: "trait Bar<T>",
|
description: "trait Bar<T>",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "test::Foo",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
full_range: 0..12,
|
||||||
|
focus_range: 6..9,
|
||||||
|
name: "Foo",
|
||||||
|
kind: Trait,
|
||||||
|
description: "trait Foo",
|
||||||
|
},
|
||||||
|
},
|
||||||
HoverGotoTypeData {
|
HoverGotoTypeData {
|
||||||
mod_path: "test::S",
|
mod_path: "test::S",
|
||||||
nav: NavigationTarget {
|
nav: NavigationTarget {
|
||||||
|
@ -3080,19 +3094,6 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
|
||||||
[
|
[
|
||||||
GoToType(
|
GoToType(
|
||||||
[
|
[
|
||||||
HoverGotoTypeData {
|
|
||||||
mod_path: "test::ImplTrait",
|
|
||||||
nav: NavigationTarget {
|
|
||||||
file_id: FileId(
|
|
||||||
0,
|
|
||||||
),
|
|
||||||
full_range: 0..21,
|
|
||||||
focus_range: 6..15,
|
|
||||||
name: "ImplTrait",
|
|
||||||
kind: Trait,
|
|
||||||
description: "trait ImplTrait<T>",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
HoverGotoTypeData {
|
HoverGotoTypeData {
|
||||||
mod_path: "test::B",
|
mod_path: "test::B",
|
||||||
nav: NavigationTarget {
|
nav: NavigationTarget {
|
||||||
|
@ -3119,6 +3120,19 @@ fn foo(a$0rg: &impl ImplTrait<B<dyn DynTrait<B<S>>>>) {}
|
||||||
description: "trait DynTrait<T>",
|
description: "trait DynTrait<T>",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "test::ImplTrait",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
full_range: 0..21,
|
||||||
|
focus_range: 6..15,
|
||||||
|
name: "ImplTrait",
|
||||||
|
kind: Trait,
|
||||||
|
description: "trait ImplTrait<T>",
|
||||||
|
},
|
||||||
|
},
|
||||||
HoverGotoTypeData {
|
HoverGotoTypeData {
|
||||||
mod_path: "test::S",
|
mod_path: "test::S",
|
||||||
nav: NavigationTarget {
|
nav: NavigationTarget {
|
||||||
|
@ -6989,7 +7003,7 @@ fn main() {
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
*34325236457856836345234*
|
*34325236457856836345234*
|
||||||
```text
|
```text
|
||||||
34325236457856836345234 (0x744C659178614489D92|0x111010001001100011001011001000101111000011000010100010010001001110110010010)
|
34325236457856836345234 (0x744C659178614489D92|0b111010001001100011001011001000101111000011000010100010010001001110110010010)
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
|
@ -7002,7 +7016,7 @@ fn main() {
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
*134_123424_21*
|
*134_123424_21*
|
||||||
```text
|
```text
|
||||||
13412342421 (0x31F701A95|0x1100011111011100000001101010010101)
|
13412342421 (0x31F701A95|0b1100011111011100000001101010010101)
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
|
@ -7015,7 +7029,7 @@ fn main() {
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
*0x12423423*
|
*0x12423423*
|
||||||
```text
|
```text
|
||||||
306328611 (0x12423423|0x10010010000100011010000100011)
|
306328611 (0x12423423|0b10010010000100011010000100011)
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
|
@ -7028,7 +7042,7 @@ fn main() {
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
*0b1111_1111*
|
*0b1111_1111*
|
||||||
```text
|
```text
|
||||||
255 (0xFF|0x11111111)
|
255 (0xFF|0b11111111)
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
|
@ -7041,7 +7055,7 @@ fn main() {
|
||||||
expect![[r#"
|
expect![[r#"
|
||||||
*0o12345*
|
*0o12345*
|
||||||
```text
|
```text
|
||||||
5349 (0x14E5|0x1010011100101)
|
5349 (0x14E5|0b1010011100101)
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
|
@ -7080,7 +7094,7 @@ fn main(notable$0: u32) {}
|
||||||
*notable*
|
*notable*
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
// notable traits impls: Notable<Assoc = &str, Assoc2 = char>
|
// notable traits implemented: Notable<Assoc = &str, Assoc2 = char>
|
||||||
// size = 4, align = 4
|
// size = 4, align = 4
|
||||||
notable: u32
|
notable: u32
|
||||||
```
|
```
|
||||||
|
@ -7112,10 +7126,127 @@ impl Iterator for S {
|
||||||
```
|
```
|
||||||
|
|
||||||
```rust
|
```rust
|
||||||
// notable traits impls: Notable, Future<Output = u32>, Iterator<Item = S>
|
// notable traits implemented: Notable, Future<Output = u32>, Iterator<Item = S>
|
||||||
// size = 0, align = 1
|
// size = 0, align = 1
|
||||||
struct S
|
struct S
|
||||||
```
|
```
|
||||||
"#]],
|
"#]],
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn notable_ranged() {
|
||||||
|
check_hover_range(
|
||||||
|
r#"
|
||||||
|
//- minicore: future, iterator
|
||||||
|
struct S;
|
||||||
|
#[doc(notable_trait)]
|
||||||
|
trait Notable {}
|
||||||
|
impl Notable for S {}
|
||||||
|
impl core::future::Future for S {
|
||||||
|
type Output = u32;
|
||||||
|
}
|
||||||
|
impl Iterator for S {
|
||||||
|
type Item = S;
|
||||||
|
}
|
||||||
|
fn main() {
|
||||||
|
$0S$0;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
// notable traits implemented: Notable, Future<Output = u32>, Iterator<Item = S>
|
||||||
|
```rust
|
||||||
|
S
|
||||||
|
```"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn notable_actions() {
|
||||||
|
check_actions(
|
||||||
|
r#"
|
||||||
|
//- minicore: future, iterator
|
||||||
|
struct S;
|
||||||
|
struct S2;
|
||||||
|
#[doc(notable_trait)]
|
||||||
|
trait Notable {}
|
||||||
|
impl Notable for S$0 {}
|
||||||
|
impl core::future::Future for S {
|
||||||
|
type Output = u32;
|
||||||
|
}
|
||||||
|
impl Iterator for S {
|
||||||
|
type Item = S2;
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
expect![[r#"
|
||||||
|
[
|
||||||
|
Implementation(
|
||||||
|
FilePosition {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
offset: 7,
|
||||||
|
},
|
||||||
|
),
|
||||||
|
GoToType(
|
||||||
|
[
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "core::future::Future",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
full_range: 6012..6220,
|
||||||
|
focus_range: 6077..6083,
|
||||||
|
name: "Future",
|
||||||
|
kind: Trait,
|
||||||
|
container_name: "future",
|
||||||
|
description: "pub trait Future",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "core::iter::traits::iterator::Iterator",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
1,
|
||||||
|
),
|
||||||
|
full_range: 6850..7316,
|
||||||
|
focus_range: 6894..6902,
|
||||||
|
name: "Iterator",
|
||||||
|
kind: Trait,
|
||||||
|
container_name: "iterator",
|
||||||
|
description: "pub trait Iterator",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "test::Notable",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
full_range: 21..59,
|
||||||
|
focus_range: 49..56,
|
||||||
|
name: "Notable",
|
||||||
|
kind: Trait,
|
||||||
|
description: "trait Notable",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
HoverGotoTypeData {
|
||||||
|
mod_path: "test::S2",
|
||||||
|
nav: NavigationTarget {
|
||||||
|
file_id: FileId(
|
||||||
|
0,
|
||||||
|
),
|
||||||
|
full_range: 10..20,
|
||||||
|
focus_range: 17..19,
|
||||||
|
name: "S2",
|
||||||
|
kind: Struct,
|
||||||
|
description: "struct S2",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
),
|
||||||
|
]
|
||||||
|
"#]],
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue