From f731d910cbfe36bbdfa3a3f1415d5c48c4a79238 Mon Sep 17 00:00:00 2001 From: Igor Aleksanov Date: Sun, 25 Oct 2020 11:26:38 +0300 Subject: Move Completions structure definition into completions module --- crates/completion/src/completions.rs | 36 +++++++++++++++++++ crates/completion/src/completions/attribute.rs | 40 +++++++++------------- crates/completion/src/completions/dot.rs | 2 +- crates/completion/src/completions/mod_.rs | 9 +++-- crates/completion/src/completions/postfix.rs | 4 +-- .../src/completions/postfix/format_like.rs | 2 +- crates/completion/src/item.rs | 29 ---------------- crates/completion/src/lib.rs | 5 +-- crates/completion/src/presentation.rs | 21 +++++++----- 9 files changed, 74 insertions(+), 74 deletions(-) (limited to 'crates/completion/src') 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; 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, +} + +impl Completions { + pub fn add(&mut self, item: CompletionItem) { + self.buf.push(item.into()) + } + + pub fn add_all(&mut self, items: I) + where + I: IntoIterator, + I::Item: Into, + { + items.into_iter().for_each(|item| self.add(item.into())) + } +} + +impl Into> for Completions { + fn into(self) -> Vec { + 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()) + } +} 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}; 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) } } } 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}; 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) { 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; 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(()) 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; 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) { 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 @@ use crate::{ completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext, - item::Completions, + Completions, }; use syntax::ast::{self, AstToken}; 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 { } 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 @@ impl<'a> Into for Builder { self.build() } } - -/// Represents an in-progress set of completions being built. -#[derive(Debug, Default)] -pub struct Completions { - buf: Vec, -} - -impl Completions { - pub fn add(&mut self, item: impl Into) { - self.buf.push(item.into()) - } - pub fn add_all(&mut self, items: I) - where - I: IntoIterator, - I::Item: Into, - { - items.into_iter().for_each(|item| self.add(item.into())) - } -} - -impl Into> for Completions { - fn into(self) -> Vec { - self.buf - } -} 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; 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, 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 { 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 @@ impl Completions { 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 @@ impl Completions { // (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 @@ impl Completions { } }; - self.add(builder); + self.add(builder.build()); } pub(crate) fn add_function( @@ -241,7 +244,7 @@ impl Completions { 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) { -- cgit v1.2.3