From b7a78729100aa2068827e5812aec03abd3a5df9a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sun, 24 Feb 2019 21:34:38 +0300 Subject: move more code to presentation --- .../ra_ide_api/src/completion/completion_item.rs | 73 +--------------------- crates/ra_ide_api/src/completion/presentation.rs | 72 ++++++++++++++++----- 2 files changed, 57 insertions(+), 88 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 91e32b3c8..cb880d92c 100644 --- a/crates/ra_ide_api/src/completion/completion_item.rs +++ b/crates/ra_ide_api/src/completion/completion_item.rs @@ -1,13 +1,11 @@ use std::fmt; -use hir::{Docs, Documentation, PerNs, Resolution}; +use hir::{Docs, Documentation}; use ra_syntax::TextRange; use ra_text_edit::{ TextEditBuilder, TextEdit}; -use test_utils::tested_by; use crate::completion::{ completion_context::CompletionContext, - function_label, const_label, type_label }; @@ -255,70 +253,6 @@ impl Builder { self.documentation = docs.map(Into::into); self } - pub(super) fn from_resolution( - mut self, - ctx: &CompletionContext, - resolution: &PerNs, - ) -> Builder { - use hir::ModuleDef::*; - - let def = resolution.as_ref().take_types().or_else(|| resolution.as_ref().take_values()); - let def = match def { - None => return self, - Some(it) => it, - }; - let (kind, docs) = match def { - Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)), - Resolution::Def(Function(func)) => return self.from_function(ctx, *func), - Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)), - Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)), - Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), - Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), - Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), - Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)), - Resolution::Def(Type(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), - Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None), - Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None), - Resolution::SelfType(..) => ( - CompletionItemKind::TypeParam, // (does this need its own kind?) - None, - ), - }; - self.kind = Some(kind); - self.documentation = docs; - - self - } - - pub(super) fn from_function( - mut self, - ctx: &CompletionContext, - function: hir::Function, - ) -> Builder { - // If not an import, add parenthesis automatically. - if ctx.use_item_syntax.is_none() && !ctx.is_call { - tested_by!(inserts_parens_for_function_calls); - let sig = function.signature(ctx.db); - if sig.params().is_empty() || sig.has_self_param() && sig.params().len() == 1 { - self.insert_text = Some(format!("{}()$0", self.label)); - } else { - self.insert_text = Some(format!("{}($0)", self.label)); - } - self.insert_text_format = InsertTextFormat::Snippet; - } - - if let Some(docs) = function.docs(ctx.db) { - self.documentation = Some(docs); - } - - if let Some(label) = function_item_label(ctx, function) { - self.detail = Some(label); - } - - self.kind = Some(CompletionItemKind::Function); - 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); @@ -373,11 +307,6 @@ impl Into> for Completions { } } -fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option { - let node = function.source(ctx.db).1; - function_label(&node) -} - fn const_item_label(ctx: &CompletionContext, ct: hir::Const) -> String { let node = ct.source(ctx.db).1; const_label(&node) diff --git a/crates/ra_ide_api/src/completion/presentation.rs b/crates/ra_ide_api/src/completion/presentation.rs index 232ec80cd..d386288ed 100644 --- a/crates/ra_ide_api/src/completion/presentation.rs +++ b/crates/ra_ide_api/src/completion/presentation.rs @@ -38,28 +38,68 @@ impl Completions { &mut self, ctx: &CompletionContext, local_name: String, - res: &PerNs, + resolution: &PerNs, ) { + use hir::ModuleDef::*; + + let def = resolution.as_ref().take_types().or_else(|| resolution.as_ref().take_values()); + let def = match def { + None => { + self.add(CompletionItem::new( + CompletionKind::Reference, + ctx.source_range(), + local_name, + )); + return; + } + Some(it) => it, + }; + let (kind, docs) = match def { + Resolution::Def(Module(it)) => (CompletionItemKind::Module, it.docs(ctx.db)), + Resolution::Def(Function(func)) => { + return self.add_function_with_name(ctx, Some(local_name), *func); + } + Resolution::Def(Struct(it)) => (CompletionItemKind::Struct, it.docs(ctx.db)), + Resolution::Def(Enum(it)) => (CompletionItemKind::Enum, it.docs(ctx.db)), + Resolution::Def(EnumVariant(it)) => (CompletionItemKind::EnumVariant, it.docs(ctx.db)), + Resolution::Def(Const(it)) => (CompletionItemKind::Const, it.docs(ctx.db)), + Resolution::Def(Static(it)) => (CompletionItemKind::Static, it.docs(ctx.db)), + Resolution::Def(Trait(it)) => (CompletionItemKind::Trait, it.docs(ctx.db)), + Resolution::Def(Type(it)) => (CompletionItemKind::TypeAlias, it.docs(ctx.db)), + Resolution::GenericParam(..) => (CompletionItemKind::TypeParam, None), + Resolution::LocalBinding(..) => (CompletionItemKind::Binding, None), + Resolution::SelfType(..) => ( + CompletionItemKind::TypeParam, // (does this need its own kind?) + None, + ), + }; CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name) - .from_resolution(ctx, res) - .add_to(self); + .kind(kind) + .set_documentation(docs) + .add_to(self) } pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) { - let sig = func.signature(ctx.db); + self.add_function_with_name(ctx, None, func) + } - let mut builder = CompletionItem::new( - CompletionKind::Reference, - ctx.source_range(), - sig.name().to_string(), - ) - .kind(if sig.has_self_param() { - CompletionItemKind::Method - } else { - CompletionItemKind::Function - }) - .set_documentation(func.docs(ctx.db)) - .set_detail(function_item_label(ctx, func)); + fn add_function_with_name( + &mut self, + ctx: &CompletionContext, + name: Option, + func: hir::Function, + ) { + let sig = func.signature(ctx.db); + let name = name.unwrap_or_else(|| sig.name().to_string()); + + let mut builder = CompletionItem::new(CompletionKind::Reference, ctx.source_range(), name) + .kind(if sig.has_self_param() { + CompletionItemKind::Method + } else { + CompletionItemKind::Function + }) + .set_documentation(func.docs(ctx.db)) + .set_detail(function_item_label(ctx, func)); // If not an import, add parenthesis automatically. if ctx.use_item_syntax.is_none() && !ctx.is_call { tested_by!(inserts_parens_for_function_calls); -- cgit v1.2.3