Don't use function signature for Display

This commit is contained in:
Aleksey Kladov 2020-07-16 21:32:20 +02:00
parent 4759a39f06
commit 0265778e86
4 changed files with 47 additions and 62 deletions

View File

@ -43,7 +43,7 @@ use crate::{
completion::{ completion::{
CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
}, },
display::function_signature::FunctionSignature, display::function_label,
}; };
pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
@ -125,8 +125,6 @@ fn add_function_impl(
ctx: &CompletionContext, ctx: &CompletionContext,
func: hir::Function, func: hir::Function,
) { ) {
let signature = FunctionSignature::from_hir(ctx.db, func);
let fn_name = func.name(ctx.db).to_string(); let fn_name = func.name(ctx.db).to_string();
let label = if !func.params(ctx.db).is_empty() { let label = if !func.params(ctx.db).is_empty() {
@ -146,13 +144,14 @@ fn add_function_impl(
}; };
let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end()); let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end());
let function_decl = function_label(&func.source(ctx.db).value);
match ctx.config.snippet_cap { match ctx.config.snippet_cap {
Some(cap) => { Some(cap) => {
let snippet = format!("{} {{\n $0\n}}", signature); let snippet = format!("{} {{\n $0\n}}", function_decl);
builder.snippet_edit(cap, TextEdit::replace(range, snippet)) builder.snippet_edit(cap, TextEdit::replace(range, snippet))
} }
None => { None => {
let header = format!("{} {{", signature); let header = format!("{} {{", function_decl);
builder.text_edit(TextEdit::replace(range, header)) builder.text_edit(TextEdit::replace(range, header))
} }
} }

View File

@ -11,7 +11,9 @@ use crate::{
completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind, completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
CompletionKind, Completions, CompletionKind, Completions,
}, },
display::{const_label, function_signature::FunctionSignature, macro_label, type_label}, display::{
const_label, function_label, function_signature::FunctionSignature, macro_label, type_label,
},
CompletionScore, RootDatabase, CompletionScore, RootDatabase,
}; };
@ -206,7 +208,7 @@ impl Completions {
}) })
.set_documentation(func.docs(ctx.db)) .set_documentation(func.docs(ctx.db))
.set_deprecated(is_deprecated(func, ctx.db)) .set_deprecated(is_deprecated(func, ctx.db))
.detail(function_signature.to_string()); .detail(function_label(&ast_node));
let params = function_signature let params = function_signature
.parameter_names .parameter_names

View File

@ -13,10 +13,46 @@ use ra_syntax::{
pub(crate) use navigation_target::{ToNav, TryToNav}; pub(crate) use navigation_target::{ToNav, TryToNav};
pub(crate) use short_label::ShortLabel; pub(crate) use short_label::ShortLabel;
use ast::VisibilityOwner;
pub use navigation_target::NavigationTarget; pub use navigation_target::NavigationTarget;
use stdx::format_to;
pub(crate) fn function_label(node: &ast::FnDef) -> String { pub(crate) fn function_label(node: &ast::FnDef) -> String {
function_signature::FunctionSignature::from(node).to_string() let mut buf = String::new();
if let Some(vis) = node.visibility() {
format_to!(buf, "{} ", vis);
}
if node.async_token().is_some() {
format_to!(buf, "async ");
}
if node.const_token().is_some() {
format_to!(buf, "const ");
}
if node.unsafe_token().is_some() {
format_to!(buf, "unsafe ");
}
if let Some(abi) = node.abi() {
// Keyword `extern` is included in the string.
format_to!(buf, "{} ", abi);
}
if let Some(name) = node.name() {
format_to!(buf, "fn {}", name)
}
if let Some(type_params) = node.type_param_list() {
format_to!(buf, "{}", type_params);
}
if let Some(param_list) = node.param_list() {
format_to!(buf, "{}", param_list);
}
if let Some(ret_type) = node.ret_type() {
if ret_type.type_ref().is_some() {
format_to!(buf, " {}", ret_type);
}
}
if let Some(where_clause) = node.where_clause() {
format_to!(buf, "\n{}", where_clause);
}
buf
} }
pub(crate) fn const_label(node: &ast::ConstDef) -> String { pub(crate) fn const_label(node: &ast::ConstDef) -> String {

View File

@ -2,15 +2,12 @@
// FIXME: this modules relies on strings and AST way too much, and it should be // FIXME: this modules relies on strings and AST way too much, and it should be
// rewritten (matklad 2020-05-07) // rewritten (matklad 2020-05-07)
use std::{ use std::convert::From;
convert::From,
fmt::{self, Display},
};
use hir::{Docs, Documentation, HasSource, HirDisplay}; use hir::{Docs, Documentation, HasSource, HirDisplay};
use ra_ide_db::RootDatabase; use ra_ide_db::RootDatabase;
use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
use stdx::{split_delim, SepBy}; use stdx::split_delim;
use crate::display::{generic_parameters, where_predicates}; use crate::display::{generic_parameters, where_predicates};
@ -247,52 +244,3 @@ impl From<&'_ ast::FnDef> for FunctionSignature {
} }
} }
} }
impl Display for FunctionSignature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(t) = &self.visibility {
write!(f, "{} ", t)?;
}
if self.qualifier.is_async {
write!(f, "async ")?;
}
if self.qualifier.is_const {
write!(f, "const ")?;
}
if self.qualifier.is_unsafe {
write!(f, "unsafe ")?;
}
if let Some(extern_abi) = &self.qualifier.extern_abi {
// Keyword `extern` is included in the string.
write!(f, "{} ", extern_abi)?;
}
if let Some(name) = &self.name {
match self.kind {
CallableKind::Function => write!(f, "fn {}", name)?,
CallableKind::StructConstructor => write!(f, "struct {}", name)?,
CallableKind::VariantConstructor => write!(f, "{}", name)?,
}
}
if !self.generic_parameters.is_empty() {
write!(f, "{}", self.generic_parameters.iter().sep_by(", ").surround_with("<", ">"))?;
}
write!(f, "{}", self.parameters.iter().sep_by(", ").surround_with("(", ")"))?;
if let Some(t) = &self.ret_type {
write!(f, " -> {}", t)?;
}
if !self.where_predicates.is_empty() {
write!(f, "\nwhere {}", self.where_predicates.iter().sep_by(",\n "))?;
}
Ok(())
}
}