Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9048332

Browse files
committed
Remove some usages of Completions::add_resolution
1 parent 7685245 commit 9048332

File tree

6 files changed

+62
-54
lines changed

6 files changed

+62
-54
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub(crate) mod vis;
2222

2323
use std::iter;
2424

25-
use hir::{db::HirDatabase, known, ScopeDef};
25+
use hir::{known, ScopeDef};
2626
use ide_db::SymbolKind;
2727
use syntax::ast;
2828

@@ -46,22 +46,6 @@ use crate::{
4646
CompletionContext, CompletionItem, CompletionItemKind,
4747
};
4848

49-
fn module_or_attr(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
50-
match def {
51-
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(db) => Some(def),
52-
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
53-
_ => None,
54-
}
55-
}
56-
57-
fn module_or_fn_macro(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
58-
match def {
59-
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(db) => Some(def),
60-
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
61-
_ => None,
62-
}
63-
}
64-
6549
/// Represents an in-progress set of completions being built.
6650
#[derive(Debug, Default)]
6751
pub struct Completions {
@@ -184,6 +168,15 @@ impl Completions {
184168
self.add(render_resolution_simple(RenderContext::new(ctx), local_name, resolution).build());
185169
}
186170

171+
pub(crate) fn add_module(
172+
&mut self,
173+
ctx: &CompletionContext,
174+
module: hir::Module,
175+
local_name: hir::Name,
176+
) {
177+
self.add_resolution(ctx, local_name, hir::ScopeDef::ModuleDef(module.into()));
178+
}
179+
187180
pub(crate) fn add_macro(
188181
&mut self,
189182
ctx: &CompletionContext,
@@ -486,16 +479,19 @@ pub(super) fn complete_name_ref(
486479
match kind {
487480
NameRefKind::Path(path_ctx) => {
488481
flyimport::import_on_the_fly_path(acc, ctx, path_ctx);
482+
489483
match &path_ctx.kind {
490484
PathKind::Expr { expr_ctx } => {
491-
dot::complete_undotted_self(acc, ctx, path_ctx, expr_ctx);
492485
expr::complete_expr_path(acc, ctx, path_ctx, expr_ctx);
486+
487+
dot::complete_undotted_self(acc, ctx, path_ctx, expr_ctx);
493488
item_list::complete_item_list_in_expr(acc, ctx, path_ctx, expr_ctx);
494489
record::complete_record_expr_func_update(acc, ctx, path_ctx, expr_ctx);
495490
snippet::complete_expr_snippet(acc, ctx, path_ctx, expr_ctx);
496491
}
497492
PathKind::Type { location } => {
498493
r#type::complete_type_path(acc, ctx, path_ctx, location);
494+
499495
match location {
500496
TypeLocation::TupleField => {
501497
field::complete_field_list_tuple_variant(acc, ctx, path_ctx);
@@ -511,13 +507,14 @@ pub(super) fn complete_name_ref(
511507
}
512508
}
513509
PathKind::Attr { attr_ctx } => {
514-
attribute::complete_attribute(acc, ctx, path_ctx, attr_ctx);
510+
attribute::complete_attribute_path(acc, ctx, path_ctx, attr_ctx);
515511
}
516512
PathKind::Derive { existing_derives } => {
517-
attribute::complete_derive(acc, ctx, path_ctx, existing_derives);
513+
attribute::complete_derive_path(acc, ctx, path_ctx, existing_derives);
518514
}
519515
PathKind::Item { kind } => {
520516
item_list::complete_item_list(acc, ctx, path_ctx, kind);
517+
521518
snippet::complete_item_snippet(acc, ctx, path_ctx, kind);
522519
if let ItemListKind::TraitImpl(impl_) = kind {
523520
item_list::trait_impl::complete_trait_impl_item_by_name(
@@ -532,7 +529,7 @@ pub(super) fn complete_name_ref(
532529
vis::complete_vis_path(acc, ctx, path_ctx, has_in_token);
533530
}
534531
PathKind::Use => {
535-
use_::complete_use_tree(acc, ctx, path_ctx, nameref);
532+
use_::complete_use_path(acc, ctx, path_ctx, nameref);
536533
}
537534
}
538535
}

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use syntax::{
1717
};
1818

1919
use crate::{
20-
completions::module_or_attr,
2120
context::{AttrCtx, CompletionContext, PathCompletionCtx, Qualified},
2221
item::CompletionItem,
2322
Completions,
@@ -28,7 +27,7 @@ mod derive;
2827
mod lint;
2928
mod repr;
3029

31-
pub(crate) use self::derive::complete_derive;
30+
pub(crate) use self::derive::complete_derive_path;
3231

3332
/// Complete inputs to known builtin attributes as well as derive attributes
3433
pub(crate) fn complete_known_attribute_input(
@@ -69,7 +68,7 @@ pub(crate) fn complete_known_attribute_input(
6968
Some(())
7069
}
7170

72-
pub(crate) fn complete_attribute(
71+
pub(crate) fn complete_attribute_path(
7372
acc: &mut Completions,
7473
ctx: &CompletionContext,
7574
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
@@ -88,8 +87,14 @@ pub(crate) fn complete_attribute(
8887
}
8988

9089
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
91-
if let Some(def) = module_or_attr(ctx.db, def) {
92-
acc.add_resolution(ctx, name, def);
90+
match def {
91+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
92+
acc.add_macro(ctx, m, name)
93+
}
94+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
95+
acc.add_module(ctx, m, name)
96+
}
97+
_ => (),
9398
}
9499
}
95100
return;
@@ -98,10 +103,12 @@ pub(crate) fn complete_attribute(
98103
Qualified::Absolute => acc.add_crate_roots(ctx),
99104
// only show modules in a fresh UseTree
100105
Qualified::No => {
101-
ctx.process_all_names(&mut |name, def| {
102-
if let Some(def) = module_or_attr(ctx.db, def) {
103-
acc.add_resolution(ctx, name, def);
106+
ctx.process_all_names(&mut |name, def| match def {
107+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(ctx.db) => {
108+
acc.add_macro(ctx, m, name)
104109
}
110+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
111+
_ => (),
105112
});
106113
acc.add_nameref_keywords_with_colon(ctx);
107114
}

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
Completions,
1111
};
1212

13-
pub(crate) fn complete_derive(
13+
pub(crate) fn complete_derive_path(
1414
acc: &mut Completions,
1515
ctx: &CompletionContext,
1616
PathCompletionCtx { qualified, .. }: &PathCompletionCtx,
@@ -29,15 +29,14 @@ pub(crate) fn complete_derive(
2929
}
3030

3131
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
32-
let add_def = match def {
33-
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac)) => {
34-
!existing_derives.contains(&mac) && mac.is_derive(ctx.db)
32+
match def {
33+
ScopeDef::ModuleDef(hir::ModuleDef::Macro(mac))
34+
if !existing_derives.contains(&mac) && mac.is_derive(ctx.db) =>
35+
{
36+
acc.add_macro(ctx, mac, name)
3537
}
36-
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => true,
37-
_ => false,
38-
};
39-
if add_def {
40-
acc.add_resolution(ctx, name, def);
38+
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
39+
_ => (),
4140
}
4241
}
4342
}
@@ -51,16 +50,16 @@ pub(crate) fn complete_derive(
5150
{
5251
mac
5352
}
54-
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => {
55-
return acc.add_resolution(ctx, name, def);
53+
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
54+
return acc.add_module(ctx, m, name);
5655
}
5756
_ => return,
5857
};
5958

6059
match (core, mac.module(ctx.db).krate()) {
6160
// show derive dependencies for `core`/`std` derives
6261
(Some(core), mac_krate) if core == mac_krate => {}
63-
_ => return acc.add_resolution(ctx, name, def),
62+
_ => return acc.add_macro(ctx, mac, name),
6463
};
6564

6665
let name_ = name.to_smol_str();
@@ -93,7 +92,7 @@ pub(crate) fn complete_derive(
9392
item.lookup_by(lookup);
9493
item.add_to(acc);
9594
}
96-
None => acc.add_resolution(ctx, name, def),
95+
None => acc.add_macro(ctx, mac, name),
9796
}
9897
});
9998
acc.add_nameref_keywords_with_colon(ctx);

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
//! Completion of paths and keywords at item list position.
22
33
use crate::{
4-
completions::module_or_fn_macro,
54
context::{ExprCtx, ItemListKind, PathCompletionCtx, Qualified},
65
CompletionContext, Completions,
76
};
@@ -41,8 +40,14 @@ pub(crate) fn complete_item_list(
4140
..
4241
} => {
4342
for (name, def) in module.scope(ctx.db, Some(ctx.module)) {
44-
if let Some(def) = module_or_fn_macro(ctx.db, def) {
45-
acc.add_resolution(ctx, name, def);
43+
match def {
44+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(ctx.db) => {
45+
acc.add_macro(ctx, m, name)
46+
}
47+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => {
48+
acc.add_module(ctx, m, name)
49+
}
50+
_ => (),
4651
}
4752
}
4853

@@ -52,10 +57,12 @@ pub(crate) fn complete_item_list(
5257
}
5358
Qualified::Absolute => acc.add_crate_roots(ctx),
5459
Qualified::No if ctx.qualifier_ctx.none() => {
55-
ctx.process_all_names(&mut |name, def| {
56-
if let Some(def) = module_or_fn_macro(ctx.db, def) {
57-
acc.add_resolution(ctx, name, def);
60+
ctx.process_all_names(&mut |name, def| match def {
61+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(ctx.db) => {
62+
acc.add_macro(ctx, m, name)
5863
}
64+
hir::ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) => acc.add_module(ctx, m, name),
65+
_ => (),
5966
});
6067
acc.add_nameref_keywords_with_colon(ctx);
6168
}

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
CompletionItem, CompletionItemKind, CompletionRelevance, Completions,
1111
};
1212

13-
pub(crate) fn complete_use_tree(
13+
pub(crate) fn complete_use_path(
1414
acc: &mut Completions,
1515
ctx: &CompletionContext,
1616
PathCompletionCtx { qualified, use_tree_parent, .. }: &PathCompletionCtx,
@@ -96,8 +96,8 @@ pub(crate) fn complete_use_tree(
9696
cov_mark::hit!(unqualified_path_selected_only);
9797
ctx.process_all_names(&mut |name, res| {
9898
match res {
99-
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => {
100-
acc.add_resolution(ctx, name, res);
99+
ScopeDef::ModuleDef(hir::ModuleDef::Module(module)) => {
100+
acc.add_module(ctx, module, name);
101101
}
102102
ScopeDef::ModuleDef(hir::ModuleDef::Adt(hir::Adt::Enum(e))) => {
103103
// exclude prelude enum

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
//! Completion for visibility specifiers.
22
3-
use hir::ScopeDef;
4-
53
use crate::{
64
context::{CompletionContext, PathCompletionCtx, Qualified},
75
Completions,
@@ -25,7 +23,7 @@ pub(crate) fn complete_vis_path(
2523
if let Some(next) = next_towards_current {
2624
if let Some(name) = next.name(ctx.db) {
2725
cov_mark::hit!(visibility_qualified);
28-
acc.add_resolution(ctx, name, ScopeDef::ModuleDef(next.into()));
26+
acc.add_module(ctx, next, name);
2927
}
3028
}
3129

0 commit comments

Comments
 (0)