Skip to content

Commit 6e9c963

Browse files
committed
internal: Lift out IdentContext from CompletionContext
1 parent bcf10cd commit 6e9c963

21 files changed

+451
-346
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 56 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ pub(crate) mod vis;
2323
use std::iter;
2424

2525
use hir::{known, ScopeDef};
26-
use ide_db::SymbolKind;
26+
use ide_db::{imports::import_assets::LocatedImport, SymbolKind};
2727
use syntax::ast;
2828

2929
use crate::{
3030
context::{
31-
ItemListKind, NameContext, NameKind, NameRefContext, NameRefKind, PathKind, PatternContext,
32-
TypeLocation, Visible,
31+
DotAccess, ItemListKind, NameContext, NameKind, NameRefContext, NameRefKind,
32+
PathCompletionCtx, PathKind, PatternContext, TypeLocation, Visible,
3333
},
3434
item::Builder,
3535
render::{
@@ -38,7 +38,7 @@ use crate::{
3838
literal::{render_struct_literal, render_variant_lit},
3939
macro_::render_macro,
4040
pattern::{render_struct_pat, render_variant_pat},
41-
render_field, render_resolution, render_resolution_simple, render_tuple_field,
41+
render_field, render_path_resolution, render_resolution_simple, render_tuple_field,
4242
type_alias::{render_type_alias, render_type_alias_with_eq},
4343
union_literal::render_union_literal,
4444
RenderContext,
@@ -137,23 +137,27 @@ impl Completions {
137137
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
138138
ctx.process_all_names(&mut |name, res| match res {
139139
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
140-
self.add_resolution(ctx, name, res);
140+
self.add_module(ctx, m, name);
141141
}
142142
_ => (),
143143
});
144144
}
145145

146-
pub(crate) fn add_resolution(
146+
pub(crate) fn add_path_resolution(
147147
&mut self,
148148
ctx: &CompletionContext,
149+
path_ctx: &PathCompletionCtx,
149150
local_name: hir::Name,
150151
resolution: hir::ScopeDef,
151152
) {
152153
if ctx.is_scope_def_hidden(resolution) {
153154
cov_mark::hit!(qualified_path_doc_hidden);
154155
return;
155156
}
156-
self.add(render_resolution(RenderContext::new(ctx), local_name, resolution).build());
157+
self.add(
158+
render_path_resolution(RenderContext::new(ctx), path_ctx, local_name, resolution)
159+
.build(),
160+
);
157161
}
158162

159163
pub(crate) fn add_resolution_simple(
@@ -174,12 +178,13 @@ impl Completions {
174178
module: hir::Module,
175179
local_name: hir::Name,
176180
) {
177-
self.add_resolution(ctx, local_name, hir::ScopeDef::ModuleDef(module.into()));
181+
self.add_resolution_simple(ctx, local_name, hir::ScopeDef::ModuleDef(module.into()));
178182
}
179183

180184
pub(crate) fn add_macro(
181185
&mut self,
182186
ctx: &CompletionContext,
187+
path_ctx: &PathCompletionCtx,
183188
mac: hir::Macro,
184189
local_name: hir::Name,
185190
) {
@@ -191,6 +196,7 @@ impl Completions {
191196
self.add(
192197
render_macro(
193198
RenderContext::new(ctx).private_editable(is_private_editable),
199+
path_ctx,
194200
local_name,
195201
mac,
196202
)
@@ -201,6 +207,7 @@ impl Completions {
201207
pub(crate) fn add_function(
202208
&mut self,
203209
ctx: &CompletionContext,
210+
path_ctx: &PathCompletionCtx,
204211
func: hir::Function,
205212
local_name: Option<hir::Name>,
206213
) {
@@ -212,6 +219,7 @@ impl Completions {
212219
self.add(
213220
render_fn(
214221
RenderContext::new(ctx).private_editable(is_private_editable),
222+
path_ctx,
215223
local_name,
216224
func,
217225
)
@@ -222,6 +230,7 @@ impl Completions {
222230
pub(crate) fn add_method(
223231
&mut self,
224232
ctx: &CompletionContext,
233+
dot_access: &DotAccess,
225234
func: hir::Function,
226235
receiver: Option<hir::Name>,
227236
local_name: Option<hir::Name>,
@@ -234,6 +243,7 @@ impl Completions {
234243
self.add(
235244
render_method(
236245
RenderContext::new(ctx).private_editable(is_private_editable),
246+
dot_access,
237247
receiver,
238248
local_name,
239249
func,
@@ -242,6 +252,32 @@ impl Completions {
242252
);
243253
}
244254

255+
pub(crate) fn add_method_with_import(
256+
&mut self,
257+
ctx: &CompletionContext,
258+
dot_access: &DotAccess,
259+
func: hir::Function,
260+
import: LocatedImport,
261+
) {
262+
let is_private_editable = match ctx.is_visible(&func) {
263+
Visible::Yes => false,
264+
Visible::Editable => true,
265+
Visible::No => return,
266+
};
267+
self.add(
268+
render_method(
269+
RenderContext::new(ctx)
270+
.private_editable(is_private_editable)
271+
.import_to_add(Some(import)),
272+
dot_access,
273+
None,
274+
None,
275+
func,
276+
)
277+
.build(),
278+
);
279+
}
280+
245281
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, konst: hir::Const) {
246282
let is_private_editable = match ctx.is_visible(&konst) {
247283
Visible::Yes => false,
@@ -277,11 +313,12 @@ impl Completions {
277313
pub(crate) fn add_qualified_enum_variant(
278314
&mut self,
279315
ctx: &CompletionContext,
316+
path_ctx: &PathCompletionCtx,
280317
variant: hir::Variant,
281318
path: hir::ModPath,
282319
) {
283320
if let Some(builder) =
284-
render_variant_lit(RenderContext::new(ctx), None, variant, Some(path))
321+
render_variant_lit(RenderContext::new(ctx), path_ctx, None, variant, Some(path))
285322
{
286323
self.add(builder.build());
287324
}
@@ -290,11 +327,12 @@ impl Completions {
290327
pub(crate) fn add_enum_variant(
291328
&mut self,
292329
ctx: &CompletionContext,
330+
path_ctx: &PathCompletionCtx,
293331
variant: hir::Variant,
294332
local_name: Option<hir::Name>,
295333
) {
296334
if let Some(builder) =
297-
render_variant_lit(RenderContext::new(ctx), local_name, variant, None)
335+
render_variant_lit(RenderContext::new(ctx), path_ctx, local_name, variant, None)
298336
{
299337
self.add(builder.build());
300338
}
@@ -324,12 +362,13 @@ impl Completions {
324362
pub(crate) fn add_struct_literal(
325363
&mut self,
326364
ctx: &CompletionContext,
365+
path_ctx: &PathCompletionCtx,
327366
strukt: hir::Struct,
328367
path: Option<hir::ModPath>,
329368
local_name: Option<hir::Name>,
330369
) {
331370
if let Some(builder) =
332-
render_struct_literal(RenderContext::new(ctx), strukt, path, local_name)
371+
render_struct_literal(RenderContext::new(ctx), path_ctx, strukt, path, local_name)
333372
{
334373
self.add(builder.build());
335374
}
@@ -369,11 +408,13 @@ impl Completions {
369408
pub(crate) fn add_variant_pat(
370409
&mut self,
371410
ctx: &CompletionContext,
411+
pattern_ctx: &PatternContext,
372412
variant: hir::Variant,
373413
local_name: Option<hir::Name>,
374414
) {
375415
self.add_opt(render_variant_pat(
376416
RenderContext::new(ctx),
417+
pattern_ctx,
377418
variant,
378419
local_name.clone(),
379420
None,
@@ -383,20 +424,22 @@ impl Completions {
383424
pub(crate) fn add_qualified_variant_pat(
384425
&mut self,
385426
ctx: &CompletionContext,
427+
pattern_ctx: &PatternContext,
386428
variant: hir::Variant,
387429
path: hir::ModPath,
388430
) {
389431
let path = Some(&path);
390-
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, path));
432+
self.add_opt(render_variant_pat(RenderContext::new(ctx), pattern_ctx, variant, None, path));
391433
}
392434

393435
pub(crate) fn add_struct_pat(
394436
&mut self,
395437
ctx: &CompletionContext,
438+
pattern_ctx: &PatternContext,
396439
strukt: hir::Struct,
397440
local_name: Option<hir::Name>,
398441
) {
399-
self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
442+
self.add_opt(render_struct_pat(RenderContext::new(ctx), pattern_ctx, strukt, local_name));
400443
}
401444
}
402445

crates/ide-completion/src/completions/attribute.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub(crate) fn complete_known_attribute_input(
7474
pub(crate) fn complete_attribute_path(
7575
acc: &mut Completions,
7676
ctx: &CompletionContext,
77-
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
77+
path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
7878
&AttrCtx { kind, annotated_item_kind }: &AttrCtx,
7979
) {
8080
let is_inner = kind == AttrKind::Inner;
@@ -92,7 +92,7 @@ pub(crate) fn complete_attribute_path(
9292
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
9393
match def {
9494
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
95-
acc.add_macro(ctx, m, name)
95+
acc.add_macro(ctx, path_ctx, m, name)
9696
}
9797
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
9898
acc.add_module(ctx, m, name)
@@ -108,7 +108,7 @@ pub(crate) fn complete_attribute_path(
108108
Qualified::No => {
109109
ctx.process_all_names(&mut |name, def| match def {
110110
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
111-
acc.add_macro(ctx, m, name)
111+
acc.add_macro(ctx, path_ctx, m, name)
112112
}
113113
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
114114
_ => (),

crates/ide-completion/src/completions/attribute/derive.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::{
1313
pub(crate) fn complete_derive_path(
1414
acc: &mut Completions,
1515
ctx: &CompletionContext,
16-
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
16+
path_ctx @ PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
1717
existing_derives: &ExistingDerives,
1818
) {
1919
let core = ctx.famous_defs().core();
@@ -33,7 +33,7 @@ pub(crate) fn complete_derive_path(
3333
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac))
3434
if !existing_derives.contains(&mac) && mac.is_derive(ctx.db) =>
3535
{
36-
acc.add_macro(ctx, mac, name)
36+
acc.add_macro(ctx, path_ctx, mac, name)
3737
}
3838
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
3939
_ => (),
@@ -59,7 +59,7 @@ pub(crate) fn complete_derive_path(
5959
match (core, mac.module(ctx.db).krate()) {
6060
// show derive dependencies for `core`/`std` derives
6161
(Some(core), mac_krate) if core == mac_krate => {}
62-
_ => return acc.add_macro(ctx, mac, name),
62+
_ => return acc.add_macro(ctx, path_ctx, mac, name),
6363
};
6464

6565
let name_ = name.to_smol_str();
@@ -92,7 +92,7 @@ pub(crate) fn complete_derive_path(
9292
item.lookup_by(lookup);
9393
item.add_to(acc);
9494
}
95-
None => acc.add_macro(ctx, mac, name),
95+
None => acc.add_macro(ctx, path_ctx, mac, name),
9696
}
9797
});
9898
acc.add_nameref_keywords_with_colon(ctx);

crates/ide-completion/src/completions/dot.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext, dot_a
3333
|acc, field, ty| acc.add_tuple_field(ctx, None, field, &ty),
3434
);
3535
}
36-
complete_methods(ctx, &receiver_ty, |func| acc.add_method(ctx, func, None, None));
36+
complete_methods(ctx, &receiver_ty, |func| acc.add_method(ctx, dot_access, func, None, None));
3737
}
3838

3939
pub(crate) fn complete_undotted_self(
@@ -68,7 +68,17 @@ pub(crate) fn complete_undotted_self(
6868
|acc, field, ty| acc.add_tuple_field(ctx, Some(hir::known::SELF_PARAM), field, &ty),
6969
);
7070
complete_methods(ctx, &ty, |func| {
71-
acc.add_method(ctx, func, Some(hir::known::SELF_PARAM), None)
71+
acc.add_method(
72+
ctx,
73+
&DotAccess {
74+
receiver: None,
75+
receiver_ty: None,
76+
kind: DotAccessKind::Method { has_parens: false },
77+
},
78+
func,
79+
Some(hir::known::SELF_PARAM),
80+
None,
81+
)
7282
});
7383
}
7484

0 commit comments

Comments
 (0)