diff options
-rw-r--r-- | crates/ra_ide_api/src/display.rs | 224 |
1 files changed, 112 insertions, 112 deletions
diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index e01635460..4ce362ebb 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs | |||
@@ -1,112 +1,112 @@ | |||
1 | //! This module contains utilities for rendering turning things into something | 1 | //! This module contains utilities for rendering turning things into something |
2 | //! that may be used to render in UI. | 2 | //! that may be used to render in UI. |
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}; | 6 | use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner, TypeParamsOwner}; |
7 | use std::convert::From; | 7 | use std::convert::From; |
8 | 8 | ||
9 | /// Contains information about a function signature | 9 | /// Contains information about a function signature |
10 | #[derive(Debug)] | 10 | #[derive(Debug)] |
11 | pub struct FunctionSignature { | 11 | pub struct FunctionSignature { |
12 | /// Optional visibility | 12 | /// Optional visibility |
13 | pub visibility: Option<String>, | 13 | pub visibility: Option<String>, |
14 | /// Name of the function | 14 | /// Name of the function |
15 | pub name: Option<String>, | 15 | pub name: Option<String>, |
16 | /// Documentation for the function | 16 | /// Documentation for the function |
17 | pub doc: Option<Documentation>, | 17 | pub doc: Option<Documentation>, |
18 | /// Generic parameters | 18 | /// Generic parameters |
19 | pub generic_parameters: Vec<String>, | 19 | pub generic_parameters: Vec<String>, |
20 | /// Parameters of the function | 20 | /// Parameters of the function |
21 | pub parameters: Vec<String>, | 21 | pub parameters: Vec<String>, |
22 | /// Optional return type | 22 | /// Optional return type |
23 | pub ret_type: Option<String>, | 23 | pub ret_type: Option<String>, |
24 | /// Where predicates | 24 | /// Where predicates |
25 | pub where_predicates: Vec<String>, | 25 | pub where_predicates: Vec<String>, |
26 | } | 26 | } |
27 | 27 | ||
28 | impl FunctionSignature { | 28 | impl FunctionSignature { |
29 | pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self { | 29 | pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self { |
30 | self.doc = doc; | 30 | self.doc = doc; |
31 | self | 31 | self |
32 | } | 32 | } |
33 | } | 33 | } |
34 | 34 | ||
35 | impl From<&'_ ast::FnDef> for FunctionSignature { | 35 | impl From<&'_ ast::FnDef> for FunctionSignature { |
36 | fn from(node: &ast::FnDef) -> FunctionSignature { | 36 | fn from(node: &ast::FnDef) -> FunctionSignature { |
37 | fn param_list(node: &ast::FnDef) -> Vec<String> { | 37 | fn param_list(node: &ast::FnDef) -> Vec<String> { |
38 | let mut res = vec![]; | 38 | let mut res = vec![]; |
39 | if let Some(param_list) = node.param_list() { | 39 | if let Some(param_list) = node.param_list() { |
40 | if let Some(self_param) = param_list.self_param() { | 40 | if let Some(self_param) = param_list.self_param() { |
41 | res.push(self_param.syntax().text().to_string()) | 41 | res.push(self_param.syntax().text().to_string()) |
42 | } | 42 | } |
43 | 43 | ||
44 | res.extend(param_list.params().map(|param| param.syntax().text().to_string())); | 44 | res.extend(param_list.params().map(|param| param.syntax().text().to_string())); |
45 | } | 45 | } |
46 | res | 46 | res |
47 | } | 47 | } |
48 | 48 | ||
49 | FunctionSignature { | 49 | FunctionSignature { |
50 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), | 50 | visibility: node.visibility().map(|n| n.syntax().text().to_string()), |
51 | name: node.name().map(|n| n.text().to_string()), | 51 | name: node.name().map(|n| n.text().to_string()), |
52 | ret_type: node | 52 | ret_type: node |
53 | .ret_type() | 53 | .ret_type() |
54 | .and_then(|r| r.type_ref()) | 54 | .and_then(|r| r.type_ref()) |
55 | .map(|n| n.syntax().text().to_string()), | 55 | .map(|n| n.syntax().text().to_string()), |
56 | parameters: param_list(node), | 56 | parameters: param_list(node), |
57 | generic_parameters: generic_parameters(node), | 57 | generic_parameters: generic_parameters(node), |
58 | where_predicates: where_predicates(node), | 58 | where_predicates: where_predicates(node), |
59 | // docs are processed separately | 59 | // docs are processed separately |
60 | doc: None, | 60 | doc: None, |
61 | } | 61 | } |
62 | } | 62 | } |
63 | } | 63 | } |
64 | 64 | ||
65 | impl Display for FunctionSignature { | 65 | impl Display for FunctionSignature { |
66 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | 66 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
67 | if let Some(t) = &self.visibility { | 67 | if let Some(t) = &self.visibility { |
68 | write!(f, "{} ", t)?; | 68 | write!(f, "{} ", t)?; |
69 | } | 69 | } |
70 | 70 | ||
71 | if let Some(name) = &self.name { | 71 | if let Some(name) = &self.name { |
72 | write!(f, "fn {}", name)?; | 72 | write!(f, "fn {}", name)?; |
73 | } | 73 | } |
74 | 74 | ||
75 | if !self.generic_parameters.is_empty() { | 75 | if !self.generic_parameters.is_empty() { |
76 | join(self.generic_parameters.iter()) | 76 | join(self.generic_parameters.iter()) |
77 | .separator(", ") | 77 | .separator(", ") |
78 | .surround_with("<", ">") | 78 | .surround_with("<", ">") |
79 | .to_fmt(f)?; | 79 | .to_fmt(f)?; |
80 | } | 80 | } |
81 | 81 | ||
82 | join(self.parameters.iter()).separator(", ").surround_with("(", ")").to_fmt(f)?; | 82 | join(self.parameters.iter()).separator(", ").surround_with("(", ")").to_fmt(f)?; |
83 | 83 | ||
84 | if let Some(t) = &self.ret_type { | 84 | if let Some(t) = &self.ret_type { |
85 | write!(f, " -> {}", t)?; | 85 | write!(f, " -> {}", t)?; |
86 | } | 86 | } |
87 | 87 | ||
88 | if !self.where_predicates.is_empty() { | 88 | if !self.where_predicates.is_empty() { |
89 | write!(f, "\nwhere ")?; | 89 | write!(f, "\nwhere ")?; |
90 | join(self.where_predicates.iter()).separator(",\n ").to_fmt(f)?; | 90 | join(self.where_predicates.iter()).separator(",\n ").to_fmt(f)?; |
91 | } | 91 | } |
92 | 92 | ||
93 | Ok(()) | 93 | Ok(()) |
94 | } | 94 | } |
95 | } | 95 | } |
96 | 96 | ||
97 | pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> { | 97 | pub(crate) fn generic_parameters<N: TypeParamsOwner>(node: &N) -> Vec<String> { |
98 | let mut res = vec![]; | 98 | let mut res = vec![]; |
99 | if let Some(type_params) = node.type_param_list() { | 99 | if let Some(type_params) = node.type_param_list() { |
100 | res.extend(type_params.lifetime_params().map(|p| p.syntax().text().to_string())); | 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())); | 101 | res.extend(type_params.type_params().map(|p| p.syntax().text().to_string())); |
102 | } | 102 | } |
103 | res | 103 | res |
104 | } | 104 | } |
105 | 105 | ||
106 | pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> { | 106 | pub(crate) fn where_predicates<N: TypeParamsOwner>(node: &N) -> Vec<String> { |
107 | let mut res = vec![]; | 107 | let mut res = vec![]; |
108 | if let Some(clause) = node.where_clause() { | 108 | if let Some(clause) = node.where_clause() { |
109 | res.extend(clause.predicates().map(|p| p.syntax().text().to_string())); | 109 | res.extend(clause.predicates().map(|p| p.syntax().text().to_string())); |
110 | } | 110 | } |
111 | res | 111 | res |
112 | } | 112 | } |