diff options
author | Lukas Wirth <[email protected]> | 2021-06-16 14:46:58 +0100 |
---|---|---|
committer | Lukas Wirth <[email protected]> | 2021-06-16 14:51:01 +0100 |
commit | 66b701ed3e9980c4441f963467e49c687a4afca5 (patch) | |
tree | 9869ae6fecd133d251adbf07db284e5f49f6a3e4 | |
parent | 1c034c084d79f20c5208208a891f38bf582433e6 (diff) |
Simplify
-rw-r--r-- | crates/ide_completion/src/completions.rs | 18 | ||||
-rw-r--r-- | crates/ide_completion/src/render.rs | 78 | ||||
-rw-r--r-- | crates/ide_completion/src/render/macro_.rs | 4 |
3 files changed, 34 insertions, 66 deletions
diff --git a/crates/ide_completion/src/completions.rs b/crates/ide_completion/src/completions.rs index aef70f26a..783305005 100644 --- a/crates/ide_completion/src/completions.rs +++ b/crates/ide_completion/src/completions.rs | |||
@@ -131,9 +131,6 @@ impl Completions { | |||
131 | func: hir::Function, | 131 | func: hir::Function, |
132 | local_name: Option<hir::Name>, | 132 | local_name: Option<hir::Name>, |
133 | ) { | 133 | ) { |
134 | if ctx.expects_type() { | ||
135 | return; | ||
136 | } | ||
137 | self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func)); | 134 | self.add_opt(render_fn(RenderContext::new(ctx), None, local_name, func)); |
138 | } | 135 | } |
139 | 136 | ||
@@ -175,9 +172,6 @@ impl Completions { | |||
175 | } | 172 | } |
176 | 173 | ||
177 | pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { | 174 | pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { |
178 | if ctx.expects_type() { | ||
179 | return; | ||
180 | } | ||
181 | self.add_opt(render_const(RenderContext::new(ctx), constant)); | 175 | self.add_opt(render_const(RenderContext::new(ctx), constant)); |
182 | } | 176 | } |
183 | 177 | ||
@@ -209,32 +203,30 @@ impl Completions { | |||
209 | variant: hir::Variant, | 203 | variant: hir::Variant, |
210 | local_name: Option<hir::Name>, | 204 | local_name: Option<hir::Name>, |
211 | ) { | 205 | ) { |
212 | if ctx.expects_type() { | ||
213 | return; | ||
214 | } | ||
215 | let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None); | 206 | let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None); |
216 | self.add(item); | 207 | self.add(item); |
217 | } | 208 | } |
218 | } | 209 | } |
219 | 210 | ||
211 | /// Calls the callback for each variant of the provided enum with the path to the variant. | ||
220 | fn complete_enum_variants( | 212 | fn complete_enum_variants( |
221 | acc: &mut Completions, | 213 | acc: &mut Completions, |
222 | ctx: &CompletionContext, | 214 | ctx: &CompletionContext, |
223 | enum_data: hir::Enum, | 215 | enum_: hir::Enum, |
224 | cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath), | 216 | cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath), |
225 | ) { | 217 | ) { |
226 | let variants = enum_data.variants(ctx.db); | 218 | let variants = enum_.variants(ctx.db); |
227 | 219 | ||
228 | let module = if let Some(module) = ctx.scope.module() { | 220 | let module = if let Some(module) = ctx.scope.module() { |
229 | // Compute path from the completion site if available. | 221 | // Compute path from the completion site if available. |
230 | module | 222 | module |
231 | } else { | 223 | } else { |
232 | // Otherwise fall back to the enum's definition site. | 224 | // Otherwise fall back to the enum's definition site. |
233 | enum_data.module(ctx.db) | 225 | enum_.module(ctx.db) |
234 | }; | 226 | }; |
235 | 227 | ||
236 | if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) { | 228 | if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) { |
237 | if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_data)) { | 229 | if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_)) { |
238 | for &variant in &variants { | 230 | for &variant in &variants { |
239 | let self_path = hir::ModPath::from_segments( | 231 | let self_path = hir::ModPath::from_segments( |
240 | hir::PathKind::Plain, | 232 | hir::PathKind::Plain, |
diff --git a/crates/ide_completion/src/render.rs b/crates/ide_completion/src/render.rs index 70bf26cf4..2bd2c44d0 100644 --- a/crates/ide_completion/src/render.rs +++ b/crates/ide_completion/src/render.rs | |||
@@ -23,50 +23,6 @@ use crate::{ | |||
23 | render::{enum_variant::render_variant, function::render_fn, macro_::render_macro}, | 23 | render::{enum_variant::render_variant, function::render_fn, macro_::render_macro}, |
24 | CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance, | 24 | CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, CompletionRelevance, |
25 | }; | 25 | }; |
26 | |||
27 | pub(crate) fn render_field( | ||
28 | ctx: RenderContext<'_>, | ||
29 | receiver: Option<hir::Name>, | ||
30 | field: hir::Field, | ||
31 | ty: &hir::Type, | ||
32 | ) -> CompletionItem { | ||
33 | render_field_(ctx, receiver, field, ty) | ||
34 | } | ||
35 | |||
36 | pub(crate) fn render_tuple_field( | ||
37 | ctx: RenderContext<'_>, | ||
38 | receiver: Option<hir::Name>, | ||
39 | field: usize, | ||
40 | ty: &hir::Type, | ||
41 | ) -> CompletionItem { | ||
42 | render_tuple_field_(ctx, receiver, field, ty) | ||
43 | } | ||
44 | |||
45 | pub(crate) fn render_resolution( | ||
46 | ctx: RenderContext<'_>, | ||
47 | local_name: hir::Name, | ||
48 | resolution: &hir::ScopeDef, | ||
49 | ) -> Option<CompletionItem> { | ||
50 | render_resolution_(ctx, local_name, None, resolution) | ||
51 | } | ||
52 | |||
53 | pub(crate) fn render_resolution_with_import( | ||
54 | ctx: RenderContext<'_>, | ||
55 | import_edit: ImportEdit, | ||
56 | ) -> Option<CompletionItem> { | ||
57 | let resolution = hir::ScopeDef::from(import_edit.import.original_item); | ||
58 | let local_name = match resolution { | ||
59 | hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db), | ||
60 | hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?, | ||
61 | hir::ScopeDef::ModuleDef(hir::ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db), | ||
62 | _ => item_name(ctx.db(), import_edit.import.original_item)?, | ||
63 | }; | ||
64 | render_resolution_(ctx, local_name, Some(import_edit), &resolution).map(|mut item| { | ||
65 | item.completion_kind = CompletionKind::Magic; | ||
66 | item | ||
67 | }) | ||
68 | } | ||
69 | |||
70 | /// Interface for data and methods required for items rendering. | 26 | /// Interface for data and methods required for items rendering. |
71 | #[derive(Debug)] | 27 | #[derive(Debug)] |
72 | pub(crate) struct RenderContext<'a> { | 28 | pub(crate) struct RenderContext<'a> { |
@@ -119,7 +75,7 @@ impl<'a> RenderContext<'a> { | |||
119 | } | 75 | } |
120 | } | 76 | } |
121 | 77 | ||
122 | fn render_field_( | 78 | pub(crate) fn render_field( |
123 | ctx: RenderContext<'_>, | 79 | ctx: RenderContext<'_>, |
124 | receiver: Option<hir::Name>, | 80 | receiver: Option<hir::Name>, |
125 | field: hir::Field, | 81 | field: hir::Field, |
@@ -132,7 +88,6 @@ fn render_field_( | |||
132 | ctx.source_range(), | 88 | ctx.source_range(), |
133 | receiver.map_or_else(|| name.clone(), |receiver| format!("{}.{}", receiver, name)), | 89 | receiver.map_or_else(|| name.clone(), |receiver| format!("{}.{}", receiver, name)), |
134 | ); | 90 | ); |
135 | |||
136 | item.set_relevance(CompletionRelevance { | 91 | item.set_relevance(CompletionRelevance { |
137 | type_match: compute_type_match(ctx.completion, ty), | 92 | type_match: compute_type_match(ctx.completion, ty), |
138 | exact_name_match: compute_exact_name_match(ctx.completion, &name), | 93 | exact_name_match: compute_exact_name_match(ctx.completion, &name), |
@@ -143,17 +98,15 @@ fn render_field_( | |||
143 | .set_documentation(field.docs(ctx.db())) | 98 | .set_documentation(field.docs(ctx.db())) |
144 | .set_deprecated(is_deprecated) | 99 | .set_deprecated(is_deprecated) |
145 | .lookup_by(name); | 100 | .lookup_by(name); |
146 | |||
147 | if let Some(_ref_match) = compute_ref_match(ctx.completion, ty) { | 101 | if let Some(_ref_match) = compute_ref_match(ctx.completion, ty) { |
148 | // FIXME | 102 | // FIXME |
149 | // For now we don't properly calculate the edits for ref match | 103 | // For now we don't properly calculate the edits for ref match |
150 | // completions on struct fields, so we've disabled them. See #8058. | 104 | // completions on struct fields, so we've disabled them. See #8058. |
151 | } | 105 | } |
152 | |||
153 | item.build() | 106 | item.build() |
154 | } | 107 | } |
155 | 108 | ||
156 | fn render_tuple_field_( | 109 | pub(crate) fn render_tuple_field( |
157 | ctx: RenderContext<'_>, | 110 | ctx: RenderContext<'_>, |
158 | receiver: Option<hir::Name>, | 111 | receiver: Option<hir::Name>, |
159 | field: usize, | 112 | field: usize, |
@@ -164,14 +117,37 @@ fn render_tuple_field_( | |||
164 | ctx.source_range(), | 117 | ctx.source_range(), |
165 | receiver.map_or_else(|| field.to_string(), |receiver| format!("{}.{}", receiver, field)), | 118 | receiver.map_or_else(|| field.to_string(), |receiver| format!("{}.{}", receiver, field)), |
166 | ); | 119 | ); |
167 | |||
168 | item.kind(SymbolKind::Field) | 120 | item.kind(SymbolKind::Field) |
169 | .detail(ty.display(ctx.db()).to_string()) | 121 | .detail(ty.display(ctx.db()).to_string()) |
170 | .lookup_by(field.to_string()); | 122 | .lookup_by(field.to_string()); |
171 | |||
172 | item.build() | 123 | item.build() |
173 | } | 124 | } |
174 | 125 | ||
126 | pub(crate) fn render_resolution( | ||
127 | ctx: RenderContext<'_>, | ||
128 | local_name: hir::Name, | ||
129 | resolution: &hir::ScopeDef, | ||
130 | ) -> Option<CompletionItem> { | ||
131 | render_resolution_(ctx, local_name, None, resolution) | ||
132 | } | ||
133 | |||
134 | pub(crate) fn render_resolution_with_import( | ||
135 | ctx: RenderContext<'_>, | ||
136 | import_edit: ImportEdit, | ||
137 | ) -> Option<CompletionItem> { | ||
138 | let resolution = hir::ScopeDef::from(import_edit.import.original_item); | ||
139 | let local_name = match resolution { | ||
140 | hir::ScopeDef::ModuleDef(hir::ModuleDef::Function(f)) => f.name(ctx.completion.db), | ||
141 | hir::ScopeDef::ModuleDef(hir::ModuleDef::Const(c)) => c.name(ctx.completion.db)?, | ||
142 | hir::ScopeDef::ModuleDef(hir::ModuleDef::TypeAlias(t)) => t.name(ctx.completion.db), | ||
143 | _ => item_name(ctx.db(), import_edit.import.original_item)?, | ||
144 | }; | ||
145 | render_resolution_(ctx, local_name, Some(import_edit), &resolution).map(|mut item| { | ||
146 | item.completion_kind = CompletionKind::Magic; | ||
147 | item | ||
148 | }) | ||
149 | } | ||
150 | |||
175 | fn render_resolution_( | 151 | fn render_resolution_( |
176 | ctx: RenderContext<'_>, | 152 | ctx: RenderContext<'_>, |
177 | local_name: hir::Name, | 153 | local_name: hir::Name, |
diff --git a/crates/ide_completion/src/render/macro_.rs b/crates/ide_completion/src/render/macro_.rs index 429d937c8..3a7238bb8 100644 --- a/crates/ide_completion/src/render/macro_.rs +++ b/crates/ide_completion/src/render/macro_.rs | |||
@@ -180,7 +180,7 @@ fn main() { frobnicate!(); } | |||
180 | /// ``` | 180 | /// ``` |
181 | macro_rules! vec { () => {} } | 181 | macro_rules! vec { () => {} } |
182 | 182 | ||
183 | fn fn main() { v$0 } | 183 | fn main() { v$0 } |
184 | "#, | 184 | "#, |
185 | r#" | 185 | r#" |
186 | /// Creates a [`Vec`] containing the arguments. | 186 | /// Creates a [`Vec`] containing the arguments. |
@@ -193,7 +193,7 @@ fn fn main() { v$0 } | |||
193 | /// ``` | 193 | /// ``` |
194 | macro_rules! vec { () => {} } | 194 | macro_rules! vec { () => {} } |
195 | 195 | ||
196 | fn fn main() { vec![$0] } | 196 | fn main() { vec![$0] } |
197 | "#, | 197 | "#, |
198 | ); | 198 | ); |
199 | 199 | ||