aboutsummaryrefslogtreecommitdiff
path: root/crates/completion/src/completions
diff options
context:
space:
mode:
Diffstat (limited to 'crates/completion/src/completions')
-rw-r--r--crates/completion/src/completions/unqualified_path.rs52
1 files changed, 27 insertions, 25 deletions
diff --git a/crates/completion/src/completions/unqualified_path.rs b/crates/completion/src/completions/unqualified_path.rs
index ecda37862..7ce92a07b 100644
--- a/crates/completion/src/completions/unqualified_path.rs
+++ b/crates/completion/src/completions/unqualified_path.rs
@@ -47,6 +47,30 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
47 fuzzy_completion(acc, ctx).unwrap_or_default() 47 fuzzy_completion(acc, ctx).unwrap_or_default()
48} 48}
49 49
50fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
51 if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
52 let variants = enum_data.variants(ctx.db);
53
54 let module = if let Some(module) = ctx.scope.module() {
55 // Compute path from the completion site if available.
56 module
57 } else {
58 // Otherwise fall back to the enum's definition site.
59 enum_data.module(ctx.db)
60 };
61
62 for variant in variants {
63 if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
64 // Variants with trivial paths are already added by the existing completion logic,
65 // so we should avoid adding these twice
66 if path.segments.len() > 1 {
67 acc.add_qualified_enum_variant(ctx, variant, path);
68 }
69 }
70 }
71 }
72}
73
50// TODO kb add a setting toggle for this feature? 74// TODO kb add a setting toggle for this feature?
51fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { 75fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
52 let _p = profile::span("fuzzy_completion®"); 76 let _p = profile::span("fuzzy_completion®");
@@ -71,6 +95,7 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
71 ScopeDef::MacroDef(macro_def), 95 ScopeDef::MacroDef(macro_def),
72 )), 96 )),
73 }) 97 })
98 .filter(|(mod_path, _)| mod_path.len() > 1)
74 .filter_map(|(mod_path, definition)| { 99 .filter_map(|(mod_path, definition)| {
75 let mut resolution_with_missing_import = render_resolution( 100 let mut resolution_with_missing_import = render_resolution(
76 RenderContext::new(ctx), 101 RenderContext::new(ctx),
@@ -89,36 +114,13 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
89 resolution_with_missing_import.update_text_edit(text_edits.finish()); 114 resolution_with_missing_import.update_text_edit(text_edits.finish());
90 115
91 Some(resolution_with_missing_import) 116 Some(resolution_with_missing_import)
92 }); 117 })
118 .take(20);
93 119
94 acc.add_all(possible_imports); 120 acc.add_all(possible_imports);
95 Some(()) 121 Some(())
96} 122}
97 123
98fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
99 if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
100 let variants = enum_data.variants(ctx.db);
101
102 let module = if let Some(module) = ctx.scope.module() {
103 // Compute path from the completion site if available.
104 module
105 } else {
106 // Otherwise fall back to the enum's definition site.
107 enum_data.module(ctx.db)
108 };
109
110 for variant in variants {
111 if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
112 // Variants with trivial paths are already added by the existing completion logic,
113 // so we should avoid adding these twice
114 if path.segments.len() > 1 {
115 acc.add_qualified_enum_variant(ctx, variant, path);
116 }
117 }
118 }
119 }
120}
121
122#[cfg(test)] 124#[cfg(test)]
123mod tests { 125mod tests {
124 use expect_test::{expect, Expect}; 126 use expect_test::{expect, Expect};