@@ -16,7 +16,8 @@ use syntax::{
1616 ast:: {
1717 self ,
1818 edit:: { self , AstNodeEdit } ,
19- make, AssocItem , GenericArgList , GenericParamList , HasGenericParams , HasName ,
19+ edit_in_place:: AttrsOwnerEdit ,
20+ make, AssocItem , GenericArgList , GenericParamList , HasAttrs , HasGenericParams , HasName ,
2021 HasTypeBounds , HasVisibility as astHasVisibility, Path , WherePred ,
2122 } ,
2223 ted:: { self , Position } ,
@@ -619,7 +620,8 @@ fn process_assoc_item(
619620 qual_path_ty : ast:: Path ,
620621 base_name : & str ,
621622) -> Option < ast:: AssocItem > {
622- match item {
623+ let attrs = item. attrs ( ) ;
624+ let assoc = match item {
623625 AssocItem :: Const ( c) => const_assoc_item ( c, qual_path_ty) ,
624626 AssocItem :: Fn ( f) => func_assoc_item ( f, qual_path_ty, base_name) ,
625627 AssocItem :: MacroCall ( _) => {
@@ -628,7 +630,18 @@ fn process_assoc_item(
628630 None
629631 }
630632 AssocItem :: TypeAlias ( ta) => ty_assoc_item ( ta, qual_path_ty) ,
633+ } ;
634+ if let Some ( assoc) = & assoc {
635+ attrs. for_each ( |attr| {
636+ assoc. add_attr ( attr. clone ( ) ) ;
637+ // fix indentations
638+ if let Some ( tok) = attr. syntax ( ) . next_sibling_or_token ( ) {
639+ let pos = Position :: after ( tok) ;
640+ ted:: insert ( pos, make:: tokens:: whitespace ( " " ) ) ;
641+ }
642+ } )
631643 }
644+ assoc
632645}
633646
634647fn const_assoc_item ( item : syntax:: ast:: Const , qual_path_ty : ast:: Path ) -> Option < AssocItem > {
@@ -1703,4 +1716,65 @@ impl some_module::SomeTrait for B {
17031716}"# ,
17041717 )
17051718 }
1719+
1720+ #[ test]
1721+ fn test_fn_with_attrs ( ) {
1722+ check_assist (
1723+ generate_delegate_trait,
1724+ r#"
1725+ struct A;
1726+
1727+ trait T {
1728+ #[cfg(test)]
1729+ fn f(&self, a: u32);
1730+ #[cfg(not(test))]
1731+ fn f(&self, a: bool);
1732+ }
1733+
1734+ impl T for A {
1735+ #[cfg(test)]
1736+ fn f(&self, a: u32) {}
1737+ #[cfg(not(test))]
1738+ fn f(&self, a: bool) {}
1739+ }
1740+
1741+ struct B {
1742+ a$0: A,
1743+ }
1744+ "# ,
1745+ r#"
1746+ struct A;
1747+
1748+ trait T {
1749+ #[cfg(test)]
1750+ fn f(&self, a: u32);
1751+ #[cfg(not(test))]
1752+ fn f(&self, a: bool);
1753+ }
1754+
1755+ impl T for A {
1756+ #[cfg(test)]
1757+ fn f(&self, a: u32) {}
1758+ #[cfg(not(test))]
1759+ fn f(&self, a: bool) {}
1760+ }
1761+
1762+ struct B {
1763+ a: A,
1764+ }
1765+
1766+ impl T for B {
1767+ #[cfg(test)]
1768+ fn f(&self, a: u32) {
1769+ <A as T>::f(&self.a, a)
1770+ }
1771+
1772+ #[cfg(not(test))]
1773+ fn f(&self, a: bool) {
1774+ <A as T>::f(&self.a, a)
1775+ }
1776+ }
1777+ "# ,
1778+ ) ;
1779+ }
17061780}
0 commit comments