bugfix : skip doc(hidden) default members

fixes  #14957
This commit is contained in:
Ali Bektas 2023-06-13 14:42:33 +02:00
parent e219999a17
commit 0df1153277
3 changed files with 72 additions and 5 deletions

View file

@ -43,6 +43,7 @@ pub(crate) fn add_missing_impl_members(acc: &mut Assists, ctx: &AssistContext<'_
acc,
ctx,
DefaultMethods::No,
true,
"add_impl_missing_members",
"Implement missing members",
)
@ -87,6 +88,7 @@ pub(crate) fn add_missing_default_members(
acc,
ctx,
DefaultMethods::Only,
true,
"add_impl_default_members",
"Implement default members",
)
@ -96,6 +98,7 @@ fn add_missing_impl_members_inner(
acc: &mut Assists,
ctx: &AssistContext<'_>,
mode: DefaultMethods,
ignore_hidden: bool,
assist_id: &'static str,
label: &'static str,
) -> Option<()> {
@ -119,6 +122,7 @@ fn add_missing_impl_members_inner(
&ctx.sema,
&ide_db::traits::get_missing_assoc_items(&ctx.sema, &impl_def),
mode,
ignore_hidden,
);
if missing_items.is_empty() {
@ -1966,4 +1970,39 @@ impl AnotherTrait<i32> for () {
"#,
);
}
#[test]
fn doc_hidden_default_impls_ignored() {
check_assist(
add_missing_default_members,
r#"
struct Foo;
trait Trait {
#[doc(hidden)]
fn func_with_default_impl() -> u32 {
42
}
fn another_default_impl() -> u32 {
43
}
}
impl Tra$0it for Foo {}"#,
r#"
struct Foo;
trait Trait {
#[doc(hidden)]
fn func_with_default_impl() -> u32 {
42
}
fn another_default_impl() -> u32 {
43
}
}
impl Trait for Foo {
$0fn another_default_impl() -> u32 {
43
}
}"#,
)
}
}

View file

@ -172,7 +172,7 @@ fn impl_def_from_trait(
) -> Option<(ast::Impl, ast::AssocItem)> {
let trait_ = trait_?;
let target_scope = sema.scope(annotated_name.syntax())?;
let trait_items = filter_assoc_items(sema, &trait_.items(sema.db), DefaultMethods::No);
let trait_items = filter_assoc_items(sema, &trait_.items(sema.db), DefaultMethods::No, true);
if trait_items.is_empty() {
return None;
}

View file

@ -3,7 +3,7 @@
use std::ops;
pub(crate) use gen_trait_fn_body::gen_trait_fn_body;
use hir::{db::HirDatabase, HirDisplay, InFile, Semantics};
use hir::{db::HirDatabase, HasAttrs as HirHasAttrs, HirDisplay, InFile, Semantics};
use ide_db::{
famous_defs::FamousDefs, path_transform::PathTransform,
syntax_helpers::insert_whitespace_into_node::insert_ws_into, RootDatabase, SnippetCap,
@ -94,16 +94,44 @@ pub fn filter_assoc_items(
sema: &Semantics<'_, RootDatabase>,
items: &[hir::AssocItem],
default_methods: DefaultMethods,
ignore_hidden: bool,
) -> Vec<InFile<ast::AssocItem>> {
// FIXME : How to use the closure below this so I can write sth like
// sema.source(hide(it)?)?...
// let hide = |it: impl HasAttrs| {
// if default_methods == DefaultMethods::IgnoreHidden && it.attrs(sema.db).has_doc_hidden() {
// None;
// }
// Some(it)
// };
return items
.iter()
// Note: This throws away items with no source.
.copied()
.filter_map(|assoc_item| {
let item = match assoc_item {
hir::AssocItem::Function(it) => sema.source(it)?.map(ast::AssocItem::Fn),
hir::AssocItem::TypeAlias(it) => sema.source(it)?.map(ast::AssocItem::TypeAlias),
hir::AssocItem::Const(it) => sema.source(it)?.map(ast::AssocItem::Const),
hir::AssocItem::Function(it) => {
if ignore_hidden && it.attrs(sema.db).has_doc_hidden() {
return None;
}
sema.source(it)?.map(ast::AssocItem::Fn)
}
hir::AssocItem::TypeAlias(it) => {
if ignore_hidden && it.attrs(sema.db).has_doc_hidden() {
return None;
}
sema.source(it)?.map(ast::AssocItem::TypeAlias)
}
hir::AssocItem::Const(it) => {
if ignore_hidden && it.attrs(sema.db).has_doc_hidden() {
return None;
}
sema.source(it)?.map(ast::AssocItem::Const)
}
};
Some(item)
})