rust-analyzer/crates/ide-completion/src/completions.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

557 lines
18 KiB
Rust
Raw Normal View History

//! This module defines an accumulator for completions which are going to be presented to user.
2020-10-25 07:59:15 +00:00
pub(crate) mod attribute;
pub(crate) mod dot;
pub(crate) mod expr;
2022-02-03 15:29:23 +00:00
pub(crate) mod extern_abi;
pub(crate) mod field;
2021-03-20 21:43:42 +00:00
pub(crate) mod flyimport;
2020-10-25 07:59:15 +00:00
pub(crate) mod fn_param;
pub(crate) mod format_string;
pub(crate) mod item_list;
2020-10-25 07:59:15 +00:00
pub(crate) mod keyword;
2021-03-20 21:43:42 +00:00
pub(crate) mod lifetime;
2020-10-25 07:59:15 +00:00
pub(crate) mod mod_;
2021-03-20 21:43:42 +00:00
pub(crate) mod pattern;
pub(crate) mod postfix;
pub(crate) mod record;
pub(crate) mod snippet;
pub(crate) mod r#type;
pub(crate) mod use_;
pub(crate) mod vis;
use std::iter;
use hir::{known, ScopeDef};
2021-03-20 21:43:42 +00:00
use ide_db::SymbolKind;
use syntax::ast;
2020-10-25 08:32:41 +00:00
use crate::{
2022-06-20 12:23:46 +00:00
context::{
ItemListKind, NameContext, NameKind, NameRefContext, NameRefKind, PathKind, PatternContext,
TypeLocation, Visible,
},
item::Builder,
render::{
2020-12-20 17:19:23 +00:00
const_::render_const,
function::{render_fn, render_method},
literal::{render_struct_literal, render_variant_lit},
macro_::render_macro,
2020-12-20 17:19:23 +00:00
pattern::{render_struct_pat, render_variant_pat},
render_field, render_resolution, render_resolution_simple, render_tuple_field,
type_alias::{render_type_alias, render_type_alias_with_eq},
union_literal::render_union_literal,
2020-12-20 17:19:23 +00:00
RenderContext,
},
2021-03-20 21:43:42 +00:00
CompletionContext, CompletionItem, CompletionItemKind,
};
/// Represents an in-progress set of completions being built.
#[derive(Debug, Default)]
pub struct Completions {
buf: Vec<CompletionItem>,
}
2021-06-16 15:37:23 +00:00
impl From<Completions> for Vec<CompletionItem> {
fn from(val: Completions) -> Self {
val.buf
2020-10-25 08:32:41 +00:00
}
}
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 {
2021-06-08 14:50:10 +00:00
fn add(&mut self, item: CompletionItem) {
self.buf.push(item)
}
2021-06-08 14:50:10 +00:00
fn add_opt(&mut self, item: Option<CompletionItem>) {
if let Some(item) = item {
self.buf.push(item)
}
}
2020-10-26 17:37:19 +00:00
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()))
}
2020-10-25 08:32:41 +00:00
pub(crate) fn add_keyword(&mut self, ctx: &CompletionContext, keyword: &'static str) {
let item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), keyword);
item.add_to(self);
}
pub(crate) fn add_nameref_keywords_with_colon(&mut self, ctx: &CompletionContext) {
2022-02-02 17:18:08 +00:00
["self::", "super::", "crate::"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
}
pub(crate) fn add_nameref_keywords(&mut self, ctx: &CompletionContext) {
["self", "super", "crate"].into_iter().for_each(|kw| self.add_keyword(ctx, kw));
}
pub(crate) fn add_keyword_snippet_expr(
&mut self,
ctx: &CompletionContext,
kw: &str,
snippet: &str,
incomplete_let: bool,
) {
2022-06-03 14:33:37 +00:00
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
match ctx.config.snippet_cap {
Some(cap) => {
if snippet.ends_with('}') && incomplete_let {
2022-06-03 14:33:37 +00:00
// complete block expression snippets with a trailing semicolon, if inside an incomplete let
cov_mark::hit!(let_semi);
item.insert_snippet(cap, format!("{};", snippet));
} else {
item.insert_snippet(cap, snippet);
}
}
None => {
item.insert_text(if snippet.contains('$') { kw } else { snippet });
}
};
item.add_to(self);
}
pub(crate) fn add_keyword_snippet(&mut self, ctx: &CompletionContext, kw: &str, snippet: &str) {
let mut item = CompletionItem::new(CompletionItemKind::Keyword, ctx.source_range(), kw);
match ctx.config.snippet_cap {
Some(cap) => item.insert_snippet(cap, snippet),
None => item.insert_text(if snippet.contains('$') { kw } else { snippet }),
};
item.add_to(self);
}
2022-02-02 17:18:08 +00:00
pub(crate) fn add_crate_roots(&mut self, ctx: &CompletionContext) {
ctx.process_all_names(&mut |name, res| match res {
ScopeDef::ModuleDef(hir::ModuleDef::Module(m)) if m.is_crate_root(ctx.db) => {
self.add_resolution(ctx, name, res);
}
_ => (),
});
}
2020-10-25 08:32:41 +00:00
pub(crate) fn add_resolution(
&mut self,
ctx: &CompletionContext,
local_name: hir::Name,
2021-12-21 13:07:48 +00:00
resolution: hir::ScopeDef,
2020-10-25 08:32:41 +00:00
) {
2021-11-16 12:25:23 +00:00
if ctx.is_scope_def_hidden(resolution) {
cov_mark::hit!(qualified_path_doc_hidden);
return;
}
2022-04-11 16:48:27 +00:00
self.add(render_resolution(RenderContext::new(ctx), local_name, resolution).build());
}
pub(crate) fn add_resolution_simple(
&mut self,
ctx: &CompletionContext,
local_name: hir::Name,
resolution: hir::ScopeDef,
) {
if ctx.is_scope_def_hidden(resolution) {
return;
}
2022-04-11 16:48:27 +00:00
self.add(render_resolution_simple(RenderContext::new(ctx), local_name, resolution).build());
}
pub(crate) fn add_module(
&mut self,
ctx: &CompletionContext,
module: hir::Module,
local_name: hir::Name,
) {
self.add_resolution(ctx, local_name, hir::ScopeDef::ModuleDef(module.into()));
}
pub(crate) fn add_macro(
&mut self,
ctx: &CompletionContext,
mac: hir::Macro,
local_name: hir::Name,
) {
let is_private_editable = match ctx.is_visible(&mac) {
Visible::Yes => false,
Visible::Editable => true,
Visible::No => return,
};
2022-04-11 16:48:27 +00:00
self.add(
render_macro(
RenderContext::new(ctx).private_editable(is_private_editable),
local_name,
mac,
)
.build(),
);
2020-10-25 08:32:41 +00:00
}
pub(crate) fn add_function(
&mut self,
ctx: &CompletionContext,
func: hir::Function,
local_name: Option<hir::Name>,
2020-10-25 08:32:41 +00:00
) {
let is_private_editable = match ctx.is_visible(&func) {
Visible::Yes => false,
Visible::Editable => true,
Visible::No => return,
};
2022-04-11 16:48:27 +00:00
self.add(
render_fn(
RenderContext::new(ctx).private_editable(is_private_editable),
local_name,
func,
)
.build(),
);
2020-10-25 08:32:41 +00:00
}
pub(crate) fn add_method(
&mut self,
ctx: &CompletionContext,
func: hir::Function,
2021-05-28 12:38:09 +00:00
receiver: Option<hir::Name>,
local_name: Option<hir::Name>,
) {
let is_private_editable = match ctx.is_visible(&func) {
Visible::Yes => false,
Visible::Editable => true,
Visible::No => return,
};
2022-04-11 16:48:27 +00:00
self.add(
render_method(
RenderContext::new(ctx).private_editable(is_private_editable),
receiver,
local_name,
func,
)
.build(),
);
}
2021-11-16 12:25:23 +00:00
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, konst: hir::Const) {
let is_private_editable = match ctx.is_visible(&konst) {
Visible::Yes => false,
Visible::Editable => true,
Visible::No => return,
};
self.add_opt(render_const(
RenderContext::new(ctx).private_editable(is_private_editable),
konst,
));
2021-06-16 15:37:23 +00:00
}
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
let is_private_editable = match ctx.is_visible(&type_alias) {
Visible::Yes => false,
Visible::Editable => true,
Visible::No => return,
};
self.add_opt(render_type_alias(
RenderContext::new(ctx).private_editable(is_private_editable),
type_alias,
));
2021-06-16 15:37:23 +00:00
}
pub(crate) fn add_type_alias_with_eq(
2020-12-20 17:19:23 +00:00
&mut self,
ctx: &CompletionContext,
2021-06-16 15:37:23 +00:00
type_alias: hir::TypeAlias,
2020-12-20 17:19:23 +00:00
) {
self.add_opt(render_type_alias_with_eq(RenderContext::new(ctx), type_alias));
}
2021-06-16 15:37:23 +00:00
pub(crate) fn add_qualified_enum_variant(
&mut self,
ctx: &CompletionContext,
variant: hir::Variant,
path: hir::ModPath,
) {
2022-04-11 16:48:27 +00:00
if let Some(builder) =
render_variant_lit(RenderContext::new(ctx), None, variant, Some(path))
{
self.add(builder.build());
}
2020-12-20 17:19:23 +00:00
}
2021-06-16 15:37:23 +00:00
pub(crate) fn add_enum_variant(
2020-12-20 17:19:23 +00:00
&mut self,
ctx: &CompletionContext,
2021-06-16 15:37:23 +00:00
variant: hir::Variant,
2020-12-20 17:19:23 +00:00
local_name: Option<hir::Name>,
) {
2022-04-11 16:48:27 +00:00
if let Some(builder) =
render_variant_lit(RenderContext::new(ctx), local_name, variant, None)
{
self.add(builder.build());
}
2020-12-20 17:19:23 +00:00
}
2021-06-16 15:37:23 +00:00
pub(crate) fn add_field(
&mut self,
ctx: &CompletionContext,
receiver: Option<hir::Name>,
field: hir::Field,
ty: &hir::Type,
) {
let is_private_editable = match ctx.is_visible(&field) {
Visible::Yes => false,
Visible::Editable => true,
Visible::No => return,
};
let item = render_field(
RenderContext::new(ctx).private_editable(is_private_editable),
receiver,
field,
ty,
);
2021-06-16 15:37:23 +00:00
self.add(item);
2020-10-25 08:32:41 +00:00
}
pub(crate) fn add_struct_literal(
&mut self,
ctx: &CompletionContext,
strukt: hir::Struct,
2021-11-23 16:57:29 +00:00
path: Option<hir::ModPath>,
local_name: Option<hir::Name>,
) {
2022-04-11 16:48:27 +00:00
if let Some(builder) =
render_struct_literal(RenderContext::new(ctx), strukt, path, local_name)
{
self.add(builder.build());
}
}
pub(crate) fn add_union_literal(
&mut self,
ctx: &CompletionContext,
un: hir::Union,
path: Option<hir::ModPath>,
local_name: Option<hir::Name>,
) {
let item = render_union_literal(RenderContext::new(ctx), un, path, local_name);
self.add_opt(item);
}
2021-06-16 15:37:23 +00:00
pub(crate) fn add_tuple_field(
&mut self,
ctx: &CompletionContext,
receiver: Option<hir::Name>,
field: usize,
ty: &hir::Type,
) {
let item = render_tuple_field(RenderContext::new(ctx), receiver, field, ty);
2021-06-16 15:37:23 +00:00
self.add(item);
}
2022-02-02 11:05:21 +00:00
pub(crate) fn add_lifetime(&mut self, ctx: &CompletionContext, name: hir::Name) {
CompletionItem::new(SymbolKind::LifetimeParam, ctx.source_range(), name.to_smol_str())
.add_to(self)
}
pub(crate) fn add_label(&mut self, ctx: &CompletionContext, name: hir::Name) {
CompletionItem::new(SymbolKind::Label, ctx.source_range(), name.to_smol_str()).add_to(self)
2021-06-16 15:37:23 +00:00
}
pub(crate) fn add_variant_pat(
&mut self,
ctx: &CompletionContext,
2021-06-16 15:37:23 +00:00
variant: hir::Variant,
local_name: Option<hir::Name>,
) {
self.add_opt(render_variant_pat(
RenderContext::new(ctx),
variant,
local_name.clone(),
None,
));
2020-10-25 08:32:41 +00:00
}
2021-06-16 15:37:23 +00:00
pub(crate) fn add_qualified_variant_pat(
2020-10-25 08:32:41 +00:00
&mut self,
ctx: &CompletionContext,
2020-12-20 07:05:24 +00:00
variant: hir::Variant,
path: hir::ModPath,
2020-10-25 08:32:41 +00:00
) {
let path = Some(&path);
2022-03-16 15:41:35 +00:00
self.add_opt(render_variant_pat(RenderContext::new(ctx), variant, None, path));
2020-10-25 08:32:41 +00:00
}
2021-06-16 15:37:23 +00:00
pub(crate) fn add_struct_pat(
2020-10-25 08:32:41 +00:00
&mut self,
ctx: &CompletionContext,
2021-06-16 15:37:23 +00:00
strukt: hir::Struct,
local_name: Option<hir::Name>,
2020-10-25 08:32:41 +00:00
) {
self.add_opt(render_struct_pat(RenderContext::new(ctx), strukt, local_name));
2020-10-25 08:32:41 +00:00
}
}
2021-06-16 13:46:58 +00:00
/// Calls the callback for each variant of the provided enum with the path to the variant.
2021-06-16 15:37:23 +00:00
/// Skips variants that are visible with single segment paths.
fn enum_variants_with_paths(
acc: &mut Completions,
ctx: &CompletionContext,
2021-06-16 13:46:58 +00:00
enum_: hir::Enum,
impl_: &Option<ast::Impl>,
cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath),
) {
2021-06-16 13:46:58 +00:00
let variants = enum_.variants(ctx.db);
2021-05-05 20:55:12 +00:00
if let Some(impl_) = impl_.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
2021-06-16 13:46:58 +00:00
if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_)) {
2021-05-05 20:55:12 +00:00
for &variant in &variants {
let self_path = hir::ModPath::from_segments(
hir::PathKind::Plain,
iter::once(known::SELF_TYPE).chain(iter::once(variant.name(ctx.db))),
);
cb(acc, ctx, variant, self_path);
}
}
2021-05-05 20:55:12 +00:00
}
2021-05-05 20:55:12 +00:00
for variant in variants {
if let Some(path) = ctx.module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
2021-05-05 20:55:12 +00:00
// Variants with trivial paths are already added by the existing completion logic,
// so we should avoid adding these twice
if path.segments().len() > 1 {
cb(acc, ctx, variant, path);
}
}
}
}
2022-06-20 12:23:46 +00:00
pub(super) fn complete_name(
acc: &mut Completions,
ctx: &CompletionContext,
NameContext { name, kind }: &NameContext,
) {
match kind {
NameKind::Const => {
item_list::trait_impl::complete_trait_impl_const(acc, ctx, name);
}
NameKind::Function => {
item_list::trait_impl::complete_trait_impl_fn(acc, ctx, name);
}
NameKind::IdentPat(pattern_ctx) => complete_patterns(acc, ctx, pattern_ctx),
NameKind::Module(mod_under_caret) => {
mod_::complete_mod(acc, ctx, mod_under_caret);
}
NameKind::TypeAlias => {
item_list::trait_impl::complete_trait_impl_type_alias(acc, ctx, name);
}
NameKind::RecordField => {
field::complete_field_list_record_variant(acc, ctx);
}
NameKind::ConstParam
| NameKind::Enum
| NameKind::MacroDef
| NameKind::MacroRules
| NameKind::Rename
| NameKind::SelfParam
| NameKind::Static
| NameKind::Struct
| NameKind::Trait
| NameKind::TypeParam
| NameKind::Union
| NameKind::Variant => (),
}
}
pub(super) fn complete_name_ref(
acc: &mut Completions,
ctx: &CompletionContext,
NameRefContext { nameref, kind }: &NameRefContext,
) {
match kind {
NameRefKind::Path(path_ctx) => {
flyimport::import_on_the_fly_path(acc, ctx, path_ctx);
2022-06-20 12:23:46 +00:00
match &path_ctx.kind {
PathKind::Expr { expr_ctx } => {
expr::complete_expr_path(acc, ctx, path_ctx, expr_ctx);
dot::complete_undotted_self(acc, ctx, path_ctx, expr_ctx);
2022-06-20 12:23:46 +00:00
item_list::complete_item_list_in_expr(acc, ctx, path_ctx, expr_ctx);
record::complete_record_expr_func_update(acc, ctx, path_ctx, expr_ctx);
snippet::complete_expr_snippet(acc, ctx, path_ctx, expr_ctx);
}
PathKind::Type { location } => {
r#type::complete_type_path(acc, ctx, path_ctx, location);
2022-06-20 12:23:46 +00:00
match location {
TypeLocation::TupleField => {
field::complete_field_list_tuple_variant(acc, ctx, path_ctx);
}
TypeLocation::TypeAscription(ascription) => {
r#type::complete_ascribed_type(acc, ctx, path_ctx, ascription);
}
TypeLocation::GenericArgList(_)
| TypeLocation::TypeBound
| TypeLocation::ImplTarget
| TypeLocation::ImplTrait
| TypeLocation::Other => (),
}
}
PathKind::Attr { attr_ctx } => {
attribute::complete_attribute_path(acc, ctx, path_ctx, attr_ctx);
2022-06-20 12:23:46 +00:00
}
PathKind::Derive { existing_derives } => {
attribute::complete_derive_path(acc, ctx, path_ctx, existing_derives);
2022-06-20 12:23:46 +00:00
}
PathKind::Item { kind } => {
item_list::complete_item_list(acc, ctx, path_ctx, kind);
2022-06-20 12:23:46 +00:00
snippet::complete_item_snippet(acc, ctx, path_ctx, kind);
if let ItemListKind::TraitImpl(impl_) = kind {
item_list::trait_impl::complete_trait_impl_item_by_name(
acc, ctx, path_ctx, nameref, impl_,
);
}
}
PathKind::Pat { .. } => {
pattern::complete_pattern_path(acc, ctx, path_ctx);
}
PathKind::Vis { has_in_token } => {
vis::complete_vis_path(acc, ctx, path_ctx, has_in_token);
}
PathKind::Use => {
use_::complete_use_path(acc, ctx, path_ctx, nameref);
2022-06-20 12:23:46 +00:00
}
}
}
NameRefKind::DotAccess(dot_access) => {
flyimport::import_on_the_fly_dot(acc, ctx, dot_access);
dot::complete_dot(acc, ctx, dot_access);
postfix::complete_postfix(acc, ctx, dot_access);
}
NameRefKind::Keyword(item) => {
keyword::complete_for_and_where(acc, ctx, item);
}
NameRefKind::RecordExpr(record_expr) => {
record::complete_record_expr_fields(acc, ctx, record_expr);
}
NameRefKind::Pattern(pattern_ctx) => complete_patterns(acc, ctx, pattern_ctx),
}
}
fn complete_patterns(acc: &mut Completions, ctx: &CompletionContext, pattern_ctx: &PatternContext) {
flyimport::import_on_the_fly_pat(acc, ctx, pattern_ctx);
fn_param::complete_fn_param(acc, ctx, pattern_ctx);
pattern::complete_pattern(acc, ctx, pattern_ctx);
record::complete_record_pattern_fields(acc, ctx, pattern_ctx);
}