2 use ra_syntax::AstNode;
3 use test_utils::tested_by;
5 use crate::completion::{Completions, CompletionContext};
7 pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
8 let path = match &ctx.path_prefix {
9 Some(path) => path.clone(),
12 let def = match ctx.resolver.resolve_path(ctx.db, &path).take_types() {
13 Some(Resolution::Def(def)) => def,
17 hir::ModuleDef::Module(module) => {
18 let module_scope = module.scope(ctx.db);
19 for (name, res) in module_scope.entries() {
20 if Some(module) == ctx.module {
21 if let Some(import) = res.import {
22 let path = module.import_source(ctx.db, import);
23 if path.syntax().range().contains_inclusive(ctx.offset) {
24 // for `use self::foo<|>`, don't suggest `foo` as a completion
25 tested_by!(dont_complete_current_use);
30 acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def));
33 hir::ModuleDef::Enum(e) => {
34 for variant in e.variants(ctx.db) {
35 acc.add_enum_variant(ctx, variant);
38 hir::ModuleDef::Struct(s) => {
39 let ty = s.ty(ctx.db);
40 ty.iterate_impl_items(ctx.db, |item| {
42 hir::ImplItem::Method(func) => {
43 let sig = func.signature(ctx.db);
44 if !sig.has_self_param() {
45 acc.add_function(ctx, func);
48 hir::ImplItem::Const(ct) => acc.add_const(ctx, ct),
49 hir::ImplItem::Type(ty) => acc.add_type(ctx, ty),
60 use test_utils::covers;
62 use crate::completion::{CompletionKind, check_completion, do_completion};
64 fn check_reference_completion(code: &str, expected_completions: &str) {
65 check_completion(code, expected_completions, CompletionKind::Reference);
69 fn dont_complete_current_use() {
70 covers!(dont_complete_current_use);
71 let completions = do_completion(r"use self::foo<|>;", CompletionKind::Reference);
72 assert!(completions.is_empty());
76 fn completes_mod_with_docs() {
77 check_reference_completion(
83 /// docs describing `mod my`.
92 fn completes_use_item_starting_with_self() {
93 check_reference_completion(
94 "use_item_starting_with_self",
106 fn completes_use_item_starting_with_crate() {
107 check_reference_completion(
108 "use_item_starting_with_crate",
120 fn completes_nested_use_tree() {
121 check_reference_completion(
134 fn completes_deeply_nested_use_tree() {
135 check_reference_completion(
136 "deeply_nested_use_tree",
146 use crate::{bar::{baz::Sp<|>}};
152 fn completes_enum_variant() {
153 check_reference_completion(
161 /// Bar Variant with i32
164 fn foo() { let _ = E::<|> }
170 fn completes_enum_variant_with_details() {
171 check_reference_completion(
172 "enum_variant_with_details",
175 struct S { field: u32 }
178 /// Foo Variant (empty)
180 /// Bar Variant with i32 and u32
185 fn foo() { let _ = E::<|> }
191 fn completes_struct_associated_method() {
192 check_reference_completion(
193 "struct_associated_method",
200 /// An associated method
204 fn foo() { let _ = S::<|> }
210 fn completes_struct_associated_const() {
211 check_reference_completion(
212 "struct_associated_const",
219 /// An associated const
223 fn foo() { let _ = S::<|> }
229 fn completes_struct_associated_type() {
230 check_reference_completion(
231 "struct_associated_type",
238 /// An associated type
242 fn foo() { let _ = S::<|> }
248 fn completes_use_paths_across_crates() {
249 check_reference_completion(
250 "completes_use_paths_across_crates",