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

Commit 5c69df9

Browse files
committed
Split remaining completion calls on the context kinds
1 parent eb9b360 commit 5c69df9

File tree

6 files changed

+130
-118
lines changed

6 files changed

+130
-118
lines changed

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

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
//! Completion of field list position.
22
33
use crate::{
4-
context::{NameContext, NameKind, PathCompletionCtx, PathKind, Qualified, TypeLocation},
4+
context::{PathCompletionCtx, PathKind, Qualified, TypeLocation},
55
CompletionContext, Completions,
66
};
77

@@ -30,17 +30,11 @@ pub(crate) fn complete_field_list_tuple_variant(
3030
}
3131
}
3232

33-
pub(crate) fn complete_field_list_record_variant(
34-
acc: &mut Completions,
35-
ctx: &CompletionContext,
36-
name_ctx: &NameContext,
37-
) {
38-
if let NameContext { kind: NameKind::RecordField, .. } = name_ctx {
39-
if ctx.qualifier_ctx.vis_node.is_none() {
40-
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
41-
add_keyword("pub(crate)", "pub(crate)");
42-
add_keyword("pub(super)", "pub(super)");
43-
add_keyword("pub", "pub");
44-
}
33+
pub(crate) fn complete_field_list_record_variant(acc: &mut Completions, ctx: &CompletionContext) {
34+
if ctx.qualifier_ctx.vis_node.is_none() {
35+
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
36+
add_keyword("pub(crate)", "pub(crate)");
37+
add_keyword("pub(super)", "pub(super)");
38+
add_keyword("pub", "pub");
4539
}
4640
}

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

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,7 @@ use syntax::{
4343
use text_edit::TextEdit;
4444

4545
use crate::{
46-
context::{
47-
ItemListKind, NameContext, NameKind, NameRefContext, NameRefKind, PathCompletionCtx,
48-
PathKind,
49-
},
46+
context::{ItemListKind, PathCompletionCtx, PathKind},
5047
CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance, Completions,
5148
};
5249

@@ -58,17 +55,36 @@ enum ImplCompletionKind {
5855
Const,
5956
}
6057

61-
pub(crate) fn complete_trait_impl_name(
58+
pub(crate) fn complete_trait_impl_const(
6259
acc: &mut Completions,
6360
ctx: &CompletionContext,
64-
NameContext { name, kind, .. }: &NameContext,
61+
name: &Option<ast::Name>,
62+
) -> Option<()> {
63+
complete_trait_impl_name(acc, ctx, name, ImplCompletionKind::Const)
64+
}
65+
66+
pub(crate) fn complete_trait_impl_type_alias(
67+
acc: &mut Completions,
68+
ctx: &CompletionContext,
69+
name: &Option<ast::Name>,
70+
) -> Option<()> {
71+
complete_trait_impl_name(acc, ctx, name, ImplCompletionKind::TypeAlias)
72+
}
73+
74+
pub(crate) fn complete_trait_impl_fn(
75+
acc: &mut Completions,
76+
ctx: &CompletionContext,
77+
name: &Option<ast::Name>,
78+
) -> Option<()> {
79+
complete_trait_impl_name(acc, ctx, name, ImplCompletionKind::Fn)
80+
}
81+
82+
fn complete_trait_impl_name(
83+
acc: &mut Completions,
84+
ctx: &CompletionContext,
85+
name: &Option<ast::Name>,
86+
kind: ImplCompletionKind,
6587
) -> Option<()> {
66-
let kind = match kind {
67-
NameKind::Const => ImplCompletionKind::Const,
68-
NameKind::Function => ImplCompletionKind::Fn,
69-
NameKind::TypeAlias => ImplCompletionKind::TypeAlias,
70-
_ => return None,
71-
};
7288
let token = ctx.token.clone();
7389
let item = match name {
7490
Some(name) => name.syntax().parent(),
@@ -89,23 +105,18 @@ pub(crate) fn complete_trait_impl_name(
89105
pub(crate) fn complete_trait_impl_name_ref(
90106
acc: &mut Completions,
91107
ctx: &CompletionContext,
92-
name_ref_ctx: &NameRefContext,
108+
path_ctx: &PathCompletionCtx,
109+
name_ref: &Option<ast::NameRef>,
93110
) -> Option<()> {
94-
match name_ref_ctx {
95-
NameRefContext {
96-
nameref,
97-
kind:
98-
NameRefKind::Path(
99-
path_ctx @ PathCompletionCtx {
100-
kind: PathKind::Item { kind: ItemListKind::TraitImpl(Some(impl_)) },
101-
..
102-
},
103-
),
111+
match path_ctx {
112+
PathCompletionCtx {
113+
kind: PathKind::Item { kind: ItemListKind::TraitImpl(Some(impl_)) },
114+
..
104115
} if path_ctx.is_trivial_path() => complete_trait_impl(
105116
acc,
106117
ctx,
107118
ImplCompletionKind::All,
108-
match nameref {
119+
match name_ref {
109120
Some(name) => name.syntax().text_range(),
110121
None => ctx.source_range(),
111122
},

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,14 @@ use ide_db::{
99
};
1010
use syntax::{ast, AstNode, SyntaxKind};
1111

12-
use crate::{
13-
context::{CompletionContext, NameContext, NameKind},
14-
CompletionItem, Completions,
15-
};
12+
use crate::{context::CompletionContext, CompletionItem, Completions};
1613

1714
/// Complete mod declaration, i.e. `mod $0;`
1815
pub(crate) fn complete_mod(
1916
acc: &mut Completions,
2017
ctx: &CompletionContext,
21-
name_ctx: &NameContext,
18+
mod_under_caret: &ast::Module,
2219
) -> Option<()> {
23-
let mod_under_caret = match name_ctx {
24-
NameContext { kind: NameKind::Module(mod_under_caret), .. } => mod_under_caret,
25-
_ => return None,
26-
};
2720
if mod_under_caret.item_list().is_some() {
2821
return None;
2922
}

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

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,21 @@ use ide_db::{FxHashSet, SymbolKind};
55
use syntax::{ast, AstNode};
66

77
use crate::{
8-
context::{
9-
CompletionContext, NameRefContext, NameRefKind, PathCompletionCtx, PathKind, Qualified,
10-
},
8+
context::{CompletionContext, PathCompletionCtx, PathKind, Qualified},
119
item::Builder,
1210
CompletionItem, CompletionItemKind, CompletionRelevance, Completions,
1311
};
1412

1513
pub(crate) fn complete_use_tree(
1614
acc: &mut Completions,
1715
ctx: &CompletionContext,
18-
name_ref_ctx: &NameRefContext,
16+
path_ctx: &PathCompletionCtx,
17+
name_ref: &Option<ast::NameRef>,
1918
) {
20-
let (qualified, name_ref, use_tree_parent) = match name_ref_ctx {
21-
NameRefContext {
22-
kind:
23-
NameRefKind::Path(PathCompletionCtx {
24-
kind: PathKind::Use,
25-
qualified,
26-
use_tree_parent,
27-
..
28-
}),
29-
nameref,
30-
..
31-
} => (qualified, nameref, use_tree_parent),
19+
let (qualified, name_ref, use_tree_parent) = match path_ctx {
20+
PathCompletionCtx { kind: PathKind::Use, qualified, use_tree_parent, .. } => {
21+
(qualified, name_ref, use_tree_parent)
22+
}
3223
_ => return,
3324
};
3425

crates/ide-completion/src/lib.rs

Lines changed: 77 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use text_edit::TextEdit;
2424

2525
use crate::{
2626
completions::Completions,
27-
context::{CompletionContext, IdentContext, NameKind, NameRefContext, NameRefKind},
27+
context::{
28+
CompletionContext, IdentContext, NameContext, NameKind, NameRefContext, NameRefKind,
29+
},
2830
};
2931

3032
pub use crate::{
@@ -164,63 +166,86 @@ pub fn completions(
164166
let acc = &mut completions;
165167

166168
match &ctx.ident_ctx {
167-
IdentContext::Name(name_ctx) => {
168-
completions::field::complete_field_list_record_variant(acc, ctx, name_ctx);
169-
completions::item_list::trait_impl::complete_trait_impl_name(acc, ctx, name_ctx);
170-
completions::mod_::complete_mod(acc, ctx, name_ctx);
171-
if let NameKind::IdentPat(pattern_ctx) = &name_ctx.kind {
169+
IdentContext::Name(NameContext { name, kind }) => match kind {
170+
NameKind::Const => {
171+
completions::item_list::trait_impl::complete_trait_impl_const(acc, ctx, name);
172+
}
173+
NameKind::Function => {
174+
completions::item_list::trait_impl::complete_trait_impl_fn(acc, ctx, name);
175+
}
176+
NameKind::IdentPat(pattern_ctx) => {
172177
completions::flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
173178
completions::fn_param::complete_fn_param(acc, ctx, pattern_ctx);
174179
completions::pattern::complete_pattern(acc, ctx, pattern_ctx);
175180
completions::record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
176181
}
177-
}
178-
IdentContext::NameRef(name_ctx @ NameRefContext { kind, .. }) => {
179-
completions::item_list::trait_impl::complete_trait_impl_name_ref(
180-
acc, ctx, name_ctx,
181-
);
182-
completions::use_::complete_use_tree(acc, ctx, name_ctx);
183-
184-
match kind {
185-
NameRefKind::Path(path_ctx) => {
186-
completions::attribute::complete_attribute(acc, ctx, path_ctx);
187-
completions::attribute::complete_derive(acc, ctx, path_ctx);
188-
completions::dot::complete_undotted_self(acc, ctx, path_ctx);
189-
completions::expr::complete_expr_path(acc, ctx, path_ctx);
190-
completions::field::complete_field_list_tuple_variant(acc, ctx, path_ctx);
191-
completions::flyimport::import_on_the_fly_path(acc, ctx, path_ctx);
192-
completions::item_list::complete_item_list(acc, ctx, path_ctx);
193-
completions::pattern::pattern_path_completion(acc, ctx, path_ctx);
194-
completions::r#type::complete_inferred_type(acc, ctx, path_ctx);
195-
completions::r#type::complete_type_path(acc, ctx, path_ctx);
196-
completions::record::complete_record_expr_func_update(acc, ctx, path_ctx);
197-
completions::snippet::complete_expr_snippet(acc, ctx, path_ctx);
198-
completions::snippet::complete_item_snippet(acc, ctx, path_ctx);
199-
completions::vis::complete_vis_path(acc, ctx, path_ctx);
200-
}
201-
NameRefKind::DotAccess(dot_access) => {
202-
completions::flyimport::import_on_the_fly_dot(acc, ctx, dot_access);
203-
completions::dot::complete_dot(acc, ctx, dot_access);
204-
completions::postfix::complete_postfix(acc, ctx, dot_access);
205-
}
206-
NameRefKind::Keyword(item) => {
207-
completions::keyword::complete_special_keywords(acc, ctx, item);
208-
}
209-
NameRefKind::RecordExpr(record_expr) => {
210-
completions::record::complete_record_expr_fields_record_expr(
211-
acc,
212-
ctx,
213-
record_expr,
214-
);
215-
}
216-
NameRefKind::Pattern(pattern_ctx) => {
217-
completions::flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
218-
completions::fn_param::complete_fn_param(acc, ctx, pattern_ctx);
219-
completions::pattern::complete_pattern(acc, ctx, pattern_ctx);
220-
completions::record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
221-
}
182+
NameKind::Module(mod_under_caret) => {
183+
completions::mod_::complete_mod(acc, ctx, mod_under_caret);
222184
}
223-
}
185+
NameKind::TypeAlias => {
186+
completions::item_list::trait_impl::complete_trait_impl_type_alias(
187+
acc, ctx, name,
188+
);
189+
}
190+
NameKind::RecordField => {
191+
completions::field::complete_field_list_record_variant(acc, ctx);
192+
}
193+
NameKind::ConstParam
194+
| NameKind::Enum
195+
| NameKind::MacroDef
196+
| NameKind::MacroRules
197+
| NameKind::Rename
198+
| NameKind::SelfParam
199+
| NameKind::Static
200+
| NameKind::Struct
201+
| NameKind::Trait
202+
| NameKind::TypeParam
203+
| NameKind::Union
204+
| NameKind::Variant => (),
205+
},
206+
IdentContext::NameRef(NameRefContext { kind, nameref }) => match kind {
207+
NameRefKind::Path(path_ctx) => {
208+
completions::attribute::complete_attribute(acc, ctx, path_ctx);
209+
completions::attribute::complete_derive(acc, ctx, path_ctx);
210+
completions::dot::complete_undotted_self(acc, ctx, path_ctx);
211+
completions::expr::complete_expr_path(acc, ctx, path_ctx);
212+
completions::field::complete_field_list_tuple_variant(acc, ctx, path_ctx);
213+
completions::flyimport::import_on_the_fly_path(acc, ctx, path_ctx);
214+
completions::item_list::complete_item_list(acc, ctx, path_ctx);
215+
completions::item_list::trait_impl::complete_trait_impl_name_ref(
216+
acc, ctx, path_ctx, nameref,
217+
);
218+
completions::pattern::pattern_path_completion(acc, ctx, path_ctx);
219+
completions::r#type::complete_inferred_type(acc, ctx, path_ctx);
220+
completions::r#type::complete_type_path(acc, ctx, path_ctx);
221+
completions::record::complete_record_expr_func_update(acc, ctx, path_ctx);
222+
completions::snippet::complete_expr_snippet(acc, ctx, path_ctx);
223+
completions::snippet::complete_item_snippet(acc, ctx, path_ctx);
224+
completions::use_::complete_use_tree(acc, ctx, path_ctx, nameref);
225+
completions::vis::complete_vis_path(acc, ctx, path_ctx);
226+
}
227+
NameRefKind::DotAccess(dot_access) => {
228+
completions::flyimport::import_on_the_fly_dot(acc, ctx, dot_access);
229+
completions::dot::complete_dot(acc, ctx, dot_access);
230+
completions::postfix::complete_postfix(acc, ctx, dot_access);
231+
}
232+
NameRefKind::Keyword(item) => {
233+
completions::keyword::complete_special_keywords(acc, ctx, item);
234+
}
235+
NameRefKind::RecordExpr(record_expr) => {
236+
completions::record::complete_record_expr_fields_record_expr(
237+
acc,
238+
ctx,
239+
record_expr,
240+
);
241+
}
242+
NameRefKind::Pattern(pattern_ctx) => {
243+
completions::flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
244+
completions::fn_param::complete_fn_param(acc, ctx, pattern_ctx);
245+
completions::pattern::complete_pattern(acc, ctx, pattern_ctx);
246+
completions::record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
247+
}
248+
},
224249
IdentContext::Lifetime(lifetime_ctx) => {
225250
completions::lifetime::complete_label(acc, ctx, lifetime_ctx);
226251
completions::lifetime::complete_lifetime(acc, ctx, lifetime_ctx);

crates/ide-completion/src/snippet.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
//!
33
//! Actual logic is implemented in [`crate::completions::postfix`] and [`crate::completions::snippet`] respectively.
44
5-
use std::ops::Deref;
6-
75
// Feature: User Snippet Completions
86
//
97
// rust-analyzer allows the user to define custom (postfix)-snippets that may depend on items to be accessible for the current scope to be applicable.
@@ -146,8 +144,8 @@ impl Snippet {
146144
let (requires, snippet, description) = validate_snippet(snippet, description, requires)?;
147145
Some(Snippet {
148146
// Box::into doesn't work as that has a Copy bound 😒
149-
postfix_triggers: postfix_triggers.iter().map(Deref::deref).map(Into::into).collect(),
150-
prefix_triggers: prefix_triggers.iter().map(Deref::deref).map(Into::into).collect(),
147+
postfix_triggers: postfix_triggers.iter().map(String::as_str).map(Into::into).collect(),
148+
prefix_triggers: prefix_triggers.iter().map(String::as_str).map(Into::into).collect(),
151149
scope,
152150
snippet,
153151
description,

0 commit comments

Comments
 (0)