From 98510ec5d3478f3e9178bbff076532487292d8f8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 21:46:04 +0300 Subject: move the rest of presentation to presentation --- .../ra_ide_api/src/completion/completion_item.rs | 41 ++-------------------- crates/ra_ide_api/src/completion/presentation.rs | 29 +++++++++------ 2 files changed, 20 insertions(+), 50 deletions(-) (limited to 'crates/ra_ide_api/src') diff --git a/crates/ra_ide_api/src/completion/completion_item.rs b/crates/ra_ide_api/src/completion/completion_item.rs index cb880d92c..f515fcc14 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -1,14 +1,8 @@ use std::fmt; -use hir::{Docs, Documentation}; +use hir::Documentation; use ra_syntax::TextRange; -use ra_text_edit::{ TextEditBuilder, TextEdit}; - -use crate::completion::{ - completion_context::CompletionContext, - const_label, - type_label -}; +use ra_text_edit::{TextEditBuilder, TextEdit}; /// `CompletionItem` describes a single completion variant in the editor pop-up. /// It is basically a POD with various properties. To construct a @@ -253,27 +247,6 @@ impl Builder { self.documentation = docs.map(Into::into); self } - pub(super) fn from_const(mut self, ctx: &CompletionContext, ct: hir::Const) -> Builder { - if let Some(docs) = ct.docs(ctx.db) { - self.documentation = Some(docs); - } - - self.detail = Some(const_item_label(ctx, ct)); - self.kind = Some(CompletionItemKind::Const); - - self - } - - pub(super) fn from_type(mut self, ctx: &CompletionContext, ty: hir::Type) -> Builder { - if let Some(docs) = ty.docs(ctx.db) { - self.documentation = Some(docs); - } - - self.detail = Some(type_item_label(ctx, ty)); - self.kind = Some(CompletionItemKind::TypeAlias); - - self - } } impl<'a> Into for Builder { @@ -307,16 +280,6 @@ impl Into> for Completions { } } -fn const_item_label(ctx: &CompletionContext, ct: hir::Const) -> String { - let node = ct.source(ctx.db).1; - const_label(&node) -} - -fn type_item_label(ctx: &CompletionContext, ty: hir::Type) -> String { - let node = ty.source(ctx.db).1; - type_label(&node) -} - #[cfg(test)] pub(crate) fn do_completion(code: &str, kind: CompletionKind) -> Vec { use crate::mock_analysis::{single_file_with_position, analysis_and_position}; diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index d386288ed..0ead52032 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -6,7 +6,7 @@ use ra_syntax::ast::NameOwner; use crate::completion::{ Completions, CompletionKind, CompletionItemKind, CompletionContext, CompletionItem, - function_label, + function_label, const_label, type_label, }; impl Completions { @@ -91,6 +91,8 @@ impl Completions { ) { let sig = func.signature(ctx.db); let name = name.unwrap_or_else(|| sig.name().to_string()); + let (_, ast_node) = func.source(ctx.db); + let detail = function_label(&ast_node); let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name) .kind(if sig.has_self_param() { @@ -99,7 +101,7 @@ impl Completions { CompletionItemKind::Function }) .set_documentation(func.docs(ctx.db)) - .set_detail(function_item_label(ctx, func)); + .set_detail(detail); // If not an import, add parenthesis automatically. if ctx.use_item_syntax.is_none() && !ctx.is_call { tested_by!(inserts_parens_for_function_calls); @@ -115,13 +117,18 @@ impl Completions { } 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() { + let (_file_id, ast_node) = constant.source(ctx.db); + let name = match ast_node.name() { Some(name) => name, _ => return, }; + let (_, ast_node) = constant.source(ctx.db); + let detail = const_label(&ast_node); + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) - .from_const(ctx, constant) + .kind(CompletionItemKind::Const) + .set_documentation(constant.docs(ctx.db)) + .detail(detail) .add_to(self); } @@ -131,8 +138,13 @@ impl Completions { Some(name) => name, _ => return, }; + let (_, ast_node) = type_alias.source(ctx.db); + let detail = type_label(&ast_node); + CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name.text().to_string()) - .from_type(ctx, type_alias) + .kind(CompletionItemKind::TypeAlias) + .set_documentation(type_alias.docs(ctx.db)) + .detail(detail) .add_to(self); } @@ -152,11 +164,6 @@ impl Completions { } } -fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option { - let node = function.source(ctx.db).1; - function_label(&node) -} - #[cfg(test)] mod tests { use test_utils::covers; -- cgit v1.2.3