@@ -19,7 +19,7 @@ use syntax::{
19
19
make, HasName , HasVisibility ,
20
20
} ,
21
21
match_ast, ted, AstNode , SourceFile ,
22
- SyntaxKind :: WHITESPACE ,
22
+ SyntaxKind :: { self , WHITESPACE } ,
23
23
SyntaxNode , TextRange ,
24
24
} ;
25
25
@@ -380,7 +380,16 @@ impl Module {
380
380
}
381
381
382
382
for ( vis, syntax) in replacements {
383
- add_change_vis ( vis, syntax. first_child_or_token ( ) ) ;
383
+ let item = syntax. children_with_tokens ( ) . find ( |node_or_token| {
384
+ match node_or_token. kind ( ) {
385
+ // We're skipping comments, doc comments, and attribute macros that may precede the keyword
386
+ // that the visibility should be placed before.
387
+ SyntaxKind :: COMMENT | SyntaxKind :: ATTR | SyntaxKind :: WHITESPACE => false ,
388
+ _ => true ,
389
+ }
390
+ } ) ;
391
+
392
+ add_change_vis ( vis, item) ;
384
393
}
385
394
}
386
395
@@ -1581,4 +1590,181 @@ mod modname {
1581
1590
" ,
1582
1591
)
1583
1592
}
1593
+
1594
+ #[ test]
1595
+ fn test_issue_12790 ( ) {
1596
+ check_assist (
1597
+ extract_module,
1598
+ r"
1599
+ $0/// A documented function
1600
+ fn documented_fn() {}
1601
+
1602
+ // A commented function with a #[] attribute macro
1603
+ #[cfg(test)]
1604
+ fn attribute_fn() {}
1605
+
1606
+ // A normally commented function
1607
+ fn normal_fn() {}
1608
+
1609
+ /// A documented Struct
1610
+ struct DocumentedStruct {
1611
+ // Normal field
1612
+ x: i32,
1613
+
1614
+ /// Documented field
1615
+ y: i32,
1616
+
1617
+ // Macroed field
1618
+ #[cfg(test)]
1619
+ z: i32,
1620
+ }
1621
+
1622
+ // A macroed Struct
1623
+ #[cfg(test)]
1624
+ struct MacroedStruct {
1625
+ // Normal field
1626
+ x: i32,
1627
+
1628
+ /// Documented field
1629
+ y: i32,
1630
+
1631
+ // Macroed field
1632
+ #[cfg(test)]
1633
+ z: i32,
1634
+ }
1635
+
1636
+ // A normal Struct
1637
+ struct NormalStruct {
1638
+ // Normal field
1639
+ x: i32,
1640
+
1641
+ /// Documented field
1642
+ y: i32,
1643
+
1644
+ // Macroed field
1645
+ #[cfg(test)]
1646
+ z: i32,
1647
+ }
1648
+
1649
+ /// A documented type
1650
+ type DocumentedType = i32;
1651
+
1652
+ // A macroed type
1653
+ #[cfg(test)]
1654
+ type MacroedType = i32;
1655
+
1656
+ /// A module to move
1657
+ mod module {}
1658
+
1659
+ /// An impl to move
1660
+ impl NormalStruct {
1661
+ /// A method
1662
+ fn new() {}
1663
+ }
1664
+
1665
+ /// A documented trait
1666
+ trait DocTrait {
1667
+ /// Inner function
1668
+ fn doc() {}
1669
+ }
1670
+
1671
+ /// An enum
1672
+ enum DocumentedEnum {
1673
+ /// A variant
1674
+ A,
1675
+ /// Another variant
1676
+ B { x: i32, y: i32 }
1677
+ }
1678
+
1679
+ /// Documented const
1680
+ const MY_CONST: i32 = 0;$0
1681
+ " ,
1682
+ r"
1683
+ mod modname {
1684
+ /// A documented function
1685
+ pub(crate) fn documented_fn() {}
1686
+
1687
+ // A commented function with a #[] attribute macro
1688
+ #[cfg(test)]
1689
+ pub(crate) fn attribute_fn() {}
1690
+
1691
+ // A normally commented function
1692
+ pub(crate) fn normal_fn() {}
1693
+
1694
+ /// A documented Struct
1695
+ pub(crate) struct DocumentedStruct {
1696
+ // Normal field
1697
+ pub(crate) x: i32,
1698
+
1699
+ /// Documented field
1700
+ pub(crate) y: i32,
1701
+
1702
+ // Macroed field
1703
+ #[cfg(test)]
1704
+ pub(crate) z: i32,
1705
+ }
1706
+
1707
+ // A macroed Struct
1708
+ #[cfg(test)]
1709
+ pub(crate) struct MacroedStruct {
1710
+ // Normal field
1711
+ pub(crate) x: i32,
1712
+
1713
+ /// Documented field
1714
+ pub(crate) y: i32,
1715
+
1716
+ // Macroed field
1717
+ #[cfg(test)]
1718
+ pub(crate) z: i32,
1719
+ }
1720
+
1721
+ // A normal Struct
1722
+ pub(crate) struct NormalStruct {
1723
+ // Normal field
1724
+ pub(crate) x: i32,
1725
+
1726
+ /// Documented field
1727
+ pub(crate) y: i32,
1728
+
1729
+ // Macroed field
1730
+ #[cfg(test)]
1731
+ pub(crate) z: i32,
1732
+ }
1733
+
1734
+ /// A documented type
1735
+ pub(crate) type DocumentedType = i32;
1736
+
1737
+ // A macroed type
1738
+ #[cfg(test)]
1739
+ pub(crate) type MacroedType = i32;
1740
+
1741
+ /// A module to move
1742
+ pub(crate) mod module {}
1743
+
1744
+ /// An impl to move
1745
+ impl NormalStruct {
1746
+ /// A method
1747
+ pub(crate) fn new() {}
1748
+ }
1749
+
1750
+ /// A documented trait
1751
+ pub(crate) trait DocTrait {
1752
+ /// Inner function
1753
+ fn doc() {}
1754
+ }
1755
+
1756
+ /// An enum
1757
+ pub(crate) enum DocumentedEnum {
1758
+ /// A variant
1759
+ A,
1760
+ /// Another variant
1761
+ B { x: i32, y: i32 }
1762
+ }
1763
+
1764
+ /// Documented const
1765
+ pub(crate) const MY_CONST: i32 = 0;
1766
+ }
1767
+ " ,
1768
+ )
1769
+ }
1584
1770
}
0 commit comments