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.
This commit is contained in:
Ville Penttinen 2019-03-12 09:24:46 +02:00
parent 5f700179fc
commit 0e49abb7fb
8 changed files with 212 additions and 71 deletions

View file

@ -0,0 +1,51 @@
use super::*;
use std::fmt::{self, Display};
impl Display for FunctionSignature {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(t) = &self.visibility {
write!(f, "{} ", t)?;
}
if let Some(name) = &self.name {
write!(f, "fn {}", name)?;
}
if !self.generic_parameters.is_empty() {
write!(f, "<")?;
write_joined(f, &self.generic_parameters, ", ")?;
write!(f, ">")?;
}
write!(f, "(")?;
write_joined(f, &self.parameters, ", ")?;
write!(f, ")")?;
if let Some(t) = &self.ret_type {
write!(f, " -> {}", t)?;
}
if !self.where_predicates.is_empty() {
write!(f, "\nwhere ")?;
write_joined(f, &self.where_predicates, ",\n ")?;
}
Ok(())
}
}
fn write_joined<T: Display>(
f: &mut fmt::Formatter,
items: impl IntoIterator<Item = T>,
sep: &str,
) -> fmt::Result {
let mut first = true;
for e in items {
if !first {
write!(f, "{}", sep)?;
}
first = false;
write!(f, "{}", e)?;
}
Ok(())
}