Skip to content

Commit 02b401b

Browse files
committed
Simplify completion render functionality
1 parent 4fe5f03 commit 02b401b

File tree

14 files changed

+490
-345
lines changed

14 files changed

+490
-345
lines changed

crates/ide_completion/src/completions.rs

Lines changed: 72 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ use crate::{
2929
item::Builder,
3030
render::{
3131
const_::render_const,
32-
enum_variant::render_variant,
3332
function::{render_fn, render_method},
33+
literal::{render_struct_literal, render_variant_lit},
34+
macro_::render_macro,
3435
pattern::{render_struct_pat, render_variant_pat},
35-
render_field, render_resolution, render_tuple_field,
36-
struct_literal::render_struct_literal,
36+
render_field, render_resolution, render_resolution_simple, render_tuple_field,
3737
type_alias::{render_type_alias, render_type_alias_with_eq},
3838
union_literal::render_union_literal,
3939
RenderContext,
@@ -124,7 +124,37 @@ impl Completions {
124124
cov_mark::hit!(qualified_path_doc_hidden);
125125
return;
126126
}
127-
self.add(render_resolution(RenderContext::new(ctx, false), local_name, resolution));
127+
self.add(render_resolution(RenderContext::new(ctx), local_name, resolution));
128+
}
129+
130+
pub(crate) fn add_resolution_simple(
131+
&mut self,
132+
ctx: &CompletionContext,
133+
local_name: hir::Name,
134+
resolution: hir::ScopeDef,
135+
) {
136+
if ctx.is_scope_def_hidden(resolution) {
137+
return;
138+
}
139+
self.add(render_resolution_simple(RenderContext::new(ctx), local_name, resolution));
140+
}
141+
142+
pub(crate) fn add_macro(
143+
&mut self,
144+
ctx: &CompletionContext,
145+
mac: hir::Macro,
146+
local_name: hir::Name,
147+
) {
148+
let is_private_editable = match ctx.is_visible(&mac) {
149+
Visible::Yes => false,
150+
Visible::Editable => true,
151+
Visible::No => return,
152+
};
153+
self.add(render_macro(
154+
RenderContext::new(ctx).private_editable(is_private_editable),
155+
local_name,
156+
mac,
157+
));
128158
}
129159

130160
pub(crate) fn add_function(
@@ -138,7 +168,11 @@ impl Completions {
138168
Visible::Editable => true,
139169
Visible::No => return,
140170
};
141-
self.add(render_fn(RenderContext::new(ctx, is_private_editable), None, local_name, func));
171+
self.add(render_fn(
172+
RenderContext::new(ctx).private_editable(is_private_editable),
173+
local_name,
174+
func,
175+
));
142176
}
143177

144178
pub(crate) fn add_method(
@@ -154,8 +188,7 @@ impl Completions {
154188
Visible::No => return,
155189
};
156190
self.add(render_method(
157-
RenderContext::new(ctx, is_private_editable),
158-
None,
191+
RenderContext::new(ctx).private_editable(is_private_editable),
159192
receiver,
160193
local_name,
161194
func,
@@ -168,7 +201,10 @@ impl Completions {
168201
Visible::Editable => true,
169202
Visible::No => return,
170203
};
171-
self.add_opt(render_const(RenderContext::new(ctx, is_private_editable), konst));
204+
self.add_opt(render_const(
205+
RenderContext::new(ctx).private_editable(is_private_editable),
206+
konst,
207+
));
172208
}
173209

174210
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
@@ -177,15 +213,18 @@ impl Completions {
177213
Visible::Editable => true,
178214
Visible::No => return,
179215
};
180-
self.add_opt(render_type_alias(RenderContext::new(ctx, is_private_editable), type_alias));
216+
self.add_opt(render_type_alias(
217+
RenderContext::new(ctx).private_editable(is_private_editable),
218+
type_alias,
219+
));
181220
}
182221

183222
pub(crate) fn add_type_alias_with_eq(
184223
&mut self,
185224
ctx: &CompletionContext,
186225
type_alias: hir::TypeAlias,
187226
) {
188-
self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx, false), type_alias));
227+
self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
189228
}
190229

191230
pub(crate) fn add_qualified_enum_variant(
@@ -194,8 +233,7 @@ impl Completions {
194233
variant: hir::Variant,
195234
path: hir::ModPath,
196235
) {
197-
let item = render_variant(RenderContext::new(ctx, false), None, None, variant, Some(path));
198-
self.add(item);
236+
self.add_opt(render_variant_lit(RenderContext::new(ctx), None, variant, Some(path)));
199237
}
200238

201239
pub(crate) fn add_enum_variant(
@@ -204,8 +242,7 @@ impl Completions {
204242
variant: hir::Variant,
205243
local_name: Option<hir::Name>,
206244
) {
207-
let item = render_variant(RenderContext::new(ctx, false), None, local_name, variant, None);
208-
self.add(item);
245+
self.add_opt(render_variant_lit(RenderContext::new(ctx), local_name, variant, None));
209246
}
210247

211248
pub(crate) fn add_field(
@@ -220,7 +257,12 @@ impl Completions {
220257
Visible::Editable => true,
221258
Visible::No => return,
222259
};
223-
let item = render_field(RenderContext::new(ctx, is_private_editable), receiver, field, ty);
260+
let item = render_field(
261+
RenderContext::new(ctx).private_editable(is_private_editable),
262+
receiver,
263+
field,
264+
ty,
265+
);
224266
self.add(item);
225267
}
226268

@@ -231,7 +273,7 @@ impl Completions {
231273
path: Option<hir::ModPath>,
232274
local_name: Option<hir::Name>,
233275
) {
234-
let item = render_struct_literal(RenderContext::new(ctx, false), strukt, path, local_name);
276+
let item = render_struct_literal(RenderContext::new(ctx), strukt, path, local_name);
235277
self.add_opt(item);
236278
}
237279

@@ -242,7 +284,7 @@ impl Completions {
242284
path: Option<hir::ModPath>,
243285
local_name: Option<hir::Name>,
244286
) {
245-
let item = render_union_literal(RenderContext::new(ctx, false), un, path, local_name);
287+
let item = render_union_literal(RenderContext::new(ctx), un, path, local_name);
246288
self.add_opt(item);
247289
}
248290

@@ -253,7 +295,7 @@ impl Completions {
253295
field: usize,
254296
ty: &hir::Type,
255297
) {
256-
let item = render_tuple_field(RenderContext::new(ctx, false), receiver, field, ty);
298+
let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
257299
self.add(item);
258300
}
259301

@@ -272,7 +314,14 @@ impl Completions {
272314
variant: hir::Variant,
273315
local_name: Option<hir::Name>,
274316
) {
275-
self.add_opt(render_variant_pat(RenderContext::new(ctx, false), variant, local_name, None));
317+
self.add_opt(render_variant_pat(
318+
RenderContext::new(ctx),
319+
variant,
320+
local_name.clone(),
321+
None,
322+
false,
323+
));
324+
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, local_name, None, true));
276325
}
277326

278327
pub(crate) fn add_qualified_variant_pat(
@@ -281,7 +330,9 @@ impl Completions {
281330
variant: hir::Variant,
282331
path: hir::ModPath,
283332
) {
284-
self.add_opt(render_variant_pat(RenderContext::new(ctx, false), variant, None, Some(path)));
333+
let path = Some(&path);
334+
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, path, false));
335+
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, path, true));
285336
}
286337

287338
pub(crate) fn add_struct_pat(
@@ -290,7 +341,7 @@ impl Completions {
290341
strukt: hir::Struct,
291342
local_name: Option<hir::Name>,
292343
) {
293-
self.add_opt(render_struct_pat(RenderContext::new(ctx, false), strukt, local_name));
344+
self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
294345
}
295346
}
296347

crates/ide_completion/src/completions/flyimport.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ pub(crate) fn import_on_the_fly(acc: &mut Completions, ctx: &CompletionContext)
198198
})
199199
.filter_map(|import| {
200200
render_resolution_with_import(
201-
RenderContext::new(ctx, false),
201+
RenderContext::new(ctx),
202202
ImportEdit { import, scope: import_scope.clone() },
203203
)
204204
}),

crates/ide_completion/src/completions/pattern.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,15 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
5454
{
5555
if refutable || single_variant_enum(e) {
5656
super::enum_variants_with_paths(acc, ctx, e, |acc, ctx, variant, path| {
57-
acc.add_qualified_variant_pat(ctx, variant, path.clone());
58-
acc.add_qualified_enum_variant(ctx, variant, path);
57+
acc.add_qualified_variant_pat(ctx, variant, path);
5958
});
6059
}
6160
}
6261

6362
// FIXME: ideally, we should look at the type we are matching against and
6463
// suggest variants + auto-imports
6564
ctx.process_all_names(&mut |name, res| {
66-
let add_resolution = match res {
65+
let add_simple_path = match res {
6766
hir::ScopeDef::ModuleDef(def) => match def {
6867
hir::ModuleDef::Adt(hir::Adt::Struct(strukt)) => {
6968
acc.add_struct_pat(ctx, strukt, Some(name.clone()));
@@ -76,22 +75,31 @@ pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
7675
true
7776
}
7877
hir::ModuleDef::Adt(hir::Adt::Enum(e)) => refutable || single_variant_enum(e),
79-
hir::ModuleDef::Const(..) | hir::ModuleDef::Module(..) => refutable,
80-
hir::ModuleDef::Macro(mac) => mac.is_fn_like(ctx.db),
78+
hir::ModuleDef::Const(..) => refutable,
79+
hir::ModuleDef::Module(..) => true,
80+
hir::ModuleDef::Macro(mac) if mac.is_fn_like(ctx.db) => {
81+
return acc.add_macro(ctx, mac, name)
82+
}
8183
_ => false,
8284
},
8385
hir::ScopeDef::ImplSelfType(impl_) => match impl_.self_ty(ctx.db).as_adt() {
8486
Some(hir::Adt::Struct(strukt)) => {
8587
acc.add_struct_pat(ctx, strukt, Some(name.clone()));
8688
true
8789
}
88-
Some(hir::Adt::Enum(_)) => refutable,
89-
_ => true,
90+
Some(hir::Adt::Enum(e)) => refutable || single_variant_enum(e),
91+
Some(hir::Adt::Union(_)) => true,
92+
_ => false,
9093
},
91-
_ => false,
94+
ScopeDef::GenericParam(hir::GenericParam::ConstParam(_)) => true,
95+
ScopeDef::GenericParam(_)
96+
| ScopeDef::AdtSelfType(_)
97+
| ScopeDef::Local(_)
98+
| ScopeDef::Label(_)
99+
| ScopeDef::Unknown => false,
92100
};
93-
if add_resolution {
94-
acc.add_resolution(ctx, name, res);
101+
if add_simple_path {
102+
acc.add_resolution_simple(ctx, name, res);
95103
}
96104
});
97105
}

crates/ide_completion/src/completions/record.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,16 @@ pub(crate) fn complete_record_literal(
8484
match ctx.expected_type.as_ref()?.as_adt()? {
8585
hir::Adt::Struct(strukt) if ctx.path_qual().is_none() => {
8686
let module = if let Some(module) = ctx.module { module } else { strukt.module(ctx.db) };
87-
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(strukt));
87+
let path = module
88+
.find_use_path(ctx.db, hir::ModuleDef::from(strukt))
89+
.filter(|it| it.len() > 1);
8890

8991
acc.add_struct_literal(ctx, strukt, path, None);
9092
}
9193
hir::Adt::Union(un) if ctx.path_qual().is_none() => {
9294
let module = if let Some(module) = ctx.module { module } else { un.module(ctx.db) };
93-
let path = module.find_use_path(ctx.db, hir::ModuleDef::from(un));
95+
let path =
96+
module.find_use_path(ctx.db, hir::ModuleDef::from(un)).filter(|it| it.len() > 1);
9497

9598
acc.add_union_literal(ctx, un, path, None);
9699
}
@@ -132,7 +135,7 @@ fn baz() {
132135
#[test]
133136
fn literal_struct_completion_from_sub_modules() {
134137
check_edit(
135-
"Struct {…}",
138+
"submod::Struct {…}",
136139
r#"
137140
mod submod {
138141
pub struct Struct {

0 commit comments

Comments
 (0)