@@ -1438,3 +1438,156 @@ fn check_guess(#[rust_analyzer::rust_fixture] ra_fixture: &str, expected: Import
14381438 let file = ImportScope { kind : ImportScopeKind :: File ( syntax) , required_cfgs : vec ! [ ] } ;
14391439 assert_eq ! ( super :: guess_granularity_from_scope( & file) , expected) ;
14401440}
1441+
1442+ #[ test]
1443+ fn insert_with_existing_imports_and_cfg_module ( ) {
1444+ check (
1445+ "std::fmt" ,
1446+ r#"
1447+ use foo::bar;
1448+
1449+ #[cfg(target_arch = "x86_64")]
1450+ pub mod api;
1451+ "# ,
1452+ r#"
1453+ use std::fmt;
1454+
1455+ use foo::bar;
1456+
1457+ #[cfg(target_arch = "x86_64")]
1458+ pub mod api;
1459+ "# ,
1460+ ImportGranularity :: Crate ,
1461+ ) ;
1462+ }
1463+
1464+ #[ test]
1465+ fn insert_before_cfg_module ( ) {
1466+ check (
1467+ "std::fmt" ,
1468+ r#"
1469+ #[cfg(target_arch = "x86_64")]
1470+ pub mod api;
1471+ "# ,
1472+ r#"
1473+ use std::fmt;
1474+
1475+ #[cfg(target_arch = "x86_64")]
1476+ pub mod api;
1477+ "# ,
1478+ ImportGranularity :: Crate ,
1479+ ) ;
1480+ }
1481+
1482+ fn check_merge ( ra_fixture0 : & str , ra_fixture1 : & str , last : & str , mb : MergeBehavior ) {
1483+ let use0 = ast:: SourceFile :: parse ( ra_fixture0, span:: Edition :: CURRENT )
1484+ . tree ( )
1485+ . syntax ( )
1486+ . descendants ( )
1487+ . find_map ( ast:: Use :: cast)
1488+ . unwrap ( ) ;
1489+
1490+ let use1 = ast:: SourceFile :: parse ( ra_fixture1, span:: Edition :: CURRENT )
1491+ . tree ( )
1492+ . syntax ( )
1493+ . descendants ( )
1494+ . find_map ( ast:: Use :: cast)
1495+ . unwrap ( ) ;
1496+
1497+ let result = try_merge_imports ( & use0, & use1, mb) ;
1498+ assert_eq ! ( result. map( |u| u. to_string( ) . trim( ) . to_owned( ) ) , Some ( last. trim( ) . to_owned( ) ) ) ;
1499+ }
1500+
1501+ #[ test]
1502+ fn merge_gated_imports ( ) {
1503+ check_merge (
1504+ r#"#[cfg(test)] use foo::bar;"# ,
1505+ r#"#[cfg(test)] use foo::baz;"# ,
1506+ r#"#[cfg(test)] use foo::{bar, baz};"# ,
1507+ MergeBehavior :: Crate ,
1508+ ) ;
1509+ }
1510+
1511+ #[ test]
1512+ fn merge_gated_imports_with_different_values ( ) {
1513+ let use0 = ast:: SourceFile :: parse ( r#"#[cfg(a)] use foo::bar;"# , span:: Edition :: CURRENT )
1514+ . tree ( )
1515+ . syntax ( )
1516+ . descendants ( )
1517+ . find_map ( ast:: Use :: cast)
1518+ . unwrap ( ) ;
1519+
1520+ let use1 = ast:: SourceFile :: parse ( r#"#[cfg(b)] use foo::baz;"# , span:: Edition :: CURRENT )
1521+ . tree ( )
1522+ . syntax ( )
1523+ . descendants ( )
1524+ . find_map ( ast:: Use :: cast)
1525+ . unwrap ( ) ;
1526+
1527+ let result = try_merge_imports ( & use0, & use1, MergeBehavior :: Crate ) ;
1528+ assert_eq ! ( result, None ) ;
1529+ }
1530+
1531+ #[ test]
1532+ fn merge_gated_imports_different_order ( ) {
1533+ check_merge (
1534+ r#"#[cfg(a)] #[cfg(b)] use foo::bar;"# ,
1535+ r#"#[cfg(b)] #[cfg(a)] use foo::baz;"# ,
1536+ r#"#[cfg(a)] #[cfg(b)] use foo::{bar, baz};"# ,
1537+ MergeBehavior :: Crate ,
1538+ ) ;
1539+ }
1540+
1541+ #[ test]
1542+ fn merge_into_existing_cfg_import ( ) {
1543+ check (
1544+ r#"foo::Foo"# ,
1545+ r#"
1546+ #[cfg(target_os = "windows")]
1547+ use bar::Baz;
1548+
1549+ #[cfg(target_os = "windows")]
1550+ fn buzz() {
1551+ Foo$0;
1552+ }
1553+ "# ,
1554+ r#"
1555+ #[cfg(target_os = "windows")]
1556+ use bar::Baz;
1557+ #[cfg(target_os = "windows")]
1558+ use foo::Foo;
1559+
1560+ #[cfg(target_os = "windows")]
1561+ fn buzz() {
1562+ Foo;
1563+ }
1564+ "# ,
1565+ ImportGranularity :: Crate ,
1566+ ) ;
1567+ }
1568+
1569+ #[ test]
1570+ fn reproduce_user_issue_missing_semicolon ( ) {
1571+ check (
1572+ "std::fmt" ,
1573+ r#"
1574+ use {
1575+ foo
1576+ }
1577+
1578+ #[cfg(target_arch = "x86_64")]
1579+ pub mod api;
1580+ "# ,
1581+ r#"
1582+ use std::fmt;
1583+
1584+ use {
1585+ foo
1586+ }
1587+
1588+ #[cfg(target_arch = "x86_64")]
1589+ pub mod api;
1590+ "# ,
1591+ ImportGranularity :: Crate ,
1592+ ) ;
1593+ }
0 commit comments