Skip to content

Commit 27b65ec

Browse files
committed
Add test case and token finder to address 12790
1 parent 567a5e9 commit 27b65ec

File tree

1 file changed

+144
-2
lines changed

1 file changed

+144
-2
lines changed

crates/ide-assists/src/handlers/extract_module.rs

Lines changed: 144 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use syntax::{
1919
make, HasName, HasVisibility,
2020
},
2121
match_ast, ted, AstNode, SourceFile,
22-
SyntaxKind::WHITESPACE,
22+
SyntaxKind::{self, WHITESPACE},
2323
SyntaxNode, TextRange,
2424
};
2525

@@ -380,7 +380,24 @@ impl Module {
380380
}
381381

382382
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 looking for the start of functions, impls, structs, traits, and other documentable/attribute
386+
// macroable items that would have pub(crate) in front of it
387+
SyntaxKind::FN_KW
388+
| SyntaxKind::IMPL_KW
389+
| SyntaxKind::STRUCT_KW
390+
| SyntaxKind::TRAIT_KW
391+
| SyntaxKind::TYPE_KW
392+
| SyntaxKind::MOD_KW => true,
393+
// If we didn't find a keyword, we want to cover the record fields
394+
SyntaxKind::NAME => true,
395+
// Otherwise, the token shouldn't have pub(crate) before it
396+
_ => false,
397+
}
398+
});
399+
400+
add_change_vis(vis, item);
384401
}
385402
}
386403

@@ -1581,4 +1598,129 @@ mod modname {
15811598
",
15821599
)
15831600
}
1601+
1602+
#[test]
1603+
fn test_issue_12790() {
1604+
check_assist(
1605+
extract_module,
1606+
r"
1607+
$0/// A documented function
1608+
fn documented_fn() {}
1609+
1610+
// A commented function with a #[] attribute macro
1611+
#[cfg(test)]
1612+
fn attribute_fn() {}
1613+
1614+
// A normally commented function
1615+
fn normal_fn() {}
1616+
1617+
/// A documented Struct
1618+
struct DocumentedStruct {
1619+
// Normal field
1620+
x: i32,
1621+
1622+
/// Documented field
1623+
y: i32,
1624+
1625+
// Macroed field
1626+
#[cfg(test)]
1627+
z: i32,
1628+
}
1629+
1630+
// A macroed Struct
1631+
#[cfg(test)]
1632+
struct MacroedStruct {
1633+
// Normal field
1634+
x: i32,
1635+
1636+
/// Documented field
1637+
y: i32,
1638+
1639+
// Macroed field
1640+
#[cfg(test)]
1641+
z: i32,
1642+
}
1643+
1644+
// A normal Struct
1645+
struct NormalStruct {
1646+
// Normal field
1647+
x: i32,
1648+
1649+
/// Documented field
1650+
y: i32,
1651+
1652+
// Macroed field
1653+
#[cfg(test)]
1654+
z: i32,
1655+
}
1656+
1657+
/// A documented type
1658+
type DocumentedType = i32;
1659+
1660+
// A macroed type
1661+
#[cfg(test)]
1662+
type MacroedType = i32;$0
1663+
",
1664+
r"
1665+
mod modname {
1666+
/// A documented function
1667+
pub(crate) fn documented_fn() {}
1668+
1669+
// A commented function with a #[] attribute macro
1670+
#[cfg(test)]
1671+
pub(crate) fn attribute_fn() {}
1672+
1673+
// A normally commented function
1674+
pub(crate) fn normal_fn() {}
1675+
1676+
/// A documented Struct
1677+
pub(crate) struct DocumentedStruct {
1678+
// Normal field
1679+
pub(crate) x: i32,
1680+
1681+
/// Documented field
1682+
pub(crate) y: i32,
1683+
1684+
// Macroed field
1685+
#[cfg(test)]
1686+
pub(crate) z: i32,
1687+
}
1688+
1689+
// A macroed Struct
1690+
#[cfg(test)]
1691+
pub(crate) struct MacroedStruct {
1692+
// Normal field
1693+
pub(crate) x: i32,
1694+
1695+
/// Documented field
1696+
pub(crate) y: i32,
1697+
1698+
// Macroed field
1699+
#[cfg(test)]
1700+
pub(crate) z: i32,
1701+
}
1702+
1703+
// A normal Struct
1704+
pub(crate) struct NormalStruct {
1705+
// Normal field
1706+
pub(crate) x: i32,
1707+
1708+
/// Documented field
1709+
pub(crate) y: i32,
1710+
1711+
// Macroed field
1712+
#[cfg(test)]
1713+
pub(crate) z: i32,
1714+
}
1715+
1716+
/// A documented type
1717+
pub(crate) type DocumentedType = i32;
1718+
1719+
// A macroed type
1720+
#[cfg(test)]
1721+
pub(crate) type MacroedType = i32;
1722+
}
1723+
",
1724+
)
1725+
}
15841726
}

0 commit comments

Comments
 (0)