aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_completion/src/completions
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2021-03-12 08:41:16 +0000
committerGitHub <[email protected]>2021-03-12 08:41:16 +0000
commitc01ef6eabab1fa70d3fdfed9c565fadec9f0a5ed (patch)
tree37160de8384855fd5fadce3ca4a3de4dacdf98b8 /crates/ide_completion/src/completions
parentdb6364fecc2b0e9a95d9aaece820a86265b4cb4f (diff)
parent99c4a41cd1a9f9ef0ee4f067f0069a23123b949a (diff)
Merge #7974
7974: use references in CompletionItem's builder r=matklad a=yonip23 @matklad This is a follow up to [this pr](https://github.com/rust-analyzer/rust-analyzer/pull/7973) Co-authored-by: yonip23 <[email protected]>
Diffstat (limited to 'crates/ide_completion/src/completions')
-rw-r--r--crates/ide_completion/src/completions/attribute.rs35
-rw-r--r--crates/ide_completion/src/completions/fn_param.rs7
-rw-r--r--crates/ide_completion/src/completions/keyword.rs44
-rw-r--r--crates/ide_completion/src/completions/mod_.rs7
-rw-r--r--crates/ide_completion/src/completions/postfix.rs1
-rw-r--r--crates/ide_completion/src/completions/record.rs15
-rw-r--r--crates/ide_completion/src/completions/snippet.rs25
-rw-r--r--crates/ide_completion/src/completions/trait_impl.rs31
8 files changed, 86 insertions, 79 deletions
diff --git a/crates/ide_completion/src/completions/attribute.rs b/crates/ide_completion/src/completions/attribute.rs
index cb05e85fc..14376b924 100644
--- a/crates/ide_completion/src/completions/attribute.rs
+++ b/crates/ide_completion/src/completions/attribute.rs
@@ -45,15 +45,15 @@ fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attr
45 CompletionKind::Attribute, 45 CompletionKind::Attribute,
46 ctx.source_range(), 46 ctx.source_range(),
47 attr_completion.label, 47 attr_completion.label,
48 ) 48 );
49 .kind(CompletionItemKind::Attribute); 49 item.kind(CompletionItemKind::Attribute);
50 50
51 if let Some(lookup) = attr_completion.lookup { 51 if let Some(lookup) = attr_completion.lookup {
52 item = item.lookup_by(lookup); 52 item.lookup_by(lookup);
53 } 53 }
54 54
55 if let Some((snippet, cap)) = attr_completion.snippet.zip(ctx.config.snippet_cap) { 55 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);
57 } 57 }
58 58
59 if attribute.kind() == ast::AttrKind::Inner || !attr_completion.prefer_inner { 59 if attribute.kind() == ast::AttrKind::Inner || !attr_completion.prefer_inner {
@@ -168,16 +168,20 @@ fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, derive_input:
168 ); 168 );
169 let lookup = components.join(", "); 169 let lookup = components.join(", ");
170 let label = components.iter().rev().join(", "); 170 let label = components.iter().rev().join(", ");
171 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label) 171 let mut builder =
172 .lookup_by(lookup) 172 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label);
173 .kind(CompletionItemKind::Attribute) 173 builder.lookup_by(lookup).kind(CompletionItemKind::Attribute);
174 .add_to(acc) 174 builder.add_to(acc);
175 } 175 }
176 176
177 for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) { 177 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) 178 let mut builder = CompletionItem::new(
179 .kind(CompletionItemKind::Attribute) 179 CompletionKind::Attribute,
180 .add_to(acc) 180 ctx.source_range(),
181 custom_derive_name,
182 );
183 builder.kind(CompletionItemKind::Attribute);
184 builder.add_to(acc);
181 } 185 }
182 } 186 }
183} 187}
@@ -193,14 +197,13 @@ fn complete_lint(
193 .into_iter() 197 .into_iter()
194 .filter(|completion| !existing_lints.contains(completion.label)) 198 .filter(|completion| !existing_lints.contains(completion.label))
195 { 199 {
196 CompletionItem::new( 200 let mut builder = CompletionItem::new(
197 CompletionKind::Attribute, 201 CompletionKind::Attribute,
198 ctx.source_range(), 202 ctx.source_range(),
199 lint_completion.label, 203 lint_completion.label,
200 ) 204 );
201 .kind(CompletionItemKind::Attribute) 205 builder.kind(CompletionItemKind::Attribute).detail(lint_completion.description);
202 .detail(lint_completion.description) 206 builder.add_to(acc)
203 .add_to(acc)
204 } 207 }
205 } 208 }
206} 209}
diff --git a/crates/ide_completion/src/completions/fn_param.rs b/crates/ide_completion/src/completions/fn_param.rs
index 1bcc8727f..9600a049a 100644
--- a/crates/ide_completion/src/completions/fn_param.rs
+++ b/crates/ide_completion/src/completions/fn_param.rs
@@ -54,10 +54,9 @@ pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext)
54 } 54 }
55 55
56 params.into_iter().for_each(|(label, lookup)| { 56 params.into_iter().for_each(|(label, lookup)| {
57 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label) 57 let mut builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
58 .kind(CompletionItemKind::Binding) 58 builder.kind(CompletionItemKind::Binding).lookup_by(lookup);
59 .lookup_by(lookup) 59 builder.add_to(acc)
60 .add_to(acc)
61 }); 60 });
62} 61}
63 62
diff --git a/crates/ide_completion/src/completions/keyword.rs b/crates/ide_completion/src/completions/keyword.rs
index 80aa9fb06..aa3be712d 100644
--- a/crates/ide_completion/src/completions/keyword.rs
+++ b/crates/ide_completion/src/completions/keyword.rs
@@ -12,21 +12,21 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
12 12
13 if ctx.use_item_syntax.is_some() { 13 if ctx.use_item_syntax.is_some() {
14 if ctx.path_qual.is_none() { 14 if ctx.path_qual.is_none() {
15 CompletionItem::new(CompletionKind::Keyword, source_range, "crate::") 15 let mut crate_builder =
16 .kind(CompletionItemKind::Keyword) 16 CompletionItem::new(CompletionKind::Keyword, source_range, "crate::");
17 .insert_text("crate::") 17 crate_builder.kind(CompletionItemKind::Keyword).insert_text("crate::");
18 .add_to(acc); 18 crate_builder.add_to(acc);
19 } 19 }
20 CompletionItem::new(CompletionKind::Keyword, source_range, "self") 20 let mut self_builder = CompletionItem::new(CompletionKind::Keyword, source_range, "self");
21 .kind(CompletionItemKind::Keyword) 21 self_builder.kind(CompletionItemKind::Keyword);
22 .add_to(acc); 22 self_builder.add_to(acc);
23 if iter::successors(ctx.path_qual.clone(), |p| p.qualifier()) 23 if iter::successors(ctx.path_qual.clone(), |p| p.qualifier())
24 .all(|p| p.segment().and_then(|s| s.super_token()).is_some()) 24 .all(|p| p.segment().and_then(|s| s.super_token()).is_some())
25 { 25 {
26 CompletionItem::new(CompletionKind::Keyword, source_range, "super::") 26 let mut super_builder =
27 .kind(CompletionItemKind::Keyword) 27 CompletionItem::new(CompletionKind::Keyword, source_range, "super::");
28 .insert_text("super::") 28 super_builder.kind(CompletionItemKind::Keyword).insert_text("super::");
29 .add_to(acc); 29 super_builder.add_to(acc);
30 } 30 }
31 } 31 }
32 32
@@ -34,11 +34,10 @@ pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
34 if let Some(receiver) = &ctx.dot_receiver { 34 if let Some(receiver) = &ctx.dot_receiver {
35 if let Some(ty) = ctx.sema.type_of_expr(receiver) { 35 if let Some(ty) = ctx.sema.type_of_expr(receiver) {
36 if ty.impls_future(ctx.db) { 36 if ty.impls_future(ctx.db) {
37 CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await") 37 let mut builder =
38 .kind(CompletionItemKind::Keyword) 38 CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), "await");
39 .detail("expr.await") 39 builder.kind(CompletionItemKind::Keyword).detail("expr.await").insert_text("await");
40 .insert_text("await") 40 builder.add_to(acc);
41 .add_to(acc);
42 } 41 }
43 }; 42 };
44 } 43 }
@@ -165,9 +164,10 @@ pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionConte
165} 164}
166 165
167fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) { 166fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet: &str) {
168 let builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw) 167 let mut builder = CompletionItem::new(CompletionKind::Keyword, ctx.source_range(), kw);
169 .kind(CompletionItemKind::Keyword); 168 builder.kind(CompletionItemKind::Keyword);
170 let builder = match ctx.config.snippet_cap { 169
170 match ctx.config.snippet_cap {
171 Some(cap) => { 171 Some(cap) => {
172 let tmp; 172 let tmp;
173 let snippet = if snippet.ends_with('}') && ctx.incomplete_let { 173 let snippet = if snippet.ends_with('}') && ctx.incomplete_let {
@@ -177,9 +177,11 @@ fn add_keyword(ctx: &CompletionContext, acc: &mut Completions, kw: &str, snippet
177 } else { 177 } else {
178 snippet 178 snippet
179 }; 179 };
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 });
181 } 184 }
182 None => builder.insert_text(if snippet.contains('$') { kw } else { snippet }),
183 }; 185 };
184 acc.add(builder.build()); 186 acc.add(builder.build());
185} 187}
diff --git a/crates/ide_completion/src/completions/mod_.rs b/crates/ide_completion/src/completions/mod_.rs
index 352fc7c77..fc4ac7a0d 100644
--- a/crates/ide_completion/src/completions/mod_.rs
+++ b/crates/ide_completion/src/completions/mod_.rs
@@ -80,9 +80,10 @@ pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
80 if mod_under_caret.semicolon_token().is_none() { 80 if mod_under_caret.semicolon_token().is_none() {
81 label.push(';'); 81 label.push(';');
82 } 82 }
83 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label) 83 let mut builder =
84 .kind(SymbolKind::Module) 84 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label);
85 .add_to(acc) 85 builder.kind(SymbolKind::Module);
86 builder.add_to(acc)
86 }); 87 });
87 88
88 Some(()) 89 Some(())
diff --git a/crates/ide_completion/src/completions/postfix.rs b/crates/ide_completion/src/completions/postfix.rs
index d45ad7944..8551e0168 100644
--- a/crates/ide_completion/src/completions/postfix.rs
+++ b/crates/ide_completion/src/completions/postfix.rs
@@ -301,6 +301,7 @@ fn postfix_snippet(
301 .detail(detail) 301 .detail(detail)
302 .kind(CompletionItemKind::Snippet) 302 .kind(CompletionItemKind::Snippet)
303 .snippet_edit(cap, edit) 303 .snippet_edit(cap, edit)
304 .clone()
304} 305}
305 306
306#[cfg(test)] 307#[cfg(test)]
diff --git a/crates/ide_completion/src/completions/record.rs b/crates/ide_completion/src/completions/record.rs
index 0a7927eb8..b9af09698 100644
--- a/crates/ide_completion/src/completions/record.rs
+++ b/crates/ide_completion/src/completions/record.rs
@@ -22,16 +22,13 @@ pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) ->
22 let completion_text = completion_text 22 let completion_text = completion_text
23 .strip_prefix(ctx.token.to_string().as_str()) 23 .strip_prefix(ctx.token.to_string().as_str())
24 .unwrap_or(completion_text); 24 .unwrap_or(completion_text);
25 acc.add( 25 let mut builder = CompletionItem::new(
26 CompletionItem::new( 26 CompletionKind::Snippet,
27 CompletionKind::Snippet, 27 ctx.source_range(),
28 ctx.source_range(), 28 "..Default::default()",
29 "..Default::default()",
30 )
31 .insert_text(completion_text)
32 .kind(SymbolKind::Field)
33 .build(),
34 ); 29 );
30 builder.insert_text(completion_text).kind(SymbolKind::Field);
31 acc.add(builder.build());
35 } 32 }
36 33
37 missing_fields 34 missing_fields
diff --git a/crates/ide_completion/src/completions/snippet.rs b/crates/ide_completion/src/completions/snippet.rs
index df17a15c5..a4d18cecd 100644
--- a/crates/ide_completion/src/completions/snippet.rs
+++ b/crates/ide_completion/src/completions/snippet.rs
@@ -8,9 +8,8 @@ use crate::{
8}; 8};
9 9
10fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder { 10fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
11 CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label) 11 let mut builder = CompletionItem::new(CompletionKind::Snippet, ctx.source_range(), label);
12 .insert_snippet(cap, snippet) 12 builder.insert_snippet(cap, snippet).kind(CompletionItemKind::Snippet).clone()
13 .kind(CompletionItemKind::Snippet)
14} 13}
15 14
16pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) { 15pub(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
35 None => return, 34 None => return,
36 }; 35 };
37 36
38 snippet( 37 let mut test_module_builder = snippet(
39 ctx, 38 ctx,
40 cap, 39 cap,
41 "tmod (Test module)", 40 "tmod (Test module)",
@@ -49,11 +48,11 @@ mod tests {
49 $0 48 $0
50 } 49 }
51}", 50}",
52 ) 51 );
53 .lookup_by("tmod") 52 test_module_builder.lookup_by("tmod");
54 .add_to(acc); 53 test_module_builder.add_to(acc);
55 54
56 snippet( 55 let mut test_function_builder = snippet(
57 ctx, 56 ctx,
58 cap, 57 cap,
59 "tfn (Test function)", 58 "tfn (Test function)",
@@ -62,11 +61,13 @@ mod tests {
62fn ${1:feature}() { 61fn ${1:feature}() {
63 $0 62 $0
64}", 63}",
65 ) 64 );
66 .lookup_by("tfn") 65 test_function_builder.lookup_by("tfn");
67 .add_to(acc); 66 test_function_builder.add_to(acc);
68 67
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);
70} 71}
71 72
72#[cfg(test)] 73#[cfg(test)]
diff --git a/crates/ide_completion/src/completions/trait_impl.rs b/crates/ide_completion/src/completions/trait_impl.rs
index b999540b8..031f42d4a 100644
--- a/crates/ide_completion/src/completions/trait_impl.rs
+++ b/crates/ide_completion/src/completions/trait_impl.rs
@@ -145,9 +145,8 @@ fn add_function_impl(
145 format!("fn {}(..)", fn_name) 145 format!("fn {}(..)", fn_name)
146 }; 146 };
147 147
148 let builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label) 148 let mut builder = CompletionItem::new(CompletionKind::Magic, ctx.source_range(), label);
149 .lookup_by(fn_name) 149 builder.lookup_by(fn_name).set_documentation(func.docs(ctx.db));
150 .set_documentation(func.docs(ctx.db));
151 150
152 let completion_kind = if func.self_param(ctx.db).is_some() { 151 let completion_kind = if func.self_param(ctx.db).is_some() {
153 CompletionItemKind::Method 152 CompletionItemKind::Method
@@ -161,15 +160,15 @@ fn add_function_impl(
161 match ctx.config.snippet_cap { 160 match ctx.config.snippet_cap {
162 Some(cap) => { 161 Some(cap) => {
163 let snippet = format!("{} {{\n $0\n}}", function_decl); 162 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));
165 } 164 }
166 None => { 165 None => {
167 let header = format!("{} {{", function_decl); 166 let header = format!("{} {{", function_decl);
168 builder.text_edit(TextEdit::replace(range, header)) 167 builder.text_edit(TextEdit::replace(range, header));
169 } 168 }
170 } 169 };
171 .kind(completion_kind) 170 builder.kind(completion_kind);
172 .add_to(acc); 171 builder.add_to(acc);
173 } 172 }
174} 173}
175 174
@@ -185,12 +184,14 @@ fn add_type_alias_impl(
185 184
186 let range = TextRange::new(type_def_node.text_range().start(), ctx.source_range().end()); 185 let range = TextRange::new(type_def_node.text_range().start(), ctx.source_range().end());
187 186
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
189 .text_edit(TextEdit::replace(range, snippet)) 190 .text_edit(TextEdit::replace(range, snippet))
190 .lookup_by(alias_name) 191 .lookup_by(alias_name)
191 .kind(SymbolKind::TypeAlias) 192 .kind(SymbolKind::TypeAlias)
192 .set_documentation(type_alias.docs(ctx.db)) 193 .set_documentation(type_alias.docs(ctx.db));
193 .add_to(acc); 194 builder.add_to(acc);
194} 195}
195 196
196fn add_const_impl( 197fn add_const_impl(
@@ -208,12 +209,14 @@ fn add_const_impl(
208 let range = 209 let range =
209 TextRange::new(const_def_node.text_range().start(), ctx.source_range().end()); 210 TextRange::new(const_def_node.text_range().start(), ctx.source_range().end());
210 211
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
212 .text_edit(TextEdit::replace(range, snippet)) 215 .text_edit(TextEdit::replace(range, snippet))
213 .lookup_by(const_name) 216 .lookup_by(const_name)
214 .kind(SymbolKind::Const) 217 .kind(SymbolKind::Const)
215 .set_documentation(const_.docs(ctx.db)) 218 .set_documentation(const_.docs(ctx.db));
216 .add_to(acc); 219 builder.add_to(acc);
217 } 220 }
218 } 221 }
219} 222}