1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 use {FmtVisitor, ReturnIndent, make_indent, MAX_WIDTH, BraceStyle,
12 IDEAL_WIDTH, LEEWAY, FN_BRACE_STYLE, FN_RETURN_INDENT};
13 use lists::{write_list, ListFormatting, SeparatorTactic, ListTactic};
14 use syntax::{ast, abi};
15 use syntax::print::pprust;
16 use syntax::parse::token;
18 impl<'a> FmtVisitor<'a> {
19 pub fn rewrite_fn(&mut self,
23 explicit_self: Option<&ast::ExplicitSelf>,
24 generics: &ast::Generics,
25 unsafety: &ast::Unsafety,
30 // FIXME we'll lose any comments in between parts of the function decl, but anyone
31 // who comments there probably deserves what they get.
33 let where_clause = &generics.where_clause;
34 let newline_brace = match FN_BRACE_STYLE {
35 BraceStyle::AlwaysNextLine => true,
36 BraceStyle::SameLineWhere if where_clause.predicates.len() > 0 => true,
40 let mut result = String::with_capacity(1024);
42 if vis == ast::Visibility::Public {
43 result.push_str("pub ");
45 if let &ast::Unsafety::Unsafe = unsafety {
46 result.push_str("unsafe ");
48 if *abi != abi::Rust {
49 result.push_str("extern ");
50 result.push_str(&abi.to_string());
55 result.push_str("fn ");
56 result.push_str(&token::get_ident(ident));
59 // FIXME convert bounds to where clauses where they get too big or if
60 // there is a where clause at all.
61 let lifetimes: &[_] = &generics.lifetimes;
62 let tys: &[_] = &generics.ty_params;
63 if lifetimes.len() + tys.len() > 0 {
64 let budget = MAX_WIDTH - indent - result.len() - 2;
65 // TODO might need to insert a newline if the generics are really long
68 let lt_strs = lifetimes.iter().map(|l| self.rewrite_lifetime_def(l));
69 let ty_strs = tys.iter().map(|ty| self.rewrite_ty_param(ty));
70 let generics_strs: Vec<_> = lt_strs.chain(ty_strs).map(|s| (s, String::new())).collect();
71 let fmt = ListFormatting {
72 tactic: ListTactic::HorizontalVertical,
74 trailing_separator: SeparatorTactic::Never,
75 indent: indent + result.len() + 1,
79 result.push_str(&write_list(&generics_strs, &fmt));
84 let ret_str = match fd.output {
85 ast::FunctionRetTy::DefaultReturn(_) => String::new(),
86 ast::FunctionRetTy::NoReturn(_) => "-> !".to_string(),
87 ast::FunctionRetTy::Return(ref ty) => "-> ".to_string() + &pprust::ty_to_string(ty),
91 let args = &fd.inputs;
93 let mut budgets = None;
95 // Try keeping everything on the same line
96 if !result.contains("\n") {
97 // 3 = `() `, space is before ret_string
98 let mut used_space = indent + result.len() + 3 + ret_str.len();
102 let one_line_budget = if used_space > MAX_WIDTH {
105 MAX_WIDTH - used_space
108 let used_space = indent + result.len() + 2;
109 let max_space = IDEAL_WIDTH + LEEWAY;
110 if used_space < max_space {
111 budgets = Some((one_line_budget,
113 max_space - used_space,
114 indent + result.len() + 1));
118 // Didn't work. we must force vertical layout and put args on a newline.
119 if let None = budgets {
121 result.push_str(&make_indent(indent + 4));
122 // 6 = new indent + `()`
123 let used_space = indent + 6;
124 let max_space = IDEAL_WIDTH + LEEWAY;
125 if used_space > max_space {
127 // TODO take evasive action, perhaps kill the indent or something.
129 // 5 = new indent + `(`
130 budgets = Some((0, max_space - used_space, indent + 5));
134 let (one_line_budget, multi_line_budget, arg_indent) = budgets.unwrap();
137 let fmt = ListFormatting {
138 tactic: ListTactic::HorizontalVertical,
140 trailing_separator: SeparatorTactic::Never,
142 h_width: one_line_budget,
143 v_width: multi_line_budget,
146 let mut arg_strs: Vec<_> = args.iter().map(|a| (self.rewrite_fn_input(a), String::new())).collect();
147 // Account for sugary self.
148 if let Some(explicit_self) = explicit_self {
149 match explicit_self.node {
150 ast::ExplicitSelf_::SelfRegion(ref lt, ref m, _) => {
151 let lt_str = match lt {
152 &Some(ref l) => format!("{} ", pprust::lifetime_to_string(l)),
153 &None => String::new(),
155 let mut_str = match m {
156 &ast::Mutability::MutMutable => "mut ".to_string(),
157 &ast::Mutability::MutImmutable => String::new(),
159 arg_strs[0].0 = format!("&{}{}self", lt_str, mut_str);
161 ast::ExplicitSelf_::SelfExplicit(ref ty, _) => {
162 arg_strs[0].0 = format!("self: {}", pprust::ty_to_string(ty));
164 ast::ExplicitSelf_::SelfValue(_) => {
165 arg_strs[0].0 = "self".to_string();
170 result.push_str(&write_list(&arg_strs, &fmt));
175 if where_clause.predicates.len() > 0 {
177 result.push_str(&make_indent(indent + 4));
178 result.push_str("where ");
180 let budget = IDEAL_WIDTH + LEEWAY - indent - 10;
181 let fmt = ListFormatting {
182 tactic: ListTactic::Vertical,
184 trailing_separator: SeparatorTactic::Always,
189 let where_strs: Vec<_> = where_clause.predicates.iter().map(|p| (self.rewrite_pred(p), String::new())).collect();
190 result.push_str(&write_list(&where_strs, &fmt));
194 if ret_str.len() > 0 {
195 // If we've already gone multi-line, or the return type would push
196 // over the max width, then put the return type on a new line.
197 if result.contains("\n") ||
198 result.len() + indent + ret_str.len() > MAX_WIDTH {
199 let indent = match FN_RETURN_INDENT {
200 ReturnIndent::WithWhereClause => indent + 4,
201 // TODO we might want to check that using the arg indent doesn't
202 // blow our budget, and if it does, then fallback to the where
204 ReturnIndent::WithArgs => arg_indent,
208 result.push_str(&make_indent(indent));
212 result.push_str(&ret_str);
215 // Prepare for the function body by possibly adding a newline and indent.
216 // FIXME we'll miss anything between the end of the signature and the start
217 // of the body, but we need more spans from the compiler to solve this.
220 result.push_str(&make_indent(self.block_indent));
228 // TODO we farm this out, but this could spill over the column limit, so we ought to handle it properly
229 fn rewrite_fn_input(&self, arg: &ast::Arg) -> String {
231 pprust::pat_to_string(&arg.pat),
232 pprust::ty_to_string(&arg.ty))