Skip to content

Commit 372de25

Browse files
committed
Simplify
1 parent 8d48b53 commit 372de25

File tree

4 files changed

+21
-32
lines changed

4 files changed

+21
-32
lines changed

crates/ide_completion/src/completions.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,19 @@ impl Completions {
9999
item.add_to(self);
100100
}
101101

102+
pub(crate) fn add_nameref_keywords(&mut self, ctx: &CompletionContext) {
103+
["self::", "super::", "crate::"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
104+
}
105+
106+
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
107+
ctx.process_all_names(&mut |name, res| match res {
108+
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
109+
self.add_resolution(ctx, name, res);
110+
}
111+
_ => (),
112+
});
113+
}
114+
102115
pub(crate) fn add_resolution(
103116
&mut self,
104117
ctx: &CompletionContext,

crates/ide_completion/src/completions/attribute.rs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
//!
33
//! This module uses a bit of static metadata to provide completions for builtin-in attributes and lints.
44
5-
use hir::ScopeDef;
65
use ide_db::{
76
helpers::{
87
generated_lints::{
@@ -103,22 +102,15 @@ pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext)
103102
return;
104103
}
105104
// fresh use tree with leading colon2, only show crate roots
106-
None if is_absolute_path => {
107-
ctx.process_all_names(&mut |name, res| match res {
108-
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
109-
acc.add_resolution(ctx, name, res);
110-
}
111-
_ => (),
112-
});
113-
}
105+
None if is_absolute_path => acc.add_crate_roots(ctx),
114106
// only show modules in a fresh UseTree
115107
None => {
116108
ctx.process_all_names(&mut |name, def| {
117109
if let Some(def) = module_or_attr(def) {
118110
acc.add_resolution(ctx, name, def);
119111
}
120112
});
121-
["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
113+
acc.add_nameref_keywords(ctx);
122114
}
123115
}
124116

crates/ide_completion/src/completions/pattern.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,6 @@ fn pattern_path_completion(
141141
_ => return,
142142
};
143143

144-
// Note associated consts cannot be referenced in patterns
145144
if let Some(hir::Adt::Enum(e)) = ty.as_adt() {
146145
e.variants(ctx.db)
147146
.into_iter()
@@ -157,9 +156,9 @@ fn pattern_path_completion(
157156
ctx.module,
158157
None,
159158
|_ty, item| {
160-
// We might iterate candidates of a trait multiple times here, so deduplicate
161-
// them.
159+
// Note associated consts cannot be referenced in patterns
162160
if let AssocItem::TypeAlias(ta) = item {
161+
// We might iterate candidates of a trait multiple times here, so deduplicate them.
163162
if seen.insert(item) {
164163
acc.add_type_alias(ctx, ta);
165164
}
@@ -174,25 +173,15 @@ fn pattern_path_completion(
174173
}
175174
// qualifier can only be none here if we are in a TuplePat or RecordPat in which case special characters have to follow the path
176175
// so executing the rest of this completion doesn't make sense
177-
// fresh use tree with leading colon2, only show crate roots
178-
None if *is_absolute_path => {
179-
cov_mark::hit!(use_tree_crate_roots_only);
180-
ctx.process_all_names(&mut |name, res| match res {
181-
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
182-
acc.add_resolution(ctx, name, res);
183-
}
184-
_ => (),
185-
});
186-
}
187-
// only show modules in a fresh UseTree
176+
None if *is_absolute_path => acc.add_crate_roots(ctx),
188177
None => {
189178
cov_mark::hit!(unqualified_path_only_modules_in_import);
190179
ctx.process_all_names(&mut |name, res| {
191180
if let ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) = res {
192181
acc.add_resolution(ctx, name, res);
193182
}
194183
});
195-
["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
184+
acc.add_nameref_keywords(ctx);
196185
}
197186
}
198187
}

crates/ide_completion/src/completions/use_.rs

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
7979
// fresh use tree with leading colon2, only show crate roots
8080
None if is_absolute_path => {
8181
cov_mark::hit!(use_tree_crate_roots_only);
82-
ctx.process_all_names(&mut |name, res| match res {
83-
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
84-
acc.add_resolution(ctx, name, res);
85-
}
86-
_ => (),
87-
});
82+
acc.add_crate_roots(ctx);
8883
}
8984
// only show modules in a fresh UseTree
9085
None => {
@@ -94,7 +89,7 @@ pub(crate) fn complete_use_tree(acc: &mut Completions, ctx: &CompletionContext)
9489
acc.add_resolution(ctx, name, res);
9590
}
9691
});
97-
["self::", "super::", "crate::"].into_iter().for_each(|kw| acc.add_keyword(ctx, kw));
92+
acc.add_nameref_keywords(ctx);
9893
}
9994
}
10095
}

0 commit comments

Comments
 (0)