aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/display.rs
diff options
context:
space:
mode:
authorVille Penttinen <[email protected]>2019-04-04 18:05:01 +0100
committerVille Penttinen <[email protected]>2019-04-09 12:45:05 +0100
commitf4aa15c16b352d4ac9a90c1668311e4762c5e494 (patch)
treeedd18388ae9bce22ae303658e7b3232592553699 /crates/ra_ide_api/src/display.rs
parented65e2619a42aea7c375d0cbf81d337fffb11a46 (diff)
Move FunctionSignature creation to display
Diffstat (limited to 'crates/ra_ide_api/src/display.rs')
-rw-r--r--crates/ra_ide_api/src/display.rs49
1 files changed, 49 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs
index 9d9d2097f..e01635460 100644
--- a/crates/ra_ide_api/src/display.rs
+++ b/crates/ra_ide_api/src/display.rs
@@ -3,6 +3,8 @@
3use super::*; 3use super::*;
4use std::fmt::{self, Display}; 4use std::fmt::{self, Display};
5use join_to_string::join; 5use join_to_string::join;
6use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner, TypeParamsOwner};
7use std::convert::From;
6 8
7/// Contains information about a function signature 9/// Contains information about a function signature
8#[derive(Debug)] 10#[derive(Debug)]
@@ -30,6 +32,36 @@ impl FunctionSignature {
30 } 32 }
31} 33}
32 34
35impl From<&'_ ast::FnDef> for FunctionSignature {
36 fn from(node: &ast::FnDef) -> FunctionSignature {
37 fn param_list(node: &ast::FnDef) -> Vec<String> {
38 let mut res = vec![];
39 if let Some(param_list) = node.param_list() {
40 if let Some(self_param) = param_list.self_param() {
41 res.push(self_param.syntax().text().to_string())
42 }
43
44 res.extend(param_list.params().map(|param| param.syntax().text().to_string()));
45 }
46 res
47 }
48
49 FunctionSignature {
50 visibility: node.visibility().map(|n| n.syntax().text().to_string()),
51 name: node.name().map(|n| n.text().to_string()),
52 ret_type: node
53 .ret_type()
54 .and_then(|r| r.type_ref())
55 .map(|n| n.syntax().text().to_string()),
56 parameters: param_list(node),
57 generic_parameters: generic_parameters(node),
58 where_predicates: where_predicates(node),
59 // docs are processed separately
60 doc: None,
61 }
62 }
63}
64
33impl Display for FunctionSignature { 65impl Display for FunctionSignature {
34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 66 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
35 if let Some(t) = &self.visibility { 67 if let Some(t) = &self.visibility {
@@ -61,3 +93,20 @@ impl Display for FunctionSignature {
61 Ok(()) 93 Ok(())
62 } 94 }
63} 95}
96
97pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> {
98 let mut res = vec![];
99 if let Some(type_params) = node.type_param_list() {
100 res.extend(type_params.lifetime_params().map(|p| p.syntax().text().to_string()));
101 res.extend(type_params.type_params().map(|p| p.syntax().text().to_string()));
102 }
103 res
104}
105
106pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> {
107 let mut res = vec![];
108 if let Some(clause) = node.where_clause() {
109 res.extend(clause.predicates().map(|p| p.syntax().text().to_string()));
110 }
111 res
112}