Skip to content

Commit

Permalink
Auto merge of #16766 - roife:fix-issue-15198, r=Veykril
Browse files Browse the repository at this point in the history
fix: keep attributes in assist 'generate_delegate_trait'

fix #15198.

This PR address the issue that `impl` generated by `generate_delegate_trait` doesn't keep attributes.
  • Loading branch information
bors committed Mar 6, 2024
2 parents 767d5d3 + 5c9ce7b commit 52d8ae7
Showing 1 changed file with 76 additions and 2 deletions.
78 changes: 76 additions & 2 deletions crates/ide-assists/src/handlers/generate_delegate_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use syntax::{
ast::{
self,
edit::{self, AstNodeEdit},
make, AssocItem, GenericArgList, GenericParamList, HasGenericParams, HasName,
edit_in_place::AttrsOwnerEdit,
make, AssocItem, GenericArgList, GenericParamList, HasAttrs, HasGenericParams, HasName,
HasTypeBounds, HasVisibility as astHasVisibility, Path, WherePred,
},
ted::{self, Position},
Expand Down Expand Up @@ -619,7 +620,8 @@ fn process_assoc_item(
qual_path_ty: ast::Path,
base_name: &str,
) -> Option<ast::AssocItem> {
match item {
let attrs = item.attrs();
let assoc = match item {
AssocItem::Const(c) => const_assoc_item(c, qual_path_ty),
AssocItem::Fn(f) => func_assoc_item(f, qual_path_ty, base_name),
AssocItem::MacroCall(_) => {
Expand All @@ -628,7 +630,18 @@ fn process_assoc_item(
None
}
AssocItem::TypeAlias(ta) => ty_assoc_item(ta, qual_path_ty),
};
if let Some(assoc) = &assoc {
attrs.for_each(|attr| {
assoc.add_attr(attr.clone());
// fix indentations
if let Some(tok) = attr.syntax().next_sibling_or_token() {
let pos = Position::after(tok);
ted::insert(pos, make::tokens::whitespace(" "));
}
})
}
assoc
}

fn const_assoc_item(item: syntax::ast::Const, qual_path_ty: ast::Path) -> Option<AssocItem> {
Expand Down Expand Up @@ -1703,4 +1716,65 @@ impl some_module::SomeTrait for B {
}"#,
)
}

#[test]
fn test_fn_with_attrs() {
check_assist(
generate_delegate_trait,
r#"
struct A;
trait T {
#[cfg(test)]
fn f(&self, a: u32);
#[cfg(not(test))]
fn f(&self, a: bool);
}
impl T for A {
#[cfg(test)]
fn f(&self, a: u32) {}
#[cfg(not(test))]
fn f(&self, a: bool) {}
}
struct B {
a$0: A,
}
"#,
r#"
struct A;
trait T {
#[cfg(test)]
fn f(&self, a: u32);
#[cfg(not(test))]
fn f(&self, a: bool);
}
impl T for A {
#[cfg(test)]
fn f(&self, a: u32) {}
#[cfg(not(test))]
fn f(&self, a: bool) {}
}
struct B {
a: A,
}
impl T for B {
#[cfg(test)]
fn f(&self, a: u32) {
<A as T>::f(&self.a, a)
}
#[cfg(not(test))]
fn f(&self, a: bool) {
<A as T>::f(&self.a, a)
}
}
"#,
);
}
}

0 comments on commit 52d8ae7

Please sign in to comment.