From 7ba22f1c19c8fbfe45630c35ebd963d4c5475bc9 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Thu, 4 Apr 2019 19:30:20 +0300 Subject: Move FunctionSignature to display, remove write_joined write_joined is replaced with `join_to_string::join` which provides the necessary functionality. --- crates/ra_ide_api/src/display.rs | 58 ++++++++++++++++++++++++---------------- crates/ra_ide_api/src/lib.rs | 27 +------------------ 2 files changed, 36 insertions(+), 49 deletions(-) (limited to 'crates/ra_ide_api') 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 @@ +//! This module contains utilities for rendering turning things into something +//! that may be used to render in UI. use super::*; use std::fmt::{self, Display}; +use join_to_string::join; + +/// Contains information about a function signature +#[derive(Debug)] +pub struct FunctionSignature { + /// Optional visibility + pub visibility: Option, + /// Name of the function + pub name: Option, + /// Documentation for the function + pub doc: Option, + /// Generic parameters + pub generic_parameters: Vec, + /// Parameters of the function + pub parameters: Vec, + /// Optional return type + pub ret_type: Option, + /// Where predicates + pub where_predicates: Vec, +} + +impl FunctionSignature { + pub(crate) fn with_doc_opt(mut self, doc: Option) -> Self { + self.doc = doc; + self + } +} impl Display for FunctionSignature { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -12,14 +41,13 @@ impl Display for FunctionSignature { } if !self.generic_parameters.is_empty() { - write!(f, "<")?; - write_joined(f, &self.generic_parameters, ", ")?; - write!(f, ">")?; + join(self.generic_parameters.iter()) + .separator(", ") + .surround_with("<", ">") + .to_fmt(f)?; } - write!(f, "(")?; - write_joined(f, &self.parameters, ", ")?; - write!(f, ")")?; + join(self.parameters.iter()).separator(", ").surround_with("(", ")").to_fmt(f)?; if let Some(t) = &self.ret_type { write!(f, " -> {}", t)?; @@ -27,25 +55,9 @@ impl Display for FunctionSignature { if !self.where_predicates.is_empty() { write!(f, "\nwhere ")?; - write_joined(f, &self.where_predicates, ",\n ")?; + join(self.where_predicates.iter()).separator(",\n ").to_fmt(f)?; } Ok(()) } } - -fn write_joined( - f: &mut fmt::Formatter, - items: impl IntoIterator, - sep: &str, -) -> fmt::Result { - let mut first = true; - for e in items { - if !first { - write!(f, "{}", sep)?; - } - first = false; - write!(f, "{}", e)?; - } - Ok(()) -} diff --git a/crates/ra_ide_api/src/lib.rs b/crates/ra_ide_api/src/lib.rs index 7f8f454bc..816bab94f 100644 --- a/crates/ra_ide_api/src/lib.rs +++ b/crates/ra_ide_api/src/lib.rs @@ -73,6 +73,7 @@ pub use crate::{ syntax_highlighting::HighlightedRange, structure::{StructureNode, file_structure}, diagnostics::Severity, + display::FunctionSignature, }; pub use ra_db::{ @@ -248,32 +249,6 @@ pub struct CallInfo { pub active_parameter: Option, } -/// Contains information about a function signature -#[derive(Debug)] -pub struct FunctionSignature { - /// Optional visibility - pub visibility: Option, - /// Name of the function - pub name: Option, - /// Documentation for the function - pub doc: Option, - /// Generic parameters - pub generic_parameters: Vec, - /// Parameters of the function - pub parameters: Vec, - /// Optional return type - pub ret_type: Option, - /// Where predicates - pub where_predicates: Vec, -} - -impl FunctionSignature { - pub(crate) fn with_doc_opt(mut self, doc: Option) -> Self { - self.doc = doc; - self - } -} - /// `AnalysisHost` stores the current state of the world. #[derive(Debug, Default)] pub struct AnalysisHost { -- cgit v1.2.3