From a650a93bf5d0217e6f366ffc91ca8b25bfd37be4 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 21:21:31 +0300 Subject: move res completion to presentation --- crates/ra_ide_api/src/completion/complete_path.rs | 39 +++------------------- crates/ra_ide_api/src/completion/complete_scope.rs | 8 ++--- crates/ra_ide_api/src/completion/presentation.rs | 36 +++++++++++++++++++- 3 files changed, 42 insertions(+), 41 deletions(-) (limited to 'crates/ra_ide_api/src/completion') diff --git a/crates/ra_ide_api/src/completion/complete_path.rs b/crates/ra_ide_api/src/completion/complete_path.rs index 8713fb0f0..8e453b0e0 100644 --- a/crates/ra_ide_api/src/completion/complete_path.rs +++ b/crates/ra_ide_api/src/completion/complete_path.rs @@ -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::<()> }); diff --git a/crates/ra_ide_api/src/completion/complete_scope.rs b/crates/ra_ide_api/src/completion/complete_scope.rs index eeaf26d93..b44b943ae 100644 --- a/crates/ra_ide_api/src/completion/complete_scope.rs +++ b/crates/ra_ide_api/src/completion/complete_scope.rs @@ -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)] diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index bc2cb4a0f..232ec80cd 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -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, + ) { + 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, -- cgit v1.2.3