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

Commit a0c1816

Browse files
committed
Simplify
1 parent 98c0578 commit a0c1816

File tree

3 files changed

+113
-106
lines changed

3 files changed

+113
-106
lines changed

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

Lines changed: 67 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -32,74 +32,11 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
3232
_ => return,
3333
};
3434

35-
let in_item_list = matches!(kind, Some(ItemListKind::SourceFile | ItemListKind::Module) | None);
36-
let in_assoc_non_trait_impl = matches!(kind, Some(ItemListKind::Impl | ItemListKind::Trait));
37-
let in_extern_block = matches!(kind, Some(ItemListKind::ExternBlock));
38-
let in_trait = matches!(kind, Some(ItemListKind::Trait));
39-
let in_trait_impl = matches!(kind, Some(ItemListKind::TraitImpl));
40-
let in_inherent_impl = matches!(kind, Some(ItemListKind::Impl));
41-
let no_qualifiers = ctx.qualifier_ctx.vis_node.is_none();
42-
let in_block = matches!(kind, None);
43-
44-
if in_trait_impl {
35+
if matches!(kind, Some(ItemListKind::TraitImpl)) {
4536
trait_impl::complete_trait_impl(acc, ctx);
4637
}
47-
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
48-
49-
'block: loop {
50-
if ctx.is_non_trivial_path() {
51-
break 'block;
52-
}
53-
if !in_trait_impl {
54-
if ctx.qualifier_ctx.unsafe_tok.is_some() {
55-
if in_item_list || in_assoc_non_trait_impl {
56-
add_keyword("fn", "fn $1($2) {\n $0\n}");
57-
}
58-
if in_item_list {
59-
add_keyword("trait", "trait $1 {\n $0\n}");
60-
if no_qualifiers {
61-
add_keyword("impl", "impl $1 {\n $0\n}");
62-
}
63-
}
64-
break 'block;
65-
}
66-
67-
if in_item_list {
68-
add_keyword("enum", "enum $1 {\n $0\n}");
69-
add_keyword("mod", "mod $0");
70-
add_keyword("static", "static $0");
71-
add_keyword("struct", "struct $0");
72-
add_keyword("trait", "trait $1 {\n $0\n}");
73-
add_keyword("union", "union $1 {\n $0\n}");
74-
add_keyword("use", "use $0");
75-
if no_qualifiers {
76-
add_keyword("impl", "impl $1 {\n $0\n}");
77-
}
78-
}
7938

80-
if !in_trait && !in_block && no_qualifiers {
81-
add_keyword("pub(crate)", "pub(crate)");
82-
add_keyword("pub(super)", "pub(super)");
83-
add_keyword("pub", "pub");
84-
}
85-
86-
if in_extern_block {
87-
add_keyword("fn", "fn $1($2);");
88-
} else {
89-
if !in_inherent_impl {
90-
if !in_trait {
91-
add_keyword("extern", "extern $0");
92-
}
93-
add_keyword("type", "type $0");
94-
}
95-
96-
add_keyword("fn", "fn $1($2) {\n $0\n}");
97-
add_keyword("unsafe", "unsafe");
98-
add_keyword("const", "const $0");
99-
}
100-
}
101-
break 'block;
102-
}
39+
add_keywords(acc, ctx, kind);
10340

10441
if kind.is_none() {
10542
// this is already handled by expression
@@ -132,3 +69,68 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
13269
None => {}
13370
}
13471
}
72+
73+
fn add_keywords(acc: &mut Completions, ctx: &CompletionContext, kind: Option<&ItemListKind>) {
74+
if ctx.is_non_trivial_path() {
75+
return;
76+
}
77+
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
78+
79+
let in_item_list = matches!(kind, Some(ItemListKind::SourceFile | ItemListKind::Module) | None);
80+
let in_assoc_non_trait_impl = matches!(kind, Some(ItemListKind::Impl | ItemListKind::Trait));
81+
let in_extern_block = matches!(kind, Some(ItemListKind::ExternBlock));
82+
let in_trait = matches!(kind, Some(ItemListKind::Trait));
83+
let in_trait_impl = matches!(kind, Some(ItemListKind::TraitImpl));
84+
let in_inherent_impl = matches!(kind, Some(ItemListKind::Impl));
85+
let no_qualifiers = ctx.qualifier_ctx.vis_node.is_none();
86+
let in_block = matches!(kind, None);
87+
88+
if !in_trait_impl {
89+
if ctx.qualifier_ctx.unsafe_tok.is_some() {
90+
if in_item_list || in_assoc_non_trait_impl {
91+
add_keyword("fn", "fn $1($2) {\n $0\n}");
92+
}
93+
if in_item_list {
94+
add_keyword("trait", "trait $1 {\n $0\n}");
95+
if no_qualifiers {
96+
add_keyword("impl", "impl $1 {\n $0\n}");
97+
}
98+
}
99+
return;
100+
}
101+
102+
if in_item_list {
103+
add_keyword("enum", "enum $1 {\n $0\n}");
104+
add_keyword("mod", "mod $0");
105+
add_keyword("static", "static $0");
106+
add_keyword("struct", "struct $0");
107+
add_keyword("trait", "trait $1 {\n $0\n}");
108+
add_keyword("union", "union $1 {\n $0\n}");
109+
add_keyword("use", "use $0");
110+
if no_qualifiers {
111+
add_keyword("impl", "impl $1 {\n $0\n}");
112+
}
113+
}
114+
115+
if !in_trait && !in_block && no_qualifiers {
116+
add_keyword("pub(crate)", "pub(crate)");
117+
add_keyword("pub(super)", "pub(super)");
118+
add_keyword("pub", "pub");
119+
}
120+
121+
if in_extern_block {
122+
add_keyword("fn", "fn $1($2);");
123+
} else {
124+
if !in_inherent_impl {
125+
if !in_trait {
126+
add_keyword("extern", "extern $0");
127+
}
128+
add_keyword("type", "type $0");
129+
}
130+
131+
add_keyword("fn", "fn $1($2) {\n $0\n}");
132+
add_keyword("unsafe", "unsafe");
133+
add_keyword("const", "const $0");
134+
}
135+
}
136+
}

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

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

4545
use crate::{
46-
context::{ItemListKind, NameContext, NameKind, NameRefContext, PathCompletionCtx, PathKind},
46+
context::{
47+
IdentContext, ItemListKind, NameContext, NameKind, NameRefContext, PathCompletionCtx,
48+
PathKind,
49+
},
4750
CompletionContext, CompletionItem, CompletionItemKind, CompletionRelevance, Completions,
4851
};
4952

@@ -78,47 +81,49 @@ pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext
7881
}
7982

8083
fn completion_match(ctx: &CompletionContext) -> Option<(ImplCompletionKind, TextRange, ast::Impl)> {
81-
let token = ctx.token.clone();
82-
83-
if let Some(NameContext { name, kind, .. }) = ctx.name_ctx() {
84-
let kind = match kind {
85-
NameKind::Const => ImplCompletionKind::Const,
86-
NameKind::Function => ImplCompletionKind::Fn,
87-
NameKind::TypeAlias => ImplCompletionKind::TypeAlias,
88-
_ => return None,
89-
};
90-
let item = match name {
91-
Some(name) => name.syntax().parent(),
92-
None => {
93-
if token.kind() == SyntaxKind::WHITESPACE { token.prev_token()? } else { token }
94-
.parent()
95-
}
96-
}?;
97-
return Some((
98-
kind,
99-
replacement_range(ctx, &item),
100-
// item -> ASSOC_ITEM_LIST -> IMPL
101-
ast::Impl::cast(item.parent()?.parent()?)?,
102-
));
103-
} else if let Some(NameRefContext {
104-
nameref,
105-
path_ctx:
106-
Some(PathCompletionCtx { kind: PathKind::Item { kind: ItemListKind::TraitImpl }, .. }),
107-
..
108-
}) = ctx.nameref_ctx()
109-
{
110-
if !ctx.is_non_trivial_path() {
111-
return Some((
112-
ImplCompletionKind::All,
113-
match nameref {
114-
Some(name) => name.syntax().text_range(),
115-
None => TextRange::empty(ctx.position.offset),
116-
},
117-
ctx.impl_def.clone()?,
118-
));
84+
match &ctx.ident_ctx {
85+
IdentContext::Name(NameContext { name, kind, .. }) => {
86+
let kind = match kind {
87+
NameKind::Const => ImplCompletionKind::Const,
88+
NameKind::Function => ImplCompletionKind::Fn,
89+
NameKind::TypeAlias => ImplCompletionKind::TypeAlias,
90+
_ => return None,
91+
};
92+
let token = ctx.token.clone();
93+
let item = match name {
94+
Some(name) => name.syntax().parent(),
95+
None => {
96+
if token.kind() == SyntaxKind::WHITESPACE { token.prev_token()? } else { token }
97+
.parent()
98+
}
99+
}?;
100+
Some((
101+
kind,
102+
replacement_range(ctx, &item),
103+
// item -> ASSOC_ITEM_LIST -> IMPL
104+
ast::Impl::cast(item.parent()?.parent()?)?,
105+
))
119106
}
107+
IdentContext::NameRef(NameRefContext {
108+
nameref,
109+
path_ctx:
110+
Some(
111+
path_ctx @ PathCompletionCtx {
112+
kind: PathKind::Item { kind: ItemListKind::TraitImpl },
113+
..
114+
},
115+
),
116+
..
117+
}) if path_ctx.is_trivial_path() => Some((
118+
ImplCompletionKind::All,
119+
match nameref {
120+
Some(name) => name.syntax().text_range(),
121+
None => TextRange::empty(ctx.position.offset),
122+
},
123+
ctx.impl_def.clone()?,
124+
)),
125+
_ => None,
120126
}
121-
None
122127
}
123128

124129
fn add_function_impl(

crates/ide-completion/src/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ pub(crate) struct PathCompletionCtx {
109109
}
110110

111111
impl PathCompletionCtx {
112-
fn is_trivial_path(&self) -> bool {
112+
pub(super) fn is_trivial_path(&self) -> bool {
113113
matches!(
114114
self,
115115
PathCompletionCtx {

0 commit comments

Comments
 (0)