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 // Formatting top-level items - functions, structs, enums, traits, impls.
15 use syntax::{abi, ast, ptr, symbol};
16 use syntax::ast::ImplItem;
17 use syntax::codemap::{BytePos, Span};
19 use {Indent, Shape, Spanned};
20 use codemap::{LineRangeUtils, SpanUtils};
21 use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
22 rewrite_comment, FindUncommented};
23 use config::{BraceStyle, Config, Density, IndentStyle, ReturnIndent, Style};
24 use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
25 rewrite_call_inner, ExprType};
26 use lists::{definitive_tactic, itemize_list, write_list, DefinitiveListTactic, ListFormatting,
27 ListItem, ListTactic, Separator, SeparatorPlace, SeparatorTactic};
28 use rewrite::{Rewrite, RewriteContext};
29 use types::join_bounds;
30 use utils::{colon_spaces, contains_skip, end_typaram, first_line_width, format_abi,
31 format_constness, format_defaultness, format_mutability, format_unsafety,
32 format_visibility, is_attributes_extendable, last_line_contains_single_line_comment,
33 last_line_used_width, last_line_width, mk_sp, semicolon_for_expr, stmt_expr,
34 trim_newlines, trimmed_last_line_width, wrap_str};
35 use vertical::rewrite_with_alignment;
36 use visitor::FmtVisitor;
38 fn type_annotation_separator(config: &Config) -> &str {
40 config.space_before_type_annotation(),
41 config.space_after_type_annotation_colon(),
45 // Statements of the form
46 // let pat: ty = init;
47 impl Rewrite for ast::Local {
48 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
50 "Local::rewrite {:?} {} {:?}",
56 skip_out_of_file_lines_range!(context, self.span);
58 let mut result = "let ".to_owned();
61 let pat_shape = try_opt!(shape.offset_left(4));
63 let pat_shape = try_opt!(pat_shape.sub_width(1));
64 let pat_str = try_opt!(self.pat.rewrite(&context, pat_shape));
65 result.push_str(&pat_str);
67 // String that is placed within the assignment pattern and expression.
69 let mut infix = String::new();
71 if let Some(ref ty) = self.ty {
72 let separator = type_annotation_separator(context.config);
73 let indent = shape.indent + last_line_width(&result) + separator.len();
75 let budget = try_opt!(shape.width.checked_sub(indent.width() + 1));
76 let rewrite = try_opt!(ty.rewrite(context, Shape::legacy(budget, indent)));
78 infix.push_str(separator);
79 infix.push_str(&rewrite);
82 if self.init.is_some() {
89 result.push_str(&infix);
91 if let Some(ref ex) = self.init {
92 // 1 = trailing semicolon;
93 let nested_shape = try_opt!(shape.sub_width(1));
95 result = try_opt!(rewrite_assign_rhs(&context, result, ex, nested_shape));
103 // TODO convert to using rewrite style rather than visitor
104 // TODO format modules in this style
107 keyword: &'static str,
109 vis: Option<&'a ast::Visibility>,
110 body: Vec<BodyElement<'a>>,
115 fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> {
116 let abi = if fm.abi == abi::Abi::C && !config.force_explicit_abi() {
119 format!("extern {}", fm.abi)
127 .map(|i| BodyElement::ForeignItem(i))
134 enum BodyElement<'a> {
135 // Stmt(&'a ast::Stmt),
136 // Field(&'a ast::Field),
137 // Variant(&'a ast::Variant),
138 // Item(&'a ast::Item),
139 ForeignItem(&'a ast::ForeignItem),
142 impl<'a> FmtVisitor<'a> {
143 fn format_item(&mut self, item: Item) {
144 self.buffer.push_str(&item.abi);
145 self.buffer.push_str(" ");
147 let snippet = self.snippet(item.span);
148 let brace_pos = snippet.find_uncommented("{").unwrap();
150 self.buffer.push_str("{");
151 if !item.body.is_empty() || contains_comment(&snippet[brace_pos..]) {
152 // FIXME: this skips comments between the extern keyword and the opening
154 self.last_pos = item.span.lo + BytePos(brace_pos as u32 + 1);
155 self.block_indent = self.block_indent.block_indent(self.config);
157 if item.body.is_empty() {
158 self.format_missing_no_indent(item.span.hi - BytePos(1));
159 self.block_indent = self.block_indent.block_unindent(self.config);
162 .push_str(&self.block_indent.to_string(self.config));
164 for item in &item.body {
165 self.format_body_element(item);
168 self.block_indent = self.block_indent.block_unindent(self.config);
169 self.format_missing_with_indent(item.span.hi - BytePos(1));
173 self.buffer.push_str("}");
174 self.last_pos = item.span.hi;
177 fn format_body_element(&mut self, element: &BodyElement) {
179 BodyElement::ForeignItem(ref item) => self.format_foreign_item(item),
183 pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
184 let item = Item::from_foreign_mod(fm, span, self.config);
185 self.format_item(item);
189 fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
190 let shape = Shape::indented(self.block_indent, self.config);
191 let rewrite = item.rewrite(&self.get_context(), shape);
192 self.push_rewrite(item.span(), rewrite);
193 self.last_pos = item.span.hi;
201 generics: &ast::Generics,
202 unsafety: ast::Unsafety,
203 constness: ast::Constness,
204 defaultness: ast::Defaultness,
206 vis: &ast::Visibility,
209 ) -> Option<String> {
210 let context = self.get_context();
212 let block_snippet = self.snippet(mk_sp(block.span.lo, block.span.hi));
213 let has_body = !block_snippet[1..block_snippet.len() - 1].trim().is_empty() ||
214 !context.config.fn_empty_single_line();
215 let mut newline_brace = newline_for_brace(self.config, &generics.where_clause, has_body);
217 let (mut result, force_newline_brace) = try_opt!(rewrite_fn_base(
234 if force_newline_brace {
235 newline_brace = true;
236 } else if self.config.fn_brace_style() != BraceStyle::AlwaysNextLine &&
237 !result.contains('\n')
239 newline_brace = false;
242 // Prepare for the function body by possibly adding a newline and
244 // FIXME we'll miss anything between the end of the signature and the
245 // start of the body, but we need more spans from the compiler to solve
249 result.push_str(&indent.to_string(self.config));
254 self.single_line_fn(&result, block).or_else(|| Some(result))
257 pub fn rewrite_required_fn(
261 sig: &ast::MethodSig,
263 ) -> Option<String> {
264 // Drop semicolon or it will be interpreted as comment.
265 let span = mk_sp(span.lo, span.hi - BytePos(1));
266 let context = self.get_context();
268 let (mut result, _) = try_opt!(rewrite_fn_base(
276 ast::Defaultness::Final,
278 &ast::Visibility::Inherited,
285 // Re-attach semicolon
291 fn single_line_fn(&self, fn_str: &str, block: &ast::Block) -> Option<String> {
292 if fn_str.contains('\n') {
296 let codemap = self.get_context().codemap;
298 if self.config.fn_empty_single_line() && is_empty_block(block, codemap) &&
299 self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width()
301 return Some(format!("{}{{}}", fn_str));
304 if self.config.fn_single_line() && is_simple_block_stmt(block, codemap) {
306 if let Some(ref stmt) = block.stmts.first() {
307 match stmt_expr(stmt) {
309 let suffix = if semicolon_for_expr(&self.get_context(), e) {
319 Shape::indented(self.block_indent, self.config),
320 ).map(|s| s + suffix)
321 .or_else(|| Some(self.snippet(e.span)))
323 None => stmt.rewrite(
325 Shape::indented(self.block_indent, self.config),
333 if let Some(res) = rewrite {
334 let width = self.block_indent.width() + fn_str.len() + res.len() + 4;
335 if !res.contains('\n') && width <= self.config.max_width() {
336 return Some(format!("{}{{ {} }}", fn_str, res));
347 vis: &ast::Visibility,
348 enum_def: &ast::EnumDef,
349 generics: &ast::Generics,
352 let enum_header = format_header("enum ", ident, vis);
353 self.buffer.push_str(&enum_header);
355 let enum_snippet = self.snippet(span);
356 let brace_pos = enum_snippet.find_uncommented("{").unwrap();
357 let body_start = span.lo + BytePos(brace_pos as u32 + 1);
358 let generics_str = format_generics(
363 self.config.item_brace_style(),
364 enum_def.variants.is_empty(),
366 mk_sp(span.lo, body_start),
367 last_line_width(&enum_header),
369 self.buffer.push_str(&generics_str);
371 self.last_pos = body_start;
373 self.block_indent = self.block_indent.block_indent(self.config);
374 let variant_list = self.format_variant_list(enum_def, body_start, span.hi - BytePos(1));
376 Some(ref body_str) => self.buffer.push_str(body_str),
377 None => if contains_comment(&enum_snippet[brace_pos..]) {
378 self.format_missing_no_indent(span.hi - BytePos(1))
380 self.format_missing(span.hi - BytePos(1))
383 self.block_indent = self.block_indent.block_unindent(self.config);
385 if variant_list.is_some() || contains_comment(&enum_snippet[brace_pos..]) {
387 .push_str(&self.block_indent.to_string(self.config));
389 self.buffer.push_str("}");
390 self.last_pos = span.hi;
393 // Format the body of an enum definition
394 fn format_variant_list(
396 enum_def: &ast::EnumDef,
399 ) -> Option<String> {
400 if enum_def.variants.is_empty() {
403 let mut result = String::with_capacity(1024);
405 let indentation = self.block_indent.to_string(self.config);
406 result.push_str(&indentation);
408 let items = itemize_list(
410 enum_def.variants.iter(),
412 |f| if !f.node.attrs.is_empty() {
413 f.node.attrs[0].span.lo
418 |f| self.format_variant(f),
424 let shape = Shape::indented(self.block_indent, self.config)
427 let fmt = ListFormatting {
428 tactic: DefinitiveListTactic::Vertical,
430 trailing_separator: self.config.trailing_comma(),
431 separator_place: SeparatorPlace::Back,
433 ends_with_newline: true,
434 preserve_newline: true,
438 let list = try_opt!(write_list(&items.collect::<Vec<_>>(), &fmt));
439 result.push_str(&list);
444 // Variant of an enum.
445 fn format_variant(&self, field: &ast::Variant) -> Option<String> {
446 if contains_skip(&field.node.attrs) {
447 let lo = field.node.attrs[0].span.lo;
448 let span = mk_sp(lo, field.span.hi);
449 return Some(self.snippet(span));
452 let context = self.get_context();
453 let indent = self.block_indent;
454 let shape = Shape::indented(indent, self.config);
455 let attrs_str = try_opt!(field.node.attrs.rewrite(&context, shape));
460 .map_or(field.span.lo, |attr| attr.span.hi);
461 let span = mk_sp(lo, field.span.lo);
463 let variant_body = match field.node.data {
464 ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => {
465 // FIXME: Should limit the width, as we have a trailing comma
466 try_opt!(format_struct(
470 &ast::Visibility::Inherited,
475 Some(self.config.struct_variant_width()),
478 ast::VariantData::Unit(..) => if let Some(ref expr) = field.node.disr_expr {
480 field.node.name.to_string().len() + self.snippet(expr.span).len() + 3;
481 if one_line_width <= shape.width {
482 format!("{} = {}", field.node.name, self.snippet(expr.span))
489 .block_indent(self.config)
490 .to_string(self.config),
491 self.snippet(expr.span)
495 String::from(field.node.name.to_string())
499 let attrs_extendable = attrs_str.is_empty() ||
500 (context.config.attributes_on_same_line_as_variant() &&
501 is_attributes_extendable(&attrs_str));
502 combine_strs_with_missing_comments(
514 context: &RewriteContext,
517 where_span_end: Option<BytePos>,
518 ) -> Option<String> {
519 if let ast::ItemKind::Impl(_, _, _, ref generics, _, ref self_ty, ref items) = item.node {
520 let mut result = String::new();
521 let ref_and_type = try_opt!(format_impl_ref_and_type(context, item, offset));
522 result.push_str(&ref_and_type);
524 let where_budget = if result.contains('\n') {
525 context.config.max_width()
530 .checked_sub(last_line_width(&result))
533 let option = WhereClauseOption::snuggled(&ref_and_type);
534 let where_clause_str = try_opt!(rewrite_where_clause(
536 &generics.where_clause,
537 context.config.item_brace_style(),
538 Shape::legacy(where_budget, offset.block_only()),
539 context.config.where_density(),
546 if try_opt!(is_impl_single_line(
553 result.push_str(&where_clause_str);
554 if where_clause_str.contains('\n') {
555 let white_space = offset.to_string(context.config);
556 result.push_str(&format!("\n{}{{\n{}}}", &white_space, &white_space));
558 result.push_str(" {}");
563 if !where_clause_str.is_empty() && !where_clause_str.contains('\n') {
565 let width = offset.block_indent + context.config.tab_spaces() - 1;
566 let where_indent = Indent::new(0, width);
567 result.push_str(&where_indent.to_string(context.config));
569 result.push_str(&where_clause_str);
571 match context.config.item_brace_style() {
572 BraceStyle::AlwaysNextLine => {
574 result.push_str(&offset.to_string(context.config));
576 BraceStyle::PreferSameLine => result.push(' '),
577 BraceStyle::SameLineWhere => if !where_clause_str.is_empty() {
579 result.push_str(&offset.to_string(context.config));
587 let snippet = context.snippet(item.span);
588 let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
590 if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
591 let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config);
592 visitor.block_indent = offset.block_only().block_indent(context.config);
593 visitor.last_pos = item.span.lo + BytePos(open_pos as u32);
595 visitor.visit_attrs(&item.attrs, ast::AttrStyle::Inner);
597 visitor.visit_impl_item(item);
600 visitor.format_missing(item.span.hi - BytePos(1));
602 let inner_indent_str = visitor.block_indent.to_string(context.config);
603 let outer_indent_str = offset.block_only().to_string(context.config);
606 result.push_str(&inner_indent_str);
607 result.push_str(trim_newlines(visitor.buffer.to_string().trim()));
609 result.push_str(&outer_indent_str);
612 if result.chars().last().unwrap() == '{' {
614 result.push_str(&offset.to_string(context.config));
624 fn is_impl_single_line(
625 context: &RewriteContext,
628 where_clause_str: &str,
631 let snippet = context.snippet(item.span);
632 let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
635 context.config.impl_empty_single_line() && items.is_empty() &&
636 result.len() + where_clause_str.len() <= context.config.max_width() &&
637 !contains_comment(&snippet[open_pos..]),
641 fn format_impl_ref_and_type(
642 context: &RewriteContext,
645 ) -> Option<String> {
646 if let ast::ItemKind::Impl(
656 let mut result = String::new();
658 result.push_str(&format_visibility(&item.vis));
659 result.push_str(&format_defaultness(defaultness));
660 result.push_str(format_unsafety(unsafety));
661 result.push_str("impl");
663 let lo = context.codemap.span_after(item.span, "impl");
664 let hi = match *trait_ref {
665 Some(ref tr) => tr.path.span.lo,
666 None => self_ty.span.lo,
668 let shape = try_opt!(generics_shape_from_config(
670 Shape::indented(offset + last_line_width(&result), context.config),
673 let one_line_budget = try_opt!(shape.width.checked_sub(last_line_width(&result) + 2));
674 let generics_str = try_opt!(rewrite_generics_inner(
682 let polarity_str = if polarity == ast::ImplPolarity::Negative {
688 if let Some(ref trait_ref) = *trait_ref {
689 let result_len = result.len();
690 if let Some(trait_ref_str) = rewrite_trait_ref(
699 result.push_str(&trait_ref_str);
701 let generics_str = try_opt!(rewrite_generics_inner(
708 result.push_str(&try_opt!(rewrite_trait_ref(
719 result.push_str(&generics_str);
722 // Try to put the self type in a single line.
724 let trait_ref_overhead = if trait_ref.is_some() { 4 } else { 0 };
725 let curly_brace_overhead = if generics.where_clause.predicates.is_empty() {
726 // If there is no where clause adapt budget for type formatting to take space and curly
727 // brace into account.
728 match context.config.item_brace_style() {
729 BraceStyle::AlwaysNextLine => 0,
735 let used_space = last_line_width(&result) + trait_ref_overhead + curly_brace_overhead;
736 // 1 = space before the type.
740 .checked_sub(used_space + 1)
742 if let Some(self_ty_str) = self_ty.rewrite(context, Shape::legacy(budget, offset)) {
743 if !self_ty_str.contains('\n') {
744 if trait_ref.is_some() {
745 result.push_str(" for ");
749 result.push_str(&self_ty_str);
754 // Couldn't fit the self type on a single line, put it on a new line.
756 // Add indentation of one additional tab.
757 let new_line_offset = offset.block_indent(context.config);
758 result.push_str(&new_line_offset.to_string(context.config));
759 if trait_ref.is_some() {
760 result.push_str("for ");
762 let budget = context.config.max_width() - last_line_width(&result);
763 let type_offset = match context.config.where_style() {
764 Style::Legacy => new_line_offset + trait_ref_overhead,
765 Style::Rfc => new_line_offset,
767 result.push_str(&*try_opt!(
768 self_ty.rewrite(context, Shape::legacy(budget, type_offset))
776 fn rewrite_trait_ref(
777 context: &RewriteContext,
778 trait_ref: &ast::TraitRef,
784 ) -> Option<String> {
785 // 1 = space between generics and trait_ref
786 let used_space = 1 + polarity_str.len() + last_line_used_width(generics_str, result_len);
787 let shape = Shape::indented(offset + used_space, context.config);
788 if let Some(trait_ref_str) = trait_ref.rewrite(context, shape) {
789 if !(retry && trait_ref_str.contains('\n')) {
798 // We could not make enough space for trait_ref, so put it on new line.
800 let offset = offset.block_indent(context.config);
801 let shape = Shape::indented(offset, context.config);
802 let trait_ref_str = try_opt!(trait_ref.rewrite(context, shape));
806 &offset.to_string(context.config),
815 pub fn format_struct(
816 context: &RewriteContext,
819 vis: &ast::Visibility,
820 struct_def: &ast::VariantData,
821 generics: Option<&ast::Generics>,
824 one_line_width: Option<usize>,
825 ) -> Option<String> {
827 ast::VariantData::Unit(..) => Some(format_unit_struct(item_name, ident, vis)),
828 ast::VariantData::Tuple(ref fields, _) => format_tuple_struct(
838 ast::VariantData::Struct(ref fields, _) => format_struct_struct(
852 pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> {
853 if let ast::ItemKind::Trait(unsafety, ref generics, ref type_param_bounds, ref trait_items) =
856 let mut result = String::new();
857 let header = format!(
859 format_visibility(&item.vis),
860 format_unsafety(unsafety),
864 result.push_str(&header);
866 let body_lo = context.codemap.span_after(item.span, "{");
868 let shape = Shape::indented(offset + last_line_width(&result), context.config);
869 let generics_str = try_opt!(rewrite_generics(
873 mk_sp(item.span.lo, body_lo),
875 result.push_str(&generics_str);
877 let trait_bound_str = try_opt!(rewrite_trait_bounds(
880 Shape::indented(offset, context.config),
882 // If the trait, generics, and trait bound cannot fit on the same line,
883 // put the trait bounds on an indented new line
884 if offset.width() + last_line_width(&result) + trait_bound_str.len() >
885 context.config.comment_width()
888 let trait_indent = offset.block_only().block_indent(context.config);
889 result.push_str(&trait_indent.to_string(context.config));
891 result.push_str(&trait_bound_str);
893 let has_body = !trait_items.is_empty();
895 let where_density = if (context.config.where_density() == Density::Compressed &&
896 (!result.contains('\n') || context.config.fn_args_layout() == IndentStyle::Block)) ||
897 (context.config.fn_args_layout() == IndentStyle::Block && result.is_empty()) ||
898 (context.config.where_density() == Density::CompressedIfEmpty && !has_body &&
899 !result.contains('\n'))
906 let where_budget = try_opt!(
910 .checked_sub(last_line_width(&result))
912 let pos_before_where = if type_param_bounds.is_empty() {
913 if generics.where_clause.predicates.is_empty() {
914 // We do not use this, so it does not matter
917 let snippet = context.snippet(item.span);
918 let where_pos = snippet.find_uncommented("where");
919 item.span.lo + where_pos.map_or(BytePos(0), |p| BytePos(p as u32))
922 type_param_bounds[type_param_bounds.len() - 1].span().hi
924 let option = WhereClauseOption::snuggled(&generics_str);
925 let where_clause_str = try_opt!(rewrite_where_clause(
927 &generics.where_clause,
928 context.config.item_brace_style(),
929 Shape::legacy(where_budget, offset.block_only()),
936 // If the where clause cannot fit on the same line,
937 // put the where clause on a new line
938 if !where_clause_str.contains('\n') &&
939 last_line_width(&result) + where_clause_str.len() + offset.width() >
940 context.config.comment_width()
943 let width = offset.block_indent + context.config.tab_spaces() - 1;
944 let where_indent = Indent::new(0, width);
945 result.push_str(&where_indent.to_string(context.config));
947 result.push_str(&where_clause_str);
949 match context.config.item_brace_style() {
950 BraceStyle::AlwaysNextLine => {
952 result.push_str(&offset.to_string(context.config));
954 BraceStyle::PreferSameLine => result.push(' '),
955 BraceStyle::SameLineWhere => if !where_clause_str.is_empty() &&
956 (!trait_items.is_empty() || result.contains('\n'))
959 result.push_str(&offset.to_string(context.config));
966 let snippet = context.snippet(item.span);
967 let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
969 if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
970 let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config);
971 visitor.block_indent = offset.block_only().block_indent(context.config);
972 visitor.last_pos = item.span.lo + BytePos(open_pos as u32);
974 for item in trait_items {
975 visitor.visit_trait_item(item);
978 visitor.format_missing(item.span.hi - BytePos(1));
980 let inner_indent_str = visitor.block_indent.to_string(context.config);
981 let outer_indent_str = offset.block_only().to_string(context.config);
984 result.push_str(&inner_indent_str);
985 result.push_str(trim_newlines(visitor.buffer.to_string().trim()));
987 result.push_str(&outer_indent_str);
988 } else if result.contains('\n') {
999 fn format_unit_struct(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
1000 format!("{};", format_header(item_name, ident, vis))
1003 pub fn format_struct_struct(
1004 context: &RewriteContext,
1007 vis: &ast::Visibility,
1008 fields: &[ast::StructField],
1009 generics: Option<&ast::Generics>,
1012 one_line_width: Option<usize>,
1013 ) -> Option<String> {
1014 let mut result = String::with_capacity(1024);
1016 let header_str = format_header(item_name, ident, vis);
1017 result.push_str(&header_str);
1019 let body_lo = context.codemap.span_after(span, "{");
1021 let generics_str = match generics {
1022 Some(g) => try_opt!(format_generics(
1027 context.config.item_brace_style(),
1030 mk_sp(span.lo, body_lo),
1031 last_line_width(&result),
1034 // 3 = ` {}`, 2 = ` {`.
1035 let overhead = if fields.is_empty() { 3 } else { 2 };
1036 if (context.config.item_brace_style() == BraceStyle::AlwaysNextLine &&
1037 !fields.is_empty()) ||
1041 .checked_sub(result.len())
1042 .unwrap_or(0) < overhead
1044 format!("\n{}{{", offset.block_only().to_string(context.config))
1051 let overhead = if fields.is_empty() { 1 } else { 0 };
1052 let max_len = context
1055 .checked_sub(offset.width())
1057 if !generics_str.contains('\n') && result.len() + generics_str.len() + overhead > max_len {
1059 result.push_str(&offset.to_string(context.config));
1060 result.push_str(&generics_str.trim_left());
1062 result.push_str(&generics_str);
1065 if fields.is_empty() {
1066 let snippet = context.snippet(mk_sp(body_lo, span.hi - BytePos(1)));
1067 if snippet.trim().is_empty() {
1069 } else if snippet.trim_right_matches(&[' ', '\t'][..]).ends_with('\n') {
1071 result.push_str(&snippet.trim_right());
1073 result.push_str(&offset.to_string(context.config));
1075 result.push_str(&snippet);
1078 return Some(result);
1082 let one_line_budget = context
1085 .checked_sub(result.len() + 3 + offset.width())
1087 let one_line_budget =
1088 one_line_width.map_or(0, |one_line_width| min(one_line_width, one_line_budget));
1090 let items_str = try_opt!(rewrite_with_alignment(
1093 Shape::indented(offset, context.config),
1094 mk_sp(body_lo, span.hi),
1098 if !items_str.contains('\n') && !result.contains('\n') && items_str.len() <= one_line_budget {
1099 Some(format!("{} {} }}", result, items_str))
1105 .block_indent(context.config)
1106 .to_string(context.config),
1108 offset.to_string(context.config)
1113 fn format_tuple_struct(
1114 context: &RewriteContext,
1117 vis: &ast::Visibility,
1118 fields: &[ast::StructField],
1119 generics: Option<&ast::Generics>,
1122 ) -> Option<String> {
1123 let mut result = String::with_capacity(1024);
1125 let header_str = format_header(item_name, ident, vis);
1126 result.push_str(&header_str);
1128 let body_lo = if fields.is_empty() {
1129 context.codemap.span_after(span, "(")
1133 let body_hi = if fields.is_empty() {
1134 context.codemap.span_after(span, ")")
1136 // This is a dirty hack to work around a missing `)` from the span of the last field.
1137 let last_arg_span = fields[fields.len() - 1].span;
1138 if context.snippet(last_arg_span).ends_with(")") {
1143 .span_after(mk_sp(last_arg_span.hi, span.hi), ")")
1147 let where_clause_str = match generics {
1149 let budget = context.budget(last_line_width(&header_str));
1150 let shape = Shape::legacy(budget, offset);
1151 let g_span = mk_sp(span.lo, body_lo);
1152 let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
1153 result.push_str(&generics_str);
1155 let where_budget = context.budget(last_line_width(&result));
1156 let option = WhereClauseOption::new(true, false);
1157 try_opt!(rewrite_where_clause(
1159 &generics.where_clause,
1160 context.config.item_brace_style(),
1161 Shape::legacy(where_budget, offset.block_only()),
1162 Density::Compressed,
1169 None => "".to_owned(),
1172 if fields.is_empty() {
1174 let used_width = last_line_used_width(&result, offset.width()) + 3;
1175 if used_width > context.config.max_width() {
1177 result.push_str(&offset
1178 .block_indent(context.config)
1179 .to_string(context.config))
1182 let snippet = context.snippet(mk_sp(body_lo, context.codemap.span_before(span, ")")));
1183 if snippet.is_empty() {
1185 } else if snippet.trim_right_matches(&[' ', '\t'][..]).ends_with('\n') {
1186 result.push_str(&snippet.trim_right());
1188 result.push_str(&offset.to_string(context.config));
1190 result.push_str(&snippet);
1195 let body = try_opt!(
1199 &fields.iter().map(|field| field).collect::<Vec<_>>()[..],
1201 Shape::legacy(context.budget(last_line_width(&result) + 3), offset),
1202 context.config.fn_call_width(),
1206 result.push_str(&body);
1209 if !where_clause_str.is_empty() && !where_clause_str.contains('\n') &&
1210 (result.contains('\n') ||
1211 offset.block_indent + result.len() + where_clause_str.len() + 1 >
1212 context.config.max_width())
1214 // We need to put the where clause on a new line, but we didn't
1215 // know that earlier, so the where clause will not be indented properly.
1217 result.push_str(&(offset.block_only() + (context.config.tab_spaces() - 1))
1218 .to_string(context.config));
1220 result.push_str(&where_clause_str);
1225 pub fn rewrite_type_alias(
1226 context: &RewriteContext,
1230 generics: &ast::Generics,
1231 vis: &ast::Visibility,
1233 ) -> Option<String> {
1234 let mut result = String::new();
1236 result.push_str(&format_visibility(vis));
1237 result.push_str("type ");
1238 result.push_str(&ident.to_string());
1241 let shape = try_opt!(Shape::indented(indent + result.len(), context.config).sub_width(2));
1242 let g_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
1243 let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
1244 result.push_str(&generics_str);
1246 let where_budget = try_opt!(
1250 .checked_sub(last_line_width(&result))
1252 let option = WhereClauseOption::snuggled(&result);
1253 let where_clause_str = try_opt!(rewrite_where_clause(
1255 &generics.where_clause,
1256 context.config.item_brace_style(),
1257 Shape::legacy(where_budget, indent),
1258 context.config.where_density(),
1264 result.push_str(&where_clause_str);
1265 if where_clause_str.is_empty() {
1266 result.push_str(" = ");
1268 result.push_str(&format!("\n{}= ", indent.to_string(context.config)));
1271 let line_width = last_line_width(&result);
1272 // This checked_sub may fail as the extra space after '=' is not taken into account
1273 // In that case the budget is set to 0 which will make ty.rewrite retry on a new line
1274 let budget = context
1277 .checked_sub(indent.width() + line_width + ";".len())
1279 let type_indent = indent + line_width;
1280 // Try to fit the type on the same line
1281 let ty_str = try_opt!(
1282 ty.rewrite(context, Shape::legacy(budget, type_indent))
1284 // The line was too short, try to put the type on the next line
1286 // Remove the space after '='
1288 let type_indent = indent.block_indent(context.config);
1290 result.push_str(&type_indent.to_string(context.config));
1291 let budget = try_opt!(
1295 .checked_sub(type_indent.width() + ";".len())
1297 ty.rewrite(context, Shape::legacy(budget, type_indent))
1300 result.push_str(&ty_str);
1301 result.push_str(";");
1305 fn type_annotation_spacing(config: &Config) -> (&str, &str) {
1307 if config.space_before_type_annotation() {
1312 if config.space_after_type_annotation_colon() {
1320 pub fn rewrite_struct_field_prefix(
1321 context: &RewriteContext,
1322 field: &ast::StructField,
1323 ) -> Option<String> {
1324 let vis = format_visibility(&field.vis);
1325 let type_annotation_spacing = type_annotation_spacing(context.config);
1326 Some(match field.ident {
1327 Some(name) => format!("{}{}{}:", vis, name, type_annotation_spacing.0),
1328 None => format!("{}", vis),
1332 fn rewrite_struct_field_type(
1333 context: &RewriteContext,
1334 last_line_width: usize,
1335 field: &ast::StructField,
1338 ) -> Option<String> {
1339 let ty_shape = try_opt!(shape.offset_left(last_line_width + spacing.len()));
1342 .rewrite(context, ty_shape)
1343 .map(|ty| format!("{}{}", spacing, ty))
1346 impl Rewrite for ast::StructField {
1347 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
1348 rewrite_struct_field(context, self, shape, 0)
1352 pub fn rewrite_struct_field(
1353 context: &RewriteContext,
1354 field: &ast::StructField,
1356 lhs_max_width: usize,
1357 ) -> Option<String> {
1358 if contains_skip(&field.attrs) {
1359 let span = context.snippet(mk_sp(field.attrs[0].span.lo, field.span.hi));
1360 return wrap_str(span, context.config.max_width(), shape);
1363 let type_annotation_spacing = type_annotation_spacing(context.config);
1364 let prefix = try_opt!(rewrite_struct_field_prefix(context, field));
1366 let attrs_str = try_opt!(field.attrs.rewrite(context, shape));
1367 let attrs_extendable = attrs_str.is_empty() ||
1368 (context.config.attributes_on_same_line_as_field() && is_attributes_extendable(&attrs_str));
1369 let missing_span = if field.attrs.is_empty() {
1370 mk_sp(field.span.lo, field.span.lo)
1372 mk_sp(field.attrs.last().unwrap().span.hi, field.span.lo)
1374 let mut spacing = String::from(if field.ident.is_some() {
1375 type_annotation_spacing.1
1379 // Try to put everything on a single line.
1380 let attr_prefix = try_opt!(combine_strs_with_missing_comments(
1388 let overhead = last_line_width(&attr_prefix);
1389 let lhs_offset = lhs_max_width.checked_sub(overhead).unwrap_or(0);
1390 for _ in 0..lhs_offset {
1393 // In this extreme case we will be missing a space betweeen an attribute and a field.
1394 if prefix.is_empty() && !attrs_str.is_empty() && attrs_extendable && spacing.is_empty() {
1397 let ty_rewritten = rewrite_struct_field_type(context, overhead, field, &spacing, shape);
1398 if let Some(ref ty) = ty_rewritten {
1399 if !ty.contains('\n') {
1400 return Some(attr_prefix + &ty);
1404 // We must use multiline.
1405 let last_line_width = last_line_width(&prefix);
1406 let ty_rewritten = rewrite_struct_field_type(context, last_line_width, field, &spacing, shape);
1408 let type_offset = shape.indent.block_indent(context.config);
1409 let rewrite_type_in_next_line = || {
1412 .rewrite(context, Shape::indented(type_offset, context.config))
1415 let field_str = match ty_rewritten {
1416 // If we start from the next line and type fits in a single line, then do so.
1417 Some(ref ty) => match rewrite_type_in_next_line() {
1418 Some(ref new_ty) if !new_ty.contains('\n') => format!(
1421 type_offset.to_string(&context.config),
1427 let ty = try_opt!(rewrite_type_in_next_line());
1431 type_offset.to_string(&context.config),
1436 combine_strs_with_missing_comments(
1446 pub fn rewrite_static(
1448 vis: &ast::Visibility,
1451 mutability: ast::Mutability,
1452 expr_opt: Option<&ptr::P<ast::Expr>>,
1455 context: &RewriteContext,
1456 ) -> Option<String> {
1457 let colon = colon_spaces(
1458 context.config.space_before_type_annotation(),
1459 context.config.space_after_type_annotation_colon(),
1461 let prefix = format!(
1463 format_visibility(vis),
1465 format_mutability(mutability),
1470 let ty_str = try_opt!(ty.rewrite(
1473 Shape::indented(offset.block_only(), context.config).offset_left(prefix.len() + 2)
1477 if let Some(expr) = expr_opt {
1478 let lhs = format!("{}{} =", prefix, ty_str);
1480 let remaining_width = context.config.max_width() - offset.block_indent - 1;
1485 Shape::legacy(remaining_width, offset.block_only()),
1487 recover_comment_removed(res, span, context, Shape::indented(offset, context.config))
1489 .map(|s| if s.ends_with(';') { s } else { s + ";" })
1491 Some(format!("{}{};", prefix, ty_str))
1495 pub fn rewrite_associated_type(
1497 ty_opt: Option<&ptr::P<ast::Ty>>,
1498 ty_param_bounds_opt: Option<&ast::TyParamBounds>,
1499 context: &RewriteContext,
1501 ) -> Option<String> {
1502 let prefix = format!("type {}", ident);
1504 let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt {
1506 let shape = try_opt!(Shape::indented(indent, context.config).offset_left(prefix.len() + 2));
1507 let bounds: &[_] = ty_param_bounds;
1508 let bound_str = try_opt!(
1511 .map(|ty_bound| ty_bound.rewrite(context, shape))
1512 .collect::<Option<Vec<_>>>()
1514 if bounds.len() > 0 {
1515 format!(": {}", join_bounds(context, shape, &bound_str))
1523 if let Some(ty) = ty_opt {
1524 let ty_str = try_opt!(ty.rewrite(
1527 context.config.max_width() - indent.block_indent - prefix.len() - 2,
1528 indent.block_only(),
1531 Some(format!("{}{} = {};", prefix, type_bounds_str, ty_str))
1533 Some(format!("{}{};", prefix, type_bounds_str))
1537 pub fn rewrite_associated_impl_type(
1539 defaultness: ast::Defaultness,
1540 ty_opt: Option<&ptr::P<ast::Ty>>,
1541 ty_param_bounds_opt: Option<&ast::TyParamBounds>,
1542 context: &RewriteContext,
1544 ) -> Option<String> {
1545 let result = try_opt!(rewrite_associated_type(
1548 ty_param_bounds_opt,
1554 ast::Defaultness::Default => Some(format!("default {}", result)),
1559 impl Rewrite for ast::FunctionRetTy {
1560 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
1562 ast::FunctionRetTy::Default(_) => Some(String::new()),
1563 ast::FunctionRetTy::Ty(ref ty) => {
1564 let inner_width = try_opt!(shape.width.checked_sub(3));
1565 ty.rewrite(context, Shape::legacy(inner_width, shape.indent + 3))
1566 .map(|r| format!("-> {}", r))
1572 fn is_empty_infer(context: &RewriteContext, ty: &ast::Ty) -> bool {
1574 ast::TyKind::Infer => {
1575 let original = context.snippet(ty.span);
1582 impl Rewrite for ast::Arg {
1583 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
1584 if is_named_arg(self) {
1585 let mut result = try_opt!(
1587 .rewrite(context, Shape::legacy(shape.width, shape.indent))
1590 if !is_empty_infer(context, &*self.ty) {
1591 if context.config.space_before_type_annotation() {
1592 result.push_str(" ");
1594 result.push_str(":");
1595 if context.config.space_after_type_annotation_colon() {
1596 result.push_str(" ");
1598 let max_width = try_opt!(shape.width.checked_sub(result.len()));
1599 let ty_str = try_opt!(self.ty.rewrite(
1601 Shape::legacy(max_width, shape.indent + result.len()),
1603 result.push_str(&ty_str);
1608 self.ty.rewrite(context, shape)
1613 fn rewrite_explicit_self(
1614 explicit_self: &ast::ExplicitSelf,
1616 context: &RewriteContext,
1617 ) -> Option<String> {
1618 match explicit_self.node {
1619 ast::SelfKind::Region(lt, m) => {
1620 let mut_str = format_mutability(m);
1623 let lifetime_str = try_opt!(l.rewrite(
1625 Shape::legacy(context.config.max_width(), Indent::empty()),
1627 Some(format!("&{} {}self", lifetime_str, mut_str))
1629 None => Some(format!("&{}self", mut_str)),
1632 ast::SelfKind::Explicit(ref ty, _) => {
1633 assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
1635 let mutability = explicit_self_mutability(&args[0]);
1636 let type_str = try_opt!(ty.rewrite(
1638 Shape::legacy(context.config.max_width(), Indent::empty()),
1643 format_mutability(mutability),
1647 ast::SelfKind::Value(_) => {
1648 assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
1650 let mutability = explicit_self_mutability(&args[0]);
1652 Some(format!("{}self", format_mutability(mutability)))
1657 // Hacky solution caused by absence of `Mutability` in `SelfValue` and
1658 // `SelfExplicit` variants of `ast::ExplicitSelf_`.
1659 fn explicit_self_mutability(arg: &ast::Arg) -> ast::Mutability {
1660 if let ast::PatKind::Ident(ast::BindingMode::ByValue(mutability), _, _) = arg.pat.node {
1667 pub fn span_lo_for_arg(arg: &ast::Arg) -> BytePos {
1668 if is_named_arg(arg) {
1675 pub fn span_hi_for_arg(context: &RewriteContext, arg: &ast::Arg) -> BytePos {
1677 ast::TyKind::Infer if context.snippet(arg.ty.span) == "_" => arg.ty.span.hi,
1678 ast::TyKind::Infer if is_named_arg(arg) => arg.pat.span.hi,
1679 _ => arg.ty.span.hi,
1683 pub fn is_named_arg(arg: &ast::Arg) -> bool {
1684 if let ast::PatKind::Ident(_, ident, _) = arg.pat.node {
1685 ident.node != symbol::keywords::Invalid.ident()
1691 // Return type is (result, force_new_line_for_brace)
1693 context: &RewriteContext,
1697 generics: &ast::Generics,
1698 unsafety: ast::Unsafety,
1699 constness: ast::Constness,
1700 defaultness: ast::Defaultness,
1702 vis: &ast::Visibility,
1704 newline_brace: bool,
1707 ) -> Option<(String, bool)> {
1708 let mut force_new_line_for_brace = false;
1710 let where_clause = &generics.where_clause;
1712 let mut result = String::with_capacity(1024);
1713 // Vis defaultness constness unsafety abi.
1714 result.push_str(&*format_visibility(vis));
1715 result.push_str(format_defaultness(defaultness));
1716 result.push_str(format_constness(constness));
1717 result.push_str(format_unsafety(unsafety));
1718 if abi != abi::Abi::Rust {
1719 result.push_str(&format_abi(abi, context.config.force_explicit_abi()));
1723 result.push_str("fn ");
1724 result.push_str(&ident.to_string());
1727 let overhead = if has_braces && !newline_brace {
1734 let used_width = last_line_used_width(&result, indent.width());
1735 let one_line_budget = context
1738 .checked_sub(used_width + overhead)
1741 width: one_line_budget,
1745 let g_span = mk_sp(span.lo, fd.output.span().lo);
1746 let generics_str = try_opt!(rewrite_generics(context, generics, shape, g_span));
1747 result.push_str(&generics_str);
1749 let snuggle_angle_bracket = generics_str
1752 .map_or(false, |l| l.trim_left().len() == 1);
1754 // Note that the width and indent don't really matter, we'll re-layout the
1755 // return type later anyway.
1756 let ret_str = try_opt!(
1758 .rewrite(&context, Shape::indented(indent, context.config))
1761 let multi_line_ret_str = ret_str.contains('\n');
1762 let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
1765 let (one_line_budget, multi_line_budget, mut arg_indent) = try_opt!(compute_budgets_for_args(
1776 "rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, arg_indent: {:?}",
1782 // Check if vertical layout was forced.
1783 if one_line_budget == 0 {
1784 if snuggle_angle_bracket {
1787 if context.config.fn_args_paren_newline() {
1789 result.push_str(&arg_indent.to_string(context.config));
1790 if context.config.fn_args_layout() == IndentStyle::Visual {
1791 arg_indent = arg_indent + 1; // extra space for `(`
1795 result.push_str("(");
1796 if context.config.fn_args_layout() == IndentStyle::Visual {
1798 result.push_str(&arg_indent.to_string(context.config));
1805 if context.config.spaces_within_parens() && fd.inputs.len() > 0 && result.ends_with('(') {
1809 // A conservative estimation, to goal is to be over all parens in generics
1810 let args_start = generics
1813 .map_or(span.lo, |tp| end_typaram(tp));
1814 let args_end = if fd.inputs.is_empty() {
1815 context.codemap.span_after(mk_sp(args_start, span.hi), ")")
1817 let last_span = mk_sp(fd.inputs[fd.inputs.len() - 1].span().hi, span.hi);
1818 context.codemap.span_after(last_span, ")")
1820 let args_span = mk_sp(
1821 context.codemap.span_after(mk_sp(args_start, span.hi), "("),
1824 let arg_str = try_opt!(rewrite_args(
1827 fd.get_self().as_ref(),
1834 generics_str.contains('\n'),
1837 let put_args_in_block = match context.config.fn_args_layout() {
1838 IndentStyle::Block => arg_str.contains('\n') || arg_str.len() > one_line_budget,
1840 } && !fd.inputs.is_empty();
1842 let mut args_last_line_contains_comment = false;
1843 if put_args_in_block {
1844 arg_indent = indent.block_indent(context.config);
1846 result.push_str(&arg_indent.to_string(context.config));
1847 result.push_str(&arg_str);
1849 result.push_str(&indent.to_string(context.config));
1852 result.push_str(&arg_str);
1853 let used_width = last_line_used_width(&result, indent.width()) + first_line_width(&ret_str);
1854 // Put the closing brace on the next line if it overflows the max width.
1856 if fd.inputs.len() == 0 && used_width + 1 > context.config.max_width() {
1859 if context.config.spaces_within_parens() && fd.inputs.len() > 0 {
1862 // If the last line of args contains comment, we cannot put the closing paren
1863 // on the same line.
1867 .map_or(false, |last_line| last_line.contains("//"))
1869 args_last_line_contains_comment = true;
1871 result.push_str(&arg_indent.to_string(context.config));
1877 if let ast::FunctionRetTy::Ty(..) = fd.output {
1878 let ret_should_indent = match context.config.fn_args_layout() {
1879 // If our args are block layout then we surely must have space.
1880 IndentStyle::Block if put_args_in_block || fd.inputs.len() == 0 => false,
1881 _ if args_last_line_contains_comment => false,
1882 _ if result.contains('\n') || multi_line_ret_str => true,
1884 // If the return type would push over the max width, then put the return type on
1885 // a new line. With the +1 for the signature length an additional space between
1886 // the closing parenthesis of the argument and the arrow '->' is considered.
1887 let mut sig_length = result.len() + indent.width() + ret_str_len + 1;
1889 // If there is no where clause, take into account the space after the return type
1891 if where_clause.predicates.is_empty() {
1895 sig_length > context.config.max_width()
1898 let ret_indent = if ret_should_indent {
1899 let indent = match context.config.fn_return_indent() {
1900 ReturnIndent::WithWhereClause => indent + 4,
1901 // Aligning with non-existent args looks silly.
1902 _ if arg_str.is_empty() => {
1903 force_new_line_for_brace = true;
1906 // FIXME: we might want to check that using the arg indent
1907 // doesn't blow our budget, and if it does, then fallback to
1908 // the where clause indent.
1913 result.push_str(&indent.to_string(context.config));
1917 Indent::new(indent.block_indent, last_line_width(&result))
1920 if multi_line_ret_str || ret_should_indent {
1921 // Now that we know the proper indent and width, we need to
1922 // re-layout the return type.
1923 let ret_str = try_opt!(
1925 .rewrite(context, Shape::indented(ret_indent, context.config))
1927 result.push_str(&ret_str);
1929 result.push_str(&ret_str);
1932 // Comment between return type and the end of the decl.
1933 let snippet_lo = fd.output.span().hi;
1934 if where_clause.predicates.is_empty() {
1935 let snippet_hi = span.hi;
1936 let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
1937 // Try to preserve the layout of the original snippet.
1938 let original_starts_with_newline = snippet
1940 .map_or(false, |i| snippet[i..].starts_with('\n'));
1941 let original_ends_with_newline = snippet
1942 .rfind(|c| c != ' ')
1943 .map_or(false, |i| snippet[i..].ends_with('\n'));
1944 let snippet = snippet.trim();
1945 if !snippet.is_empty() {
1946 result.push(if original_starts_with_newline {
1951 result.push_str(snippet);
1952 if original_ends_with_newline {
1953 force_new_line_for_brace = true;
1959 let should_compress_where = match context.config.where_density() {
1960 Density::Compressed => !result.contains('\n'),
1961 Density::CompressedIfEmpty => !has_body && !result.contains('\n'),
1965 let pos_before_where = match fd.output {
1966 ast::FunctionRetTy::Default(..) => args_span.hi,
1967 ast::FunctionRetTy::Ty(ref ty) => ty.span.hi,
1970 if where_clause.predicates.len() == 1 && should_compress_where {
1971 let budget = context
1974 .checked_sub(last_line_used_width(&result, indent.width()))
1976 if let Some(where_clause_str) = rewrite_where_clause(
1979 context.config.fn_brace_style(),
1980 Shape::legacy(budget, indent),
1981 Density::Compressed,
1985 WhereClauseOption::compressed(),
1987 result.push_str(&where_clause_str);
1988 force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
1989 return Some((result, force_new_line_for_brace));
1993 let option = WhereClauseOption::new(!has_braces, put_args_in_block && ret_str.is_empty());
1994 let where_clause_str = try_opt!(rewrite_where_clause(
1997 context.config.fn_brace_style(),
1998 Shape::indented(indent, context.config),
2005 // If there are neither where clause nor return type, we may be missing comments between
2007 if where_clause_str.is_empty() {
2008 if let ast::FunctionRetTy::Default(ret_span) = fd.output {
2009 let sp = mk_sp(args_span.hi, ret_span.hi);
2010 let missing_snippet = context.snippet(sp);
2011 let trimmed_snippet = missing_snippet.trim();
2012 let missing_comment = if trimmed_snippet.is_empty() {
2015 try_opt!(rewrite_comment(
2018 Shape::indented(indent, context.config),
2022 if !missing_comment.is_empty() {
2023 let pos = missing_snippet.chars().position(|c| c == '/').unwrap_or(0);
2025 let total_width = missing_comment.len() + last_line_width(&result) + 1;
2026 let force_new_line_before_comment = missing_snippet[..pos].contains('\n') ||
2027 total_width > context.config.max_width();
2028 let sep = if force_new_line_before_comment {
2029 format!("\n{}", indent.to_string(context.config))
2033 result.push_str(&sep);
2034 result.push_str(&missing_comment);
2035 force_new_line_for_brace = true;
2040 result.push_str(&where_clause_str);
2042 force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
2043 return Some((result, force_new_line_for_brace));
2046 struct WhereClauseOption {
2047 suppress_comma: bool, // Force no trailing comma
2048 snuggle: bool, // Do not insert newline before `where`
2049 compress_where: bool, // Try single line where clause instead of vertical layout
2052 impl WhereClauseOption {
2053 pub fn new(suppress_comma: bool, snuggle: bool) -> WhereClauseOption {
2055 suppress_comma: suppress_comma,
2057 compress_where: false,
2061 pub fn compressed() -> WhereClauseOption {
2063 suppress_comma: true,
2065 compress_where: true,
2069 pub fn snuggled(current: &str) -> WhereClauseOption {
2071 suppress_comma: false,
2072 snuggle: trimmed_last_line_width(current) == 1,
2073 compress_where: false,
2079 context: &RewriteContext,
2081 explicit_self: Option<&ast::ExplicitSelf>,
2082 one_line_budget: usize,
2083 multi_line_budget: usize,
2088 generics_str_contains_newline: bool,
2089 ) -> Option<String> {
2090 let mut arg_item_strs = try_opt!(
2093 arg.rewrite(&context, Shape::legacy(multi_line_budget, arg_indent))
2095 .collect::<Option<Vec<_>>>()
2098 // Account for sugary self.
2099 // FIXME: the comment for the self argument is dropped. This is blocked
2100 // on rust issue #27522.
2101 let min_args = explicit_self
2102 .and_then(|explicit_self| {
2103 rewrite_explicit_self(explicit_self, args, context)
2105 .map_or(1, |self_str| {
2106 arg_item_strs[0] = self_str;
2110 // Comments between args.
2111 let mut arg_items = Vec::new();
2113 arg_items.push(ListItem::from_str(""));
2116 // FIXME(#21): if there are no args, there might still be a comment, but
2117 // without spans for the comment or parens, there is no chance of
2118 // getting it right. You also don't get to put a comment on self, unless
2120 if args.len() >= min_args || variadic {
2121 let comment_span_start = if min_args == 2 {
2122 let second_arg_start = if arg_has_pattern(&args[1]) {
2127 let reduced_span = mk_sp(span.lo, second_arg_start);
2129 context.codemap.span_after_last(reduced_span, ",")
2134 enum ArgumentKind<'a> {
2135 Regular(&'a ast::Arg),
2139 let variadic_arg = if variadic {
2140 let variadic_span = mk_sp(args.last().unwrap().ty.span.hi, span.hi);
2141 let variadic_start = context.codemap.span_after(variadic_span, "...") - BytePos(3);
2142 Some(ArgumentKind::Variadic(variadic_start))
2147 let more_items = itemize_list(
2149 args[min_args - 1..]
2151 .map(ArgumentKind::Regular)
2152 .chain(variadic_arg),
2155 ArgumentKind::Regular(arg) => span_lo_for_arg(arg),
2156 ArgumentKind::Variadic(start) => start,
2159 ArgumentKind::Regular(arg) => arg.ty.span.hi,
2160 ArgumentKind::Variadic(start) => start + BytePos(3),
2163 ArgumentKind::Regular(..) => None,
2164 ArgumentKind::Variadic(..) => Some("...".to_owned()),
2171 arg_items.extend(more_items);
2174 let fits_in_one_line = !generics_str_contains_newline &&
2175 (arg_items.len() == 0 || arg_items.len() == 1 && arg_item_strs[0].len() <= one_line_budget);
2177 for (item, arg) in arg_items.iter_mut().zip(arg_item_strs) {
2178 item.item = Some(arg);
2181 let last_line_ends_with_comment = arg_items
2184 .and_then(|item| item.post_comment.as_ref())
2185 .map_or(false, |s| s.trim().starts_with("//"));
2187 let (indent, trailing_comma) = match context.config.fn_args_layout() {
2188 IndentStyle::Block if fits_in_one_line => {
2189 (indent.block_indent(context.config), SeparatorTactic::Never)
2191 IndentStyle::Block => (
2192 indent.block_indent(context.config),
2193 context.config.trailing_comma(),
2195 IndentStyle::Visual if last_line_ends_with_comment => {
2196 (arg_indent, context.config.trailing_comma())
2198 IndentStyle::Visual => (arg_indent, SeparatorTactic::Never),
2201 let tactic = definitive_tactic(
2203 context.config.fn_args_density().to_list_tactic(),
2207 let budget = match tactic {
2208 DefinitiveListTactic::Horizontal => one_line_budget,
2209 _ => multi_line_budget,
2212 debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);
2214 let fmt = ListFormatting {
2217 trailing_separator: if variadic {
2218 SeparatorTactic::Never
2222 separator_place: SeparatorPlace::Back,
2223 shape: Shape::legacy(budget, indent),
2224 ends_with_newline: tactic.ends_with_newline(context.config.fn_args_layout()),
2225 preserve_newline: true,
2226 config: context.config,
2229 write_list(&arg_items, &fmt)
2232 fn arg_has_pattern(arg: &ast::Arg) -> bool {
2233 if let ast::PatKind::Ident(_, ident, _) = arg.pat.node {
2234 ident.node != symbol::keywords::Invalid.ident()
2240 fn compute_budgets_for_args(
2241 context: &RewriteContext,
2245 newline_brace: bool,
2247 force_vertical_layout: bool,
2248 ) -> Option<((usize, usize, Indent))> {
2250 "compute_budgets_for_args {} {:?}, {}, {}",
2256 // Try keeping everything on the same line.
2257 if !result.contains('\n') && !force_vertical_layout {
2258 // 2 = `()`, 3 = `() `, space is before ret_string.
2259 let overhead = if ret_str_len == 0 { 2 } else { 3 };
2260 let mut used_space = indent.width() + result.len() + ret_str_len + overhead;
2270 let one_line_budget = context
2273 .checked_sub(used_space)
2276 if one_line_budget > 0 {
2278 let (indent, multi_line_budget) = match context.config.fn_args_layout() {
2279 IndentStyle::Block => {
2280 let indent = indent.block_indent(context.config);
2282 try_opt!(context.config.max_width().checked_sub(indent.width() + 1));
2285 IndentStyle::Visual => {
2286 let indent = indent + result.len() + 1;
2287 let multi_line_overhead =
2288 indent.width() + result.len() + if newline_brace { 2 } else { 4 };
2290 try_opt!(context.config.max_width().checked_sub(multi_line_overhead));
2295 return Some((one_line_budget, multi_line_budget, indent));
2299 // Didn't work. we must force vertical layout and put args on a newline.
2300 let new_indent = indent.block_indent(context.config);
2301 let used_space = match context.config.fn_args_layout() {
2303 IndentStyle::Block => new_indent.width() + 1,
2304 // Account for `)` and possibly ` {`.
2305 IndentStyle::Visual => new_indent.width() + if ret_str_len == 0 { 1 } else { 3 },
2307 let max_space = try_opt!(context.config.max_width().checked_sub(used_space));
2308 Some((0, max_space, new_indent))
2311 fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause, has_body: bool) -> bool {
2312 match (config.fn_brace_style(), config.where_density()) {
2313 (BraceStyle::AlwaysNextLine, _) => true,
2314 (_, Density::Compressed) if where_clause.predicates.len() == 1 => false,
2315 (_, Density::CompressedIfEmpty) if where_clause.predicates.len() == 1 && !has_body => false,
2316 (BraceStyle::SameLineWhere, _) if !where_clause.predicates.is_empty() => true,
2321 fn rewrite_generics(
2322 context: &RewriteContext,
2323 generics: &ast::Generics,
2326 ) -> Option<String> {
2327 let g_shape = try_opt!(generics_shape_from_config(context.config, shape, 0));
2328 let one_line_width = shape.width.checked_sub(2).unwrap_or(0);
2329 rewrite_generics_inner(context, generics, g_shape, one_line_width, span).or_else(|| {
2330 rewrite_generics_inner(context, generics, g_shape, 0, span)
2334 fn rewrite_generics_inner(
2335 context: &RewriteContext,
2336 generics: &ast::Generics,
2338 one_line_width: usize,
2340 ) -> Option<String> {
2341 // FIXME: convert bounds to where clauses where they get too big or if
2342 // there is a where clause at all.
2343 let lifetimes: &[_] = &generics.lifetimes;
2344 let tys: &[_] = &generics.ty_params;
2345 if lifetimes.is_empty() && tys.is_empty() {
2346 return Some(String::new());
2349 // Strings for the generics.
2350 let lt_strs = lifetimes.iter().map(|lt| lt.rewrite(context, shape));
2351 let ty_strs = tys.iter().map(|ty_param| ty_param.rewrite(context, shape));
2353 // Extract comments between generics.
2354 let lt_spans = lifetimes.iter().map(|l| {
2355 let hi = if l.bounds.is_empty() {
2358 l.bounds[l.bounds.len() - 1].span.hi
2360 mk_sp(l.lifetime.span.lo, hi)
2362 let ty_spans = tys.iter().map(|ty| ty.span());
2364 let items = itemize_list(
2366 lt_spans.chain(ty_spans).zip(lt_strs.chain(ty_strs)),
2370 // FIXME: don't clone
2371 |&(_, ref str)| str.clone(),
2372 context.codemap.span_after(span, "<"),
2376 format_generics_item_list(context, items, shape, one_line_width)
2379 pub fn generics_shape_from_config(config: &Config, shape: Shape, offset: usize) -> Option<Shape> {
2380 match config.generics_indent() {
2381 IndentStyle::Visual => shape.visual_indent(1 + offset).sub_width(offset + 2),
2382 IndentStyle::Block => {
2386 .block_indent(config.tab_spaces())
2387 .with_max_width(config)
2393 pub fn format_generics_item_list<I>(
2394 context: &RewriteContext,
2397 one_line_budget: usize,
2400 I: Iterator<Item = ListItem>,
2402 let item_vec = items.collect::<Vec<_>>();
2404 let tactic = definitive_tactic(
2406 ListTactic::HorizontalVertical,
2410 let fmt = ListFormatting {
2413 trailing_separator: if context.config.generics_indent() == IndentStyle::Visual {
2414 SeparatorTactic::Never
2416 context.config.trailing_comma()
2418 separator_place: SeparatorPlace::Back,
2420 ends_with_newline: tactic.ends_with_newline(context.config.generics_indent()),
2421 preserve_newline: true,
2422 config: context.config,
2425 let list_str = try_opt!(write_list(&item_vec, &fmt));
2427 Some(wrap_generics_with_angle_brackets(
2434 pub fn wrap_generics_with_angle_brackets(
2435 context: &RewriteContext,
2437 list_offset: Indent,
2439 if context.config.generics_indent() == IndentStyle::Block &&
2440 (list_str.contains('\n') || list_str.ends_with(','))
2444 list_offset.to_string(context.config),
2447 .block_unindent(context.config)
2448 .to_string(context.config)
2450 } else if context.config.spaces_within_angle_brackets() {
2451 format!("< {} >", list_str)
2453 format!("<{}>", list_str)
2457 fn rewrite_trait_bounds(
2458 context: &RewriteContext,
2459 type_param_bounds: &ast::TyParamBounds,
2461 ) -> Option<String> {
2462 let bounds: &[_] = type_param_bounds;
2464 if bounds.is_empty() {
2465 return Some(String::new());
2467 let bound_str = try_opt!(
2470 .map(|ty_bound| ty_bound.rewrite(&context, shape))
2471 .collect::<Option<Vec<_>>>()
2473 Some(format!(": {}", join_bounds(context, shape, &bound_str)))
2476 fn rewrite_where_clause_rfc_style(
2477 context: &RewriteContext,
2478 where_clause: &ast::WhereClause,
2481 span_end: Option<BytePos>,
2482 span_end_before_where: BytePos,
2483 where_clause_option: WhereClauseOption,
2484 ) -> Option<String> {
2485 let block_shape = shape.block().with_max_width(context.config);
2487 let (span_before, span_after) =
2488 missing_span_before_after_where(span_end_before_where, where_clause);
2489 let (comment_before, comment_after) = try_opt!(rewrite_comments_before_after_where(
2496 let starting_newline = if where_clause_option.snuggle && comment_before.is_empty() {
2499 "\n".to_owned() + &block_shape.indent.to_string(context.config)
2502 let clause_shape = block_shape.block_indent(context.config.tab_spaces());
2503 // each clause on one line, trailing comma (except if suppress_comma)
2504 let span_start = where_clause.predicates[0].span().lo;
2505 // If we don't have the start of the next span, then use the end of the
2506 // predicates, but that means we miss comments.
2507 let len = where_clause.predicates.len();
2508 let end_of_preds = where_clause.predicates[len - 1].span().hi;
2509 let span_end = span_end.unwrap_or(end_of_preds);
2510 let items = itemize_list(
2512 where_clause.predicates.iter(),
2514 |pred| pred.span().lo,
2515 |pred| pred.span().hi,
2516 |pred| pred.rewrite(context, block_shape),
2521 let comma_tactic = if where_clause_option.suppress_comma {
2522 SeparatorTactic::Never
2524 context.config.trailing_comma()
2527 let fmt = ListFormatting {
2528 tactic: DefinitiveListTactic::Vertical,
2530 trailing_separator: comma_tactic,
2531 separator_place: SeparatorPlace::Back,
2532 shape: clause_shape,
2533 ends_with_newline: true,
2534 preserve_newline: true,
2535 config: context.config,
2537 let preds_str = try_opt!(write_list(&items.collect::<Vec<_>>(), &fmt));
2539 let comment_separator = |comment: &str, shape: Shape| if comment.is_empty() {
2542 format!("\n{}", shape.indent.to_string(context.config))
2544 let newline_before_where = comment_separator(&comment_before, shape);
2545 let newline_after_where = comment_separator(&comment_after, clause_shape);
2548 let clause_sep = if where_clause_option.compress_where && comment_before.is_empty() &&
2549 comment_after.is_empty() && !preds_str.contains('\n') &&
2550 6 + preds_str.len() <= shape.width
2554 format!("\n{}", clause_shape.indent.to_string(context.config))
2557 "{}{}{}where{}{}{}{}",
2560 newline_before_where,
2561 newline_after_where,
2568 fn rewrite_where_clause(
2569 context: &RewriteContext,
2570 where_clause: &ast::WhereClause,
2571 brace_style: BraceStyle,
2575 span_end: Option<BytePos>,
2576 span_end_before_where: BytePos,
2577 where_clause_option: WhereClauseOption,
2578 ) -> Option<String> {
2579 if where_clause.predicates.is_empty() {
2580 return Some(String::new());
2583 if context.config.where_style() == Style::Rfc {
2584 return rewrite_where_clause_rfc_style(
2590 span_end_before_where,
2591 where_clause_option,
2595 let extra_indent = Indent::new(context.config.tab_spaces(), 0);
2597 let offset = match context.config.where_pred_indent() {
2598 IndentStyle::Block => shape.indent + extra_indent.block_indent(context.config),
2599 // 6 = "where ".len()
2600 IndentStyle::Visual => shape.indent + extra_indent + 6,
2602 // FIXME: if where_pred_indent != Visual, then the budgets below might
2603 // be out by a char or two.
2605 let budget = context.config.max_width() - offset.width();
2606 let span_start = where_clause.predicates[0].span().lo;
2607 // If we don't have the start of the next span, then use the end of the
2608 // predicates, but that means we miss comments.
2609 let len = where_clause.predicates.len();
2610 let end_of_preds = where_clause.predicates[len - 1].span().hi;
2611 let span_end = span_end.unwrap_or(end_of_preds);
2612 let items = itemize_list(
2614 where_clause.predicates.iter(),
2616 |pred| pred.span().lo,
2617 |pred| pred.span().hi,
2618 |pred| pred.rewrite(context, Shape::legacy(budget, offset)),
2623 let item_vec = items.collect::<Vec<_>>();
2624 // FIXME: we don't need to collect here if the where_layout isn't
2625 // HorizontalVertical.
2626 let tactic = definitive_tactic(
2628 context.config.where_layout(),
2633 let mut comma_tactic = context.config.trailing_comma();
2634 // Kind of a hack because we don't usually have trailing commas in where clauses.
2635 if comma_tactic == SeparatorTactic::Vertical || where_clause_option.suppress_comma {
2636 comma_tactic = SeparatorTactic::Never;
2639 let fmt = ListFormatting {
2642 trailing_separator: comma_tactic,
2643 separator_place: SeparatorPlace::Back,
2644 shape: Shape::legacy(budget, offset),
2645 ends_with_newline: tactic.ends_with_newline(context.config.where_pred_indent()),
2646 preserve_newline: true,
2647 config: context.config,
2649 let preds_str = try_opt!(write_list(&item_vec, &fmt));
2651 let end_length = if terminator == "{" {
2652 // If the brace is on the next line we don't need to count it otherwise it needs two
2655 BraceStyle::AlwaysNextLine | BraceStyle::SameLineWhere => 0,
2656 BraceStyle::PreferSameLine => 2,
2658 } else if terminator == "=" {
2663 if density == Density::Tall || preds_str.contains('\n') ||
2664 shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
2668 (shape.indent + extra_indent).to_string(context.config),
2672 Some(format!(" where {}", preds_str))
2676 fn missing_span_before_after_where(
2677 before_item_span_end: BytePos,
2678 where_clause: &ast::WhereClause,
2680 let missing_span_before = mk_sp(before_item_span_end, where_clause.span.lo);
2682 let pos_after_where = where_clause.span.lo + BytePos(5);
2683 let missing_span_after = mk_sp(pos_after_where, where_clause.predicates[0].span().lo);
2684 (missing_span_before, missing_span_after)
2687 fn rewrite_missing_comment_in_where(
2688 context: &RewriteContext,
2691 ) -> Option<String> {
2692 let comment = comment.trim();
2693 if comment.is_empty() {
2696 rewrite_comment(comment, false, shape, context.config)
2700 fn rewrite_comments_before_after_where(
2701 context: &RewriteContext,
2702 span_before_where: Span,
2703 span_after_where: Span,
2705 ) -> Option<(String, String)> {
2706 let before_comment = try_opt!(rewrite_missing_comment_in_where(
2708 &context.snippet(span_before_where),
2711 let after_comment = try_opt!(rewrite_missing_comment_in_where(
2713 &context.snippet(span_after_where),
2714 shape.block_indent(context.config.tab_spaces()),
2716 Some((before_comment, after_comment))
2719 fn format_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
2720 format!("{}{}{}", format_visibility(vis), item_name, ident)
2724 context: &RewriteContext,
2725 generics: &ast::Generics,
2728 brace_style: BraceStyle,
2729 force_same_line_brace: bool,
2733 ) -> Option<String> {
2734 let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
2735 let mut result = try_opt!(rewrite_generics(context, generics, shape, span));
2737 let same_line_brace = if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
2738 let budget = context
2741 .checked_sub(last_line_used_width(&result, offset.width()))
2743 let option = WhereClauseOption::snuggled(&result);
2744 let where_clause_str = try_opt!(rewrite_where_clause(
2746 &generics.where_clause,
2748 Shape::legacy(budget, offset.block_only()),
2755 result.push_str(&where_clause_str);
2756 force_same_line_brace || brace_style == BraceStyle::PreferSameLine ||
2757 (generics.where_clause.predicates.is_empty() && trimmed_last_line_width(&result) == 1)
2759 force_same_line_brace || trimmed_last_line_width(&result) == 1 ||
2760 brace_style != BraceStyle::AlwaysNextLine
2762 let total_used_width = last_line_used_width(&result, used_width);
2763 let remaining_budget = context
2766 .checked_sub(total_used_width)
2768 // If the same line brace if forced, it indicates that we are rewriting an item with empty body,
2769 // and hence we take the closer into account as well for one line budget.
2770 // We assume that the closer has the same length as the opener.
2771 let overhead = if force_same_line_brace {
2772 1 + opener.len() + opener.len()
2776 let forbid_same_line_brace = overhead > remaining_budget;
2777 if !forbid_same_line_brace && same_line_brace {
2781 result.push_str(&offset.block_only().to_string(context.config));
2783 result.push_str(opener);
2788 impl Rewrite for ast::ForeignItem {
2789 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
2790 let attrs_str = try_opt!(self.attrs.rewrite(context, shape));
2791 // Drop semicolon or it will be interpreted as comment.
2792 // FIXME: this may be a faulty span from libsyntax.
2793 let span = mk_sp(self.span.lo, self.span.hi - BytePos(1));
2795 let item_str = try_opt!(match self.node {
2796 ast::ForeignItemKind::Fn(ref fn_decl, ref generics) => {
2803 ast::Unsafety::Normal,
2804 ast::Constness::NotConst,
2805 ast::Defaultness::Final,
2806 // These are not actually rust functions,
2807 // but we format them as such.
2814 ).map(|(s, _)| format!("{};", s))
2816 ast::ForeignItemKind::Static(ref ty, is_mutable) => {
2817 // FIXME(#21): we're dropping potential comments in between the
2818 // function keywords here.
2819 let vis = format_visibility(&self.vis);
2820 let mut_str = if is_mutable { "mut " } else { "" };
2821 let prefix = format!("{}static {}{}:", vis, mut_str, self.ident);
2823 let shape = try_opt!(shape.sub_width(1));
2824 ty.rewrite(context, shape).map(|ty_str| {
2825 // 1 = space between prefix and type.
2826 let sep = if prefix.len() + ty_str.len() + 1 <= shape.width {
2829 let nested_indent = shape.indent.block_indent(context.config);
2830 format!("\n{}", nested_indent.to_string(context.config))
2832 format!("{}{}{};", prefix, sep, ty_str)
2837 let missing_span = if self.attrs.is_empty() {
2838 mk_sp(self.span.lo, self.span.lo)
2840 mk_sp(self.attrs[self.attrs.len() - 1].span.hi, self.span.lo)
2842 combine_strs_with_missing_comments(