Skip to content

Commit 38ef1fd

Browse files
Better filter mod paths
1 parent ee99620 commit 38ef1fd

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

crates/assists/src/utils/import_assets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ impl ImportAssets {
195195
}
196196
.map(|path| (path, item))
197197
})
198-
.filter(|(use_path, _)| !use_path.segments.is_empty())
198+
.filter(|(use_path, _)| use_path.len() > 1)
199199
.take(20)
200200
.collect::<Vec<_>>();
201201
res.sort_by_key(|(path, _)| path.clone());

crates/completion/src/completions/unqualified_path.rs

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,30 @@ pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionC
4747
fuzzy_completion(acc, ctx).unwrap_or_default()
4848
}
4949

50+
fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
51+
if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
52+
let variants = enum_data.variants(ctx.db);
53+
54+
let module = if let Some(module) = ctx.scope.module() {
55+
// Compute path from the completion site if available.
56+
module
57+
} else {
58+
// Otherwise fall back to the enum's definition site.
59+
enum_data.module(ctx.db)
60+
};
61+
62+
for variant in variants {
63+
if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
64+
// Variants with trivial paths are already added by the existing completion logic,
65+
// so we should avoid adding these twice
66+
if path.segments.len() > 1 {
67+
acc.add_qualified_enum_variant(ctx, variant, path);
68+
}
69+
}
70+
}
71+
}
72+
}
73+
5074
// TODO kb add a setting toggle for this feature?
5175
fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
5276
let _p = profile::span("fuzzy_completion®");
@@ -71,6 +95,7 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
7195
ScopeDef::MacroDef(macro_def),
7296
)),
7397
})
98+
.filter(|(mod_path, _)| mod_path.len() > 1)
7499
.filter_map(|(mod_path, definition)| {
75100
let mut resolution_with_missing_import = render_resolution(
76101
RenderContext::new(ctx),
@@ -89,36 +114,13 @@ fn fuzzy_completion(acc: &mut Completions, ctx: &CompletionContext) -> Option<()
89114
resolution_with_missing_import.update_text_edit(text_edits.finish());
90115

91116
Some(resolution_with_missing_import)
92-
});
117+
})
118+
.take(20);
93119

94120
acc.add_all(possible_imports);
95121
Some(())
96122
}
97123

98-
fn complete_enum_variants(acc: &mut Completions, ctx: &CompletionContext, ty: &Type) {
99-
if let Some(Adt::Enum(enum_data)) = ty.as_adt() {
100-
let variants = enum_data.variants(ctx.db);
101-
102-
let module = if let Some(module) = ctx.scope.module() {
103-
// Compute path from the completion site if available.
104-
module
105-
} else {
106-
// Otherwise fall back to the enum's definition site.
107-
enum_data.module(ctx.db)
108-
};
109-
110-
for variant in variants {
111-
if let Some(path) = module.find_use_path(ctx.db, ModuleDef::from(variant)) {
112-
// Variants with trivial paths are already added by the existing completion logic,
113-
// so we should avoid adding these twice
114-
if path.segments.len() > 1 {
115-
acc.add_qualified_enum_variant(ctx, variant, path);
116-
}
117-
}
118-
}
119-
}
120-
}
121-
122124
#[cfg(test)]
123125
mod tests {
124126
use expect_test::{expect, Expect};

0 commit comments

Comments
 (0)