diff options
author | Ville Penttinen <[email protected]> | 2019-03-12 07:24:46 +0000 |
---|---|---|
committer | Ville Penttinen <[email protected]> | 2019-04-09 12:45:04 +0100 |
commit | 0e49abb7fbe9239b97f0b7168ec359014c63f8c0 (patch) | |
tree | b76168a2cf9f09336e446dcc584879a918681055 /crates/ra_ide_api/src/display.rs | |
parent | 5f700179fc7ed16d2848a6dbc7cf23da3b8df6c7 (diff) |
Refactor CallInfo function signatures to new FunctionSignature type
This is used by CallInfo to create a pretty printed function signature that can
be used with completions and other places as well.
Diffstat (limited to 'crates/ra_ide_api/src/display.rs')
-rw-r--r-- | crates/ra_ide_api/src/display.rs | 51 |
1 files changed, 51 insertions, 0 deletions
diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs new file mode 100644 index 000000000..60fa72f1b --- /dev/null +++ b/crates/ra_ide_api/src/display.rs | |||
@@ -0,0 +1,51 @@ | |||
1 | use super::*; | ||
2 | use std::fmt::{self, Display}; | ||
3 | |||
4 | impl Display for FunctionSignature { | ||
5 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
6 | if let Some(t) = &self.visibility { | ||
7 | write!(f, "{} ", t)?; | ||
8 | } | ||
9 | |||
10 | if let Some(name) = &self.name { | ||
11 | write!(f, "fn {}", name)?; | ||
12 | } | ||
13 | |||
14 | if !self.generic_parameters.is_empty() { | ||
15 | write!(f, "<")?; | ||
16 | write_joined(f, &self.generic_parameters, ", ")?; | ||
17 | write!(f, ">")?; | ||
18 | } | ||
19 | |||
20 | write!(f, "(")?; | ||
21 | write_joined(f, &self.parameters, ", ")?; | ||
22 | write!(f, ")")?; | ||
23 | |||
24 | if let Some(t) = &self.ret_type { | ||
25 | write!(f, " -> {}", t)?; | ||
26 | } | ||
27 | |||
28 | if !self.where_predicates.is_empty() { | ||
29 | write!(f, "\nwhere ")?; | ||
30 | write_joined(f, &self.where_predicates, ",\n ")?; | ||
31 | } | ||
32 | |||
33 | Ok(()) | ||
34 | } | ||
35 | } | ||
36 | |||
37 | fn write_joined<T: Display>( | ||
38 | f: &mut fmt::Formatter, | ||
39 | items: impl IntoIterator<Item = T>, | ||
40 | sep: &str, | ||
41 | ) -> fmt::Result { | ||
42 | let mut first = true; | ||
43 | for e in items { | ||
44 | if !first { | ||
45 | write!(f, "{}", sep)?; | ||
46 | } | ||
47 | first = false; | ||
48 | write!(f, "{}", e)?; | ||
49 | } | ||
50 | Ok(()) | ||
51 | } | ||