aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions.rs
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-06-16 20:53:43 +0100
committerGitHub <[email protected]>2021-06-16 20:53:43 +0100
commitd6b8af44829521a9f925c4d87599efa3fef38edc (patch)
tree47119538effd381ecd8e15d422103512f2b47406 /crates/ide_completion/src/completions.rs
parentf38770cd2606148bfe764351849ea7ebea45132c (diff)
parentaa644b55859c6b5c6695a5d4fb35d1b6efbbebcc (diff)
Merge #9301
9301: internal: Start refactoring ide_completion tests r=Veykril a=Veykril Our current completion test infra resovles around usually just checking a specific `CompletionKind` which is suboptimal. We only see what we want to see in tests with this causing us to miss a lot of incorrect completions we are doing. Instead we should test for different cursor locations for all kinds(sans the magic kind maybe? not sure yet). This way we will also see potential duplicate completions that merely different in their kind. Also since most completion submodules complete things in tests of other modules due to the tests overlapping it makes more sense to group these tests differently which implies moving them to a new module. Exceptions for this might be stuff like attribute completion as these cannot currently interfere. I only wrote a few tests to check for completions in `ItemList` position so far and I already found a few incorrect/irrelevant completions as these haven't been tested properly due to them being hidden by the `CompletionKind` filtering. I think `CompletionKind` doesn't really seem to be beneficial to me as to I can't think of a occasion where we would want to only check a specific completion kind. Co-authored-by: Lukas Wirth <[email protected]>
Diffstat (limited to 'crates/ide_completion/src/completions.rs')
-rw-r--r--crates/ide_completion/src/completions.rs113
1 files changed, 57 insertions, 56 deletions
diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs
index 783305005..cba5eb0c6 100644
--- a/crates/ide_completion/src/completions.rs
+++ b/crates/ide_completion/src/completions.rs
@@ -41,9 +41,9 @@ pub struct Completions {
41 buf: Vec<CompletionItem>, 41 buf: Vec<CompletionItem>,
42} 42}
43 43
44impl Into<Vec<CompletionItem>> for Completions { 44impl From<Completions> for Vec<CompletionItem> {
45 fn into(self) -> Vec<CompletionItem> { 45 fn from(val: Completions) -> Self {
46 self.buf 46 val.buf
47 } 47 }
48} 48}
49 49
@@ -74,35 +74,6 @@ impl Completions {
74 items.into_iter().for_each(|item| self.add(item.into())) 74 items.into_iter().for_each(|item| self.add(item.into()))
75 } 75 }
76 76
77 pub(crate) fn add_field(
78 &mut self,
79 ctx: &CompletionContext,
80 receiver: Option<hir::Name>,
81 field: hir::Field,
82 ty: &hir::Type,
83 ) {
84 let item = render_field(RenderContext::new(ctx), receiver, field, ty);
85 self.add(item);
86 }
87
88 pub(crate) fn add_tuple_field(
89 &mut self,
90 ctx: &CompletionContext,
91 receiver: Option<hir::Name>,
92 field: usize,
93 ty: &hir::Type,
94 ) {
95 let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
96 self.add(item);
97 }
98
99 pub(crate) fn add_static_lifetime(&mut self, ctx: &CompletionContext) {
100 let mut item =
101 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), "'static");
102 item.kind(CompletionItemKind::SymbolKind(SymbolKind::LifetimeParam));
103 self.add(item.build());
104 }
105
106 pub(crate) fn add_resolution( 77 pub(crate) fn add_resolution(
107 &mut self, 78 &mut self,
108 ctx: &CompletionContext, 79 ctx: &CompletionContext,
@@ -144,72 +115,102 @@ impl Completions {
144 self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func)); 115 self.add_opt(render_method(RenderContext::new(ctx), None, receiver, local_name, func));
145 } 116 }
146 117
147 pub(crate) fn add_variant_pat( 118 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
119 self.add_opt(render_const(RenderContext::new(ctx), constant));
120 }
121
122 pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
123 self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias));
124 }
125
126 pub(crate) fn add_type_alias_with_eq(
148 &mut self, 127 &mut self,
149 ctx: &CompletionContext, 128 ctx: &CompletionContext,
150 variant: hir::Variant, 129 type_alias: hir::TypeAlias,
151 local_name: Option<hir::Name>,
152 ) { 130 ) {
153 self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None)); 131 self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
154 } 132 }
155 133
156 pub(crate) fn add_qualified_variant_pat( 134 pub(crate) fn add_qualified_enum_variant(
157 &mut self, 135 &mut self,
158 ctx: &CompletionContext, 136 ctx: &CompletionContext,
159 variant: hir::Variant, 137 variant: hir::Variant,
160 path: hir::ModPath, 138 path: hir::ModPath,
161 ) { 139 ) {
162 self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path))); 140 let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path));
141 self.add(item);
163 } 142 }
164 143
165 pub(crate) fn add_struct_pat( 144 pub(crate) fn add_enum_variant(
166 &mut self, 145 &mut self,
167 ctx: &CompletionContext, 146 ctx: &CompletionContext,
168 strukt: hir::Struct, 147 variant: hir::Variant,
169 local_name: Option<hir::Name>, 148 local_name: Option<hir::Name>,
170 ) { 149 ) {
171 self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name)); 150 let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
151 self.add(item);
172 } 152 }
173 153
174 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { 154 pub(crate) fn add_field(
175 self.add_opt(render_const(RenderContext::new(ctx), constant)); 155 &mut self,
156 ctx: &CompletionContext,
157 receiver: Option<hir::Name>,
158 field: hir::Field,
159 ty: &hir::Type,
160 ) {
161 let item = render_field(RenderContext::new(ctx), receiver, field, ty);
162 self.add(item);
176 } 163 }
177 164
178 pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) { 165 pub(crate) fn add_tuple_field(
179 self.add_opt(render_type_alias(RenderContext::new(ctx), type_alias)); 166 &mut self,
167 ctx: &CompletionContext,
168 receiver: Option<hir::Name>,
169 field: usize,
170 ty: &hir::Type,
171 ) {
172 let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
173 self.add(item);
180 } 174 }
181 175
182 pub(crate) fn add_type_alias_with_eq( 176 pub(crate) fn add_static_lifetime(&mut self, ctx: &CompletionContext) {
177 let mut item =
178 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), "'static");
179 item.kind(CompletionItemKind::SymbolKind(SymbolKind::LifetimeParam));
180 self.add(item.build());
181 }
182
183 pub(crate) fn add_variant_pat(
183 &mut self, 184 &mut self,
184 ctx: &CompletionContext, 185 ctx: &CompletionContext,
185 type_alias: hir::TypeAlias, 186 variant: hir::Variant,
187 local_name: Option<hir::Name>,
186 ) { 188 ) {
187 self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias)); 189 self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None));
188 } 190 }
189 191
190 pub(crate) fn add_qualified_enum_variant( 192 pub(crate) fn add_qualified_variant_pat(
191 &mut self, 193 &mut self,
192 ctx: &CompletionContext, 194 ctx: &CompletionContext,
193 variant: hir::Variant, 195 variant: hir::Variant,
194 path: hir::ModPath, 196 path: hir::ModPath,
195 ) { 197 ) {
196 let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path)); 198 self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)));
197 self.add(item);
198 } 199 }
199 200
200 pub(crate) fn add_enum_variant( 201 pub(crate) fn add_struct_pat(
201 &mut self, 202 &mut self,
202 ctx: &CompletionContext, 203 ctx: &CompletionContext,
203 variant: hir::Variant, 204 strukt: hir::Struct,
204 local_name: Option<hir::Name>, 205 local_name: Option<hir::Name>,
205 ) { 206 ) {
206 let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None); 207 self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
207 self.add(item);
208 } 208 }
209} 209}
210 210
211/// Calls the callback for each variant of the provided enum with the path to the variant. 211/// Calls the callback for each variant of the provided enum with the path to the variant.
212fn complete_enum_variants( 212/// Skips variants that are visible with single segment paths.
213fn enum_variants_with_paths(
213 acc: &mut Completions, 214 acc: &mut Completions,
214 ctx: &CompletionContext, 215 ctx: &CompletionContext,
215 enum_: hir::Enum, 216 enum_: hir::Enum,