diff options
Diffstat (limited to 'crates/ra_ide_api/src/display.rs')
-rw-r--r-- | crates/ra_ide_api/src/display.rs | 49 |
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 @@ | |||
3 | use super::*; | 3 | use super::*; |
4 | use std::fmt::{self, Display}; | 4 | use std::fmt::{self, Display}; |
5 | use join_to_string::join; | 5 | use join_to_string::join; |
6 | use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner, TypeParamsOwner}; | ||
7 | use 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 | ||
35 | impl 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 | |||
33 | impl Display for FunctionSignature { | 65 | impl 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 | |||
97 | pub(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 | |||
106 | pub(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 | } | ||