mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-29 05:15:04 +00:00
Move Completions structure definition into completions module
This commit is contained in:
parent
19c1067202
commit
f731d910cb
9 changed files with 74 additions and 74 deletions
|
@ -11,3 +11,39 @@ pub(crate) mod postfix;
|
||||||
pub(crate) mod macro_in_item_position;
|
pub(crate) mod macro_in_item_position;
|
||||||
pub(crate) mod trait_impl;
|
pub(crate) mod trait_impl;
|
||||||
pub(crate) mod mod_;
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -9,7 +9,8 @@ use syntax::{ast, AstNode, SyntaxKind};
|
||||||
use crate::{
|
use crate::{
|
||||||
context::CompletionContext,
|
context::CompletionContext,
|
||||||
generated_lint_completions::{CLIPPY_LINTS, FEATURES},
|
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<()> {
|
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 {
|
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(", ");
|
||||||
label.push_str(dependency);
|
label.push_str(dependency);
|
||||||
}
|
}
|
||||||
acc.add(
|
|
||||||
CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label)
|
CompletionItem::new(CompletionKind::Attribute, ctx.source_range(), label)
|
||||||
.kind(CompletionItemKind::Attribute),
|
.kind(CompletionItemKind::Attribute)
|
||||||
);
|
.add_to(acc)
|
||||||
}
|
}
|
||||||
|
|
||||||
for custom_derive_name in get_derive_names_in_scope(ctx).difference(&existing_derives) {
|
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)
|
||||||
CompletionItem::new(
|
.kind(CompletionItemKind::Attribute)
|
||||||
CompletionKind::Attribute,
|
.add_to(acc)
|
||||||
ctx.source_range(),
|
|
||||||
custom_derive_name,
|
|
||||||
)
|
|
||||||
.kind(CompletionItemKind::Attribute),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -182,15 +177,14 @@ fn complete_lint(
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.filter(|completion| !existing_lints.contains(completion.label))
|
.filter(|completion| !existing_lints.contains(completion.label))
|
||||||
{
|
{
|
||||||
acc.add(
|
|
||||||
CompletionItem::new(
|
CompletionItem::new(
|
||||||
CompletionKind::Attribute,
|
CompletionKind::Attribute,
|
||||||
ctx.source_range(),
|
ctx.source_range(),
|
||||||
lint_completion.label,
|
lint_completion.label,
|
||||||
)
|
)
|
||||||
.kind(CompletionItemKind::Attribute)
|
.kind(CompletionItemKind::Attribute)
|
||||||
.detail(lint_completion.description),
|
.detail(lint_completion.description)
|
||||||
);
|
.add_to(acc)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use hir::{HasVisibility, Type};
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
use test_utils::mark;
|
use test_utils::mark;
|
||||||
|
|
||||||
use crate::{context::CompletionContext, item::Completions};
|
use crate::{context::CompletionContext, Completions};
|
||||||
|
|
||||||
/// Complete dot accesses, i.e. fields or methods.
|
/// Complete dot accesses, i.e. fields or methods.
|
||||||
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
|
|
|
@ -7,7 +7,7 @@ use rustc_hash::FxHashSet;
|
||||||
|
|
||||||
use crate::{CompletionItem, CompletionItemKind};
|
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 <|> ;`
|
/// Complete mod declaration, i.e. `mod <|> ;`
|
||||||
pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
|
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() {
|
if mod_under_caret.semicolon_token().is_none() {
|
||||||
label.push(';')
|
label.push(';')
|
||||||
}
|
}
|
||||||
acc.add(
|
|
||||||
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
|
CompletionItem::new(CompletionKind::Magic, ctx.source_range(), &label)
|
||||||
.kind(CompletionItemKind::Module),
|
.kind(CompletionItemKind::Module)
|
||||||
)
|
.add_to(acc)
|
||||||
});
|
});
|
||||||
|
|
||||||
Some(())
|
Some(())
|
||||||
|
|
|
@ -13,8 +13,8 @@ use self::format_like::add_format_like_completions;
|
||||||
use crate::{
|
use crate::{
|
||||||
config::SnippetCap,
|
config::SnippetCap,
|
||||||
context::CompletionContext,
|
context::CompletionContext,
|
||||||
item::{Builder, CompletionKind, Completions},
|
item::{Builder, CompletionKind},
|
||||||
CompletionItem, CompletionItemKind,
|
CompletionItem, CompletionItemKind, Completions,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
|
||||||
|
|
|
@ -16,7 +16,7 @@
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext,
|
completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext,
|
||||||
item::Completions,
|
Completions,
|
||||||
};
|
};
|
||||||
use syntax::ast::{self, AstToken};
|
use syntax::ast::{self, AstToken};
|
||||||
|
|
||||||
|
|
|
@ -272,10 +272,6 @@ pub(crate) struct Builder {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Builder {
|
impl Builder {
|
||||||
pub(crate) fn add_to(self, acc: &mut Completions) {
|
|
||||||
acc.add(self.build())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) fn build(self) -> CompletionItem {
|
pub(crate) fn build(self) -> CompletionItem {
|
||||||
let label = self.label;
|
let label = self.label;
|
||||||
let text_edit = match self.text_edit {
|
let text_edit = match self.text_edit {
|
||||||
|
@ -376,28 +372,3 @@ impl<'a> Into<CompletionItem> for Builder {
|
||||||
self.build()
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -14,10 +14,7 @@ mod completions;
|
||||||
use ide_db::base_db::FilePosition;
|
use ide_db::base_db::FilePosition;
|
||||||
use ide_db::RootDatabase;
|
use ide_db::RootDatabase;
|
||||||
|
|
||||||
use crate::{
|
use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
|
||||||
context::CompletionContext,
|
|
||||||
item::{CompletionKind, Completions},
|
|
||||||
};
|
|
||||||
|
|
||||||
pub use crate::{
|
pub use crate::{
|
||||||
config::CompletionConfig,
|
config::CompletionConfig,
|
||||||
|
|
|
@ -57,7 +57,8 @@ impl Completions {
|
||||||
let kind = match resolution {
|
let kind = match resolution {
|
||||||
ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module,
|
ScopeDef::ModuleDef(Module(..)) => CompletionItemKind::Module,
|
||||||
ScopeDef::ModuleDef(Function(func)) => {
|
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,
|
ScopeDef::ModuleDef(Adt(hir::Adt::Struct(_))) => CompletionItemKind::Struct,
|
||||||
// FIXME: add CompletionItemKind::Union
|
// FIXME: add CompletionItemKind::Union
|
||||||
|
@ -65,7 +66,8 @@ impl Completions {
|
||||||
ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum,
|
ScopeDef::ModuleDef(Adt(hir::Adt::Enum(_))) => CompletionItemKind::Enum,
|
||||||
|
|
||||||
ScopeDef::ModuleDef(EnumVariant(var)) => {
|
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(Const(..)) => CompletionItemKind::Const,
|
||||||
ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static,
|
ScopeDef::ModuleDef(Static(..)) => CompletionItemKind::Static,
|
||||||
|
@ -77,13 +79,14 @@ impl Completions {
|
||||||
// (does this need its own kind?)
|
// (does this need its own kind?)
|
||||||
ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam,
|
ScopeDef::AdtSelfType(..) | ScopeDef::ImplSelfType(..) => CompletionItemKind::TypeParam,
|
||||||
ScopeDef::MacroDef(mac) => {
|
ScopeDef::MacroDef(mac) => {
|
||||||
return self.add_macro(ctx, Some(local_name), *mac);
|
self.add_macro(ctx, Some(local_name), *mac);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
ScopeDef::Unknown => {
|
ScopeDef::Unknown => {
|
||||||
return self.add(
|
|
||||||
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name)
|
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name)
|
||||||
.kind(CompletionItemKind::UnresolvedReference),
|
.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(
|
pub(crate) fn add_function(
|
||||||
|
@ -241,7 +244,7 @@ impl Completions {
|
||||||
|
|
||||||
builder = builder.add_call_parens(ctx, name, Params::Named(params));
|
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) {
|
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue