diff options
author | Kirill Bulatov <[email protected]> | 2020-11-14 09:59:23 +0000 |
---|---|---|
committer | Kirill Bulatov <[email protected]> | 2020-11-16 19:19:06 +0000 |
commit | 38ef1fd4ad7fd26439201a1a4147a7d90a13601f (patch) | |
tree | 5966e1410112455b0ec4957befb311bba41f03bc | |
parent | ee99620754cdcfbab28a2c067dfa31087376d6c3 (diff) |
Better filter mod paths
-rw-r--r-- | crates/assists/src/utils/import_assets.rs | 2 | ||||
-rw-r--r-- | crates/completion/src/completions/unqualified_path.rs | 52 |
2 files changed, 28 insertions, 26 deletions
diff --git a/crates/assists/src/utils/import_assets.rs b/crates/assists/src/utils/import_assets.rs index 89e62ba79..ff5c0e78e 100644 --- a/crates/assists/src/utils/import_assets.rs +++ b/crates/assists/src/utils/import_assets.rs | |||
@@ -195,7 +195,7 @@ impl ImportAssets { | |||
195 | } | 195 | } |
196 | .map(|path| (path, item)) | 196 | .map(|path| (path, item)) |
197 | }) | 197 | }) |
198 | .filter(|(use_path, _)| !use_path.segments.is_empty()) | 198 | .filter(|(use_path, _)| use_path.len() > 1) |
199 | .take(20) | 199 | .take(20) |
200 | .collect::<Vec<_>>(); | 200 | .collect::<Vec<_>>(); |
201 | res.sort_by_key(|(path, _)| path.clone()); | 201 | res.sort_by_key(|(path, _)| path.clone()); |
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 | ||
50 | fn 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? |
51 | fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { | 75 | fn 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 | ||
98 | fn 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)] |
123 | mod tests { | 125 | mod tests { |
124 | use expect_test::{expect, Expect}; | 126 | use expect_test::{expect, Expect}; |