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

Commit 2a60b84

Browse files
committed
Deduplicate
1 parent 522f665 commit 2a60b84

File tree

5 files changed

+27
-70
lines changed

5 files changed

+27
-70
lines changed

crates/ide-completion/src/completions.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,26 @@ impl Completions {
111111
["self", "super", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
112112
}
113113

114+
pub(crate) fn add_keyword_snippet(&mut self, ctx: &CompletionContext, kw: &str, snippet: &str) {
115+
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
116+
117+
match ctx.config.snippet_cap {
118+
Some(cap) => {
119+
if snippet.ends_with('}') && ctx.incomplete_let {
120+
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
121+
cov_mark::hit!(let_semi);
122+
item.insert_snippet(cap, format!("{};", snippet));
123+
} else {
124+
item.insert_snippet(cap, snippet);
125+
}
126+
}
127+
None => {
128+
item.insert_text(if snippet.contains('$') { kw } else { snippet });
129+
}
130+
};
131+
item.add_to(self);
132+
}
133+
114134
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
115135
ctx.process_all_names(&mut |name, res| match res {
116136
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,7 @@ pub(crate) fn complete_expr_path(acc: &mut Completions, ctx: &CompletionContext)
178178
});
179179

180180
if !is_func_update {
181-
let mut add_keyword =
182-
|kw, snippet| super::keyword::add_keyword(acc, ctx, kw, snippet);
181+
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
183182

184183
if ctx.expects_expression() {
185184
if !in_block_expr {

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use crate::{
44
context::{IdentContext, NameContext, NameKind, NameRefContext, PathCompletionCtx, PathKind},
5-
CompletionContext, CompletionItem, CompletionItemKind, Completions,
5+
CompletionContext, Completions,
66
};
77

88
pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext) {
@@ -22,7 +22,7 @@ pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext
2222
..
2323
}) => {
2424
if ctx.qualifier_ctx.vis_node.is_none() {
25-
let mut add_keyword = |kw, snippet| add_keyword(acc, ctx, kw, snippet);
25+
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
2626
add_keyword("pub(crate)", "pub(crate)");
2727
add_keyword("pub(super)", "pub(super)");
2828
add_keyword("pub", "pub");
@@ -31,23 +31,3 @@ pub(crate) fn complete_field_list(acc: &mut Completions, ctx: &CompletionContext
3131
_ => return,
3232
}
3333
}
34-
35-
pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) {
36-
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
37-
38-
match ctx.config.snippet_cap {
39-
Some(cap) => {
40-
if snippet.ends_with('}') && ctx.incomplete_let {
41-
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
42-
cov_mark::hit!(let_semi);
43-
item.insert_snippet(cap, format!("{};", snippet));
44-
} else {
45-
item.insert_snippet(cap, snippet);
46-
}
47-
}
48-
None => {
49-
item.insert_text(if snippet.contains('$') { kw } else { snippet });
50-
}
51-
};
52-
item.add_to(acc);
53-
}

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

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use crate::{
44
completions::module_or_fn_macro,
55
context::{ItemListKind, PathCompletionCtx, PathKind, PathQualifierCtx},
6-
CompletionContext, CompletionItem, CompletionItemKind, Completions,
6+
CompletionContext, Completions,
77
};
88

99
pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext) {
@@ -24,7 +24,7 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
2424
}) => (is_absolute_path, qualifier, None),
2525
_ => return,
2626
};
27-
let mut add_keyword = |kw, snippet| add_keyword(acc, ctx, kw, snippet);
27+
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
2828

2929
let in_item_list = matches!(kind, Some(ItemListKind::SourceFile | ItemListKind::Module) | None);
3030
let in_assoc_non_trait_impl = matches!(kind, Some(ItemListKind::Impl | ItemListKind::Trait));
@@ -121,23 +121,3 @@ pub(crate) fn complete_item_list(acc: &mut Completions, ctx: &CompletionContext)
121121
None => {}
122122
}
123123
}
124-
125-
pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) {
126-
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
127-
128-
match ctx.config.snippet_cap {
129-
Some(cap) => {
130-
if snippet.ends_with('}') && ctx.incomplete_let {
131-
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
132-
cov_mark::hit!(let_semi);
133-
item.insert_snippet(cap, format!("{};", snippet));
134-
} else {
135-
item.insert_snippet(cap, snippet);
136-
}
137-
}
138-
None => {
139-
item.insert_text(if snippet.contains('$') { kw } else { snippet });
140-
}
141-
};
142-
item.add_to(acc);
143-
}

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

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
55
use syntax::ast::Item;
66

7-
use crate::{
8-
context::NameRefContext, CompletionContext, CompletionItem, CompletionItemKind, Completions,
9-
};
7+
use crate::{context::NameRefContext, CompletionContext, Completions};
108

119
pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
1210
let item = match ctx.nameref_ctx() {
@@ -18,7 +16,7 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
1816
_ => return,
1917
};
2018

21-
let mut add_keyword = |kw, snippet| add_keyword(acc, ctx, kw, snippet);
19+
let mut add_keyword = |kw, snippet| acc.add_keyword_snippet(ctx, kw, snippet);
2220

2321
match item {
2422
Item::Impl(it) => {
@@ -39,26 +37,6 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
3937
}
4038
}
4139

42-
pub(super) fn add_keyword(acc: &mut Completions, ctx: &CompletionContext, kw: &str, snippet: &str) {
43-
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
44-
45-
match ctx.config.snippet_cap {
46-
Some(cap) => {
47-
if snippet.ends_with('}') && ctx.incomplete_let {
48-
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
49-
cov_mark::hit!(let_semi);
50-
item.insert_snippet(cap, format!("{};", snippet));
51-
} else {
52-
item.insert_snippet(cap, snippet);
53-
}
54-
}
55-
None => {
56-
item.insert_text(if snippet.contains('$') { kw } else { snippet });
57-
}
58-
};
59-
item.add_to(acc);
60-
}
61-
6240
#[cfg(test)]
6341
mod tests {
6442
use expect_test::{expect, Expect};

0 commit comments

Comments
 (0)