Auto merge of #17523 - wada314:master, r=Veykril

Add an option to use "::" for the external crate prefix.

Fixes #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!
This commit is contained in:
bors 2024-07-07 08:32:46 +00:00
commit a494aaba87
43 changed files with 233 additions and 11 deletions

View file

@ -183,6 +183,8 @@ fn find_path_for_module(
let kind = if name_already_occupied_in_type_ns { let kind = if name_already_occupied_in_type_ns {
cov_mark::hit!(ambiguous_crate_start); cov_mark::hit!(ambiguous_crate_start);
PathKind::Abs PathKind::Abs
} else if ctx.cfg.prefer_absolute {
PathKind::Abs
} else { } else {
PathKind::Plain PathKind::Plain
}; };
@ -564,7 +566,13 @@ mod tests {
/// item the `path` refers to returns that same path when called from the /// item the `path` refers to returns that same path when called from the
/// module the cursor is in. /// module the cursor is in.
#[track_caller] #[track_caller]
fn check_found_path_(ra_fixture: &str, path: &str, prefer_prelude: bool, expect: Expect) { fn check_found_path_(
ra_fixture: &str,
path: &str,
prefer_prelude: bool,
prefer_absolute: bool,
expect: Expect,
) {
let (db, pos) = TestDB::with_position(ra_fixture); let (db, pos) = TestDB::with_position(ra_fixture);
let module = db.module_at_position(pos); let module = db.module_at_position(pos);
let parsed_path_file = let parsed_path_file =
@ -604,7 +612,7 @@ mod tests {
module, module,
prefix, prefix,
ignore_local_imports, ignore_local_imports,
ImportPathConfig { prefer_no_std: false, prefer_prelude }, ImportPathConfig { prefer_no_std: false, prefer_prelude, prefer_absolute },
); );
format_to!( format_to!(
res, res,
@ -619,11 +627,15 @@ mod tests {
} }
fn check_found_path(ra_fixture: &str, path: &str, expect: Expect) { fn check_found_path(ra_fixture: &str, path: &str, expect: Expect) {
check_found_path_(ra_fixture, path, false, expect); check_found_path_(ra_fixture, path, false, false, expect);
} }
fn check_found_path_prelude(ra_fixture: &str, path: &str, expect: Expect) { fn check_found_path_prelude(ra_fixture: &str, path: &str, expect: Expect) {
check_found_path_(ra_fixture, path, true, expect); check_found_path_(ra_fixture, path, true, false, expect);
}
fn check_found_path_absolute(ra_fixture: &str, path: &str, expect: Expect) {
check_found_path_(ra_fixture, path, false, true, expect);
} }
#[test] #[test]
@ -870,6 +882,39 @@ pub mod ast {
); );
} }
#[test]
fn partially_imported_with_prefer_absolute() {
cov_mark::check!(partially_imported);
// Similar to partially_imported test case above, but with prefer_absolute enabled.
// Even if the actual imported item is in external crate, if the path to that item
// is starting from the imported name, then the path should not start from "::".
// i.e. The first line in the expected output should not start from "::".
check_found_path_absolute(
r#"
//- /main.rs crate:main deps:syntax
use syntax::ast;
$0
//- /lib.rs crate:syntax
pub mod ast {
pub enum ModuleItem {
A, B, C,
}
}
"#,
"syntax::ast::ModuleItem",
expect![[r#"
Plain (imports ): ast::ModuleItem
Plain (imports ): ::syntax::ast::ModuleItem
ByCrate(imports ): crate::ast::ModuleItem
ByCrate(imports ): ::syntax::ast::ModuleItem
BySelf (imports ): self::ast::ModuleItem
BySelf (imports ): ::syntax::ast::ModuleItem
"#]],
);
}
#[test] #[test]
fn same_crate_reexport() { fn same_crate_reexport() {
check_found_path( check_found_path(
@ -1769,6 +1814,43 @@ pub mod foo {
); );
} }
#[test]
fn respects_absolute_setting() {
let ra_fixture = r#"
//- /main.rs crate:main deps:krate
$0
//- /krate.rs crate:krate
pub mod foo {
pub struct Foo;
}
"#;
check_found_path(
ra_fixture,
"krate::foo::Foo",
expect![[r#"
Plain (imports ): krate::foo::Foo
Plain (imports ): krate::foo::Foo
ByCrate(imports ): krate::foo::Foo
ByCrate(imports ): krate::foo::Foo
BySelf (imports ): krate::foo::Foo
BySelf (imports ): krate::foo::Foo
"#]],
);
check_found_path_absolute(
ra_fixture,
"krate::foo::Foo",
expect![[r#"
Plain (imports ): ::krate::foo::Foo
Plain (imports ): ::krate::foo::Foo
ByCrate(imports ): ::krate::foo::Foo
ByCrate(imports ): ::krate::foo::Foo
BySelf (imports ): ::krate::foo::Foo
BySelf (imports ): ::krate::foo::Foo
"#]],
);
}
#[test] #[test]
fn respect_segment_length() { fn respect_segment_length() {
check_found_path( check_found_path(

View file

@ -116,6 +116,8 @@ pub struct ImportPathConfig {
pub prefer_no_std: bool, pub prefer_no_std: bool,
/// If true, prefer import paths containing a prelude module. /// If true, prefer import paths containing a prelude module.
pub prefer_prelude: bool, pub prefer_prelude: bool,
/// If true, prefer abs path (starting with `::`) where it is available.
pub prefer_absolute: bool,
} }
#[derive(Debug)] #[derive(Debug)]

View file

@ -1043,7 +1043,11 @@ impl HirDisplay for Ty {
module_id, module_id,
PrefixKind::Plain, PrefixKind::Plain,
false, false,
ImportPathConfig { prefer_no_std: false, prefer_prelude: true }, ImportPathConfig {
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
},
) { ) {
write!(f, "{}", path.display(f.db.upcast()))?; write!(f, "{}", path.display(f.db.upcast()))?;
} else { } else {

View file

@ -15,6 +15,7 @@ pub struct AssistConfig {
pub insert_use: InsertUseConfig, pub insert_use: InsertUseConfig,
pub prefer_no_std: bool, pub prefer_no_std: bool,
pub prefer_prelude: bool, pub prefer_prelude: bool,
pub prefer_absolute: bool,
pub assist_emit_must_use: bool, pub assist_emit_must_use: bool,
pub term_search_fuel: u64, pub term_search_fuel: u64,
pub term_search_borrowck: bool, pub term_search_borrowck: bool,

View file

@ -74,6 +74,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
let module = ctx.sema.scope(expr.syntax())?.module(); let module = ctx.sema.scope(expr.syntax())?.module();

View file

@ -93,6 +93,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
let (import_assets, syntax_under_caret) = find_importable_node(ctx)?; let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;

View file

@ -340,6 +340,7 @@ fn augment_references_with_imports(
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
references references

View file

@ -47,6 +47,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) -
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
let src_type_path = { let src_type_path = {

View file

@ -186,6 +186,7 @@ fn augment_references_with_imports(
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
references references

View file

@ -90,6 +90,7 @@ fn collect_data(ident_pat: ast::IdentPat, ctx: &AssistContext<'_>) -> Option<Str
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
let module = ctx.sema.scope(ident_pat.syntax())?.module(); let module = ctx.sema.scope(ident_pat.syntax())?.module();

View file

@ -216,6 +216,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
); );

View file

@ -393,6 +393,7 @@ fn process_references(
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
); );
if let Some(mut mod_path) = mod_path { if let Some(mut mod_path) = mod_path {

View file

@ -64,6 +64,7 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
)?; )?;
@ -111,6 +112,7 @@ fn generate_tuple_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
)?; )?;

View file

@ -65,6 +65,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
)?; )?;

View file

@ -53,6 +53,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
)?; )?;

View file

@ -40,6 +40,7 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
let mut proposed_imports: Vec<_> = let mut proposed_imports: Vec<_> =

View file

@ -89,6 +89,7 @@ pub(crate) fn replace_derive_with_manual_impl(
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
) )
.as_ref() .as_ref()

View file

@ -70,6 +70,7 @@ pub(crate) fn replace_qualified_name_with_use(
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
) )
}) })

View file

@ -60,6 +60,7 @@ pub(crate) fn term_search(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
) )
.ok() .ok()

View file

@ -142,6 +142,7 @@ pub(crate) fn desugar_async_into_impl_future(
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
)?; )?;
let trait_path = trait_path.display(ctx.db()); let trait_path = trait_path.display(ctx.db());

View file

@ -30,6 +30,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: true, prefer_prelude: true,
prefer_absolute: false,
assist_emit_must_use: false, assist_emit_must_use: false,
term_search_fuel: 400, term_search_fuel: 400,
term_search_borrowck: true, term_search_borrowck: true,
@ -47,6 +48,7 @@ pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: true, prefer_prelude: true,
prefer_absolute: false,
assist_emit_must_use: false, assist_emit_must_use: false,
term_search_fuel: 400, term_search_fuel: 400,
term_search_borrowck: true, term_search_borrowck: true,
@ -64,6 +66,7 @@ pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: true, prefer_prelude: true,
prefer_absolute: false,
assist_emit_must_use: false, assist_emit_must_use: false,
term_search_fuel: 400, term_search_fuel: 400,
term_search_borrowck: true, term_search_borrowck: true,

View file

@ -639,6 +639,7 @@ fn enum_variants_with_paths(
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
) { ) {
// Variants with trivial paths are already added by the existing completion logic, // Variants with trivial paths are already added by the existing completion logic,

View file

@ -177,6 +177,7 @@ pub(crate) fn complete_expr_path(
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
) )
.filter(|it| it.len() > 1); .filter(|it| it.len() > 1);
@ -202,6 +203,7 @@ pub(crate) fn complete_expr_path(
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
) )
.filter(|it| it.len() > 1); .filter(|it| it.len() > 1);

View file

@ -259,6 +259,7 @@ fn import_on_the_fly(
let import_cfg = ImportPathConfig { let import_cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
import_assets import_assets
@ -309,6 +310,7 @@ fn import_on_the_fly_pat_(
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
import_assets import_assets
@ -354,6 +356,7 @@ fn import_on_the_fly_method(
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
import_assets import_assets

View file

@ -63,6 +63,7 @@ pub(crate) fn complete_postfix(
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() { if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {

View file

@ -22,6 +22,7 @@ pub struct CompletionConfig {
pub insert_use: InsertUseConfig, pub insert_use: InsertUseConfig,
pub prefer_no_std: bool, pub prefer_no_std: bool,
pub prefer_prelude: bool, pub prefer_prelude: bool,
pub prefer_absolute: bool,
pub snippets: Vec<Snippet>, pub snippets: Vec<Snippet>,
pub limit: Option<usize>, pub limit: Option<usize>,
} }

View file

@ -253,6 +253,7 @@ pub fn resolve_completion_edits(
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: config.prefer_no_std, prefer_no_std: config.prefer_no_std,
prefer_prelude: config.prefer_prelude, prefer_prelude: config.prefer_prelude,
prefer_absolute: config.prefer_absolute,
}; };
imports.into_iter().for_each(|(full_import_path, imported_name)| { imports.into_iter().for_each(|(full_import_path, imported_name)| {

View file

@ -298,6 +298,7 @@ pub(crate) fn render_expr(
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
let label = expr.gen_source_code(&ctx.scope, &mut label_formatter, cfg).ok()?; let label = expr.gen_source_code(&ctx.scope, &mut label_formatter, cfg).ok()?;

View file

@ -172,6 +172,7 @@ fn import_edits(ctx: &CompletionContext<'_>, requires: &[GreenNode]) -> Option<V
let import_cfg = ImportPathConfig { let import_cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}; };
let resolve = |import: &GreenNode| { let resolve = |import: &GreenNode| {

View file

@ -79,6 +79,7 @@ pub(crate) const TEST_CONFIG: CompletionConfig = CompletionConfig {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: true, prefer_prelude: true,
prefer_absolute: false,
snippets: Vec::new(), snippets: Vec::new(),
limit: None, limit: None,
}; };

View file

@ -870,6 +870,38 @@ fn main() {
); );
} }
#[test]
fn config_prefer_absolute() {
let fixture = r#"
//- /lib.rs crate:dep
pub mod foo {
pub mod bar {
pub struct Item;
}
}
//- /main.rs crate:main deps:dep
use ::dep::foo::bar;
fn main() {
Ite$0
}"#;
let mut config = TEST_CONFIG;
config.prefer_absolute = true;
check_edit_with_config(
config.clone(),
"Item",
fixture,
r#"
use ::dep::foo::bar::{self, Item};
fn main() {
Item
}"#,
);
}
#[test] #[test]
fn unresolved_qualifier() { fn unresolved_qualifier() {
let fixture = r#" let fixture = r#"

View file

@ -1222,6 +1222,26 @@ use self::foo::{self, Bar, Foo};
); );
} }
#[test]
fn insert_with_double_colon_prefixed_import_merge() {
check_with_config(
"use ::ext::foo::Foo",
r#"
use ::ext::foo::Foo as _;
"#,
r#"
use ::ext::foo::Foo;
"#,
&InsertUseConfig {
granularity: ImportGranularity::Crate,
prefix_kind: hir::PrefixKind::BySelf,
enforce_granularity: true,
group: true,
skip_glob_imports: true,
},
);
}
fn check_with_config( fn check_with_config(
path: &str, path: &str,
ra_fixture_before: &str, ra_fixture_before: &str,

View file

@ -308,8 +308,11 @@ impl Ctx<'_> {
parent.segment()?.name_ref()?, parent.segment()?.name_ref()?,
) )
.and_then(|trait_ref| { .and_then(|trait_ref| {
let cfg = let cfg = ImportPathConfig {
ImportPathConfig { prefer_no_std: false, prefer_prelude: true }; prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
};
let found_path = self.target_module.find_path( let found_path = self.target_module.find_path(
self.source_scope.db.upcast(), self.source_scope.db.upcast(),
hir::ModuleDef::Trait(trait_ref), hir::ModuleDef::Trait(trait_ref),
@ -348,7 +351,11 @@ impl Ctx<'_> {
} }
} }
let cfg = ImportPathConfig { prefer_no_std: false, prefer_prelude: true }; let cfg = ImportPathConfig {
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
};
let found_path = let found_path =
self.target_module.find_path(self.source_scope.db.upcast(), def, cfg)?; self.target_module.find_path(self.source_scope.db.upcast(), def, cfg)?;
let res = mod_path_to_ast(&found_path).clone_for_update(); let res = mod_path_to_ast(&found_path).clone_for_update();
@ -383,7 +390,11 @@ impl Ctx<'_> {
if let Some(adt) = ty.as_adt() { if let Some(adt) = ty.as_adt() {
if let ast::Type::PathType(path_ty) = &ast_ty { if let ast::Type::PathType(path_ty) = &ast_ty {
let cfg = ImportPathConfig { prefer_no_std: false, prefer_prelude: true }; let cfg = ImportPathConfig {
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
};
let found_path = self.target_module.find_path( let found_path = self.target_module.find_path(
self.source_scope.db.upcast(), self.source_scope.db.upcast(),
ModuleDef::from(adt), ModuleDef::from(adt),

View file

@ -146,6 +146,7 @@ pub(crate) fn json_in_items(
let cfg = ImportPathConfig { let cfg = ImportPathConfig {
prefer_no_std: config.prefer_no_std, prefer_no_std: config.prefer_no_std,
prefer_prelude: config.prefer_prelude, prefer_prelude: config.prefer_prelude,
prefer_absolute: config.prefer_absolute,
}; };
if !scope_has("Serialize") { if !scope_has("Serialize") {

View file

@ -128,6 +128,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::MissingFields) -> Option<Vec<Ass
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
)?; )?;

View file

@ -67,6 +67,7 @@ fn fixes(ctx: &DiagnosticsContext<'_>, d: &hir::TypedHole) -> Option<Vec<Assist>
ImportPathConfig { ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std, prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude, prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
}, },
) )
.ok() .ok()

View file

@ -231,6 +231,7 @@ pub struct DiagnosticsConfig {
pub insert_use: InsertUseConfig, pub insert_use: InsertUseConfig,
pub prefer_no_std: bool, pub prefer_no_std: bool,
pub prefer_prelude: bool, pub prefer_prelude: bool,
pub prefer_absolute: bool,
pub term_search_fuel: u64, pub term_search_fuel: u64,
pub term_search_borrowck: bool, pub term_search_borrowck: bool,
} }
@ -258,6 +259,7 @@ impl DiagnosticsConfig {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: true, prefer_prelude: true,
prefer_absolute: false,
term_search_fuel: 400, term_search_fuel: 400,
term_search_borrowck: true, term_search_borrowck: true,
} }

View file

@ -663,7 +663,11 @@ impl Match {
.module(); .module();
for (path, resolved_path) in &template.resolved_paths { for (path, resolved_path) in &template.resolved_paths {
if let hir::PathResolution::Def(module_def) = resolved_path.resolution { if let hir::PathResolution::Def(module_def) = resolved_path.resolution {
let cfg = ImportPathConfig { prefer_no_std: false, prefer_prelude: true }; let cfg = ImportPathConfig {
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
};
let mod_path = module.find_path(sema.db, module_def, cfg).ok_or_else(|| { let mod_path = module.find_path(sema.db, module_def, cfg).ok_or_else(|| {
match_error!("Failed to render template path `{}` at match location") match_error!("Failed to render template path `{}` at match location")
})?; })?;

View file

@ -439,7 +439,11 @@ impl flags::AnalysisStats {
.gen_source_code( .gen_source_code(
&scope, &scope,
&mut formatter, &mut formatter,
ImportPathConfig { prefer_no_std: false, prefer_prelude: true }, ImportPathConfig {
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
},
) )
.unwrap(); .unwrap();
syntax_hit_found |= trim(&original_text) == trim(&generated); syntax_hit_found |= trim(&original_text) == trim(&generated);
@ -988,6 +992,7 @@ impl flags::AnalysisStats {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: true, prefer_prelude: true,
prefer_absolute: false,
style_lints: false, style_lints: false,
term_search_fuel: 400, term_search_fuel: 400,
term_search_borrowck: true, term_search_borrowck: true,

View file

@ -357,6 +357,8 @@ config_data! {
imports_preferPrelude: bool = false, imports_preferPrelude: bool = false,
/// The path structure for newly inserted paths to use. /// The path structure for newly inserted paths to use.
imports_prefix: ImportPrefixDef = ImportPrefixDef::Plain, imports_prefix: ImportPrefixDef = ImportPrefixDef::Plain,
/// Whether to prefix external (including std, core) crate imports with `::`. e.g. "use ::std::io::Read;".
imports_prefixExternPrelude: bool = false,
} }
} }
@ -1270,6 +1272,7 @@ impl Config {
prefer_no_std: self.imports_preferNoStd(source_root).to_owned(), prefer_no_std: self.imports_preferNoStd(source_root).to_owned(),
assist_emit_must_use: self.assist_emitMustUse(source_root).to_owned(), assist_emit_must_use: self.assist_emitMustUse(source_root).to_owned(),
prefer_prelude: self.imports_preferPrelude(source_root).to_owned(), prefer_prelude: self.imports_preferPrelude(source_root).to_owned(),
prefer_absolute: self.imports_prefixExternPrelude(source_root).to_owned(),
term_search_fuel: self.assist_termSearch_fuel(source_root).to_owned() as u64, term_search_fuel: self.assist_termSearch_fuel(source_root).to_owned() as u64,
term_search_borrowck: self.assist_termSearch_borrowcheck(source_root).to_owned(), term_search_borrowck: self.assist_termSearch_borrowcheck(source_root).to_owned(),
} }
@ -1292,6 +1295,7 @@ impl Config {
insert_use: self.insert_use_config(source_root), insert_use: self.insert_use_config(source_root),
prefer_no_std: self.imports_preferNoStd(source_root).to_owned(), prefer_no_std: self.imports_preferNoStd(source_root).to_owned(),
prefer_prelude: self.imports_preferPrelude(source_root).to_owned(), prefer_prelude: self.imports_preferPrelude(source_root).to_owned(),
prefer_absolute: self.imports_prefixExternPrelude(source_root).to_owned(),
snippets: self.snippets.clone().to_vec(), snippets: self.snippets.clone().to_vec(),
limit: self.completion_limit().to_owned(), limit: self.completion_limit().to_owned(),
enable_term_search: self.completion_termSearch_enable().to_owned(), enable_term_search: self.completion_termSearch_enable().to_owned(),
@ -1320,6 +1324,7 @@ impl Config {
insert_use: self.insert_use_config(source_root), insert_use: self.insert_use_config(source_root),
prefer_no_std: self.imports_preferNoStd(source_root).to_owned(), prefer_no_std: self.imports_preferNoStd(source_root).to_owned(),
prefer_prelude: self.imports_preferPrelude(source_root).to_owned(), prefer_prelude: self.imports_preferPrelude(source_root).to_owned(),
prefer_absolute: self.imports_prefixExternPrelude(source_root).to_owned(),
style_lints: self.diagnostics_styleLints_enable().to_owned(), style_lints: self.diagnostics_styleLints_enable().to_owned(),
term_search_fuel: self.assist_termSearch_fuel(source_root).to_owned() as u64, term_search_fuel: self.assist_termSearch_fuel(source_root).to_owned() as u64,
term_search_borrowck: self.assist_termSearch_borrowcheck(source_root).to_owned(), term_search_borrowck: self.assist_termSearch_borrowcheck(source_root).to_owned(),

View file

@ -152,6 +152,7 @@ fn integrated_completion_benchmark() {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: true, prefer_prelude: true,
prefer_absolute: false,
snippets: Vec::new(), snippets: Vec::new(),
limit: None, limit: None,
}; };
@ -197,6 +198,7 @@ fn integrated_completion_benchmark() {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: true, prefer_prelude: true,
prefer_absolute: false,
snippets: Vec::new(), snippets: Vec::new(),
limit: None, limit: None,
}; };
@ -240,6 +242,7 @@ fn integrated_completion_benchmark() {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: true, prefer_prelude: true,
prefer_absolute: false,
snippets: Vec::new(), snippets: Vec::new(),
limit: None, limit: None,
}; };
@ -299,6 +302,7 @@ fn integrated_diagnostics_benchmark() {
}, },
prefer_no_std: false, prefer_no_std: false,
prefer_prelude: false, prefer_prelude: false,
prefer_absolute: false,
term_search_fuel: 400, term_search_fuel: 400,
term_search_borrowck: true, term_search_borrowck: true,
}; };

View file

@ -594,6 +594,11 @@ Whether to prefer import paths containing a `prelude` module.
-- --
The path structure for newly inserted paths to use. The path structure for newly inserted paths to use.
-- --
[[rust-analyzer.imports.prefixExternPrelude]]rust-analyzer.imports.prefixExternPrelude (default: `false`)::
+
--
Whether to prefix external (including std, core) crate imports with `::`. e.g. "use ::std::io::Read;".
--
[[rust-analyzer.inlayHints.bindingModeHints.enable]]rust-analyzer.inlayHints.bindingModeHints.enable (default: `false`):: [[rust-analyzer.inlayHints.bindingModeHints.enable]]rust-analyzer.inlayHints.bindingModeHints.enable (default: `false`)::
+ +
-- --

View file

@ -1734,6 +1734,16 @@
} }
} }
}, },
{
"title": "imports",
"properties": {
"rust-analyzer.imports.prefixExternPrelude": {
"markdownDescription": "Whether to prefix external (including std, core) crate imports with `::`. e.g. \"use ::std::io::Read;\".",
"default": false,
"type": "boolean"
}
}
},
{ {
"title": "inlayHints", "title": "inlayHints",
"properties": { "properties": {