Move Completions structure definition into completions module

This commit is contained in:
Igor Aleksanov 2020-10-25 11:26:38 +03:00
parent 19c1067202
commit f731d910cb
9 changed files with 74 additions and 74 deletions

View File

@ -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<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())
}
}

View File

@ -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)
}
}
}

View File

@ -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) {

View File

@ -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(())

View File

@ -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) {

View File

@ -16,7 +16,7 @@
use crate::{
completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext,
item::Completions,
Completions,
};
use syntax::ast::{self, AstToken};

View 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 @@ impl<'a> Into<CompletionItem> for Builder {
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
}
}

View File

@ -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,

View File

@ -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) {