mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
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:
parent
5f700179fc
commit
0e49abb7fb
8 changed files with 212 additions and 71 deletions
51
crates/ra_ide_api/src/display.rs
Normal file
51
crates/ra_ide_api/src/display.rs
Normal 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(())
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue