rust-analyzer/crates/ide_completion/src/completions.rs

337 lines
10 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;
2022-02-03 15:29:23 +00:00
pub(crate) mod extern_abi;
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;
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 qualified_path;
pub(crate) mod record;
pub(crate) mod snippet;
pub(crate) mod trait_impl;
pub(crate) mod unqualified_path;
pub(crate) mod use_;
pub(crate) mod vis;
use std::iter;
2022-03-08 22:52:26 +00:00
use hir::{db::HirDatabase, known, ScopeDef};
2021-03-20 21:43:42 +00:00
use ide_db::SymbolKind;
2020-10-25 08:32:41 +00:00
use crate::{
context::Visible,
item::Builder,
render::{
2020-12-20 17:19:23 +00:00
const_::render_const,
enum_variant::render_variant,
function::{render_fn, render_method},
2020-12-20 17:19:23 +00:00
pattern::{render_struct_pat, render_variant_pat},
render_field, render_resolution, render_tuple_field,
struct_literal::render_struct_literal,
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,
};
2022-03-08 22:52:26 +00:00
fn module_or_attr(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
2022-02-02 11:05:21 +00:00
match def {
2022-03-08 22:52:26 +00:00
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_attr(db) => Some(def),
2022-02-02 11:05:21 +00:00
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
_ => None,
}
}
2022-03-08 22:52:26 +00:00
fn module_or_fn_macro(db: &dyn HirDatabase, def: ScopeDef) -> Option<ScopeDef> {
2022-02-02 11:05:21 +00:00
match def {
2022-03-08 22:52:26 +00:00
ScopeDef::ModuleDef(hir::ModuleDef::Macro(m)) if m.is_fn_like(db) => Some(def),
2022-02-02 11:05:21 +00:00
ScopeDef::ModuleDef(hir::ModuleDef::Module(_)) => Some(def),
_ => None,
}
}
/// 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);
}
2022-02-02 17:18:08 +00:00
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_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;
}
self.add(render_resolution(RenderContext::new(ctx, false), local_name, resolution));
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,
};
self.add(render_fn(RenderContext::new(ctx, is_private_editable), None, local_name, func));
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,
};
self.add(render_method(
RenderContext::new(ctx, is_private_editable),
None,
receiver,
local_name,
func,
));
}
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, 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, 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, false), 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,
) {
let item = render_variant(RenderContext::new(ctx, false), None, None, variant, Some(path));
2021-06-16 15:37:23 +00:00
self.add(item);
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>,
) {
let item = render_variant(RenderContext::new(ctx, false), None, local_name, variant, None);
2021-06-16 15:37:23 +00:00
self.add(item);
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, 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>,
) {
let item = render_struct_literal(RenderContext::new(ctx, false), strukt, path, local_name);
self.add_opt(item);
}
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, false), 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, false), 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, false), variant, local_name, 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
) {
self.add_opt(render_variant_pat(RenderContext::new(ctx, false), variant, None, Some(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, false), 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,
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
let module = if let Some(module) = ctx.module {
2021-05-05 20:55:12 +00:00
// Compute path from the completion site if available.
module
} else {
// Otherwise fall back to the enum's definition site.
2021-06-16 13:46:58 +00:00
enum_.module(ctx.db)
2021-05-05 20:55:12 +00:00
};
if let Some(impl_) = ctx.impl_def.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) = module.find_use_path(ctx.db, hir::ModuleDef::from(variant)) {
// 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);
}
}
}
}