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

241 lines
7 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;
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 keyword;
2021-03-20 21:43:42 +00:00
pub(crate) mod lifetime;
2020-10-25 07:59:15 +00:00
pub(crate) mod macro_in_item_position;
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;
use std::iter;
use hir::{known, ModPath, ScopeDef, Type};
2021-03-20 21:43:42 +00:00
use ide_db::SymbolKind;
2020-10-25 08:32:41 +00:00
use crate::{
2021-03-20 21:43:42 +00:00
item::{Builder, CompletionKind},
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
macro_::render_macro,
pattern::{render_struct_pat, render_variant_pat},
render_field, render_resolution, render_tuple_field,
type_alias::render_type_alias,
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>,
}
2020-10-25 08:32:41 +00:00
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 {
2020-10-26 17:37:19 +00:00
pub(crate) fn add(&mut self, item: CompletionItem) {
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_field(&mut self, ctx: &CompletionContext, field: hir::Field, ty: &Type) {
2020-11-03 07:33:13 +00:00
let item = render_field(RenderContext::new(ctx), field, ty);
self.add(item);
2020-10-25 08:32:41 +00:00
}
pub(crate) fn add_tuple_field(&mut self, ctx: &CompletionContext, field: usize, ty: &Type) {
2020-11-03 07:33:13 +00:00
let item = render_tuple_field(RenderContext::new(ctx), field, ty);
self.add(item);
2020-10-25 08:32:41 +00:00
}
2021-03-20 21:43:42 +00:00
pub(crate) fn add_static_lifetime(&mut self, ctx: &CompletionContext) {
let mut item =
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), "'static");
item.kind(CompletionItemKind::SymbolKind(SymbolKind::LifetimeParam));
self.add(item.build());
}
2020-10-25 08:32:41 +00:00
pub(crate) fn add_resolution(
&mut self,
ctx: &CompletionContext,
local_name: String,
resolution: &ScopeDef,
) {
2020-11-03 07:33:13 +00:00
if let Some(item) = render_resolution(RenderContext::new(ctx), local_name, resolution) {
self.add(item);
2020-10-25 08:32:41 +00:00
}
}
pub(crate) fn add_macro(
&mut self,
ctx: &CompletionContext,
name: Option<String>,
macro_: hir::MacroDef,
) {
let name = match name {
Some(it) => it,
None => return,
};
if let Some(item) = render_macro(RenderContext::new(ctx), None, name, macro_) {
2020-11-01 09:35:04 +00:00
self.add(item);
}
2020-10-25 08:32:41 +00:00
}
pub(crate) fn add_function(
&mut self,
ctx: &CompletionContext,
func: hir::Function,
local_name: Option<String>,
) {
if let Some(item) = render_fn(RenderContext::new(ctx), None, local_name, func) {
self.add(item)
}
2020-10-25 08:32:41 +00:00
}
pub(crate) fn add_method(
&mut self,
ctx: &CompletionContext,
func: hir::Function,
local_name: Option<String>,
) {
if let Some(item) = render_method(RenderContext::new(ctx), None, local_name, func) {
self.add(item)
}
}
2020-12-20 17:19:23 +00:00
pub(crate) fn add_variant_pat(
&mut self,
ctx: &CompletionContext,
variant: hir::Variant,
local_name: Option<hir::Name>,
) {
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, local_name, None) {
self.add(item);
}
}
pub(crate) fn add_qualified_variant_pat(
&mut self,
ctx: &CompletionContext,
variant: hir::Variant,
path: ModPath,
) {
if let Some(item) = render_variant_pat(RenderContext::new(ctx), variant, None, Some(path)) {
2020-12-20 17:19:23 +00:00
self.add(item);
}
}
pub(crate) fn add_struct_pat(
&mut self,
ctx: &CompletionContext,
strukt: hir::Struct,
local_name: Option<hir::Name>,
) {
if let Some(item) = render_struct_pat(RenderContext::new(ctx), strukt, local_name) {
self.add(item);
}
}
2020-10-25 08:32:41 +00:00
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
2020-11-03 07:33:13 +00:00
if let Some(item) = render_const(RenderContext::new(ctx), constant) {
2020-11-01 09:59:43 +00:00
self.add(item);
}
2020-10-25 08:32:41 +00:00
}
pub(crate) fn add_type_alias(&mut self, ctx: &CompletionContext, type_alias: hir::TypeAlias) {
2020-11-03 07:33:13 +00:00
if let Some(item) = render_type_alias(RenderContext::new(ctx), type_alias) {
2020-11-01 10:10:02 +00:00
self.add(item)
}
2020-10-25 08:32:41 +00:00
}
pub(crate) fn add_qualified_enum_variant(
&mut self,
ctx: &CompletionContext,
2020-12-20 07:05:24 +00:00
variant: hir::Variant,
2020-10-25 08:32:41 +00:00
path: ModPath,
) {
2020-12-20 07:05:24 +00:00
let item = render_variant(RenderContext::new(ctx), None, None, variant, Some(path));
2020-11-01 09:35:04 +00:00
self.add(item);
2020-10-25 08:32:41 +00:00
}
pub(crate) fn add_enum_variant(
&mut self,
ctx: &CompletionContext,
2020-12-20 07:05:24 +00:00
variant: hir::Variant,
2020-10-25 08:32:41 +00:00
local_name: Option<String>,
) {
2020-12-20 07:05:24 +00:00
let item = render_variant(RenderContext::new(ctx), None, local_name, variant, None);
2020-11-01 09:35:04 +00:00
self.add(item);
2020-10-25 08:32:41 +00:00
}
}
fn complete_enum_variants(
acc: &mut Completions,
ctx: &CompletionContext,
2021-05-05 20:55:12 +00:00
enum_data: hir::Enum,
cb: impl Fn(&mut Completions, &CompletionContext, hir::Variant, hir::ModPath),
) {
2021-05-05 20:55:12 +00:00
let variants = enum_data.variants(ctx.db);
let module = if let Some(module) = ctx.scope.module() {
// Compute path from the completion site if available.
module
} else {
// Otherwise fall back to the enum's definition site.
enum_data.module(ctx.db)
};
if let Some(impl_) = ctx.impl_def.as_ref().and_then(|impl_| ctx.sema.to_def(impl_)) {
if impl_.self_ty(ctx.db).as_adt() == Some(hir::Adt::Enum(enum_data)) {
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);
}
}
}
}