aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2020-07-16 20:32:20 +0100
committerAleksey Kladov <[email protected]>2020-07-16 20:32:20 +0100
commit0265778e86f7e130a921ab6307cfdcc0ad953fe0 (patch)
tree9aab4e60f3f55b783e6b2b105b239115e8fb958d
parent4759a39f06be1ec1469101a8aac39039b8743806 (diff)
Don't use function signature for Display
-rw-r--r--crates/ra_ide/src/completion/complete_trait_impl.rs9
-rw-r--r--crates/ra_ide/src/completion/presentation.rs6
-rw-r--r--crates/ra_ide/src/display.rs38
-rw-r--r--crates/ra_ide/src/display/function_signature.rs56
4 files changed, 47 insertions, 62 deletions
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::{
43 completion::{ 43 completion::{
44 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions, 44 CompletionContext, CompletionItem, CompletionItemKind, CompletionKind, Completions,
45 }, 45 },
46 display::function_signature::FunctionSignature, 46 display::function_label,
47}; 47};
48 48
49pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) { 49pub(crate) fn complete_trait_impl(acc: &mut Completions, ctx: &CompletionContext) {
@@ -125,8 +125,6 @@ fn add_function_impl(
125 ctx: &CompletionContext, 125 ctx: &CompletionContext,
126 func: hir::Function, 126 func: hir::Function,
127) { 127) {
128 let signature = FunctionSignature::from_hir(ctx.db, func);
129
130 let fn_name = func.name(ctx.db).to_string(); 128 let fn_name = func.name(ctx.db).to_string();
131 129
132 let label = if !func.params(ctx.db).is_empty() { 130 let label = if !func.params(ctx.db).is_empty() {
@@ -146,13 +144,14 @@ fn add_function_impl(
146 }; 144 };
147 let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end()); 145 let range = TextRange::new(fn_def_node.text_range().start(), ctx.source_range().end());
148 146
147 let function_decl = function_label(&func.source(ctx.db).value);
149 match ctx.config.snippet_cap { 148 match ctx.config.snippet_cap {
150 Some(cap) => { 149 Some(cap) => {
151 let snippet = format!("{} {{\n $0\n}}", signature); 150 let snippet = format!("{} {{\n $0\n}}", function_decl);
152 builder.snippet_edit(cap, TextEdit::replace(range, snippet)) 151 builder.snippet_edit(cap, TextEdit::replace(range, snippet))
153 } 152 }
154 None => { 153 None => {
155 let header = format!("{} {{", signature); 154 let header = format!("{} {{", function_decl);
156 builder.text_edit(TextEdit::replace(range, header)) 155 builder.text_edit(TextEdit::replace(range, header))
157 } 156 }
158 } 157 }
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::{
11 completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind, 11 completion_item::Builder, CompletionContext, CompletionItem, CompletionItemKind,
12 CompletionKind, Completions, 12 CompletionKind, Completions,
13 }, 13 },
14 display::{const_label, function_signature::FunctionSignature, macro_label, type_label}, 14 display::{
15 const_label, function_label, function_signature::FunctionSignature, macro_label, type_label,
16 },
15 CompletionScore, RootDatabase, 17 CompletionScore, RootDatabase,
16}; 18};
17 19
@@ -206,7 +208,7 @@ impl Completions {
206 }) 208 })
207 .set_documentation(func.docs(ctx.db)) 209 .set_documentation(func.docs(ctx.db))
208 .set_deprecated(is_deprecated(func, ctx.db)) 210 .set_deprecated(is_deprecated(func, ctx.db))
209 .detail(function_signature.to_string()); 211 .detail(function_label(&ast_node));
210 212
211 let params = function_signature 213 let params = function_signature
212 .parameter_names 214 .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::{
13pub(crate) use navigation_target::{ToNav, TryToNav}; 13pub(crate) use navigation_target::{ToNav, TryToNav};
14pub(crate) use short_label::ShortLabel; 14pub(crate) use short_label::ShortLabel;
15 15
16use ast::VisibilityOwner;
16pub use navigation_target::NavigationTarget; 17pub use navigation_target::NavigationTarget;
18use stdx::format_to;
17 19
18pub(crate) fn function_label(node: &ast::FnDef) -> String { 20pub(crate) fn function_label(node: &ast::FnDef) -> String {
19 function_signature::FunctionSignature::from(node).to_string() 21 let mut buf = String::new();
22 if let Some(vis) = node.visibility() {
23 format_to!(buf, "{} ", vis);
24 }
25 if node.async_token().is_some() {
26 format_to!(buf, "async ");
27 }
28 if node.const_token().is_some() {
29 format_to!(buf, "const ");
30 }
31 if node.unsafe_token().is_some() {
32 format_to!(buf, "unsafe ");
33 }
34 if let Some(abi) = node.abi() {
35 // Keyword `extern` is included in the string.
36 format_to!(buf, "{} ", abi);
37 }
38 if let Some(name) = node.name() {
39 format_to!(buf, "fn {}", name)
40 }
41 if let Some(type_params) = node.type_param_list() {
42 format_to!(buf, "{}", type_params);
43 }
44 if let Some(param_list) = node.param_list() {
45 format_to!(buf, "{}", param_list);
46 }
47 if let Some(ret_type) = node.ret_type() {
48 if ret_type.type_ref().is_some() {
49 format_to!(buf, " {}", ret_type);
50 }
51 }
52 if let Some(where_clause) = node.where_clause() {
53 format_to!(buf, "\n{}", where_clause);
54 }
55 buf
20} 56}
21 57
22pub(crate) fn const_label(node: &ast::ConstDef) -> String { 58pub(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 @@
2 2
3// FIXME: this modules relies on strings and AST way too much, and it should be 3// FIXME: this modules relies on strings and AST way too much, and it should be
4// rewritten (matklad 2020-05-07) 4// rewritten (matklad 2020-05-07)
5use std::{ 5use std::convert::From;
6 convert::From,
7 fmt::{self, Display},
8};
9 6
10use hir::{Docs, Documentation, HasSource, HirDisplay}; 7use hir::{Docs, Documentation, HasSource, HirDisplay};
11use ra_ide_db::RootDatabase; 8use ra_ide_db::RootDatabase;
12use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; 9use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner};
13use stdx::{split_delim, SepBy}; 10use stdx::split_delim;
14 11
15use crate::display::{generic_parameters, where_predicates}; 12use crate::display::{generic_parameters, where_predicates};
16 13
@@ -247,52 +244,3 @@ impl From<&'_ ast::FnDef> for FunctionSignature {
247 } 244 }
248 } 245 }
249} 246}
250
251impl Display for FunctionSignature {
252 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
253 if let Some(t) = &self.visibility {
254 write!(f, "{} ", t)?;
255 }
256
257 if self.qualifier.is_async {
258 write!(f, "async ")?;
259 }
260
261 if self.qualifier.is_const {
262 write!(f, "const ")?;
263 }
264
265 if self.qualifier.is_unsafe {
266 write!(f, "unsafe ")?;
267 }
268
269 if let Some(extern_abi) = &self.qualifier.extern_abi {
270 // Keyword `extern` is included in the string.
271 write!(f, "{} ", extern_abi)?;
272 }
273
274 if let Some(name) = &self.name {
275 match self.kind {
276 CallableKind::Function => write!(f, "fn {}", name)?,
277 CallableKind::StructConstructor => write!(f, "struct {}", name)?,
278 CallableKind::VariantConstructor => write!(f, "{}", name)?,
279 }
280 }
281
282 if !self.generic_parameters.is_empty() {
283 write!(f, "{}", self.generic_parameters.iter().sep_by(", ").surround_with("<", ">"))?;
284 }
285
286 write!(f, "{}", self.parameters.iter().sep_by(", ").surround_with("(", ")"))?;
287
288 if let Some(t) = &self.ret_type {
289 write!(f, " -> {}", t)?;
290 }
291
292 if !self.where_predicates.is_empty() {
293 write!(f, "\nwhere {}", self.where_predicates.iter().sep_by(",\n "))?;
294 }
295
296 Ok(())
297 }
298}