]> git.lizzy.rs Git - rust.git/commitdiff
Move Completions structure definition into completions module
authorIgor Aleksanov <popzxc@yandex.ru>
Sun, 25 Oct 2020 08:26:38 +0000 (11:26 +0300)
committerIgor Aleksanov <popzxc@yandex.ru>
Sun, 25 Oct 2020 08:26:38 +0000 (11:26 +0300)
crates/completion/src/completions.rs
crates/completion/src/completions/attribute.rs
crates/completion/src/completions/dot.rs
crates/completion/src/completions/mod_.rs
crates/completion/src/completions/postfix.rs
crates/completion/src/completions/postfix/format_like.rs
crates/completion/src/item.rs
crates/completion/src/lib.rs
crates/completion/src/presentation.rs

index 5b280c5ba67280fa4b88959fd88828422995239b..db27bdd9cbd9d0cb18033e869d2ee68e5b474685 100644 (file)
 pub(crate) mod macro_in_item_position;
 pub(crate) mod trait_impl;
 pub(crate) mod mod_;
+
+use crate::item::{Builder, CompletionItem};
+
+/// Represents an in-progress set of completions being built.
+#[derive(Debug, Default)]
+pub struct Completions {
+    buf: Vec<CompletionItem>,
+}
+
+impl Completions {
+    pub fn add(&mut self, item: CompletionItem) {
+        self.buf.push(item.into())
+    }
+
+    pub fn add_all<I>(&mut self, items: I)
+    where
+        I: IntoIterator,
+        I::Item: Into<CompletionItem>,
+    {
+        items.into_iter().for_each(|item| self.add(item.into()))
+    }
+}
+
+impl Into<Vec<CompletionItem>> for Completions {
+    fn into(self) -> Vec<CompletionItem> {
+        self.buf
+    }
+}
+
+impl Builder {
+    /// Convenience method, which allows to add a freshly created completion into accumulator
+    /// without binding it to the variable.
+    pub(crate) fn add_to(self, acc: &mut Completions) {
+        acc.add(self.build())
+    }
+}
index 3d517c886659330fc2f30d47e207ae187411f244..f3d6694589b3e9a5979f4998c5eabb09169db165 100644 (file)
@@ -9,7 +9,8 @@
 use crate::{
     context::CompletionContext,
     generated_lint_completions::{CLIPPY_LINTS, FEATURES},
-    item::{CompletionItem, CompletionItemKind, CompletionKind, Completions},
+    item::{CompletionItem, CompletionItemKind, CompletionKind},
+    Completions,
 };
 
 pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
@@ -60,7 +61,7 @@ fn complete_attribute_start(acc: &mut Completions, ctx: &CompletionContext, attr
         }
 
         if attribute.kind() == ast::AttrKind::Inner || !attr_completion.prefer_inner {
-            acc.add(item);
+            acc.add(item.build());
         }
     }
 }
@@ -152,21 +153,15 @@ fn complete_derive(acc: &mut Completions, ctx: &CompletionContext, derive_input:
                 label.push_str(", ");
                 label.push_str(dependency);
             }
-            acc.add(
-                CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label)
-                    .kind(CompletionItemKind::Attribute),
-            );
+            CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label)
+                .kind(CompletionItemKind::Attribute)
+                .add_to(acc)
         }
 
         for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) {
-            acc.add(
-                CompletionItem::new(
-                    CompletionKind::Attribute,
-                    ctx.source_range(),
-                    custom_derive_name,
-                )
-                .kind(CompletionItemKind::Attribute),
-            );
+            CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), custom_derive_name)
+                .kind(CompletionItemKind::Attribute)
+                .add_to(acc)
         }
     }
 }
@@ -182,15 +177,14 @@ fn complete_lint(
             .into_iter()
             .filter(|completion| !existing_lints.contains(completion.label))
         {
-            acc.add(
-                CompletionItem::new(
-                    CompletionKind::Attribute,
-                    ctx.source_range(),
-                    lint_completion.label,
-                )
-                .kind(CompletionItemKind::Attribute)
-                .detail(lint_completion.description),
-            );
+            CompletionItem::new(
+                CompletionKind::Attribute,
+                ctx.source_range(),
+                lint_completion.label,
+            )
+            .kind(CompletionItemKind::Attribute)
+            .detail(lint_completion.description)
+            .add_to(acc)
         }
     }
 }
index 92b1f489d986207c24e3779e978374cf4252fb50..c9875045ad4676367058f49684b62c807005ebff 100644 (file)
@@ -4,7 +4,7 @@
 use rustc_hash::FxHashSet;
 use test_utils::mark;
 
-use crate::{context::CompletionContext, item::Completions};
+use crate::{context::CompletionContext, Completions};
 
 /// Complete dot accesses, i.e. fields or methods.
 pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
index 9612ca36aae3764afa8724baf6389584c0758d47..c96f84171bc74d98f6f622ae853ceede8c48a741 100644 (file)
@@ -7,7 +7,7 @@
 
 use crate::{CompletionItem, CompletionItemKind};
 
-use crate::{context::CompletionContext, item::CompletionKind, item::Completions};
+use crate::{context::CompletionContext, item::CompletionKind, Completions};
 
 /// Complete mod declaration, i.e. `mod <|> ;`
 pub(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
             if mod_under_caret.semicolon_token().is_none() {
                 label.push(';')
             }
-            acc.add(
-                CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
-                    .kind(CompletionItemKind::Module),
-            )
+            CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
+                .kind(CompletionItemKind::Module)
+                .add_to(acc)
         });
 
     Some(())
index f83ab8d2d3168770819305e90f0aa1cd3804eb84..348f017bd703c00abd29377055ef2f7c6291769b 100644 (file)
@@ -13,8 +13,8 @@
 use crate::{
     config::SnippetCap,
     context::CompletionContext,
-    item::{Builder, CompletionKind, Completions},
-    CompletionItem, CompletionItemKind,
+    item::{Builder, CompletionKind},
+    CompletionItem, CompletionItemKind, Completions,
 };
 
 pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
index 8a91665b6a975022c7dd707b2e6608a88de63c32..3595e0fcef895e75f3fb4c2a2d54261223f30c34 100644 (file)
@@ -16,7 +16,7 @@
 
 use crate::{
     completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext,
-    item::Completions,
+    Completions,
 };
 use syntax::ast::{self, AstToken};
 
index 3cb625a06e1451be022c5f9e8a6da5209f48511f..6d1d085f4bc779e63ebfe1750b11577ac18494c3 100644 (file)
@@ -272,10 +272,6 @@ pub(crate) struct Builder {
 }
 
 impl Builder {
-    pub(crate) fn add_to(self, acc: &mut Completions) {
-        acc.add(self.build())
-    }
-
     pub(crate) fn build(self) -> CompletionItem {
         let label = self.label;
         let text_edit = match self.text_edit {
@@ -376,28 +372,3 @@ fn into(self) -> CompletionItem {
         self.build()
     }
 }
-
-/// Represents an in-progress set of completions being built.
-#[derive(Debug, Default)]
-pub struct Completions {
-    buf: Vec<CompletionItem>,
-}
-
-impl Completions {
-    pub fn add(&mut self, item: impl Into<CompletionItem>) {
-        self.buf.push(item.into())
-    }
-    pub fn add_all<I>(&mut self, items: I)
-    where
-        I: IntoIterator,
-        I::Item: Into<CompletionItem>,
-    {
-        items.into_iter().for_each(|item| self.add(item.into()))
-    }
-}
-
-impl Into<Vec<CompletionItem>> for Completions {
-    fn into(self) -> Vec<CompletionItem> {
-        self.buf
-    }
-}
index 786cd26402031e9b4439fefb78fc430f7cdd8608..d8e5cf0dad8a311527fe74072abc6b8cd3c37e48 100644 (file)
 use ide_db::base_db::FilePosition;
 use ide_db::RootDatabase;
 
-use crate::{
-    context::CompletionContext,
-    item::{CompletionKind, Completions},
-};
+use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
 
 pub use crate::{
     config::CompletionConfig,
index 38bb8fa3e46d8fb2523bd7f45499c86f7d3409d7..17584f734429fe0806c77f612510d68bae300d35 100644 (file)
@@ -57,7 +57,8 @@ pub(crate) fn add_resolution(
         let kind = match resolution {
             ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module,
             ScopeDef::ModuleDef(Function(func)) => {
-                return self.add_function(ctx, *func, Some(local_name));
+                self.add_function(ctx, *func, Some(local_name));
+                return;
             }
             ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct,
             // FIXME: add CompletionItemKind::Union
@@ -65,7 +66,8 @@ pub(crate) fn add_resolution(
             ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum,
 
             ScopeDef::ModuleDef(EnumVariant(var)) => {
-                return self.add_enum_variant(ctx, *var, Some(local_name));
+                self.add_enum_variant(ctx, *var, Some(local_name));
+                return;
             }
             ScopeDef::ModuleDef(Const(..)) => CompletionItemKind::Const,
             ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static,
@@ -77,13 +79,14 @@ pub(crate) fn add_resolution(
             // (does this need its own kind?)
             ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam,
             ScopeDef::MacroDef(mac) => {
-                return self.add_macro(ctx, Some(local_name), *mac);
+                self.add_macro(ctx, Some(local_name), *mac);
+                return;
             }
             ScopeDef::Unknown => {
-                return self.add(
-                    CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name)
-                        .kind(CompletionItemKind::UnresolvedReference),
-                );
+                CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name)
+                    .kind(CompletionItemKind::UnresolvedReference)
+                    .add_to(self);
+                return;
             }
         };
 
@@ -189,7 +192,7 @@ pub(crate) fn add_macro(
             }
         };
 
-        self.add(builder);
+        self.add(builder.build());
     }
 
     pub(crate) fn add_function(
@@ -241,7 +244,7 @@ fn add_arg(arg: &str, ty: &Type, ctx: &CompletionContext) -> String {
 
         builder = builder.add_call_parens(ctx, name, Params::Named(params));
 
-        self.add(builder)
+        self.add(builder.build())
     }
 
     pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {