6351: Organized completions r=popzxc a=popzxc

This PR continues the work on refactoring of the `completions` crate.

In this episode:

- Actual completions methods are encapsulated into `completions` module, so they aren't mixed with the rest of the code.
- Name duplication was removed (`complete_attribute` => `completions::attribute`, `completion_context` => `context`).
- `Completions` structure was moved from `item` module to the `completions`.
- `presentation` module was removed, as it was basically a module with `impl` for `Completions`.
- Code approaches were a bit unified here and there.


Co-authored-by: Igor Aleksanov <popzxc@yandex.ru>
This commit is contained in:
bors[bot] 2020-10-26 19:06:34 +00:00 committed by GitHub
commit d10e2a04c8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
22 changed files with 165 additions and 185 deletions

View file

@ -1,5 +1,18 @@
//! This modules takes care of rendering various definitions as completion items.
//! It also handles scoring (sorting) completions.
//! This module defines an accumulator for completions which are going to be presented to user.
pub(crate) mod attribute;
pub(crate) mod dot;
pub(crate) mod record;
pub(crate) mod pattern;
pub(crate) mod fn_param;
pub(crate) mod keyword;
pub(crate) mod snippet;
pub(crate) mod qualified_path;
pub(crate) mod unqualified_path;
pub(crate) mod postfix;
pub(crate) mod macro_in_item_position;
pub(crate) mod trait_impl;
pub(crate) mod mod_;
use hir::{HasAttrs, HasSource, HirDisplay, ModPath, Mutability, ScopeDef, StructKind, Type};
use itertools::Itertools;
@ -7,20 +20,47 @@ use syntax::{ast::NameOwner, display::*};
use test_utils::mark;
use crate::{
// display::{const_label, function_declaration, macro_label, type_label},
CompletionScore,
RootDatabase,
{
completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
CompletionKind, Completions,
},
item::Builder, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
CompletionScore, RootDatabase,
};
/// Represents an in-progress set of completions being built.
#[derive(Debug, Default)]
pub struct Completions {
buf: Vec<CompletionItem>,
}
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())
}
}
impl Completions {
pub(crate) fn add(&mut self, item: CompletionItem) {
self.buf.push(item.into())
}
pub(crate) 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()))
}
pub(crate) fn add_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
let is_deprecated = is_deprecated(field, ctx.db);
let name = field.name(ctx.db);
let mut completion_item =
let mut item =
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string())
.kind(CompletionItemKind::Field)
.detail(ty.display(ctx.db).to_string())
@ -28,10 +68,10 @@ impl Completions {
.set_deprecated(is_deprecated);
if let Some(score) = compute_score(ctx, &ty, &name.to_string()) {
completion_item = completion_item.set_score(score);
item = item.set_score(score);
}
completion_item.add_to(self);
item.add_to(self);
}
pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) {
@ -57,7 +97,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 +106,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 +119,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;
}
};
@ -98,12 +141,11 @@ impl Completions {
_ => None,
};
let mut completion_item =
CompletionItem::new(completion_kind, ctx.source_range(), local_name.clone());
let mut item = CompletionItem::new(completion_kind, ctx.source_range(), local_name.clone());
if let ScopeDef::Local(local) = resolution {
let ty = local.ty(ctx.db);
if !ty.is_unknown() {
completion_item = completion_item.detail(ty.display(ctx.db).to_string());
item = item.detail(ty.display(ctx.db).to_string());
}
};
@ -114,7 +156,7 @@ impl Completions {
if let Some(score) =
compute_score_from_active(&active_type, &active_name, &ty, &local_name)
{
completion_item = completion_item.set_score(score);
item = item.set_score(score);
}
ref_match = refed_type_matches(&active_type, &active_name, &ty, &local_name);
}
@ -130,7 +172,7 @@ impl Completions {
};
if has_non_default_type_params {
mark::hit!(inserts_angle_brackets_for_generics);
completion_item = completion_item
item = item
.lookup_by(local_name.clone())
.label(format!("{}<…>", local_name))
.insert_snippet(cap, format!("{}<$0>", local_name));
@ -138,7 +180,7 @@ impl Completions {
}
}
completion_item.kind(kind).set_documentation(docs).set_ref_match(ref_match).add_to(self)
item.kind(kind).set_documentation(docs).set_ref_match(ref_match).add_to(self)
}
pub(crate) fn add_macro(
@ -190,7 +232,7 @@ impl Completions {
}
};
self.add(builder);
self.add(builder.build());
}
pub(crate) fn add_function(
@ -242,7 +284,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) {
@ -506,7 +548,7 @@ mod tests {
use test_utils::mark;
use crate::{
test_utils::{check_edit, check_edit_with_config, do_completion, get_all_completion_items},
test_utils::{check_edit, check_edit_with_config, do_completion, get_all_items},
CompletionConfig, CompletionKind, CompletionScore,
};
@ -524,7 +566,7 @@ mod tests {
}
}
let mut completions = get_all_completion_items(CompletionConfig::default(), ra_fixture);
let mut completions = get_all_items(CompletionConfig::default(), ra_fixture);
completions.sort_by_key(|it| (Reverse(it.score()), it.label().to_string()));
let actual = completions
.into_iter()
@ -661,7 +703,7 @@ fn main() { let _: m::Spam = S<|> }
}
#[test]
fn sets_deprecated_flag_in_completion_items() {
fn sets_deprecated_flag_in_items() {
check(
r#"
#[deprecated]

View file

@ -7,12 +7,13 @@ use rustc_hash::FxHashSet;
use syntax::{ast, AstNode, SyntaxKind};
use crate::{
completion_context::CompletionContext,
completion_item::{CompletionItem, CompletionItemKind, CompletionKind, Completions},
context::CompletionContext,
generated_lint_completions::{CLIPPY_LINTS, FEATURES},
item::{CompletionItem, CompletionItemKind, CompletionKind},
Completions,
};
pub(super) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
pub(crate) fn complete_attribute(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
if ctx.mod_declaration_under_caret.is_some() {
return None;
}
@ -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)
}
}
}
@ -262,9 +256,9 @@ const DEFAULT_DERIVE_COMPLETIONS: &[DeriveCompletion] = &[
DeriveCompletion { label: "Ord", dependencies: &["PartialOrd", "Eq", "PartialEq"] },
];
pub(super) struct LintCompletion {
pub(super) label: &'static str,
pub(super) description: &'static str,
pub(crate) struct LintCompletion {
pub(crate) label: &'static str,
pub(crate) description: &'static str,
}
#[rustfmt::skip]

View file

@ -4,10 +4,10 @@ use hir::{HasVisibility, Type};
use rustc_hash::FxHashSet;
use test_utils::mark;
use crate::{completion_context::CompletionContext, completion_item::Completions};
use crate::{context::CompletionContext, Completions};
/// Complete dot accesses, i.e. fields or methods.
pub(super) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_dot(acc: &mut Completions, ctx: &CompletionContext) {
let dot_receiver = match &ctx.dot_receiver {
Some(expr) => expr,
_ => return,
@ -141,7 +141,7 @@ mod inner {
private_field: u32,
pub pub_field: u32,
pub(crate) crate_field: u32,
pub(super) super_field: u32,
pub(crate) super_field: u32,
}
}
fn foo(a: inner::A) { a.<|> }
@ -159,13 +159,13 @@ struct A {}
mod m {
impl super::A {
fn private_method(&self) {}
pub(super) fn the_method(&self) {}
pub(crate) fn the_method(&self) {}
}
}
fn foo(a: A) { a.<|> }
"#,
expect![[r#"
me the_method() pub(super) fn the_method(&self)
me the_method() pub(crate) fn the_method(&self)
"#]],
);
}

View file

@ -12,7 +12,7 @@ use crate::{CompletionContext, CompletionItem, CompletionKind, Completions};
/// functions in a file have a `spam: &mut Spam` parameter, a completion with
/// `spam: &mut Spam` insert text/label and `spam` lookup string will be
/// suggested.
pub(super) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_fn_param(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_param {
return;
}

View file

@ -5,7 +5,7 @@ use test_utils::mark;
use crate::{CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions};
pub(super) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionContext) {
// complete keyword "crate" in use stmt
let source_range = ctx.source_range();
@ -39,7 +39,7 @@ pub(super) fn complete_use_tree_keyword(acc: &mut Completions, ctx: &CompletionC
}
}
pub(super) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_expr_keyword(acc: &mut Completions, ctx: &CompletionContext) {
if ctx.token.kind() == SyntaxKind::COMMENT {
mark::hit!(no_keyword_completion_in_comments);
return;

View file

@ -2,7 +2,7 @@
use crate::{CompletionContext, Completions};
pub(super) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_macro_in_item_position(acc: &mut Completions, ctx: &CompletionContext) {
// Show only macros in top level.
if ctx.is_new_item {
ctx.scope.process_all_names(&mut |name, res| {

View file

@ -7,13 +7,10 @@ use rustc_hash::FxHashSet;
use crate::{CompletionItem, CompletionItemKind};
use super::{
completion_context::CompletionContext, completion_item::CompletionKind,
completion_item::Completions,
};
use crate::{context::CompletionContext, item::CompletionKind, Completions};
/// Complete mod declaration, i.e. `mod <|> ;`
pub(super) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
pub(crate) fn complete_mod(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
let mod_under_caret = match &ctx.mod_declaration_under_caret {
Some(mod_under_caret) if mod_under_caret.item_list().is_some() => return None,
Some(mod_under_caret) => mod_under_caret,
@ -78,10 +75,9 @@ pub(super) 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

@ -3,7 +3,7 @@
use crate::{CompletionContext, Completions};
/// Completes constats and paths in patterns.
pub(super) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_pattern(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_pat_binding_or_const {
return;
}

View file

@ -11,13 +11,13 @@ use text_edit::TextEdit;
use self::format_like::add_format_like_completions;
use crate::{
completion_config::SnippetCap,
completion_context::CompletionContext,
completion_item::{Builder, CompletionKind, Completions},
CompletionItem, CompletionItemKind,
config::SnippetCap,
context::CompletionContext,
item::{Builder, CompletionKind},
CompletionItem, CompletionItemKind, Completions,
};
pub(super) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_postfix(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.config.enable_postfix_completions {
return;
}

View file

@ -15,8 +15,8 @@
// + `loge` -> `log::error!(...)`
use crate::{
complete_postfix::postfix_snippet, completion_config::SnippetCap,
completion_context::CompletionContext, completion_item::Completions,
completions::postfix::postfix_snippet, config::SnippetCap, context::CompletionContext,
Completions,
};
use syntax::ast::{self, AstToken};
@ -33,7 +33,7 @@ static KINDS: &[(&str, &str)] = &[
("loge", "log::error!"),
];
pub(super) fn add_format_like_completions(
pub(crate) fn add_format_like_completions(
acc: &mut Completions,
ctx: &CompletionContext,
dot_receiver: &ast::Expr,
@ -70,7 +70,7 @@ fn string_literal_contents(item: &ast::String) -> Option<String> {
/// Parser for a format-like string. It is more allowing in terms of string contents,
/// as we expect variable placeholders to be filled with expressions.
#[derive(Debug)]
pub struct FormatStrParser {
pub(crate) struct FormatStrParser {
input: String,
output: String,
extracted_expressions: Vec<String>,

View file

@ -7,7 +7,7 @@ use test_utils::mark;
use crate::{CompletionContext, Completions};
pub(super) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_qualified_path(acc: &mut Completions, ctx: &CompletionContext) {
let path = match &ctx.path_qual {
Some(path) => path.clone(),
None => return,
@ -369,11 +369,11 @@ struct S;
mod m {
impl super::S {
pub(super) fn public_method() { }
pub(crate) fn public_method() { }
fn private_method() { }
pub(super) type PublicType = u32;
pub(crate) type PublicType = u32;
type PrivateType = u32;
pub(super) const PUBLIC_CONST: u32 = 1;
pub(crate) const PUBLIC_CONST: u32 = 1;
const PRIVATE_CONST: u32 = 1;
}
}
@ -381,9 +381,9 @@ mod m {
fn foo() { let _ = S::<|> }
"#,
expect![[r#"
ct PUBLIC_CONST pub(super) const PUBLIC_CONST: u32 = 1;
ta PublicType pub(super) type PublicType = u32;
fn public_method() pub(super) fn public_method()
ct PUBLIC_CONST pub(crate) const PUBLIC_CONST: u32 = 1;
ta PublicType pub(crate) type PublicType = u32;
fn public_method() pub(crate) fn public_method()
"#]],
);
}

View file

@ -1,7 +1,7 @@
//! Complete fields in record literals and patterns.
use crate::{CompletionContext, Completions};
pub(super) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
pub(crate) fn complete_record(acc: &mut Completions, ctx: &CompletionContext) -> Option<()> {
let missing_fields = match (ctx.record_pat_syntax.as_ref(), ctx.record_lit_syntax.as_ref()) {
(None, None) => return None,
(Some(_), Some(_)) => unreachable!("A record cannot be both a literal and a pattern"),

View file

@ -1,8 +1,8 @@
//! This file provides snippet completions, like `pd` => `eprintln!(...)`.
use crate::{
completion_config::SnippetCap, completion_item::Builder, CompletionContext, CompletionItem,
CompletionItemKind, CompletionKind, Completions,
config::SnippetCap, item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
CompletionKind, Completions,
};
fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str) -> Builder {
@ -11,7 +11,7 @@ fn snippet(ctx: &CompletionContext, cap: SnippetCap, label: &str, snippet: &str)
.kind(CompletionItemKind::Snippet)
}
pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionContext) {
if !(ctx.is_trivial_path && ctx.function_syntax.is_some()) {
return;
}
@ -24,7 +24,7 @@ pub(super) fn complete_expr_snippet(acc: &mut Completions, ctx: &CompletionConte
snippet(ctx, cap, "ppd", "eprintln!(\"$0 = {:#?}\", $0);").add_to(acc);
}
pub(super) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_item_snippet(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_new_item {
return;
}

View file

@ -6,7 +6,7 @@ use test_utils::mark;
use crate::{CompletionContext, Completions};
pub(super) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
pub(crate) fn complete_unqualified_path(acc: &mut Completions, ctx: &CompletionContext) {
if !(ctx.is_trivial_path || ctx.is_pat_binding_or_const) {
return;
}

File diff suppressed because one or more lines are too long

View file

@ -6,7 +6,7 @@ use hir::{Documentation, Mutability};
use syntax::TextRange;
use text_edit::TextEdit;
use crate::completion_config::SnippetCap;
use crate::config::SnippetCap;
/// `CompletionItem` describes a single completion variant in the editor pop-up.
/// It is basically a POD with various properties. To construct a
@ -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

@ -1,39 +1,23 @@
//! `completions` crate provides utilities for generating completions of user input.
mod completion_config;
mod completion_item;
mod completion_context;
mod presentation;
mod config;
mod item;
mod context;
mod patterns;
mod generated_lint_completions;
#[cfg(test)]
mod test_utils;
mod complete_attribute;
mod complete_dot;
mod complete_record;
mod complete_pattern;
mod complete_fn_param;
mod complete_keyword;
mod complete_snippet;
mod complete_qualified_path;
mod complete_unqualified_path;
mod complete_postfix;
mod complete_macro_in_item_position;
mod complete_trait_impl;
mod complete_mod;
mod completions;
use ide_db::base_db::FilePosition;
use ide_db::RootDatabase;
use crate::{
completion_context::CompletionContext,
completion_item::{CompletionKind, Completions},
};
use crate::{completions::Completions, context::CompletionContext, item::CompletionKind};
pub use crate::{
completion_config::CompletionConfig,
completion_item::{CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat},
config::CompletionConfig,
item::{CompletionItem, CompletionItemKind, CompletionScore, InsertTextFormat},
};
//FIXME: split the following feature into fine-grained features.
@ -118,28 +102,28 @@ pub fn completions(
}
let mut acc = Completions::default();
complete_attribute::complete_attribute(&mut acc, &ctx);
complete_fn_param::complete_fn_param(&mut acc, &ctx);
complete_keyword::complete_expr_keyword(&mut acc, &ctx);
complete_keyword::complete_use_tree_keyword(&mut acc, &ctx);
complete_snippet::complete_expr_snippet(&mut acc, &ctx);
complete_snippet::complete_item_snippet(&mut acc, &ctx);
complete_qualified_path::complete_qualified_path(&mut acc, &ctx);
complete_unqualified_path::complete_unqualified_path(&mut acc, &ctx);
complete_dot::complete_dot(&mut acc, &ctx);
complete_record::complete_record(&mut acc, &ctx);
complete_pattern::complete_pattern(&mut acc, &ctx);
complete_postfix::complete_postfix(&mut acc, &ctx);
complete_macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
complete_trait_impl::complete_trait_impl(&mut acc, &ctx);
complete_mod::complete_mod(&mut acc, &ctx);
completions::attribute::complete_attribute(&mut acc, &ctx);
completions::fn_param::complete_fn_param(&mut acc, &ctx);
completions::keyword::complete_expr_keyword(&mut acc, &ctx);
completions::keyword::complete_use_tree_keyword(&mut acc, &ctx);
completions::snippet::complete_expr_snippet(&mut acc, &ctx);
completions::snippet::complete_item_snippet(&mut acc, &ctx);
completions::qualified_path::complete_qualified_path(&mut acc, &ctx);
completions::unqualified_path::complete_unqualified_path(&mut acc, &ctx);
completions::dot::complete_dot(&mut acc, &ctx);
completions::record::complete_record(&mut acc, &ctx);
completions::pattern::complete_pattern(&mut acc, &ctx);
completions::postfix::complete_postfix(&mut acc, &ctx);
completions::macro_in_item_position::complete_macro_in_item_position(&mut acc, &ctx);
completions::trait_impl::complete_trait_impl(&mut acc, &ctx);
completions::mod_::complete_mod(&mut acc, &ctx);
Some(acc)
}
#[cfg(test)]
mod tests {
use crate::completion_config::CompletionConfig;
use crate::config::CompletionConfig;
use crate::test_utils;
struct DetailAndDocumentation<'a> {

View file

@ -8,7 +8,7 @@ use stdx::{format_to, trim_indent};
use syntax::{AstNode, NodeOrToken, SyntaxElement};
use test_utils::{assert_eq_text, RangeOrOffset};
use crate::{completion_item::CompletionKind, CompletionConfig, CompletionItem};
use crate::{item::CompletionKind, CompletionConfig, CompletionItem};
/// Creates analysis from a multi-file fixture, returns positions marked with <|>.
pub(crate) fn position(ra_fixture: &str) -> (RootDatabase, FilePosition) {
@ -32,10 +32,8 @@ pub(crate) fn do_completion_with_config(
code: &str,
kind: CompletionKind,
) -> Vec<CompletionItem> {
let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(config, code)
.into_iter()
.filter(|c| c.completion_kind == kind)
.collect();
let mut kind_completions: Vec<CompletionItem> =
get_all_items(config, code).into_iter().filter(|c| c.completion_kind == kind).collect();
kind_completions.sort_by(|l, r| l.label().cmp(r.label()));
kind_completions
}
@ -49,10 +47,8 @@ pub(crate) fn completion_list_with_config(
code: &str,
kind: CompletionKind,
) -> String {
let mut kind_completions: Vec<CompletionItem> = get_all_completion_items(config, code)
.into_iter()
.filter(|c| c.completion_kind == kind)
.collect();
let mut kind_completions: Vec<CompletionItem> =
get_all_items(config, code).into_iter().filter(|c| c.completion_kind == kind).collect();
kind_completions.sort_by_key(|c| c.label().to_owned());
let label_width = kind_completions
.iter()
@ -121,10 +117,7 @@ pub(crate) fn check_pattern_is_not_applicable(code: &str, check: fn(SyntaxElemen
assert!(!check(NodeOrToken::Token(token)));
}
pub(crate) fn get_all_completion_items(
config: CompletionConfig,
code: &str,
) -> Vec<CompletionItem> {
pub(crate) fn get_all_items(config: CompletionConfig, code: &str) -> Vec<CompletionItem> {
let (db, position) = position(code);
crate::completions(&db, &config, position).unwrap().into()
}

View file

@ -20,7 +20,7 @@ pub fn generate_lint_completions(mode: Mode) -> Result<()> {
let ts_clippy = generate_descriptor_clippy(&Path::new("./target/clippy_lints.json"))?;
let ts = quote! {
use crate::complete_attribute::LintCompletion;
use crate::completions::attribute::LintCompletion;
#ts_features
#ts_clippy
};