Reorganize completions structure

This commit is contained in:
Igor Aleksanov 2020-10-25 10:59:15 +03:00
parent bf84e4958e
commit 19c1067202
23 changed files with 99 additions and 109 deletions

View file

@ -0,0 +1,13 @@
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_;

View file

@ -7,12 +7,12 @@ 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;
}
@ -262,9 +262,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, item::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, item::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,

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},
config::SnippetCap,
context::CompletionContext,
item::{Builder, CompletionKind, Completions},
CompletionItem, CompletionItemKind,
};
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,
item::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,

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

View file

@ -1,39 +1,27 @@
//! `completions` crate provides utilities for generating completions of user input.
mod completion_config;
mod completion_item;
mod completion_context;
mod config;
mod item;
mod context;
mod presentation;
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},
context::CompletionContext,
item::{CompletionKind, Completions},
};
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 +106,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

@ -11,8 +11,8 @@ use crate::{
CompletionScore,
RootDatabase,
{
completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
CompletionKind, Completions,
item::Builder, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind,
Completions,
},
};
@ -20,7 +20,7 @@ impl Completions {
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 +28,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) {
@ -98,12 +98,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 +113,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 +129,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 +137,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(
@ -506,7 +505,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 +523,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 +660,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

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