aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api/src/display.rs
diff options
context:
space:
mode:
authorVille Penttinen <[email protected]>2019-04-04 17:30:20 +0100
committerVille Penttinen <[email protected]>2019-04-09 12:45:05 +0100
commit7ba22f1c19c8fbfe45630c35ebd963d4c5475bc9 (patch)
treecd6e12e42ef50e1638c64c69bd34ce3d085c4f6b /crates/ra_ide_api/src/display.rs
parent84fde47d00bb3ccba3876ad2b2e46c5c59cd07c4 (diff)
Move FunctionSignature to display, remove write_joined
write_joined is replaced with `join_to_string::join` which provides the necessary functionality.
Diffstat (limited to 'crates/ra_ide_api/src/display.rs')
-rw-r--r--crates/ra_ide_api/src/display.rs58
1 files changed, 35 insertions, 23 deletions
diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs
index 60fa72f1b..9d9d2097f 100644
--- a/crates/ra_ide_api/src/display.rs
+++ b/crates/ra_ide_api/src/display.rs
@@ -1,5 +1,34 @@
1//! This module contains utilities for rendering turning things into something
2//! that may be used to render in UI.
1use super::*; 3use super::*;
2use std::fmt::{self, Display}; 4use std::fmt::{self, Display};
5use join_to_string::join;
6
7/// Contains information about a function signature
8#[derive(Debug)]
9pub struct FunctionSignature {
10 /// Optional visibility
11 pub visibility: Option<String>,
12 /// Name of the function
13 pub name: Option<String>,
14 /// Documentation for the function
15 pub doc: Option<Documentation>,
16 /// Generic parameters
17 pub generic_parameters: Vec<String>,
18 /// Parameters of the function
19 pub parameters: Vec<String>,
20 /// Optional return type
21 pub ret_type: Option<String>,
22 /// Where predicates
23 pub where_predicates: Vec<String>,
24}
25
26impl FunctionSignature {
27 pub(crate) fn with_doc_opt(mut self, doc: Option<Documentation>) -> Self {
28 self.doc = doc;
29 self
30 }
31}
3 32
4impl Display for FunctionSignature { 33impl Display for FunctionSignature {
5 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { 34 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
@@ -12,14 +41,13 @@ impl Display for FunctionSignature {
12 } 41 }
13 42
14 if !self.generic_parameters.is_empty() { 43 if !self.generic_parameters.is_empty() {
15 write!(f, "<")?; 44 join(self.generic_parameters.iter())
16 write_joined(f, &self.generic_parameters, ", ")?; 45 .separator(", ")
17 write!(f, ">")?; 46 .surround_with("<", ">")
47 .to_fmt(f)?;
18 } 48 }
19 49
20 write!(f, "(")?; 50 join(self.parameters.iter()).separator(", ").surround_with("(", ")").to_fmt(f)?;
21 write_joined(f, &self.parameters, ", ")?;
22 write!(f, ")")?;
23 51
24 if let Some(t) = &self.ret_type { 52 if let Some(t) = &self.ret_type {
25 write!(f, " -> {}", t)?; 53 write!(f, " -> {}", t)?;
@@ -27,25 +55,9 @@ impl Display for FunctionSignature {
27 55
28 if !self.where_predicates.is_empty() { 56 if !self.where_predicates.is_empty() {
29 write!(f, "\nwhere ")?; 57 write!(f, "\nwhere ")?;
30 write_joined(f, &self.where_predicates, ",\n ")?; 58 join(self.where_predicates.iter()).separator(",\n ").to_fmt(f)?;
31 } 59 }
32 60
33 Ok(()) 61 Ok(())
34 } 62 }
35} 63}
36
37fn 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}