From 0df1153277cc581533c97ccf826231898eb214ed Mon Sep 17 00:00:00 2001 From: Ali Bektas Date: Tue, 13 Jun 2023 14:42:33 +0200 Subject: [PATCH] bugfix : skip doc(hidden) default members fixes #14957 --- .../src/handlers/add_missing_impl_members.rs | 39 +++++++++++++++++++ .../replace_derive_with_manual_impl.rs | 2 +- crates/ide-assists/src/utils.rs | 36 +++++++++++++++-- 3 files changed, 72 insertions(+), 5 deletions(-) diff --git a/crates/ide-assists/src/handlers/add_missing_impl_members.rs b/crates/ide-assists/src/handlers/add_missing_impl_members.rs index d07c637262..ecf1abc277 100644 --- a/crates/ide-assists/src/handlers/add_missing_impl_members.rs +++ b/crates/ide-assists/src/handlers/add_missing_impl_members.rs @@ -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 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 + } +}"#, + ) + } } diff --git a/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs b/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs index c03bc2f41d..4fa3394233 100644 --- a/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs +++ b/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs @@ -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; } diff --git a/crates/ide-assists/src/utils.rs b/crates/ide-assists/src/utils.rs index 03d8553506..e4d242815e 100644 --- a/crates/ide-assists/src/utils.rs +++ b/crates/ide-assists/src/utils.rs @@ -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> { + // 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) })