From fead60aa27e54d3ce48819c30d1db59c52d856c9 Mon Sep 17 00:00:00 2001 From: Ville Penttinen Date: Mon, 8 Apr 2019 16:34:59 +0300 Subject: Move FunctionSignature to display/function_signature --- crates/ra_ide_api/src/display.rs | 103 +-------------------- .../ra_ide_api/src/display/function_signature.rs | 101 ++++++++++++++++++++ 2 files changed, 104 insertions(+), 100 deletions(-) create mode 100644 crates/ra_ide_api/src/display/function_signature.rs diff --git a/crates/ra_ide_api/src/display.rs b/crates/ra_ide_api/src/display.rs index e29ae3371..f1717b008 100644 --- a/crates/ra_ide_api/src/display.rs +++ b/crates/ra_ide_api/src/display.rs @@ -1,18 +1,15 @@ //! This module contains utilities for turning SyntaxNodes and HIR types //! into things that may be used to render in a UI. +mod function_signature; mod navigation_target; mod structure; -use super::*; -use std::fmt::{self, Display}; -use join_to_string::join; -use ra_syntax::{ast::{self, AstNode, NameOwner, VisibilityOwner, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}}; -use std::convert::From; -use hir::Docs; +use ra_syntax::{ast::{self, AstNode, TypeParamsOwner}, SyntaxKind::{ATTR, COMMENT}}; pub use navigation_target::NavigationTarget; pub use structure::{StructureNode, file_structure}; +pub use function_signature::FunctionSignature; pub(crate) fn function_label(node: &ast::FnDef) -> String { FunctionSignature::from(node).to_string() @@ -40,100 +37,6 @@ pub(crate) fn type_label(node: &ast::TypeAliasDef) -> String { label.trim().to_owned() } -/// 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 - } - - pub(crate) fn from_hir(db: &db::RootDatabase, function: hir::Function) -> Self { - let doc = function.docs(db); - let (_, ast_node) = function.source(db); - FunctionSignature::from(&*ast_node).with_doc_opt(doc) - } -} - -impl From<&'_ ast::FnDef> for FunctionSignature { - fn from(node: &ast::FnDef) -> FunctionSignature { - fn param_list(node: &ast::FnDef) -> Vec { - let mut res = vec![]; - if let Some(param_list) = node.param_list() { - if let Some(self_param) = param_list.self_param() { - res.push(self_param.syntax().text().to_string()) - } - - res.extend(param_list.params().map(|param| param.syntax().text().to_string())); - } - res - } - - FunctionSignature { - visibility: node.visibility().map(|n| n.syntax().text().to_string()), - name: node.name().map(|n| n.text().to_string()), - ret_type: node - .ret_type() - .and_then(|r| r.type_ref()) - .map(|n| n.syntax().text().to_string()), - parameters: param_list(node), - generic_parameters: generic_parameters(node), - where_predicates: where_predicates(node), - // docs are processed separately - doc: None, - } - } -} - -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() { - join(self.generic_parameters.iter()) - .separator(", ") - .surround_with("<", ">") - .to_fmt(f)?; - } - - join(self.parameters.iter()).separator(", ").surround_with("(", ")").to_fmt(f)?; - - if let Some(t) = &self.ret_type { - write!(f, " -> {}", t)?; - } - - if !self.where_predicates.is_empty() { - write!(f, "\nwhere ")?; - join(self.where_predicates.iter()).separator(",\n ").to_fmt(f)?; - } - - Ok(()) - } -} - pub(crate) fn generic_parameters(node: &N) -> Vec { let mut res = vec![]; if let Some(type_params) = node.type_param_list() { diff --git a/crates/ra_ide_api/src/display/function_signature.rs b/crates/ra_ide_api/src/display/function_signature.rs new file mode 100644 index 000000000..d09950bce --- /dev/null +++ b/crates/ra_ide_api/src/display/function_signature.rs @@ -0,0 +1,101 @@ +use super::{where_predicates, generic_parameters}; +use crate::db; +use std::fmt::{self, Display}; +use join_to_string::join; +use ra_syntax::ast::{self, AstNode, NameOwner, VisibilityOwner}; +use std::convert::From; +use hir::{Docs, Documentation}; + +/// 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 + } + + pub(crate) fn from_hir(db: &db::RootDatabase, function: hir::Function) -> Self { + let doc = function.docs(db); + let (_, ast_node) = function.source(db); + FunctionSignature::from(&*ast_node).with_doc_opt(doc) + } +} + +impl From<&'_ ast::FnDef> for FunctionSignature { + fn from(node: &ast::FnDef) -> FunctionSignature { + fn param_list(node: &ast::FnDef) -> Vec { + let mut res = vec![]; + if let Some(param_list) = node.param_list() { + if let Some(self_param) = param_list.self_param() { + res.push(self_param.syntax().text().to_string()) + } + + res.extend(param_list.params().map(|param| param.syntax().text().to_string())); + } + res + } + + FunctionSignature { + visibility: node.visibility().map(|n| n.syntax().text().to_string()), + name: node.name().map(|n| n.text().to_string()), + ret_type: node + .ret_type() + .and_then(|r| r.type_ref()) + .map(|n| n.syntax().text().to_string()), + parameters: param_list(node), + generic_parameters: generic_parameters(node), + where_predicates: where_predicates(node), + // docs are processed separately + doc: None, + } + } +} + +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() { + join(self.generic_parameters.iter()) + .separator(", ") + .surround_with("<", ">") + .to_fmt(f)?; + } + + join(self.parameters.iter()).separator(", ").surround_with("(", ")").to_fmt(f)?; + + if let Some(t) = &self.ret_type { + write!(f, " -> {}", t)?; + } + + if !self.where_predicates.is_empty() { + write!(f, "\nwhere ")?; + join(self.where_predicates.iter()).separator(",\n ").to_fmt(f)?; + } + + Ok(()) + } +} -- cgit v1.2.3