move more code to presentation

This commit is contained in:
Aleksey Kladov 2019-02-24 21:34:38 +03:00
parent a650a93bf5
commit b7a7872910
2 changed files with 57 additions and 88 deletions

View file

@ -1,13 +1,11 @@
use std::fmt; use std::fmt;
use hir::{Docs, Documentation, PerNs, Resolution}; use hir::{Docs, Documentation};
use ra_syntax::TextRange; use ra_syntax::TextRange;
use ra_text_edit::{ TextEditBuilder, TextEdit}; use ra_text_edit::{ TextEditBuilder, TextEdit};
use test_utils::tested_by;
use crate::completion::{ use crate::completion::{
completion_context::CompletionContext, completion_context::CompletionContext,
function_label,
const_label, const_label,
type_label type_label
}; };
@ -255,70 +253,6 @@ impl Builder {
self.documentation = docs.map(Into::into); self.documentation = docs.map(Into::into);
self self
} }
pub(super) fn from_resolution(
mut self,
ctx: &CompletionContext,
resolution: &PerNs<Resolution>,
) -> 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 { pub(super) fn from_const(mut self, ctx: &CompletionContext, ct: hir::Const) -> Builder {
if let Some(docs) = ct.docs(ctx.db) { if let Some(docs) = ct.docs(ctx.db) {
self.documentation = Some(docs); self.documentation = Some(docs);
@ -373,11 +307,6 @@ impl Into<Vec<CompletionItem>> for Completions {
} }
} }
fn function_item_label(ctx: &CompletionContext, function: hir::Function) -> Option<String> {
let node = function.source(ctx.db).1;
function_label(&node)
}
fn const_item_label(ctx: &CompletionContext, ct: hir::Const) -> String { fn const_item_label(ctx: &CompletionContext, ct: hir::Const) -> String {
let node = ct.source(ctx.db).1; let node = ct.source(ctx.db).1;
const_label(&node) const_label(&node)

View file

@ -38,21 +38,61 @@ impl Completions {
&mut self, &mut self,
ctx: &CompletionContext, ctx: &CompletionContext,
local_name: String, local_name: String,
res: &PerNs<Resolution>, resolution: &PerNs<Resolution>,
) { ) {
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) CompletionItem::new(CompletionKind::Reference, ctx.source_range(), local_name)
.from_resolution(ctx, res) .kind(kind)
.add_to(self); .set_documentation(docs)
.add_to(self)
} }
pub(crate) fn add_function(&mut self, ctx: &CompletionContext, func: hir::Function) { 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( fn add_function_with_name(
CompletionKind::Reference, &mut self,
ctx.source_range(), ctx: &CompletionContext,
sig.name().to_string(), name: Option<String>,
) 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() { .kind(if sig.has_self_param() {
CompletionItemKind::Method CompletionItemKind::Method
} else { } else {