From 0265778e86f7e130a921ab6307cfdcc0ad953fe0 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 16 Jul 2020 21:32:20 +0200 Subject: Don't use function signature for Display --- .../ra_ide/src/completion/complete_trait_impl.rs | 9 ++-- crates/ra_ide/src/completion/presentation.rs | 6 ++- crates/ra_ide/src/display.rs | 38 ++++++++++++++- crates/ra_ide/src/display/function_signature.rs | 56 +--------------------- 4 files changed, 47 insertions(+), 62 deletions(-) (limited to 'crates/ra_ide') diff --git a/crates/ra_ide/src/completion/complete_trait_impl.rs b/crates/ra_ide/src/completion/complete_trait_impl.rs index 90f5b1c25..05e605670 100644 --- a/crates/ra_ide/src/completion/complete_trait_impl.rs +++ b/crates/ra_ide/src/completion/complete_trait_impl.rs @@ -43,7 +43,7 @@ use crate::{ completion::{ CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, }, - display::function_signature::FunctionSignature, + display::function_label, }; pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { @@ -125,8 +125,6 @@ fn add_function_impl( ctx: &CompletionContext, func: hir::Function, ) { - let signature = FunctionSignature::from_hir(ctx.db, func); - let fn_name = func.name(ctx.db).to_string(); 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 function_decl = function_label(&func.source(ctx.db).value); match ctx.config.snippet_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)) } None => { - let header = format!("{} {{", signature); + let header = format!("{} {{", function_decl); builder.text_edit(TextEdit::replace(range, header)) } } diff --git a/crates/ra_ide/src/completion/presentation.rs b/crates/ra_ide/src/completion/presentation.rs index e29b82017..160f2f319 100644 --- a/crates/ra_ide/src/completion/presentation.rs +++ b/crates/ra_ide/src/completion/presentation.rs @@ -11,7 +11,9 @@ use crate::{ completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind, 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, }; @@ -206,7 +208,7 @@ impl Completions { }) .set_documentation(func.docs(ctx.db)) .set_deprecated(is_deprecated(func, ctx.db)) - .detail(function_signature.to_string()); + .detail(function_label(&ast_node)); let params = function_signature .parameter_names diff --git a/crates/ra_ide/src/display.rs b/crates/ra_ide/src/display.rs index 1ec946369..9d413cf0a 100644 --- a/crates/ra_ide/src/display.rs +++ b/crates/ra_ide/src/display.rs @@ -13,10 +13,46 @@ use ra_syntax::{ pub(crate) use navigation_target::{ToNav, TryToNav}; pub(crate) use short_label::ShortLabel; +use ast::VisibilityOwner; pub use navigation_target::NavigationTarget; +use stdx::format_to; 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 { diff --git a/crates/ra_ide/src/display/function_signature.rs b/crates/ra_ide/src/display/function_signature.rs index 9b7220d1f..77551117b 100644 --- a/crates/ra_ide/src/display/function_signature.rs +++ b/crates/ra_ide/src/display/function_signature.rs @@ -2,15 +2,12 @@ // FIXME: this modules relies on strings and AST way too much, and it should be // rewritten (matklad 2020-05-07) -use std::{ - convert::From, - fmt::{self, Display}, -}; +use std::convert::From; use hir::{Docs, Documentation, HasSource, HirDisplay}; use ra_ide_db::RootDatabase; use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; -use stdx::{split_delim, SepBy}; +use stdx::split_delim; 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(()) - } -} -- cgit v1.2.3