Skip to content

Commit 99c4a41

Browse files
committed
use references in CompletionItem's builder
1 parent db6364f commit 99c4a41

File tree

17 files changed

+183
-166
lines changed

17 files changed

+183
-166
lines changed

crates/ide_completion/src/completions/attribute.rs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attr
4545
CompletionKind::Attribute,
4646
ctx.source_range(),
4747
attr_completion.label,
48-
)
49-
.kind(CompletionItemKind::Attribute);
48+
);
49+
item.kind(CompletionItemKind::Attribute);
5050

5151
if let Some(lookup) = attr_completion.lookup {
52-
item = item.lookup_by(lookup);
52+
item.lookup_by(lookup);
5353
}
5454

5555
if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) {
56-
item = item.insert_snippet(cap, snippet);
56+
item.insert_snippet(cap, snippet);
5757
}
5858

5959
if attribute.kind() == ast::AttrKind::Inner || !attr_completion.prefer_inner {
@@ -168,16 +168,20 @@ fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, derive_input:
168168
);
169169
let lookup = components.join(", ");
170170
let label = components.iter().rev().join(", ");
171-
CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label)
172-
.lookup_by(lookup)
173-
.kind(CompletionItemKind::Attribute)
174-
.add_to(acc)
171+
let mut builder =
172+
CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
173+
builder.lookup_by(lookup).kind(CompletionItemKind::Attribute);
174+
builder.add_to(acc);
175175
}
176176

177177
for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) {
178-
CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), custom_derive_name)
179-
.kind(CompletionItemKind::Attribute)
180-
.add_to(acc)
178+
let mut builder = CompletionItem::new(
179+
CompletionKind::Attribute,
180+
ctx.source_range(),
181+
custom_derive_name,
182+
);
183+
builder.kind(CompletionItemKind::Attribute);
184+
builder.add_to(acc);
181185
}
182186
}
183187
}
@@ -193,14 +197,13 @@ fn complete_lint(
193197
.into_iter()
194198
.filter(|completion| !existing_lints.contains(completion.label))
195199
{
196-
CompletionItem::new(
200+
let mut builder = CompletionItem::new(
197201
CompletionKind::Attribute,
198202
ctx.source_range(),
199203
lint_completion.label,
200-
)
201-
.kind(CompletionItemKind::Attribute)
202-
.detail(lint_completion.description)
203-
.add_to(acc)
204+
);
205+
builder.kind(CompletionItemKind::Attribute).detail(lint_completion.description);
206+
builder.add_to(acc)
204207
}
205208
}
206209
}

crates/ide_completion/src/completions/fn_param.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
5454
}
5555

5656
params.into_iter().for_each(|(label, lookup)| {
57-
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
58-
.kind(CompletionItemKind::Binding)
59-
.lookup_by(lookup)
60-
.add_to(acc)
57+
let mut builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
58+
builder.kind(CompletionItemKind::Binding).lookup_by(lookup);
59+
builder.add_to(acc)
6160
});
6261
}
6362

crates/ide_completion/src/completions/keyword.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,33 +12,32 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
1212

1313
if ctx.use_item_syntax.is_some() {
1414
if ctx.path_qual.is_none() {
15-
CompletionItem::new(CompletionKind::Keyword, source_range, "crate::")
16-
.kind(CompletionItemKind::Keyword)
17-
.insert_text("crate::")
18-
.add_to(acc);
15+
let mut crate_builder =
16+
CompletionItem::new(CompletionKind::Keyword, source_range, "crate::");
17+
crate_builder.kind(CompletionItemKind::Keyword).insert_text("crate::");
18+
crate_builder.add_to(acc);
1919
}
20-
CompletionItem::new(CompletionKind::Keyword, source_range, "self")
21-
.kind(CompletionItemKind::Keyword)
22-
.add_to(acc);
20+
let mut self_builder = CompletionItem::new(CompletionKind::Keyword, source_range, "self");
21+
self_builder.kind(CompletionItemKind::Keyword);
22+
self_builder.add_to(acc);
2323
if iter::successors(ctx.path_qual.clone(), |p| p.qualifier())
2424
.all(|p| p.segment().and_then(|s| s.super_token()).is_some())
2525
{
26-
CompletionItem::new(CompletionKind::Keyword, source_range, "super::")
27-
.kind(CompletionItemKind::Keyword)
28-
.insert_text("super::")
29-
.add_to(acc);
26+
let mut super_builder =
27+
CompletionItem::new(CompletionKind::Keyword, source_range, "super::");
28+
super_builder.kind(CompletionItemKind::Keyword).insert_text("super::");
29+
super_builder.add_to(acc);
3030
}
3131
}
3232

3333
// Suggest .await syntax for types that implement Future trait
3434
if let Some(receiver) = &ctx.dot_receiver {
3535
if let Some(ty) = ctx.sema.type_of_expr(receiver) {
3636
if ty.impls_future(ctx.db) {
37-
CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await")
38-
.kind(CompletionItemKind::Keyword)
39-
.detail("expr.await")
40-
.insert_text("await")
41-
.add_to(acc);
37+
let mut builder =
38+
CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await");
39+
builder.kind(CompletionItemKind::Keyword).detail("expr.await").insert_text("await");
40+
builder.add_to(acc);
4241
}
4342
};
4443
}
@@ -165,9 +164,10 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
165164
}
166165

167166
fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) {
168-
let builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw)
169-
.kind(CompletionItemKind::Keyword);
170-
let builder = match ctx.config.snippet_cap {
167+
let mut builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw);
168+
builder.kind(CompletionItemKind::Keyword);
169+
170+
match ctx.config.snippet_cap {
171171
Some(cap) => {
172172
let tmp;
173173
let snippet = if snippet.ends_with('}') && ctx.incomplete_let {
@@ -177,9 +177,11 @@ fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet
177177
} else {
178178
snippet
179179
};
180-
builder.insert_snippet(cap, snippet)
180+
builder.insert_snippet(cap, snippet);
181+
}
182+
None => {
183+
builder.insert_text(if snippet.contains('$') { kw } else { snippet });
181184
}
182-
None => builder.insert_text(if snippet.contains('$') { kw } else { snippet }),
183185
};
184186
acc.add(builder.build());
185187
}

crates/ide_completion/src/completions/mod_.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,10 @@ pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
8080
if mod_under_caret.semicolon_token().is_none() {
8181
label.push(';');
8282
}
83-
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
84-
.kind(SymbolKind::Module)
85-
.add_to(acc)
83+
let mut builder =
84+
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label);
85+
builder.kind(SymbolKind::Module);
86+
builder.add_to(acc)
8687
});
8788

8889
Some(())

crates/ide_completion/src/completions/postfix.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ fn postfix_snippet(
301301
.detail(detail)
302302
.kind(CompletionItemKind::Snippet)
303303
.snippet_edit(cap, edit)
304+
.clone()
304305
}
305306

306307
#[cfg(test)]

crates/ide_completion/src/completions/record.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,16 +22,13 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
2222
let completion_text = completion_text
2323
.strip_prefix(ctx.token.to_string().as_str())
2424
.unwrap_or(completion_text);
25-
acc.add(
26-
CompletionItem::new(
27-
CompletionKind::Snippet,
28-
ctx.source_range(),
29-
"..Default::default()",
30-
)
31-
.insert_text(completion_text)
32-
.kind(SymbolKind::Field)
33-
.build(),
25+
let mut builder = CompletionItem::new(
26+
CompletionKind::Snippet,
27+
ctx.source_range(),
28+
"..Default::default()",
3429
);
30+
builder.insert_text(completion_text).kind(SymbolKind::Field);
31+
acc.add(builder.build());
3532
}
3633

3734
missing_fields

crates/ide_completion/src/completions/snippet.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ use crate::{
88
};
99

1010
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
11-
CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label)
12-
.insert_snippet(cap, snippet)
13-
.kind(CompletionItemKind::Snippet)
11+
let mut builder = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label);
12+
builder.insert_snippet(cap, snippet).kind(CompletionItemKind::Snippet).clone()
1413
}
1514

1615
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
@@ -35,7 +34,7 @@ pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionConte
3534
None => return,
3635
};
3736

38-
snippet(
37+
let mut test_module_builder = snippet(
3938
ctx,
4039
cap,
4140
"tmod (Test module)",
@@ -49,11 +48,11 @@ mod tests {
4948
$0
5049
}
5150
}",
52-
)
53-
.lookup_by("tmod")
54-
.add_to(acc);
51+
);
52+
test_module_builder.lookup_by("tmod");
53+
test_module_builder.add_to(acc);
5554

56-
snippet(
55+
let mut test_function_builder = snippet(
5756
ctx,
5857
cap,
5958
"tfn (Test function)",
@@ -62,11 +61,13 @@ mod tests {
6261
fn ${1:feature}() {
6362
$0
6463
}",
65-
)
66-
.lookup_by("tfn")
67-
.add_to(acc);
64+
);
65+
test_function_builder.lookup_by("tfn");
66+
test_function_builder.add_to(acc);
6867

69-
snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}").add_to(acc);
68+
let macro_rules_builder =
69+
snippet(ctx, cap, "macro_rules", "macro_rules! $1 {\n\t($2) => {\n\t\t$0\n\t};\n}");
70+
macro_rules_builder.add_to(acc);
7071
}
7172

7273
#[cfg(test)]

crates/ide_completion/src/completions/trait_impl.rs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,8 @@ fn add_function_impl(
145145
format!("fn {}(..)", fn_name)
146146
};
147147

148-
let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label)
149-
.lookup_by(fn_name)
150-
.set_documentation(func.docs(ctx.db));
148+
let mut builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
149+
builder.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
151150

152151
let completion_kind = if func.self_param(ctx.db).is_some() {
153152
CompletionItemKind::Method
@@ -161,15 +160,15 @@ fn add_function_impl(
161160
match ctx.config.snippet_cap {
162161
Some(cap) => {
163162
let snippet = format!("{} {{\n $0\n}}", function_decl);
164-
builder.snippet_edit(cap, TextEdit::replace(range, snippet))
163+
builder.snippet_edit(cap, TextEdit::replace(range, snippet));
165164
}
166165
None => {
167166
let header = format!("{} {{", function_decl);
168-
builder.text_edit(TextEdit::replace(range, header))
167+
builder.text_edit(TextEdit::replace(range, header));
169168
}
170-
}
171-
.kind(completion_kind)
172-
.add_to(acc);
169+
};
170+
builder.kind(completion_kind);
171+
builder.add_to(acc);
173172
}
174173
}
175174

@@ -185,12 +184,14 @@ fn add_type_alias_impl(
185184

186185
let range = TextRange::new(type_def_node.text_range().start(), ctx.source_range().end());
187186

188-
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
187+
let mut builder =
188+
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
189+
builder
189190
.text_edit(TextEdit::replace(range, snippet))
190191
.lookup_by(alias_name)
191192
.kind(SymbolKind::TypeAlias)
192-
.set_documentation(type_alias.docs(ctx.db))
193-
.add_to(acc);
193+
.set_documentation(type_alias.docs(ctx.db));
194+
builder.add_to(acc);
194195
}
195196

196197
fn add_const_impl(
@@ -208,12 +209,14 @@ fn add_const_impl(
208209
let range =
209210
TextRange::new(const_def_node.text_range().start(), ctx.source_range().end());
210211

211-
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone())
212+
let mut builder =
213+
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), snippet.clone());
214+
builder
212215
.text_edit(TextEdit::replace(range, snippet))
213216
.lookup_by(const_name)
214217
.kind(SymbolKind::Const)
215-
.set_documentation(const_.docs(ctx.db))
216-
.add_to(acc);
218+
.set_documentation(const_.docs(ctx.db));
219+
builder.add_to(acc);
217220
}
218221
}
219222
}

0 commit comments

Comments
 (0)