Skip to content

Commit

Permalink
squash.
Browse files Browse the repository at this point in the history
  • Loading branch information
wada314 committed Jul 1, 2024
1 parent e6fd485 commit c1fff1e
Show file tree
Hide file tree
Showing 43 changed files with 233 additions and 11 deletions.
90 changes: 86 additions & 4 deletions crates/hir-def/src/find_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,8 @@ fn find_path_for_module(
let kind = if name_already_occupied_in_type_ns {
cov_mark::hit!(ambiguous_crate_start);
PathKind::Abs
} else if ctx.cfg.prefer_absolute {
PathKind::Abs
} else {
PathKind::Plain
};
Expand Down Expand Up @@ -564,7 +566,13 @@ mod tests {
/// item the `path` refers to returns that same path when called from the
/// module the cursor is in.
#[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 module = db.module_at_position(pos);
let parsed_path_file =
Expand Down Expand Up @@ -604,7 +612,7 @@ mod tests {
module,
prefix,
ignore_local_imports,
ImportPathConfig { prefer_no_std: false, prefer_prelude },
ImportPathConfig { prefer_no_std: false, prefer_prelude, prefer_absolute },
);
format_to!(
res,
Expand All @@ -619,11 +627,15 @@ mod tests {
}

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) {
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]
Expand Down Expand Up @@ -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]
fn same_crate_reexport() {
check_found_path(
Expand Down Expand Up @@ -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]
fn respect_segment_length() {
check_found_path(
Expand Down
2 changes: 2 additions & 0 deletions crates/hir-def/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ pub struct ImportPathConfig {
pub prefer_no_std: bool,
/// If true, prefer import paths containing a prelude module.
pub prefer_prelude: bool,
/// If true, prefer abs path (starting with `::`) where it is available.
pub prefer_absolute: bool,
}

#[derive(Debug)]
Expand Down
6 changes: 5 additions & 1 deletion crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1043,7 +1043,11 @@ impl HirDisplay for Ty {
module_id,
PrefixKind::Plain,
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()))?;
} else {
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/assist_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct AssistConfig {
pub insert_use: InsertUseConfig,
pub prefer_no_std: bool,
pub prefer_prelude: bool,
pub prefer_absolute: bool,
pub assist_emit_must_use: bool,
pub term_search_fuel: u64,
pub term_search_borrowck: bool,
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/add_missing_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext<'_>)
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

let module = ctx.sema.scope(expr.syntax())?.module();
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/auto_import.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub(crate) fn auto_import(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

let (import_assets, syntax_under_caret) = find_importable_node(ctx)?;
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/bool_to_enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ fn augment_references_with_imports(
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

references
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/convert_into_to_from.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub(crate) fn convert_into_to_from(acc: &mut Assists, ctx: &AssistContext<'_>) -
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

let src_type_path = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ fn augment_references_with_imports(
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

references
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ fn collect_data(ident_pat: ast::IdentPat, ctx: &AssistContext<'_>) -> Option<Str
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

let module = ctx.sema.scope(ident_pat.syntax())?.module();
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/extract_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ pub(crate) fn extract_function(acc: &mut Assists, ctx: &AssistContext<'_>) -> Op
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,7 @@ fn process_references(
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
);
if let Some(mut mod_path) = mod_path {
Expand Down
2 changes: 2 additions & 0 deletions crates/ide-assists/src/handlers/generate_deref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fn generate_record_deref(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<(
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)?;

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

Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/generate_new.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ pub(crate) fn generate_new(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)?;

Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/qualify_method_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ pub(crate) fn qualify_method_call(acc: &mut Assists, ctx: &AssistContext<'_>) ->
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)?;

Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/qualify_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ pub(crate) fn qualify_path(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

let mut proposed_imports: Vec<_> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ pub(crate) fn replace_derive_with_manual_impl(
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)
.as_ref()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub(crate) fn replace_qualified_name_with_use(
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)
})
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/term_search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub(crate) fn term_search(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)
.ok()
Expand Down
1 change: 1 addition & 0 deletions crates/ide-assists/src/handlers/toggle_async_sugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ pub(crate) fn desugar_async_into_impl_future(
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)?;
let trait_path = trait_path.display(ctx.db());
Expand Down
3 changes: 3 additions & 0 deletions crates/ide-assists/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub(crate) const TEST_CONFIG: AssistConfig = AssistConfig {
},
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
assist_emit_must_use: false,
term_search_fuel: 400,
term_search_borrowck: true,
Expand All @@ -47,6 +48,7 @@ pub(crate) const TEST_CONFIG_NO_SNIPPET_CAP: AssistConfig = AssistConfig {
},
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
assist_emit_must_use: false,
term_search_fuel: 400,
term_search_borrowck: true,
Expand All @@ -64,6 +66,7 @@ pub(crate) const TEST_CONFIG_IMPORT_ONE: AssistConfig = AssistConfig {
},
prefer_no_std: false,
prefer_prelude: true,
prefer_absolute: false,
assist_emit_must_use: false,
term_search_fuel: 400,
term_search_borrowck: true,
Expand Down
1 change: 1 addition & 0 deletions crates/ide-completion/src/completions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,7 @@ fn enum_variants_with_paths(
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
) {
// Variants with trivial paths are already added by the existing completion logic,
Expand Down
2 changes: 2 additions & 0 deletions crates/ide-completion/src/completions/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ pub(crate) fn complete_expr_path(
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)
.filter(|it| it.len() > 1);
Expand All @@ -202,6 +203,7 @@ pub(crate) fn complete_expr_path(
ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
},
)
.filter(|it| it.len() > 1);
Expand Down
3 changes: 3 additions & 0 deletions crates/ide-completion/src/completions/flyimport.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ fn import_on_the_fly(
let import_cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

import_assets
Expand Down Expand Up @@ -309,6 +310,7 @@ fn import_on_the_fly_pat_(
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

import_assets
Expand Down Expand Up @@ -354,6 +356,7 @@ fn import_on_the_fly_method(
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

import_assets
Expand Down
1 change: 1 addition & 0 deletions crates/ide-completion/src/completions/postfix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ pub(crate) fn complete_postfix(
let cfg = ImportPathConfig {
prefer_no_std: ctx.config.prefer_no_std,
prefer_prelude: ctx.config.prefer_prelude,
prefer_absolute: ctx.config.prefer_absolute,
};

if let Some(drop_trait) = ctx.famous_defs().core_ops_Drop() {
Expand Down
1 change: 1 addition & 0 deletions crates/ide-completion/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct CompletionConfig {
pub insert_use: InsertUseConfig,
pub prefer_no_std: bool,
pub prefer_prelude: bool,
pub prefer_absolute: bool,
pub snippets: Vec<Snippet>,
pub limit: Option<usize>,
}
Expand Down
1 change: 1 addition & 0 deletions crates/ide-completion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,7 @@ pub fn resolve_completion_edits(
let cfg = ImportPathConfig {
prefer_no_std: config.prefer_no_std,
prefer_prelude: config.prefer_prelude,
prefer_absolute: config.prefer_absolute,
};

imports.into_iter().for_each(|(full_import_path, imported_name)| {
Expand Down
Loading

0 comments on commit c1fff1e

Please sign in to comment.