move res completion to presentation

This commit is contained in:
Aleksey Kladov 2019-02-24 21:21:31 +03:00
parent 2369af8c82
commit a650a93bf5
3 changed files with 42 additions and 41 deletions

View file

@ -1,8 +1,8 @@
use hir::Resolution;
use ra_syntax::{AstNode, ast::NameOwner};
use ra_syntax::AstNode;
use test_utils::tested_by;
use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext};
use crate::completion::{Completions, CompletionContext};
pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
let path = match &ctx.path_prefix {
@ -27,14 +27,7 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
}
}
}
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
name.to_string(),
)
.from_resolution(ctx, &res.def.map(hir::Resolution::Def))
.add_to(acc);
acc.add_resolution(ctx, name.to_string(), &res.def.map(hir::Resolution::Def));
}
}
hir::ModuleDef::Enum(e) => {
@ -52,30 +45,8 @@ pub(super) fn complete_path(acc: &mut Completions, ctx: &CompletionContext) {
acc.add_function(ctx, func);
}
}
hir::ImplItem::Const(ct) => {
let source = ct.source(ctx.db);
if let Some(name) = source.1.name() {
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
name.text().to_string(),
)
.from_const(ctx, ct)
.add_to(acc);
}
}
hir::ImplItem::Type(ty) => {
let source = ty.source(ctx.db);
if let Some(name) = source.1.name() {
CompletionItem::new(
CompletionKind::Reference,
ctx.source_range(),
name.text().to_string(),
)
.from_type(ctx, ty)
.add_to(acc);
}
}
hir::ImplItem::Const(ct) => acc.add_const(ctx, ct),
hir::ImplItem::Type(ty) => acc.add_type(ctx, ty),
}
None::<()>
});

View file

@ -1,4 +1,4 @@
use crate::completion::{CompletionItem, Completions, CompletionKind, CompletionContext};
use crate::completion::{Completions, CompletionContext};
pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
if !ctx.is_trivial_path {
@ -6,11 +6,7 @@ pub(super) fn complete_scope(acc: &mut Completions, ctx: &CompletionContext) {
}
let names = ctx.resolver.all_names(ctx.db);
names.into_iter().for_each(|(name, res)| {
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.to_string())
.from_resolution(ctx, &res)
.add_to(acc)
});
names.into_iter().for_each(|(name, res)| acc.add_resolution(ctx, name.to_string(), &res));
}
#[cfg(test)]

View file

@ -1,7 +1,8 @@
//! This modules takes care of rendering various defenitions as completion items.
use join_to_string::join;
use test_utils::tested_by;
use hir::Docs;
use hir::{Docs, PerNs, Resolution};
use ra_syntax::ast::NameOwner;
use crate::completion::{
Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem,
@ -33,6 +34,17 @@ impl Completions {
.add_to(self);
}
pub(crate) fn add_resolution(
&mut self,
ctx: &CompletionContext,
local_name: String,
res: &PerNs<Resolution>,
) {
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name)
.from_resolution(ctx, res)
.add_to(self);
}
pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) {
let sig = func.signature(ctx.db);
@ -62,6 +74,28 @@ impl Completions {
self.add(builder)
}
pub(crate) fn add_const(&mut self, ctx: &CompletionContext, constant: hir::Const) {
let (_file_id, cosnt_def) = constant.source(ctx.db);
let name = match cosnt_def.name() {
Some(name) => name,
_ => return,
};
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
.from_const(ctx, constant)
.add_to(self);
}
pub(crate) fn add_type(&mut self, ctx: &CompletionContext, type_alias: hir::Type) {
let (_file_id, type_def) = type_alias.source(ctx.db);
let name = match type_def.name() {
Some(name) => name,
_ => return,
};
CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string())
.from_type(ctx, type_alias)
.add_to(self);
}
pub(crate) fn add_enum_variant(&mut self, ctx: &CompletionContext, variant: hir::EnumVariant) {
let name = match variant.name(ctx.db) {
Some(it) => it,