aboutsummaryrefslogtreecommitdiff
path: root/crates/completion
diff options
context:
space:
mode:
Diffstat (limited to 'crates/completion')
-rw-r--r--crates/completion/src/completions.rs36
-rw-r--r--crates/completion/src/completions/attribute.rs40
-rw-r--r--crates/completion/src/completions/dot.rs2
-rw-r--r--crates/completion/src/completions/mod_.rs9
-rw-r--r--crates/completion/src/completions/postfix.rs4
-rw-r--r--crates/completion/src/completions/postfix/format_like.rs2
-rw-r--r--crates/completion/src/item.rs29
-rw-r--r--crates/completion/src/lib.rs5
-rw-r--r--crates/completion/src/presentation.rs21
9 files changed, 74 insertions, 74 deletions
diff --git a/crates/completion/src/completions.rs b/crates/completion/src/completions.rs
index 5b280c5ba..db27bdd9c 100644
--- a/crates/completion/src/completions.rs
+++ b/crates/completion/src/completions.rs
@@ -11,3 +11,39 @@ pub(crate) mod postfix;
11pub(crate) mod macro_in_item_position; 11pub(crate) mod macro_in_item_position;
12pub(crate) mod trait_impl; 12pub(crate) mod trait_impl;
13pub(crate) mod mod_; 13pub(crate) mod mod_;
14
15use crate::item::{Builder, CompletionItem};
16
17/// Represents an in-progress set of completions being built.
18#[derive(Debug, Default)]
19pub struct Completions {
20 buf: Vec<CompletionItem>,
21}
22
23impl Completions {
24 pub fn add(&mut self, item: CompletionItem) {
25 self.buf.push(item.into())
26 }
27
28 pub fn add_all<I>(&mut self, items: I)
29 where
30 I: IntoIterator,
31 I::Item: Into<CompletionItem>,
32 {
33 items.into_iter().for_each(|item| self.add(item.into()))
34 }
35}
36
37impl Into<Vec<CompletionItem>> for Completions {
38 fn into(self) -> Vec<CompletionItem> {
39 self.buf
40 }
41}
42
43impl Builder {
44 /// Convenience method, which allows to add a freshly created completion into accumulator
45 /// without binding it to the variable.
46 pub(crate) fn add_to(self, acc: &mut Completions) {
47 acc.add(self.build())
48 }
49}
diff --git a/crates/completion/src/completions/attribute.rs b/crates/completion/src/completions/attribute.rs
index 3d517c886..f3d669458 100644
--- a/crates/completion/src/completions/attribute.rs
+++ b/crates/completion/src/completions/attribute.rs
@@ -9,7 +9,8 @@ use syntax::{ast, AstNode, SyntaxKind};
9use crate::{ 9use crate::{
10 context::CompletionContext, 10 context::CompletionContext,
11 generated_lint_completions::{CLIPPY_LINTS, FEATURES}, 11 generated_lint_completions::{CLIPPY_LINTS, FEATURES},
12 item::{CompletionItem, CompletionItemKind, CompletionKind, Completions}, 12 item::{CompletionItem, CompletionItemKind, CompletionKind},
13 Completions,
13}; 14};
14 15
15pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { 16pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
@@ -60,7 +61,7 @@ fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attr
60 } 61 }
61 62
62 if attribute.kind() == ast::AttrKind::Inner || !attr_completion.prefer_inner { 63 if attribute.kind() == ast::AttrKind::Inner || !attr_completion.prefer_inner {
63 acc.add(item); 64 acc.add(item.build());
64 } 65 }
65 } 66 }
66} 67}
@@ -152,21 +153,15 @@ fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, derive_input:
152 label.push_str(", "); 153 label.push_str(", ");
153 label.push_str(dependency); 154 label.push_str(dependency);
154 } 155 }
155 acc.add( 156 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label)
156 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label) 157 .kind(CompletionItemKind::Attribute)
157 .kind(CompletionItemKind::Attribute), 158 .add_to(acc)
158 );
159 } 159 }
160 160
161 for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) { 161 for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) {
162 acc.add( 162 CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), custom_derive_name)
163 CompletionItem::new( 163 .kind(CompletionItemKind::Attribute)
164 CompletionKind::Attribute, 164 .add_to(acc)
165 ctx.source_range(),
166 custom_derive_name,
167 )
168 .kind(CompletionItemKind::Attribute),
169 );
170 } 165 }
171 } 166 }
172} 167}
@@ -182,15 +177,14 @@ fn complete_lint(
182 .into_iter() 177 .into_iter()
183 .filter(|completion| !existing_lints.contains(completion.label)) 178 .filter(|completion| !existing_lints.contains(completion.label))
184 { 179 {
185 acc.add( 180 CompletionItem::new(
186 CompletionItem::new( 181 CompletionKind::Attribute,
187 CompletionKind::Attribute, 182 ctx.source_range(),
188 ctx.source_range(), 183 lint_completion.label,
189 lint_completion.label, 184 )
190 ) 185 .kind(CompletionItemKind::Attribute)
191 .kind(CompletionItemKind::Attribute) 186 .detail(lint_completion.description)
192 .detail(lint_completion.description), 187 .add_to(acc)
193 );
194 } 188 }
195 } 189 }
196} 190}
diff --git a/crates/completion/src/completions/dot.rs b/crates/completion/src/completions/dot.rs
index 92b1f489d..c9875045a 100644
--- a/crates/completion/src/completions/dot.rs
+++ b/crates/completion/src/completions/dot.rs
@@ -4,7 +4,7 @@ use hir::{HasVisibility, Type};
4use rustc_hash::FxHashSet; 4use rustc_hash::FxHashSet;
5use test_utils::mark; 5use test_utils::mark;
6 6
7use crate::{context::CompletionContext, item::Completions}; 7use crate::{context::CompletionContext, Completions};
8 8
9/// Complete dot accesses, i.e. fields or methods. 9/// Complete dot accesses, i.e. fields or methods.
10pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) { 10pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
diff --git a/crates/completion/src/completions/mod_.rs b/crates/completion/src/completions/mod_.rs
index 9612ca36a..c96f84171 100644
--- a/crates/completion/src/completions/mod_.rs
+++ b/crates/completion/src/completions/mod_.rs
@@ -7,7 +7,7 @@ use rustc_hash::FxHashSet;
7 7
8use crate::{CompletionItem, CompletionItemKind}; 8use crate::{CompletionItem, CompletionItemKind};
9 9
10use crate::{context::CompletionContext, item::CompletionKind, item::Completions}; 10use crate::{context::CompletionContext, item::CompletionKind, Completions};
11 11
12/// Complete mod declaration, i.e. `mod <|> ;` 12/// Complete mod declaration, i.e. `mod <|> ;`
13pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> { 13pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
@@ -75,10 +75,9 @@ pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Op
75 if mod_under_caret.semicolon_token().is_none() { 75 if mod_under_caret.semicolon_token().is_none() {
76 label.push(';') 76 label.push(';')
77 } 77 }
78 acc.add( 78 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
79 CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label) 79 .kind(CompletionItemKind::Module)
80 .kind(CompletionItemKind::Module), 80 .add_to(acc)
81 )
82 }); 81 });
83 82
84 Some(()) 83 Some(())
diff --git a/crates/completion/src/completions/postfix.rs b/crates/completion/src/completions/postfix.rs
index f83ab8d2d..348f017bd 100644
--- a/crates/completion/src/completions/postfix.rs
+++ b/crates/completion/src/completions/postfix.rs
@@ -13,8 +13,8 @@ use self::format_like::add_format_like_completions;
13use crate::{ 13use crate::{
14 config::SnippetCap, 14 config::SnippetCap,
15 context::CompletionContext, 15 context::CompletionContext,
16 item::{Builder, CompletionKind, Completions}, 16 item::{Builder, CompletionKind},
17 CompletionItem, CompletionItemKind, 17 CompletionItem, CompletionItemKind, Completions,
18}; 18};
19 19
20pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) { 20pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
diff --git a/crates/completion/src/completions/postfix/format_like.rs b/crates/completion/src/completions/postfix/format_like.rs
index 8a91665b6..3595e0fce 100644
--- a/crates/completion/src/completions/postfix/format_like.rs
+++ b/crates/completion/src/completions/postfix/format_like.rs
@@ -16,7 +16,7 @@
16 16
17use crate::{ 17use crate::{
18 completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext, 18 completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext,
19 item::Completions, 19 Completions,
20}; 20};
21use syntax::ast::{self, AstToken}; 21use syntax::ast::{self, AstToken};
22 22
diff --git a/crates/completion/src/item.rs b/crates/completion/src/item.rs
index 3cb625a06..6d1d085f4 100644
--- a/crates/completion/src/item.rs
+++ b/crates/completion/src/item.rs
@@ -272,10 +272,6 @@ pub(crate) struct Builder {
272} 272}
273 273
274impl Builder { 274impl Builder {
275 pub(crate) fn add_to(self, acc: &mut Completions) {
276 acc.add(self.build())
277 }
278
279 pub(crate) fn build(self) -> CompletionItem { 275 pub(crate) fn build(self) -> CompletionItem {
280 let label = self.label; 276 let label = self.label;
281 let text_edit = match self.text_edit { 277 let text_edit = match self.text_edit {
@@ -376,28 +372,3 @@ impl<'a> Into<CompletionItem> for Builder {
376 self.build() 372 self.build()
377 } 373 }
378} 374}
379
380/// Represents an in-progress set of completions being built.
381#[derive(Debug, Default)]
382pub struct Completions {
383 buf: Vec<CompletionItem>,
384}
385
386impl Completions {
387 pub fn add(&mut self, item: impl Into<CompletionItem>) {
388 self.buf.push(item.into())
389 }
390 pub fn add_all<I>(&mut self, items: I)
391 where
392 I: IntoIterator,
393 I::Item: Into<CompletionItem>,
394 {
395 items.into_iter().for_each(|item| self.add(item.into()))
396 }
397}
398
399impl Into<Vec<CompletionItem>> for Completions {
400 fn into(self) -> Vec<CompletionItem> {
401 self.buf
402 }
403}
diff --git a/crates/completion/src/lib.rs b/crates/completion/src/lib.rs
index 786cd2640..d8e5cf0da 100644
--- a/crates/completion/src/lib.rs
+++ b/crates/completion/src/lib.rs
@@ -14,10 +14,7 @@ mod completions;
14use ide_db::base_db::FilePosition; 14use ide_db::base_db::FilePosition;
15use ide_db::RootDatabase; 15use ide_db::RootDatabase;
16 16
17use crate::{ 17use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
18 context::CompletionContext,
19 item::{CompletionKind, Completions},
20};
21 18
22pub use crate::{ 19pub use crate::{
23 config::CompletionConfig, 20 config::CompletionConfig,
diff --git a/crates/completion/src/presentation.rs b/crates/completion/src/presentation.rs
index 38bb8fa3e..17584f734 100644
--- a/crates/completion/src/presentation.rs
+++ b/crates/completion/src/presentation.rs
@@ -57,7 +57,8 @@ impl Completions {
57 let kind = match resolution { 57 let kind = match resolution {
58 ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module, 58 ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module,
59 ScopeDef::ModuleDef(Function(func)) => { 59 ScopeDef::ModuleDef(Function(func)) => {
60 return self.add_function(ctx, *func, Some(local_name)); 60 self.add_function(ctx, *func, Some(local_name));
61 return;
61 } 62 }
62 ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct, 63 ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct,
63 // FIXME: add CompletionItemKind::Union 64 // FIXME: add CompletionItemKind::Union
@@ -65,7 +66,8 @@ impl Completions {
65 ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum, 66 ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum,
66 67
67 ScopeDef::ModuleDef(EnumVariant(var)) => { 68 ScopeDef::ModuleDef(EnumVariant(var)) => {
68 return self.add_enum_variant(ctx, *var, Some(local_name)); 69 self.add_enum_variant(ctx, *var, Some(local_name));
70 return;
69 } 71 }
70 ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::Const, 72 ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::Const,
71 ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static, 73 ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static,
@@ -77,13 +79,14 @@ impl Completions {
77 // (does this need its own kind?) 79 // (does this need its own kind?)
78 ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam, 80 ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam,
79 ScopeDef::MacroDef(mac) => { 81 ScopeDef::MacroDef(mac) => {
80 return self.add_macro(ctx, Some(local_name), *mac); 82 self.add_macro(ctx, Some(local_name), *mac);
83 return;
81 } 84 }
82 ScopeDef::Unknown => { 85 ScopeDef::Unknown => {
83 return self.add( 86 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name)
84 CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name) 87 .kind(CompletionItemKind::UnresolvedReference)
85 .kind(CompletionItemKind::UnresolvedReference), 88 .add_to(self);
86 ); 89 return;
87 } 90 }
88 }; 91 };
89 92
@@ -189,7 +192,7 @@ impl Completions {
189 } 192 }
190 }; 193 };
191 194
192 self.add(builder); 195 self.add(builder.build());
193 } 196 }
194 197
195 pub(crate) fn add_function( 198 pub(crate) fn add_function(
@@ -241,7 +244,7 @@ impl Completions {
241 244
242 builder = builder.add_call_parens(ctx, name, Params::Named(params)); 245 builder = builder.add_call_parens(ctx, name, Params::Named(params));
243 246
244 self.add(builder) 247 self.add(builder.build())
245 } 248 }
246 249
247 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) { 250 pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {