Skip to content

Commit

Permalink
bugfix : skip doc(hidden) default members
Browse files Browse the repository at this point in the history
fixes  #14957
  • Loading branch information
alibektas committed Jun 13, 2023
1 parent 1f1fe81 commit 6a3dc68
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 5 deletions.
39 changes: 39 additions & 0 deletions crates/ide-assists/src/handlers/add_missing_impl_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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",
)
Expand Down Expand Up @@ -87,6 +88,7 @@ pub(crate) fn add_missing_default_members(
acc,
ctx,
DefaultMethods::Only,
true,
"add_impl_default_members",
"Implement default members",
)
Expand All @@ -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<()> {
Expand All @@ -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() {
Expand Down Expand Up @@ -1791,4 +1795,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
}
}"#,
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
36 changes: 32 additions & 4 deletions crates/ide-assists/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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)
})
Expand Down

0 comments on commit 6a3dc68

Please sign in to comment.