@@ -183,6 +183,8 @@ fn find_path_for_module(
183183 let kind = if name_already_occupied_in_type_ns {
184184 cov_mark:: hit!( ambiguous_crate_start) ;
185185 PathKind :: Abs
186+ } else if ctx. cfg . prefer_absolute {
187+ PathKind :: Abs
186188 } else {
187189 PathKind :: Plain
188190 } ;
@@ -564,7 +566,13 @@ mod tests {
564566 /// item the `path` refers to returns that same path when called from the
565567 /// module the cursor is in.
566568 #[ track_caller]
567- fn check_found_path_ ( ra_fixture : & str , path : & str , prefer_prelude : bool , expect : Expect ) {
569+ fn check_found_path_ (
570+ ra_fixture : & str ,
571+ path : & str ,
572+ prefer_prelude : bool ,
573+ prefer_absolute : bool ,
574+ expect : Expect ,
575+ ) {
568576 let ( db, pos) = TestDB :: with_position ( ra_fixture) ;
569577 let module = db. module_at_position ( pos) ;
570578 let parsed_path_file =
@@ -604,7 +612,7 @@ mod tests {
604612 module,
605613 prefix,
606614 ignore_local_imports,
607- ImportPathConfig { prefer_no_std : false , prefer_prelude } ,
615+ ImportPathConfig { prefer_no_std : false , prefer_prelude, prefer_absolute } ,
608616 ) ;
609617 format_to ! (
610618 res,
@@ -619,11 +627,15 @@ mod tests {
619627 }
620628
621629 fn check_found_path ( ra_fixture : & str , path : & str , expect : Expect ) {
622- check_found_path_ ( ra_fixture, path, false , expect) ;
630+ check_found_path_ ( ra_fixture, path, false , false , expect) ;
623631 }
624632
625633 fn check_found_path_prelude ( ra_fixture : & str , path : & str , expect : Expect ) {
626- check_found_path_ ( ra_fixture, path, true , expect) ;
634+ check_found_path_ ( ra_fixture, path, true , false , expect) ;
635+ }
636+
637+ fn check_found_path_absolute ( ra_fixture : & str , path : & str , expect : Expect ) {
638+ check_found_path_ ( ra_fixture, path, false , true , expect) ;
627639 }
628640
629641 #[ test]
@@ -870,6 +882,39 @@ pub mod ast {
870882 ) ;
871883 }
872884
885+ #[ test]
886+ fn partially_imported_with_prefer_absolute ( ) {
887+ cov_mark:: check!( partially_imported) ;
888+ // Similar to partially_imported test case above, but with prefer_absolute enabled.
889+ // Even if the actual imported item is in external crate, if the path to that item
890+ // is starting from the imported name, then the path should not start from "::".
891+ // i.e. The first line in the expected output should not start from "::".
892+ check_found_path_absolute (
893+ r#"
894+ //- /main.rs crate:main deps:syntax
895+
896+ use syntax::ast;
897+ $0
898+
899+ //- /lib.rs crate:syntax
900+ pub mod ast {
901+ pub enum ModuleItem {
902+ A, B, C,
903+ }
904+ }
905+ "# ,
906+ "syntax::ast::ModuleItem" ,
907+ expect ! [ [ r#"
908+ Plain (imports ✔): ast::ModuleItem
909+ Plain (imports ✖): ::syntax::ast::ModuleItem
910+ ByCrate(imports ✔): crate::ast::ModuleItem
911+ ByCrate(imports ✖): ::syntax::ast::ModuleItem
912+ BySelf (imports ✔): self::ast::ModuleItem
913+ BySelf (imports ✖): ::syntax::ast::ModuleItem
914+ "# ] ] ,
915+ ) ;
916+ }
917+
873918 #[ test]
874919 fn same_crate_reexport ( ) {
875920 check_found_path (
@@ -1769,6 +1814,43 @@ pub mod foo {
17691814 ) ;
17701815 }
17711816
1817+ #[ test]
1818+ fn respects_absolute_setting ( ) {
1819+ let ra_fixture = r#"
1820+ //- /main.rs crate:main deps:krate
1821+ $0
1822+ //- /krate.rs crate:krate
1823+ pub mod foo {
1824+ pub struct Foo;
1825+ }
1826+ "# ;
1827+ check_found_path (
1828+ ra_fixture,
1829+ "krate::foo::Foo" ,
1830+ expect ! [ [ r#"
1831+ Plain (imports ✔): krate::foo::Foo
1832+ Plain (imports ✖): krate::foo::Foo
1833+ ByCrate(imports ✔): krate::foo::Foo
1834+ ByCrate(imports ✖): krate::foo::Foo
1835+ BySelf (imports ✔): krate::foo::Foo
1836+ BySelf (imports ✖): krate::foo::Foo
1837+ "# ] ] ,
1838+ ) ;
1839+
1840+ check_found_path_absolute (
1841+ ra_fixture,
1842+ "krate::foo::Foo" ,
1843+ expect ! [ [ r#"
1844+ Plain (imports ✔): ::krate::foo::Foo
1845+ Plain (imports ✖): ::krate::foo::Foo
1846+ ByCrate(imports ✔): ::krate::foo::Foo
1847+ ByCrate(imports ✖): ::krate::foo::Foo
1848+ BySelf (imports ✔): ::krate::foo::Foo
1849+ BySelf (imports ✖): ::krate::foo::Foo
1850+ "# ] ] ,
1851+ ) ;
1852+ }
1853+
17721854 #[ test]
17731855 fn respect_segment_length ( ) {
17741856 check_found_path (
0 commit comments