Skip to content

Commit 9a04253

Browse files
committed
Auto merge of rust-lang#17523 - wada314:master, r=Veykril
Add an option to use "::" for the external crate prefix. Fixes rust-lang#11823 . Hi I'm very new to rust-analyzer and not sure how the review process are. Can somebody take a look at this PR? thanks!
2 parents 3c5595a + f398be1 commit 9a04253

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+233
-11
lines changed

src/tools/rust-analyzer/crates/hir-def/src/find_path.rs

+86-4
Original file line numberDiff line numberDiff line change
@@ -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(

src/tools/rust-analyzer/crates/hir-def/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ pub struct ImportPathConfig {
116116
pub prefer_no_std: bool,
117117
/// If true, prefer import paths containing a prelude module.
118118
pub prefer_prelude: bool,
119+
/// If true, prefer abs path (starting with `::`) where it is available.
120+
pub prefer_absolute: bool,
119121
}
120122

121123
#[derive(Debug)]

src/tools/rust-analyzer/crates/hir-ty/src/display.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -1043,7 +1043,11 @@ impl HirDisplay for Ty {
10431043
module_id,
10441044
PrefixKind::Plain,
10451045
false,
1046-
ImportPathConfig { prefer_no_std: false, prefer_prelude: true },
1046+
ImportPathConfig {
1047+
prefer_no_std: false,
1048+
prefer_prelude: true,
1049+
prefer_absolute: false,
1050+
},
10471051
) {
10481052
write!(f, "{}", path.display(f.db.upcast()))?;
10491053
} else {

src/tools/rust-analyzer/crates/ide-assists/src/assist_config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub struct AssistConfig {
1515
pub insert_use: InsertUseConfig,
1616
pub prefer_no_std: bool,
1717
pub prefer_prelude: bool,
18+
pub prefer_absolute: bool,
1819
pub assist_emit_must_use: bool,
1920
pub term_search_fuel: u64,
2021
pub term_search_borrowck: bool,

src/tools/rust-analyzer/crates/ide-assists/src/handlers/add_missing_match_arms.rs

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
7474
let cfg = ImportPathConfig {
7575
prefer_no_std: ctx.config.prefer_no_std,
7676
prefer_prelude: ctx.config.prefer_prelude,
77+
prefer_absolute: ctx.config.prefer_absolute,
7778
};
7879

7980
let module = ctx.sema.scope(expr.syntax())?.module();

src/tools/rust-analyzer/crates/ide-assists/src/handlers/auto_import.rs

+1
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
9393
let cfg = ImportPathConfig {
9494
prefer_no_std: ctx.config.prefer_no_std,
9595
prefer_prelude: ctx.config.prefer_prelude,
96+
prefer_absolute: ctx.config.prefer_absolute,
9697
};
9798

9899
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;

src/tools/rust-analyzer/crates/ide-assists/src/handlers/bool_to_enum.rs

+1
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,7 @@ fn augment_references_with_imports(
340340
let cfg = ImportPathConfig {
341341
prefer_no_std: ctx.config.prefer_no_std,
342342
prefer_prelude: ctx.config.prefer_prelude,
343+
prefer_absolute: ctx.config.prefer_absolute,
343344
};
344345

345346
references

src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_into_to_from.rs

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) -
4747
let cfg = ImportPathConfig {
4848
prefer_no_std: ctx.config.prefer_no_std,
4949
prefer_prelude: ctx.config.prefer_prelude,
50+
prefer_absolute: ctx.config.prefer_absolute,
5051
};
5152

5253
let src_type_path = {

src/tools/rust-analyzer/crates/ide-assists/src/handlers/convert_tuple_return_type_to_struct.rs

+1
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ fn augment_references_with_imports(
186186
let cfg = ImportPathConfig {
187187
prefer_no_std: ctx.config.prefer_no_std,
188188
prefer_prelude: ctx.config.prefer_prelude,
189+
prefer_absolute: ctx.config.prefer_absolute,
189190
};
190191

191192
references

src/tools/rust-analyzer/crates/ide-assists/src/handlers/destructure_struct_binding.rs

+1
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ fn collect_data(ident_pat: ast::IdentPat, ctx: &AssistContext<'_>) -> Option<Str
9090
let cfg = ImportPathConfig {
9191
prefer_no_std: ctx.config.prefer_no_std,
9292
prefer_prelude: ctx.config.prefer_prelude,
93+
prefer_absolute: ctx.config.prefer_absolute,
9394
};
9495

9596
let module = ctx.sema.scope(ident_pat.syntax())?.module();

src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_function.rs

+1
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
216216
ImportPathConfig {
217217
prefer_no_std: ctx.config.prefer_no_std,
218218
prefer_prelude: ctx.config.prefer_prelude,
219+
prefer_absolute: ctx.config.prefer_absolute,
219220
},
220221
);
221222

src/tools/rust-analyzer/crates/ide-assists/src/handlers/extract_struct_from_enum_variant.rs

+1
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ fn process_references(
393393
ImportPathConfig {
394394
prefer_no_std: ctx.config.prefer_no_std,
395395
prefer_prelude: ctx.config.prefer_prelude,
396+
prefer_absolute: ctx.config.prefer_absolute,
396397
},
397398
);
398399
if let Some(mut mod_path) = mod_path {

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_deref.rs

+2
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
6464
ImportPathConfig {
6565
prefer_no_std: ctx.config.prefer_no_std,
6666
prefer_prelude: ctx.config.prefer_prelude,
67+
prefer_absolute: ctx.config.prefer_absolute,
6768
},
6869
)?;
6970

@@ -111,6 +112,7 @@ fn generate_tuple_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()
111112
ImportPathConfig {
112113
prefer_no_std: ctx.config.prefer_no_std,
113114
prefer_prelude: ctx.config.prefer_prelude,
115+
prefer_absolute: ctx.config.prefer_absolute,
114116
},
115117
)?;
116118

src/tools/rust-analyzer/crates/ide-assists/src/handlers/generate_new.rs

+1
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
6565
ImportPathConfig {
6666
prefer_no_std: ctx.config.prefer_no_std,
6767
prefer_prelude: ctx.config.prefer_prelude,
68+
prefer_absolute: ctx.config.prefer_absolute,
6869
},
6970
)?;
7071

src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_method_call.rs

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
5353
ImportPathConfig {
5454
prefer_no_std: ctx.config.prefer_no_std,
5555
prefer_prelude: ctx.config.prefer_prelude,
56+
prefer_absolute: ctx.config.prefer_absolute,
5657
},
5758
)?;
5859

src/tools/rust-analyzer/crates/ide-assists/src/handlers/qualify_path.rs

+1
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
4040
let cfg = ImportPathConfig {
4141
prefer_no_std: ctx.config.prefer_no_std,
4242
prefer_prelude: ctx.config.prefer_prelude,
43+
prefer_absolute: ctx.config.prefer_absolute,
4344
};
4445

4546
let mut proposed_imports: Vec<_> =

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_derive_with_manual_impl.rs

+1
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ pub(crate) fn replace_derive_with_manual_impl(
8989
ImportPathConfig {
9090
prefer_no_std: ctx.config.prefer_no_std,
9191
prefer_prelude: ctx.config.prefer_prelude,
92+
prefer_absolute: ctx.config.prefer_absolute,
9293
},
9394
)
9495
.as_ref()

src/tools/rust-analyzer/crates/ide-assists/src/handlers/replace_qualified_name_with_use.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ pub(crate) fn replace_qualified_name_with_use(
7070
ImportPathConfig {
7171
prefer_no_std: ctx.config.prefer_no_std,
7272
prefer_prelude: ctx.config.prefer_prelude,
73+
prefer_absolute: ctx.config.prefer_absolute,
7374
},
7475
)
7576
})

src/tools/rust-analyzer/crates/ide-assists/src/handlers/term_search.rs

+1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ pub(crate) fn term_search(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
6060
ImportPathConfig {
6161
prefer_no_std: ctx.config.prefer_no_std,
6262
prefer_prelude: ctx.config.prefer_prelude,
63+
prefer_absolute: ctx.config.prefer_absolute,
6364
},
6465
)
6566
.ok()

src/tools/rust-analyzer/crates/ide-assists/src/handlers/toggle_async_sugar.rs

+1
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ pub(crate) fn desugar_async_into_impl_future(
142142
ImportPathConfig {
143143
prefer_no_std: ctx.config.prefer_no_std,
144144
prefer_prelude: ctx.config.prefer_prelude,
145+
prefer_absolute: ctx.config.prefer_absolute,
145146
},
146147
)?;
147148
let trait_path = trait_path.display(ctx.db());

src/tools/rust-analyzer/crates/ide-assists/src/tests.rs

+3
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
3030
},
3131
prefer_no_std: false,
3232
prefer_prelude: true,
33+
prefer_absolute: false,
3334
assist_emit_must_use: false,
3435
term_search_fuel: 400,
3536
term_search_borrowck: true,
@@ -47,6 +48,7 @@ pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
4748
},
4849
prefer_no_std: false,
4950
prefer_prelude: true,
51+
prefer_absolute: false,
5052
assist_emit_must_use: false,
5153
term_search_fuel: 400,
5254
term_search_borrowck: true,
@@ -64,6 +66,7 @@ pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
6466
},
6567
prefer_no_std: false,
6668
prefer_prelude: true,
69+
prefer_absolute: false,
6770
assist_emit_must_use: false,
6871
term_search_fuel: 400,
6972
term_search_borrowck: true,

src/tools/rust-analyzer/crates/ide-completion/src/completions.rs

+1
Original file line numberDiff line numberDiff line change
@@ -639,6 +639,7 @@ fn enum_variants_with_paths(
639639
ImportPathConfig {
640640
prefer_no_std: ctx.config.prefer_no_std,
641641
prefer_prelude: ctx.config.prefer_prelude,
642+
prefer_absolute: ctx.config.prefer_absolute,
642643
},
643644
) {
644645
// Variants with trivial paths are already added by the existing completion logic,

src/tools/rust-analyzer/crates/ide-completion/src/completions/expr.rs

+2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ pub(crate) fn complete_expr_path(
177177
ImportPathConfig {
178178
prefer_no_std: ctx.config.prefer_no_std,
179179
prefer_prelude: ctx.config.prefer_prelude,
180+
prefer_absolute: ctx.config.prefer_absolute,
180181
},
181182
)
182183
.filter(|it| it.len() > 1);
@@ -202,6 +203,7 @@ pub(crate) fn complete_expr_path(
202203
ImportPathConfig {
203204
prefer_no_std: ctx.config.prefer_no_std,
204205
prefer_prelude: ctx.config.prefer_prelude,
206+
prefer_absolute: ctx.config.prefer_absolute,
205207
},
206208
)
207209
.filter(|it| it.len() > 1);

src/tools/rust-analyzer/crates/ide-completion/src/completions/flyimport.rs

+3
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@ fn import_on_the_fly(
259259
let import_cfg = ImportPathConfig {
260260
prefer_no_std: ctx.config.prefer_no_std,
261261
prefer_prelude: ctx.config.prefer_prelude,
262+
prefer_absolute: ctx.config.prefer_absolute,
262263
};
263264

264265
import_assets
@@ -309,6 +310,7 @@ fn import_on_the_fly_pat_(
309310
let cfg = ImportPathConfig {
310311
prefer_no_std: ctx.config.prefer_no_std,
311312
prefer_prelude: ctx.config.prefer_prelude,
313+
prefer_absolute: ctx.config.prefer_absolute,
312314
};
313315

314316
import_assets
@@ -354,6 +356,7 @@ fn import_on_the_fly_method(
354356
let cfg = ImportPathConfig {
355357
prefer_no_std: ctx.config.prefer_no_std,
356358
prefer_prelude: ctx.config.prefer_prelude,
359+
prefer_absolute: ctx.config.prefer_absolute,
357360
};
358361

359362
import_assets

src/tools/rust-analyzer/crates/ide-completion/src/completions/postfix.rs

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub(crate) fn complete_postfix(
6363
let cfg = ImportPathConfig {
6464
prefer_no_std: ctx.config.prefer_no_std,
6565
prefer_prelude: ctx.config.prefer_prelude,
66+
prefer_absolute: ctx.config.prefer_absolute,
6667
};
6768

6869
if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {

src/tools/rust-analyzer/crates/ide-completion/src/config.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct CompletionConfig {
2222
pub insert_use: InsertUseConfig,
2323
pub prefer_no_std: bool,
2424
pub prefer_prelude: bool,
25+
pub prefer_absolute: bool,
2526
pub snippets: Vec<Snippet>,
2627
pub limit: Option<usize>,
2728
}

src/tools/rust-analyzer/crates/ide-completion/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ pub fn resolve_completion_edits(
253253
let cfg = ImportPathConfig {
254254
prefer_no_std: config.prefer_no_std,
255255
prefer_prelude: config.prefer_prelude,
256+
prefer_absolute: config.prefer_absolute,
256257
};
257258

258259
imports.into_iter().for_each(|(full_import_path, imported_name)| {

0 commit comments

Comments
 (0)