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.
14 use std::cmp::{min, Ordering};
18 use rustc_target::spec::abi;
19 use syntax::codemap::{self, BytePos, Span};
21 use syntax::{ast, ptr, symbol};
23 use codemap::{LineRangeUtils, SpanUtils};
24 use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
25 recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented};
26 use config::{BraceStyle, Config, Density, IndentStyle};
27 use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
28 rewrite_assign_rhs_with, ExprType, RhsTactics};
29 use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
30 use macros::{rewrite_macro, MacroPosition};
32 use rewrite::{Rewrite, RewriteContext};
33 use shape::{Indent, Shape};
35 use types::TraitTyParamBounds;
37 use vertical::rewrite_with_alignment;
38 use visitor::FmtVisitor;
40 const DEFAULT_VISIBILITY: ast::Visibility = codemap::Spanned {
41 node: ast::VisibilityKind::Inherited,
42 span: codemap::DUMMY_SP,
45 fn type_annotation_separator(config: &Config) -> &str {
46 colon_spaces(config.space_before_colon(), config.space_after_colon())
49 // Statements of the form
50 // let pat: ty = init;
51 impl Rewrite for ast::Local {
52 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
54 "Local::rewrite {:?} {} {:?}",
55 self, shape.width, shape.indent
58 skip_out_of_file_lines_range!(context, self.span);
60 if contains_skip(&self.attrs) {
64 let attrs_str = self.attrs.rewrite(context, shape)?;
65 let mut result = if attrs_str.is_empty() {
68 combine_strs_with_missing_comments(
73 self.attrs.last().map(|a| a.span.hi()).unwrap(),
82 let pat_shape = shape.offset_left(4)?;
84 let pat_shape = pat_shape.sub_width(1)?;
85 let pat_str = self.pat.rewrite(context, pat_shape)?;
86 result.push_str(&pat_str);
88 // String that is placed within the assignment pattern and expression.
90 let mut infix = String::with_capacity(32);
92 if let Some(ref ty) = self.ty {
93 let separator = type_annotation_separator(context.config);
94 let indent = shape.indent + last_line_width(&result) + separator.len();
96 let budget = shape.width.checked_sub(indent.width() + 1)?;
97 let rewrite = ty.rewrite(context, Shape::legacy(budget, indent))?;
99 infix.push_str(separator);
100 infix.push_str(&rewrite);
103 if self.init.is_some() {
104 infix.push_str(" =");
110 result.push_str(&infix);
112 if let Some(ref ex) = self.init {
113 // 1 = trailing semicolon;
114 let nested_shape = shape.sub_width(1)?;
116 result = rewrite_assign_rhs(context, result, &**ex, nested_shape)?;
124 // TODO convert to using rewrite style rather than visitor
125 // TODO format modules in this style
128 keyword: &'static str,
129 abi: Cow<'static, str>,
130 vis: Option<&'a ast::Visibility>,
131 body: Vec<BodyElement<'a>>,
136 fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> {
139 abi: format_abi(fm.abi, config.force_explicit_abi(), true),
143 .map(|i| BodyElement::ForeignItem(i))
150 enum BodyElement<'a> {
151 // Stmt(&'a ast::Stmt),
152 // Field(&'a ast::Field),
153 // Variant(&'a ast::Variant),
154 // Item(&'a ast::Item),
155 ForeignItem(&'a ast::ForeignItem),
158 /// Represents a fn's signature.
159 pub struct FnSig<'a> {
160 decl: &'a ast::FnDecl,
161 generics: &'a ast::Generics,
163 constness: ast::Constness,
164 defaultness: ast::Defaultness,
165 unsafety: ast::Unsafety,
166 visibility: ast::Visibility,
171 decl: &'a ast::FnDecl,
172 generics: &'a ast::Generics,
173 vis: ast::Visibility,
179 constness: ast::Constness::NotConst,
180 defaultness: ast::Defaultness::Final,
181 unsafety: ast::Unsafety::Normal,
186 pub fn from_method_sig(
187 method_sig: &'a ast::MethodSig,
188 generics: &'a ast::Generics,
191 unsafety: method_sig.unsafety,
192 constness: method_sig.constness.node,
193 defaultness: ast::Defaultness::Final,
195 decl: &*method_sig.decl,
197 visibility: DEFAULT_VISIBILITY,
202 fn_kind: &'a visit::FnKind,
203 generics: &'a ast::Generics,
204 decl: &'a ast::FnDecl,
205 defaultness: ast::Defaultness,
208 visit::FnKind::ItemFn(_, unsafety, constness, abi, visibility, _) => FnSig {
212 constness: constness.node,
215 visibility: visibility.clone(),
217 visit::FnKind::Method(_, method_sig, vis, _) => {
218 let mut fn_sig = FnSig::from_method_sig(method_sig, generics);
219 fn_sig.defaultness = defaultness;
220 if let Some(vis) = vis {
221 fn_sig.visibility = vis.clone();
229 fn to_str(&self, context: &RewriteContext) -> String {
230 let mut result = String::with_capacity(128);
231 // Vis defaultness constness unsafety abi.
232 result.push_str(&*format_visibility(&self.visibility));
233 result.push_str(format_defaultness(self.defaultness));
234 result.push_str(format_constness(self.constness));
235 result.push_str(format_unsafety(self.unsafety));
236 result.push_str(&format_abi(
238 context.config.force_explicit_abi(),
245 impl<'a> FmtVisitor<'a> {
246 fn format_item(&mut self, item: &Item) {
247 self.buffer.push_str(&item.abi);
249 let snippet = self.snippet(item.span);
250 let brace_pos = snippet.find_uncommented("{").unwrap();
253 if !item.body.is_empty() || contains_comment(&snippet[brace_pos..]) {
254 // FIXME: this skips comments between the extern keyword and the opening
256 self.last_pos = item.span.lo() + BytePos(brace_pos as u32 + 1);
257 self.block_indent = self.block_indent.block_indent(self.config);
259 if item.body.is_empty() {
260 self.format_missing_no_indent(item.span.hi() - BytePos(1));
261 self.block_indent = self.block_indent.block_unindent(self.config);
262 let indent_str = self.block_indent.to_string(self.config);
263 self.push_str(&indent_str);
265 for item in &item.body {
266 self.format_body_element(item);
269 self.block_indent = self.block_indent.block_unindent(self.config);
270 self.format_missing_with_indent(item.span.hi() - BytePos(1));
275 self.last_pos = item.span.hi();
278 fn format_body_element(&mut self, element: &BodyElement) {
280 BodyElement::ForeignItem(item) => self.format_foreign_item(item),
284 pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
285 let item = Item::from_foreign_mod(fm, span, self.config);
286 self.format_item(&item);
289 fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
290 let rewrite = item.rewrite(&self.get_context(), self.shape());
291 self.push_rewrite(item.span(), rewrite);
292 self.last_pos = item.span.hi();
302 inner_attrs: Option<&[ast::Attribute]>,
303 ) -> Option<String> {
304 let context = self.get_context();
306 let mut newline_brace = newline_for_brace(self.config, &fn_sig.generics.where_clause);
308 let (mut result, force_newline_brace) =
309 rewrite_fn_base(&context, indent, ident, fn_sig, span, newline_brace, true)?;
312 if self.config.brace_style() == BraceStyle::AlwaysNextLine || force_newline_brace
313 || last_line_width(&result) + 2 > self.shape().width
315 newline_brace = true;
316 } else if !result.contains('\n') {
317 newline_brace = false;
320 // Prepare for the function body by possibly adding a newline and
322 // FIXME we'll miss anything between the end of the signature and the
323 // start of the body, but we need more spans from the compiler to solve
326 result.push_str(&indent.to_string_with_newline(self.config));
331 self.single_line_fn(&result, block, inner_attrs)
332 .or_else(|| Some(result))
335 pub fn rewrite_required_fn(
339 sig: &ast::MethodSig,
340 generics: &ast::Generics,
342 ) -> Option<String> {
343 // Drop semicolon or it will be interpreted as comment.
344 let span = mk_sp(span.lo(), span.hi() - BytePos(1));
345 let context = self.get_context();
347 let (mut result, _) = rewrite_fn_base(
351 &FnSig::from_method_sig(sig, generics),
357 // Re-attach semicolon
367 inner_attrs: Option<&[ast::Attribute]>,
368 ) -> Option<String> {
369 if fn_str.contains('\n') || inner_attrs.map_or(false, |a| !a.is_empty()) {
373 let codemap = self.get_context().codemap;
375 if self.config.empty_item_single_line() && is_empty_block(block, None, codemap)
376 && self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width()
378 return Some(format!("{}{{}}", fn_str));
381 if self.config.fn_single_line() && is_simple_block_stmt(block, None, codemap) {
383 if let Some(stmt) = block.stmts.first() {
384 match stmt_expr(stmt) {
386 let suffix = if semicolon_for_expr(&self.get_context(), e) {
392 format_expr(e, ExprType::Statement, &self.get_context(), self.shape())
394 .or_else(|| Some(self.snippet(e.span).to_owned()))
396 None => stmt.rewrite(&self.get_context(), self.shape()),
403 if let Some(res) = rewrite {
404 let width = self.block_indent.width() + fn_str.len() + res.len() + 4;
405 if !res.contains('\n') && width <= self.config.max_width() {
406 return Some(format!("{}{{ {} }}", fn_str, res));
414 pub fn visit_static(&mut self, static_parts: &StaticParts) {
415 let rewrite = rewrite_static(&self.get_context(), static_parts, self.block_indent);
416 self.push_rewrite(static_parts.span, rewrite);
419 pub fn visit_struct(&mut self, struct_parts: &StructParts) {
420 let is_tuple = struct_parts.def.is_tuple();
421 let rewrite = format_struct(&self.get_context(), struct_parts, self.block_indent, None)
422 .map(|s| if is_tuple { s + ";" } else { s });
423 self.push_rewrite(struct_parts.span, rewrite);
429 vis: &ast::Visibility,
430 enum_def: &ast::EnumDef,
431 generics: &ast::Generics,
434 let enum_header = format_header("enum ", ident, vis);
435 self.push_str(&enum_header);
437 let enum_snippet = self.snippet(span);
438 let brace_pos = enum_snippet.find_uncommented("{").unwrap();
439 let body_start = span.lo() + BytePos(brace_pos as u32 + 1);
440 let generics_str = format_generics(
443 self.config.brace_style(),
444 if enum_def.variants.is_empty() {
445 BracePos::ForceSameLine
450 mk_sp(span.lo(), body_start),
451 last_line_width(&enum_header),
453 self.push_str(&generics_str);
455 self.last_pos = body_start;
457 match self.format_variant_list(enum_def, body_start, span.hi()) {
458 Some(ref s) if enum_def.variants.is_empty() => self.push_str(s),
460 self.push_rewrite(mk_sp(body_start, span.hi()), rw);
461 self.block_indent = self.block_indent.block_unindent(self.config);
466 // Format the body of an enum definition
467 fn format_variant_list(
469 enum_def: &ast::EnumDef,
472 ) -> Option<String> {
473 if enum_def.variants.is_empty() {
474 let mut buffer = String::with_capacity(128);
476 let span = mk_sp(body_lo, body_hi - BytePos(1));
477 format_empty_struct_or_tuple(
487 let mut result = String::with_capacity(1024);
488 let original_offset = self.block_indent;
489 self.block_indent = self.block_indent.block_indent(self.config);
491 let itemize_list_with = |one_line_width: usize| {
493 self.snippet_provider,
494 enum_def.variants.iter(),
498 if !f.node.attrs.is_empty() {
499 f.node.attrs[0].span.lo()
505 |f| self.format_variant(f, one_line_width),
511 let mut items: Vec<_> =
512 itemize_list_with(self.config.width_heuristics().struct_variant_width);
513 // If one of the variants use multiple lines, use multi-lined formatting for all variants.
514 let has_multiline_variant = items.iter().any(|item| item.inner_as_ref().contains('\n'));
515 let has_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains('\n'));
516 if has_multiline_variant && has_single_line_variant {
517 items = itemize_list_with(0);
520 let shape = self.shape().sub_width(2)?;
521 let fmt = ListFormatting {
522 tactic: DefinitiveListTactic::Vertical,
524 trailing_separator: self.config.trailing_comma(),
525 separator_place: SeparatorPlace::Back,
527 ends_with_newline: true,
528 preserve_newline: true,
532 let list = write_list(&items, &fmt)?;
533 result.push_str(&list);
534 result.push_str(&original_offset.to_string_with_newline(self.config));
539 // Variant of an enum.
540 fn format_variant(&self, field: &ast::Variant, one_line_width: usize) -> Option<String> {
541 if contains_skip(&field.node.attrs) {
542 let lo = field.node.attrs[0].span.lo();
543 let span = mk_sp(lo, field.span.hi());
544 return Some(self.snippet(span).to_owned());
547 let context = self.get_context();
549 let shape = self.shape().sub_width(1)?;
550 let attrs_str = field.node.attrs.rewrite(&context, shape)?;
555 .map_or(field.span.lo(), |attr| attr.span.hi());
556 let span = mk_sp(lo, field.span.lo());
558 let variant_body = match field.node.data {
559 ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => format_struct(
561 &StructParts::from_variant(field),
563 Some(one_line_width),
565 ast::VariantData::Unit(..) => {
566 if let Some(ref expr) = field.node.disr_expr {
567 let lhs = format!("{} =", field.node.ident.name);
568 rewrite_assign_rhs(&context, lhs, &**expr, shape)?
570 field.node.ident.name.to_string()
575 combine_strs_with_missing_comments(&context, &attrs_str, &variant_body, span, shape, false)
580 context: &RewriteContext,
583 where_span_end: Option<BytePos>,
584 ) -> Option<String> {
585 if let ast::ItemKind::Impl(_, _, _, ref generics, _, ref self_ty, ref items) = item.node {
586 let mut result = String::with_capacity(128);
587 let ref_and_type = format_impl_ref_and_type(context, item, offset)?;
588 let sep = offset.to_string_with_newline(context.config);
589 result.push_str(&ref_and_type);
591 let where_budget = if result.contains('\n') {
592 context.config.max_width()
594 context.budget(last_line_width(&result))
596 let option = WhereClauseOption::snuggled(&ref_and_type);
597 let where_clause_str = rewrite_where_clause(
599 &generics.where_clause,
600 context.config.brace_style(),
601 Shape::legacy(where_budget, offset.block_only()),
610 // If there is no where clause, we may have missing comments between the trait name and
611 // the opening brace.
612 if generics.where_clause.predicates.is_empty() {
613 if let Some(hi) = where_span_end {
614 match recover_missing_comment_in_span(
615 mk_sp(self_ty.span.hi(), hi),
616 Shape::indented(offset, context.config),
618 last_line_width(&result),
620 Some(ref missing_comment) if !missing_comment.is_empty() => {
621 result.push_str(missing_comment);
628 if is_impl_single_line(context, items, &result, &where_clause_str, item)? {
629 result.push_str(&where_clause_str);
630 if where_clause_str.contains('\n') || last_line_contains_single_line_comment(&result) {
631 result.push_str(&format!("{}{{{}}}", &sep, &sep));
633 result.push_str(" {}");
638 if !where_clause_str.is_empty() && !where_clause_str.contains('\n') {
639 let width = offset.block_indent + context.config.tab_spaces() - 1;
640 let where_indent = Indent::new(0, width);
641 result.push_str(&where_indent.to_string_with_newline(context.config));
643 result.push_str(&where_clause_str);
645 let need_newline = last_line_contains_single_line_comment(&result) || result.contains('\n');
646 match context.config.brace_style() {
647 _ if need_newline => result.push_str(&sep),
648 BraceStyle::AlwaysNextLine => result.push_str(&sep),
649 BraceStyle::PreferSameLine => result.push(' '),
650 BraceStyle::SameLineWhere => {
651 if !where_clause_str.is_empty() {
652 result.push_str(&sep);
661 let snippet = context.snippet(item.span);
662 let open_pos = snippet.find_uncommented("{")? + 1;
664 if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
665 let mut visitor = FmtVisitor::from_context(context);
666 let item_indent = offset.block_only().block_indent(context.config);
667 visitor.block_indent = item_indent;
668 visitor.last_pos = item.span.lo() + BytePos(open_pos as u32);
670 visitor.visit_attrs(&item.attrs, ast::AttrStyle::Inner);
671 if context.config.reorder_impl_items() {
672 // Create visitor for each items, then reorder them.
673 let mut buffer = vec![];
675 visitor.visit_impl_item(item);
676 buffer.push((visitor.buffer.clone(), item.clone()));
677 visitor.buffer.clear();
679 // type -> const -> macro -> method
680 use ast::ImplItemKind::*;
681 fn need_empty_line(a: &ast::ImplItemKind, b: &ast::ImplItemKind) -> bool {
683 (Type(..), Type(..)) | (Const(..), Const(..)) => false,
688 buffer.sort_by(|(_, a), (_, b)| match (&a.node, &b.node) {
689 (Type(..), _) => Ordering::Less,
690 (_, Type(..)) => Ordering::Greater,
691 (Const(..), _) => Ordering::Less,
692 (_, Const(..)) => Ordering::Greater,
693 (Macro(..), _) => Ordering::Less,
694 (_, Macro(..)) => Ordering::Greater,
697 let mut prev_kind = None;
698 for (buf, item) in buffer {
699 // Make sure that there are at least a single empty line between
700 // different impl items.
703 .map_or(false, |prev_kind| need_empty_line(prev_kind, &item.node))
705 visitor.push_str("\n");
707 visitor.push_str(&item_indent.to_string_with_newline(context.config));
708 visitor.push_str(buf.trim());
709 prev_kind = Some(item.node.clone());
713 visitor.visit_impl_item(item);
717 visitor.format_missing(item.span.hi() - BytePos(1));
719 let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
720 let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
722 result.push_str(&inner_indent_str);
723 result.push_str(visitor.buffer.to_string().trim());
724 result.push_str(&outer_indent_str);
727 if result.ends_with('{') {
728 result.push_str(&sep);
738 fn is_impl_single_line(
739 context: &RewriteContext,
740 items: &[ast::ImplItem],
742 where_clause_str: &str,
745 let snippet = context.snippet(item.span);
746 let open_pos = snippet.find_uncommented("{")? + 1;
749 context.config.empty_item_single_line() && items.is_empty() && !result.contains('\n')
750 && result.len() + where_clause_str.len() <= context.config.max_width()
751 && !contains_comment(&snippet[open_pos..]),
755 fn format_impl_ref_and_type(
756 context: &RewriteContext,
759 ) -> Option<String> {
760 if let ast::ItemKind::Impl(
770 let mut result = String::with_capacity(128);
772 result.push_str(&format_visibility(&item.vis));
773 result.push_str(format_defaultness(defaultness));
774 result.push_str(format_unsafety(unsafety));
776 let lo = context.snippet_provider.span_after(item.span, "impl");
777 let hi = match *trait_ref {
778 Some(ref tr) => tr.path.span.lo(),
779 None => self_ty.span.lo(),
781 let shape = generics_shape_from_config(
783 Shape::indented(offset + last_line_width(&result), context.config),
786 let generics_str = rewrite_generics(context, "impl", generics, shape, mk_sp(lo, hi))?;
787 result.push_str(&generics_str);
789 let polarity_str = if polarity == ast::ImplPolarity::Negative {
795 if let Some(ref trait_ref) = *trait_ref {
796 let result_len = last_line_width(&result);
797 result.push_str(&rewrite_trait_ref(
806 // Try to put the self type in a single line.
808 let trait_ref_overhead = if trait_ref.is_some() { 4 } else { 0 };
809 let curly_brace_overhead = if generics.where_clause.predicates.is_empty() {
810 // If there is no where clause adapt budget for type formatting to take space and curly
811 // brace into account.
812 match context.config.brace_style() {
813 BraceStyle::AlwaysNextLine => 0,
819 let used_space = last_line_width(&result) + trait_ref_overhead + curly_brace_overhead;
820 // 1 = space before the type.
821 let budget = context.budget(used_space + 1);
822 if let Some(self_ty_str) = self_ty.rewrite(context, Shape::legacy(budget, offset)) {
823 if !self_ty_str.contains('\n') {
824 if trait_ref.is_some() {
825 result.push_str(" for ");
829 result.push_str(&self_ty_str);
834 // Couldn't fit the self type on a single line, put it on a new line.
836 // Add indentation of one additional tab.
837 let new_line_offset = offset.block_indent(context.config);
838 result.push_str(&new_line_offset.to_string(context.config));
839 if trait_ref.is_some() {
840 result.push_str("for ");
842 let budget = context.budget(last_line_width(&result));
843 let type_offset = match context.config.indent_style() {
844 IndentStyle::Visual => new_line_offset + trait_ref_overhead,
845 IndentStyle::Block => new_line_offset,
847 result.push_str(&*self_ty.rewrite(context, Shape::legacy(budget, type_offset))?);
854 fn rewrite_trait_ref(
855 context: &RewriteContext,
856 trait_ref: &ast::TraitRef,
860 ) -> Option<String> {
861 // 1 = space between generics and trait_ref
862 let used_space = 1 + polarity_str.len() + result_len;
863 let shape = Shape::indented(offset + used_space, context.config);
864 if let Some(trait_ref_str) = trait_ref.rewrite(context, shape) {
865 if !trait_ref_str.contains('\n') {
866 return Some(format!(" {}{}", polarity_str, &trait_ref_str));
869 // We could not make enough space for trait_ref, so put it on new line.
870 let offset = offset.block_indent(context.config);
871 let shape = Shape::indented(offset, context.config);
872 let trait_ref_str = trait_ref.rewrite(context, shape)?;
875 &offset.to_string_with_newline(context.config),
881 pub struct StructParts<'a> {
884 vis: &'a ast::Visibility,
885 def: &'a ast::VariantData,
886 generics: Option<&'a ast::Generics>,
890 impl<'a> StructParts<'a> {
891 fn format_header(&self) -> String {
892 format_header(self.prefix, self.ident, self.vis)
895 fn from_variant(variant: &'a ast::Variant) -> Self {
898 ident: variant.node.ident,
899 vis: &DEFAULT_VISIBILITY,
900 def: &variant.node.data,
906 pub fn from_item(item: &'a ast::Item) -> Self {
907 let (prefix, def, generics) = match item.node {
908 ast::ItemKind::Struct(ref def, ref generics) => ("struct ", def, generics),
909 ast::ItemKind::Union(ref def, ref generics) => ("union ", def, generics),
917 generics: Some(generics),
924 context: &RewriteContext,
925 struct_parts: &StructParts,
927 one_line_width: Option<usize>,
928 ) -> Option<String> {
929 match *struct_parts.def {
930 ast::VariantData::Unit(..) => format_unit_struct(context, struct_parts, offset),
931 ast::VariantData::Tuple(ref fields, _) => {
932 format_tuple_struct(context, struct_parts, fields, offset)
934 ast::VariantData::Struct(ref fields, _) => {
935 format_struct_struct(context, struct_parts, fields, offset, one_line_width)
940 pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> {
941 if let ast::ItemKind::Trait(
945 ref type_param_bounds,
949 let mut result = String::with_capacity(128);
950 let header = format!(
952 format_auto(is_auto),
953 format_visibility(&item.vis),
954 format_unsafety(unsafety),
956 result.push_str(&header);
958 let body_lo = context.snippet_provider.span_after(item.span, "{");
960 let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
961 let generics_str = rewrite_generics(
963 &item.ident.to_string(),
966 mk_sp(item.span.lo(), body_lo),
968 result.push_str(&generics_str);
970 // FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
971 if !type_param_bounds.is_empty() {
972 let ident_hi = context
974 .span_after(item.span, &format!("{}", item.ident));
975 let bound_hi = type_param_bounds.last().unwrap().span().hi();
976 let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
977 if contains_comment(snippet) {
981 if !type_param_bounds.is_empty() {
982 result = rewrite_assign_rhs_with(
985 &TraitTyParamBounds::new(type_param_bounds),
987 RhsTactics::ForceNextLine,
991 // Rewrite where clause.
992 if !generics.where_clause.predicates.is_empty() {
993 let where_density = if context.config.indent_style() == IndentStyle::Block {
999 let where_budget = context.budget(last_line_width(&result));
1000 let pos_before_where = if type_param_bounds.is_empty() {
1001 generics.where_clause.span.lo()
1003 type_param_bounds[type_param_bounds.len() - 1].span().hi()
1005 let option = WhereClauseOption::snuggled(&generics_str);
1006 let where_clause_str = rewrite_where_clause(
1008 &generics.where_clause,
1009 context.config.brace_style(),
1010 Shape::legacy(where_budget, offset.block_only()),
1018 // If the where clause cannot fit on the same line,
1019 // put the where clause on a new line
1020 if !where_clause_str.contains('\n')
1021 && last_line_width(&result) + where_clause_str.len() + offset.width()
1022 > context.config.comment_width()
1024 let width = offset.block_indent + context.config.tab_spaces() - 1;
1025 let where_indent = Indent::new(0, width);
1026 result.push_str(&where_indent.to_string_with_newline(context.config));
1028 result.push_str(&where_clause_str);
1030 let item_snippet = context.snippet(item.span);
1031 if let Some(lo) = item_snippet.find('/') {
1033 let comment_hi = body_lo - BytePos(1);
1034 let comment_lo = item.span.lo() + BytePos(lo as u32);
1035 if comment_lo < comment_hi {
1036 match recover_missing_comment_in_span(
1037 mk_sp(comment_lo, comment_hi),
1038 Shape::indented(offset, context.config),
1040 last_line_width(&result),
1042 Some(ref missing_comment) if !missing_comment.is_empty() => {
1043 result.push_str(missing_comment);
1051 match context.config.brace_style() {
1052 _ if last_line_contains_single_line_comment(&result)
1053 || last_line_width(&result) + 2 > context.budget(offset.width()) =>
1055 result.push_str(&offset.to_string_with_newline(context.config));
1057 BraceStyle::AlwaysNextLine => {
1058 result.push_str(&offset.to_string_with_newline(context.config));
1060 BraceStyle::PreferSameLine => result.push(' '),
1061 BraceStyle::SameLineWhere => {
1062 if result.contains('\n')
1063 || (!generics.where_clause.predicates.is_empty() && !trait_items.is_empty())
1065 result.push_str(&offset.to_string_with_newline(context.config));
1073 let snippet = context.snippet(item.span);
1074 let open_pos = snippet.find_uncommented("{")? + 1;
1076 if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
1077 let mut visitor = FmtVisitor::from_context(context);
1078 visitor.block_indent = offset.block_only().block_indent(context.config);
1079 visitor.last_pos = item.span.lo() + BytePos(open_pos as u32);
1081 for item in trait_items {
1082 visitor.visit_trait_item(item);
1085 visitor.format_missing(item.span.hi() - BytePos(1));
1087 let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
1088 let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
1090 result.push_str(&inner_indent_str);
1091 result.push_str(visitor.buffer.to_string().trim());
1092 result.push_str(&outer_indent_str);
1093 } else if result.contains('\n') {
1104 pub fn format_trait_alias(
1105 context: &RewriteContext,
1107 generics: &ast::Generics,
1108 ty_param_bounds: &ast::TyParamBounds,
1110 ) -> Option<String> {
1111 let alias = ident.name.as_str();
1112 // 6 = "trait ", 2 = " ="
1113 let g_shape = shape.offset_left(6)?.sub_width(2)?;
1114 let generics_str = rewrite_generics(context, &alias, generics, g_shape, generics.span)?;
1115 let lhs = format!("trait {} =", generics_str);
1117 rewrite_assign_rhs(context, lhs, ty_param_bounds, shape.sub_width(1)?).map(|s| s + ";")
1120 fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) -> Option<String> {
1121 let header_str = format_header(p.prefix, p.ident, p.vis);
1122 let generics_str = if let Some(generics) = p.generics {
1123 let hi = if generics.where_clause.predicates.is_empty() {
1126 generics.where_clause.span.hi()
1131 context.config.brace_style(),
1134 mk_sp(generics.span.lo(), hi),
1135 last_line_width(&header_str),
1140 Some(format!("{}{};", header_str, generics_str))
1143 pub fn format_struct_struct(
1144 context: &RewriteContext,
1145 struct_parts: &StructParts,
1146 fields: &[ast::StructField],
1148 one_line_width: Option<usize>,
1149 ) -> Option<String> {
1150 let mut result = String::with_capacity(1024);
1151 let span = struct_parts.span;
1153 let header_str = struct_parts.format_header();
1154 result.push_str(&header_str);
1156 let header_hi = span.lo() + BytePos(header_str.len() as u32);
1157 let body_lo = context.snippet_provider.span_after(span, "{");
1159 let generics_str = match struct_parts.generics {
1160 Some(g) => format_generics(
1163 context.config.brace_style(),
1164 if fields.is_empty() {
1165 BracePos::ForceSameLine
1170 mk_sp(header_hi, body_lo),
1171 last_line_width(&result),
1174 // 3 = ` {}`, 2 = ` {`.
1175 let overhead = if fields.is_empty() { 3 } else { 2 };
1176 if (context.config.brace_style() == BraceStyle::AlwaysNextLine && !fields.is_empty())
1177 || context.config.max_width() < overhead + result.len()
1179 format!("\n{}{{", offset.block_only().to_string(context.config))
1186 let overhead = if fields.is_empty() { 1 } else { 0 };
1187 let total_width = result.len() + generics_str.len() + overhead;
1188 if !generics_str.is_empty() && !generics_str.contains('\n')
1189 && total_width > context.config.max_width()
1192 result.push_str(&offset.to_string(context.config));
1193 result.push_str(generics_str.trim_left());
1195 result.push_str(&generics_str);
1198 if fields.is_empty() {
1199 let inner_span = mk_sp(body_lo, span.hi() - BytePos(1));
1200 format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "", "}");
1201 return Some(result);
1205 let one_line_budget = context.budget(result.len() + 3 + offset.width());
1206 let one_line_budget =
1207 one_line_width.map_or(0, |one_line_width| min(one_line_width, one_line_budget));
1209 let items_str = rewrite_with_alignment(
1212 Shape::indented(offset, context.config).sub_width(1)?,
1213 mk_sp(body_lo, span.hi()),
1217 if !items_str.contains('\n') && !result.contains('\n') && items_str.len() <= one_line_budget
1218 && !last_line_contains_single_line_comment(&items_str)
1220 Some(format!("{} {} }}", result, items_str))
1226 .block_indent(context.config)
1227 .to_string(context.config),
1229 offset.to_string(context.config)
1234 fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> BytePos {
1236 ast::VisibilityKind::Crate(..) | ast::VisibilityKind::Restricted { .. } => vis.span.hi(),
1237 _ => default_span.lo(),
1241 // Format tuple or struct without any fields. We need to make sure that the comments
1242 // inside the delimiters are preserved.
1243 fn format_empty_struct_or_tuple(
1244 context: &RewriteContext,
1247 result: &mut String,
1251 // 3 = " {}" or "();"
1252 let used_width = last_line_used_width(&result, offset.width()) + 3;
1253 if used_width > context.config.max_width() {
1254 result.push_str(&offset.to_string_with_newline(context.config))
1256 result.push_str(opener);
1257 match rewrite_missing_comment(span, Shape::indented(offset, context.config), context) {
1258 Some(ref s) if s.is_empty() => (),
1260 if !is_single_line(s) || first_line_contains_single_line_comment(s) {
1261 let nested_indent_str = offset
1262 .block_indent(context.config)
1263 .to_string_with_newline(context.config);
1264 result.push_str(&nested_indent_str);
1267 if last_line_contains_single_line_comment(s) {
1268 result.push_str(&offset.to_string_with_newline(context.config));
1271 None => result.push_str(context.snippet(span)),
1273 result.push_str(closer);
1276 fn format_tuple_struct(
1277 context: &RewriteContext,
1278 struct_parts: &StructParts,
1279 fields: &[ast::StructField],
1281 ) -> Option<String> {
1282 let mut result = String::with_capacity(1024);
1283 let span = struct_parts.span;
1285 let header_str = struct_parts.format_header();
1286 result.push_str(&header_str);
1288 let body_lo = if fields.is_empty() {
1289 let lo = get_bytepos_after_visibility(struct_parts.vis, span);
1292 .span_after(mk_sp(lo, span.hi()), "(")
1296 let body_hi = if fields.is_empty() {
1299 .span_after(mk_sp(body_lo, span.hi()), ")")
1301 // This is a dirty hack to work around a missing `)` from the span of the last field.
1302 let last_arg_span = fields[fields.len() - 1].span;
1303 if context.snippet(last_arg_span).ends_with(')') {
1308 .span_after(mk_sp(last_arg_span.hi(), span.hi()), ")")
1312 let where_clause_str = match struct_parts.generics {
1314 let budget = context.budget(last_line_width(&header_str));
1315 let shape = Shape::legacy(budget, offset);
1316 let g_span = mk_sp(span.lo(), body_lo);
1317 let generics_str = rewrite_generics(context, "", generics, shape, g_span)?;
1318 result.push_str(&generics_str);
1320 let where_budget = context.budget(last_line_width(&result));
1321 let option = WhereClauseOption::new(true, false);
1322 rewrite_where_clause(
1324 &generics.where_clause,
1325 context.config.brace_style(),
1326 Shape::legacy(where_budget, offset.block_only()),
1327 Density::Compressed,
1335 None => "".to_owned(),
1338 if fields.is_empty() {
1339 let body_hi = context
1341 .span_before(mk_sp(body_lo, span.hi()), ")");
1342 let inner_span = mk_sp(body_lo, body_hi);
1343 format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
1345 let shape = Shape::indented(offset, context.config).sub_width(1)?;
1346 let fields = &fields.iter().collect::<Vec<_>>();
1347 result = overflow::rewrite_with_parens(
1353 context.config.width_heuristics().fn_call_width,
1358 if !where_clause_str.is_empty() && !where_clause_str.contains('\n')
1359 && (result.contains('\n')
1360 || offset.block_indent + result.len() + where_clause_str.len() + 1
1361 > context.config.max_width())
1363 // We need to put the where clause on a new line, but we didn't
1364 // know that earlier, so the where clause will not be indented properly.
1367 .push_str(&(offset.block_only() + (context.config.tab_spaces() - 1))
1368 .to_string(context.config));
1370 result.push_str(&where_clause_str);
1375 pub fn rewrite_type_alias(
1376 context: &RewriteContext,
1380 generics: &ast::Generics,
1381 vis: &ast::Visibility,
1383 ) -> Option<String> {
1384 let mut result = String::with_capacity(128);
1386 result.push_str(&format_visibility(vis));
1387 result.push_str("type ");
1390 let g_shape = Shape::indented(indent, context.config)
1391 .offset_left(result.len())?
1394 context.snippet_provider.span_after(span, "type"),
1397 let generics_str = rewrite_generics(context, &ident.to_string(), generics, g_shape, g_span)?;
1398 result.push_str(&generics_str);
1400 let where_budget = context.budget(last_line_width(&result));
1401 let option = WhereClauseOption::snuggled(&result);
1402 let where_clause_str = rewrite_where_clause(
1404 &generics.where_clause,
1405 context.config.brace_style(),
1406 Shape::legacy(where_budget, indent),
1414 result.push_str(&where_clause_str);
1415 if where_clause_str.is_empty() {
1416 result.push_str(" =");
1418 result.push_str(&format!(
1420 indent.to_string_with_newline(context.config)
1425 let ty_shape = Shape::indented(indent, context.config).sub_width(1)?;
1426 rewrite_assign_rhs(context, result, ty, ty_shape).map(|s| s + ";")
1429 fn type_annotation_spacing(config: &Config) -> (&str, &str) {
1431 if config.space_before_colon() { " " } else { "" },
1432 if config.space_after_colon() { " " } else { "" },
1436 pub fn rewrite_struct_field_prefix(
1437 context: &RewriteContext,
1438 field: &ast::StructField,
1439 ) -> Option<String> {
1440 let vis = format_visibility(&field.vis);
1441 let type_annotation_spacing = type_annotation_spacing(context.config);
1442 Some(match field.ident {
1443 Some(name) => format!("{}{}{}:", vis, name, type_annotation_spacing.0),
1444 None => format!("{}", vis),
1448 impl Rewrite for ast::StructField {
1449 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
1450 rewrite_struct_field(context, self, shape, 0)
1454 pub fn rewrite_struct_field(
1455 context: &RewriteContext,
1456 field: &ast::StructField,
1458 lhs_max_width: usize,
1459 ) -> Option<String> {
1460 if contains_skip(&field.attrs) {
1461 return Some(context.snippet(field.span()).to_owned());
1464 let type_annotation_spacing = type_annotation_spacing(context.config);
1465 let prefix = rewrite_struct_field_prefix(context, field)?;
1467 let attrs_str = field.attrs.rewrite(context, shape)?;
1468 let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
1469 let missing_span = if field.attrs.is_empty() {
1470 mk_sp(field.span.lo(), field.span.lo())
1472 mk_sp(field.attrs.last().unwrap().span.hi(), field.span.lo())
1474 let mut spacing = String::from(if field.ident.is_some() {
1475 type_annotation_spacing.1
1479 // Try to put everything on a single line.
1480 let attr_prefix = combine_strs_with_missing_comments(
1488 let overhead = last_line_width(&attr_prefix);
1489 let lhs_offset = lhs_max_width.checked_sub(overhead).unwrap_or(0);
1490 for _ in 0..lhs_offset {
1493 // In this extreme case we will be missing a space betweeen an attribute and a field.
1494 if prefix.is_empty() && !attrs_str.is_empty() && attrs_extendable && spacing.is_empty() {
1498 .offset_left(overhead + spacing.len())
1499 .and_then(|ty_shape| field.ty.rewrite(context, ty_shape));
1500 if let Some(ref ty) = orig_ty {
1501 if !ty.contains('\n') {
1502 return Some(attr_prefix + &spacing + ty);
1506 let is_prefix_empty = prefix.is_empty();
1507 // We must use multiline. We are going to put attributes and a field on different lines.
1508 let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, shape)?;
1509 // Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
1510 let field_str = if is_prefix_empty {
1511 field_str.trim_left()
1515 combine_strs_with_missing_comments(context, &attrs_str, field_str, missing_span, shape, false)
1518 pub struct StaticParts<'a> {
1520 vis: &'a ast::Visibility,
1523 mutability: ast::Mutability,
1524 expr_opt: Option<&'a ptr::P<ast::Expr>>,
1525 defaultness: Option<ast::Defaultness>,
1529 impl<'a> StaticParts<'a> {
1530 pub fn from_item(item: &'a ast::Item) -> Self {
1531 let (prefix, ty, mutability, expr) = match item.node {
1532 ast::ItemKind::Static(ref ty, mutability, ref expr) => ("static", ty, mutability, expr),
1533 ast::ItemKind::Const(ref ty, ref expr) => {
1534 ("const", ty, ast::Mutability::Immutable, expr)
1536 _ => unreachable!(),
1544 expr_opt: Some(expr),
1550 pub fn from_trait_item(ti: &'a ast::TraitItem) -> Self {
1551 let (ty, expr_opt) = match ti.node {
1552 ast::TraitItemKind::Const(ref ty, ref expr_opt) => (ty, expr_opt),
1553 _ => unreachable!(),
1557 vis: &DEFAULT_VISIBILITY,
1560 mutability: ast::Mutability::Immutable,
1561 expr_opt: expr_opt.as_ref(),
1567 pub fn from_impl_item(ii: &'a ast::ImplItem) -> Self {
1568 let (ty, expr) = match ii.node {
1569 ast::ImplItemKind::Const(ref ty, ref expr) => (ty, expr),
1570 _ => unreachable!(),
1577 mutability: ast::Mutability::Immutable,
1578 expr_opt: Some(expr),
1579 defaultness: Some(ii.defaultness),
1586 context: &RewriteContext,
1587 static_parts: &StaticParts,
1589 ) -> Option<String> {
1590 let colon = colon_spaces(
1591 context.config.space_before_colon(),
1592 context.config.space_after_colon(),
1594 let mut prefix = format!(
1596 format_visibility(static_parts.vis),
1597 static_parts.defaultness.map_or("", format_defaultness),
1598 static_parts.prefix,
1599 format_mutability(static_parts.mutability),
1605 Shape::indented(offset.block_only(), context.config).offset_left(prefix.len() + 2)?;
1606 let ty_str = match static_parts.ty.rewrite(context, ty_shape) {
1607 Some(ty_str) => ty_str,
1609 if prefix.ends_with(' ') {
1612 let nested_indent = offset.block_indent(context.config);
1613 let nested_shape = Shape::indented(nested_indent, context.config);
1614 let ty_str = static_parts.ty.rewrite(context, nested_shape)?;
1617 nested_indent.to_string_with_newline(context.config),
1623 if let Some(expr) = static_parts.expr_opt {
1624 let lhs = format!("{}{} =", prefix, ty_str);
1626 let remaining_width = context.budget(offset.block_indent + 1);
1631 Shape::legacy(remaining_width, offset.block_only()),
1632 ).and_then(|res| recover_comment_removed(res, static_parts.span, context))
1633 .map(|s| if s.ends_with(';') { s } else { s + ";" })
1635 Some(format!("{}{};", prefix, ty_str))
1639 pub fn rewrite_associated_type(
1641 ty_opt: Option<&ptr::P<ast::Ty>>,
1642 ty_param_bounds_opt: Option<&ast::TyParamBounds>,
1643 context: &RewriteContext,
1645 ) -> Option<String> {
1646 let prefix = format!("type {}", ident);
1648 let type_bounds_str = if let Some(bounds) = ty_param_bounds_opt {
1649 if bounds.is_empty() {
1653 let shape = Shape::indented(indent, context.config).offset_left(prefix.len() + 2)?;
1654 bounds.rewrite(context, shape).map(|s| format!(": {}", s))?
1660 if let Some(ty) = ty_opt {
1662 let shape = Shape::indented(indent, context.config).sub_width(1)?;
1663 let lhs = format!("{}{} =", prefix, type_bounds_str);
1664 rewrite_assign_rhs(context, lhs, &**ty, shape).map(|s| s + ";")
1666 Some(format!("{}{};", prefix, type_bounds_str))
1670 pub fn rewrite_associated_impl_type(
1672 defaultness: ast::Defaultness,
1673 ty_opt: Option<&ptr::P<ast::Ty>>,
1674 ty_param_bounds_opt: Option<&ast::TyParamBounds>,
1675 context: &RewriteContext,
1677 ) -> Option<String> {
1678 let result = rewrite_associated_type(ident, ty_opt, ty_param_bounds_opt, context, indent)?;
1681 ast::Defaultness::Default => Some(format!("default {}", result)),
1686 impl Rewrite for ast::FunctionRetTy {
1687 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
1689 ast::FunctionRetTy::Default(_) => Some(String::new()),
1690 ast::FunctionRetTy::Ty(ref ty) => {
1691 let inner_width = shape.width.checked_sub(3)?;
1692 ty.rewrite(context, Shape::legacy(inner_width, shape.indent + 3))
1693 .map(|r| format!("-> {}", r))
1699 fn is_empty_infer(context: &RewriteContext, ty: &ast::Ty) -> bool {
1701 ast::TyKind::Infer => {
1702 let original = context.snippet(ty.span);
1709 impl Rewrite for ast::Arg {
1710 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
1711 if is_named_arg(self) {
1712 let mut result = self.pat
1713 .rewrite(context, Shape::legacy(shape.width, shape.indent))?;
1715 if !is_empty_infer(context, &*self.ty) {
1716 if context.config.space_before_colon() {
1717 result.push_str(" ");
1719 result.push_str(":");
1720 if context.config.space_after_colon() {
1721 result.push_str(" ");
1723 let overhead = last_line_width(&result);
1724 let max_width = shape.width.checked_sub(overhead)?;
1725 let ty_str = self.ty
1726 .rewrite(context, Shape::legacy(max_width, shape.indent))?;
1727 result.push_str(&ty_str);
1732 self.ty.rewrite(context, shape)
1737 fn rewrite_explicit_self(
1738 explicit_self: &ast::ExplicitSelf,
1740 context: &RewriteContext,
1741 ) -> Option<String> {
1742 match explicit_self.node {
1743 ast::SelfKind::Region(lt, m) => {
1744 let mut_str = format_mutability(m);
1747 let lifetime_str = l.rewrite(
1749 Shape::legacy(context.config.max_width(), Indent::empty()),
1751 Some(format!("&{} {}self", lifetime_str, mut_str))
1753 None => Some(format!("&{}self", mut_str)),
1756 ast::SelfKind::Explicit(ref ty, _) => {
1757 assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
1759 let mutability = explicit_self_mutability(&args[0]);
1760 let type_str = ty.rewrite(
1762 Shape::legacy(context.config.max_width(), Indent::empty()),
1767 format_mutability(mutability),
1771 ast::SelfKind::Value(_) => {
1772 assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
1774 let mutability = explicit_self_mutability(&args[0]);
1776 Some(format!("{}self", format_mutability(mutability)))
1781 // Hacky solution caused by absence of `Mutability` in `SelfValue` and
1782 // `SelfExplicit` variants of `ast::ExplicitSelf_`.
1783 fn explicit_self_mutability(arg: &ast::Arg) -> ast::Mutability {
1784 if let ast::PatKind::Ident(ast::BindingMode::ByValue(mutability), _, _) = arg.pat.node {
1791 pub fn span_lo_for_arg(arg: &ast::Arg) -> BytePos {
1792 if is_named_arg(arg) {
1799 pub fn span_hi_for_arg(context: &RewriteContext, arg: &ast::Arg) -> BytePos {
1801 ast::TyKind::Infer if context.snippet(arg.ty.span) == "_" => arg.ty.span.hi(),
1802 ast::TyKind::Infer if is_named_arg(arg) => arg.pat.span.hi(),
1803 _ => arg.ty.span.hi(),
1807 pub fn is_named_arg(arg: &ast::Arg) -> bool {
1808 if let ast::PatKind::Ident(_, ident, _) = arg.pat.node {
1809 ident != symbol::keywords::Invalid.ident()
1815 // Return type is (result, force_new_line_for_brace)
1817 context: &RewriteContext,
1822 newline_brace: bool,
1824 ) -> Option<(String, bool)> {
1825 let mut force_new_line_for_brace = false;
1827 let where_clause = &fn_sig.generics.where_clause;
1829 let mut result = String::with_capacity(1024);
1830 result.push_str(&fn_sig.to_str(context));
1833 result.push_str("fn ");
1836 let overhead = if has_body && !newline_brace {
1843 let used_width = last_line_used_width(&result, indent.width());
1844 let one_line_budget = context.budget(used_width + overhead);
1846 width: one_line_budget,
1850 let fd = fn_sig.decl;
1851 let g_span = mk_sp(span.lo(), fd.output.span().lo());
1853 rewrite_generics(context, &ident.to_string(), fn_sig.generics, shape, g_span)?;
1854 result.push_str(&generics_str);
1856 let snuggle_angle_bracket = generics_str
1859 .map_or(false, |l| l.trim_left().len() == 1);
1861 // Note that the width and indent don't really matter, we'll re-layout the
1862 // return type later anyway.
1863 let ret_str = fd.output
1864 .rewrite(context, Shape::indented(indent, context.config))?;
1866 let multi_line_ret_str = ret_str.contains('\n');
1867 let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
1870 let (one_line_budget, multi_line_budget, mut arg_indent) = compute_budgets_for_args(
1881 "rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, arg_indent: {:?}",
1882 one_line_budget, multi_line_budget, arg_indent
1885 // Check if vertical layout was forced.
1886 if one_line_budget == 0 {
1887 if snuggle_angle_bracket {
1890 result.push_str("(");
1891 if context.config.indent_style() == IndentStyle::Visual {
1892 result.push_str(&arg_indent.to_string_with_newline(context.config));
1898 if context.config.spaces_within_parens_and_brackets() && !fd.inputs.is_empty()
1899 && result.ends_with('(')
1904 // Skip `pub(crate)`.
1905 let lo_after_visibility = get_bytepos_after_visibility(&fn_sig.visibility, span);
1906 // A conservative estimation, to goal is to be over all parens in generics
1907 let args_start = fn_sig
1912 .map_or(lo_after_visibility, |param| param.span().hi());
1913 let args_end = if fd.inputs.is_empty() {
1916 .span_after(mk_sp(args_start, span.hi()), ")")
1918 let last_span = mk_sp(fd.inputs[fd.inputs.len() - 1].span().hi(), span.hi());
1919 context.snippet_provider.span_after(last_span, ")")
1921 let args_span = mk_sp(
1924 .span_after(mk_sp(args_start, span.hi()), "("),
1927 let arg_str = rewrite_args(
1930 fd.get_self().as_ref(),
1937 generics_str.contains('\n'),
1940 let put_args_in_block = match context.config.indent_style() {
1941 IndentStyle::Block => arg_str.contains('\n') || arg_str.len() > one_line_budget,
1943 } && !fd.inputs.is_empty();
1945 let mut args_last_line_contains_comment = false;
1946 if put_args_in_block {
1947 arg_indent = indent.block_indent(context.config);
1948 result.push_str(&arg_indent.to_string_with_newline(context.config));
1949 result.push_str(&arg_str);
1950 result.push_str(&indent.to_string_with_newline(context.config));
1953 result.push_str(&arg_str);
1954 let used_width = last_line_used_width(&result, indent.width()) + first_line_width(&ret_str);
1955 // Put the closing brace on the next line if it overflows the max width.
1957 if fd.inputs.is_empty() && used_width + 1 > context.config.max_width() {
1960 if context.config.spaces_within_parens_and_brackets() && !fd.inputs.is_empty() {
1963 // If the last line of args contains comment, we cannot put the closing paren
1964 // on the same line.
1968 .map_or(false, |last_line| last_line.contains("//"))
1970 args_last_line_contains_comment = true;
1971 result.push_str(&arg_indent.to_string_with_newline(context.config));
1977 if let ast::FunctionRetTy::Ty(..) = fd.output {
1978 let ret_should_indent = match context.config.indent_style() {
1979 // If our args are block layout then we surely must have space.
1980 IndentStyle::Block if put_args_in_block || fd.inputs.is_empty() => false,
1981 _ if args_last_line_contains_comment => false,
1982 _ if result.contains('\n') || multi_line_ret_str => true,
1984 // If the return type would push over the max width, then put the return type on
1985 // a new line. With the +1 for the signature length an additional space between
1986 // the closing parenthesis of the argument and the arrow '->' is considered.
1987 let mut sig_length = result.len() + indent.width() + ret_str_len + 1;
1989 // If there is no where clause, take into account the space after the return type
1991 if where_clause.predicates.is_empty() {
1995 sig_length > context.config.max_width()
1998 let ret_indent = if ret_should_indent {
1999 let indent = if arg_str.is_empty() {
2000 // Aligning with non-existent args looks silly.
2001 force_new_line_for_brace = true;
2004 // FIXME: we might want to check that using the arg indent
2005 // doesn't blow our budget, and if it does, then fallback to
2006 // the where clause indent.
2010 result.push_str(&indent.to_string_with_newline(context.config));
2014 Indent::new(indent.block_indent, last_line_width(&result))
2017 if multi_line_ret_str || ret_should_indent {
2018 // Now that we know the proper indent and width, we need to
2019 // re-layout the return type.
2020 let ret_str = fd.output
2021 .rewrite(context, Shape::indented(ret_indent, context.config))?;
2022 result.push_str(&ret_str);
2024 result.push_str(&ret_str);
2027 // Comment between return type and the end of the decl.
2028 let snippet_lo = fd.output.span().hi();
2029 if where_clause.predicates.is_empty() {
2030 let snippet_hi = span.hi();
2031 let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
2032 // Try to preserve the layout of the original snippet.
2033 let original_starts_with_newline = snippet
2035 .map_or(false, |i| starts_with_newline(&snippet[i..]));
2036 let original_ends_with_newline = snippet
2037 .rfind(|c| c != ' ')
2038 .map_or(false, |i| snippet[i..].ends_with('\n'));
2039 let snippet = snippet.trim();
2040 if !snippet.is_empty() {
2041 result.push(if original_starts_with_newline {
2046 result.push_str(snippet);
2047 if original_ends_with_newline {
2048 force_new_line_for_brace = true;
2054 let pos_before_where = match fd.output {
2055 ast::FunctionRetTy::Default(..) => args_span.hi(),
2056 ast::FunctionRetTy::Ty(ref ty) => ty.span.hi(),
2059 let is_args_multi_lined = arg_str.contains('\n');
2061 let option = WhereClauseOption::new(!has_body, put_args_in_block && ret_str.is_empty());
2062 let where_clause_str = rewrite_where_clause(
2065 context.config.brace_style(),
2066 Shape::indented(indent, context.config),
2072 is_args_multi_lined,
2074 // If there are neither where clause nor return type, we may be missing comments between
2076 if where_clause_str.is_empty() {
2077 if let ast::FunctionRetTy::Default(ret_span) = fd.output {
2078 match recover_missing_comment_in_span(
2079 mk_sp(args_span.hi(), ret_span.hi()),
2082 last_line_width(&result),
2084 Some(ref missing_comment) if !missing_comment.is_empty() => {
2085 result.push_str(missing_comment);
2086 force_new_line_for_brace = true;
2093 result.push_str(&where_clause_str);
2095 force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
2096 force_new_line_for_brace |= is_args_multi_lined && context.config.where_single_line();
2097 Some((result, force_new_line_for_brace))
2100 #[derive(Copy, Clone)]
2101 struct WhereClauseOption {
2102 suppress_comma: bool, // Force no trailing comma
2103 snuggle: bool, // Do not insert newline before `where`
2104 compress_where: bool, // Try single line where clause instead of vertical layout
2107 impl WhereClauseOption {
2108 pub fn new(suppress_comma: bool, snuggle: bool) -> WhereClauseOption {
2112 compress_where: false,
2116 pub fn snuggled(current: &str) -> WhereClauseOption {
2118 suppress_comma: false,
2119 snuggle: trimmed_last_line_width(current) == 1,
2120 compress_where: false,
2126 context: &RewriteContext,
2128 explicit_self: Option<&ast::ExplicitSelf>,
2129 one_line_budget: usize,
2130 multi_line_budget: usize,
2135 generics_str_contains_newline: bool,
2136 ) -> Option<String> {
2137 let mut arg_item_strs = args.iter()
2138 .map(|arg| arg.rewrite(context, Shape::legacy(multi_line_budget, arg_indent)))
2139 .collect::<Option<Vec<_>>>()?;
2141 // Account for sugary self.
2142 // FIXME: the comment for the self argument is dropped. This is blocked
2143 // on rust issue #27522.
2144 let min_args = explicit_self
2145 .and_then(|explicit_self| rewrite_explicit_self(explicit_self, args, context))
2146 .map_or(1, |self_str| {
2147 arg_item_strs[0] = self_str;
2151 // Comments between args.
2152 let mut arg_items = Vec::new();
2154 arg_items.push(ListItem::from_str(""));
2157 // FIXME(#21): if there are no args, there might still be a comment, but
2158 // without spans for the comment or parens, there is no chance of
2159 // getting it right. You also don't get to put a comment on self, unless
2161 if args.len() >= min_args || variadic {
2162 let comment_span_start = if min_args == 2 {
2163 let second_arg_start = if arg_has_pattern(&args[1]) {
2164 args[1].pat.span.lo()
2166 args[1].ty.span.lo()
2168 let reduced_span = mk_sp(span.lo(), second_arg_start);
2170 context.snippet_provider.span_after_last(reduced_span, ",")
2175 enum ArgumentKind<'a> {
2176 Regular(&'a ast::Arg),
2180 let variadic_arg = if variadic {
2181 let variadic_span = mk_sp(args.last().unwrap().ty.span.hi(), span.hi());
2182 let variadic_start =
2183 context.snippet_provider.span_after(variadic_span, "...") - BytePos(3);
2184 Some(ArgumentKind::Variadic(variadic_start))
2189 let more_items = itemize_list(
2190 context.snippet_provider,
2191 args[min_args - 1..]
2193 .map(ArgumentKind::Regular)
2194 .chain(variadic_arg),
2198 ArgumentKind::Regular(arg) => span_lo_for_arg(arg),
2199 ArgumentKind::Variadic(start) => start,
2202 ArgumentKind::Regular(arg) => arg.ty.span.hi(),
2203 ArgumentKind::Variadic(start) => start + BytePos(3),
2206 ArgumentKind::Regular(..) => None,
2207 ArgumentKind::Variadic(..) => Some("...".to_owned()),
2214 arg_items.extend(more_items);
2217 let fits_in_one_line = !generics_str_contains_newline
2218 && (arg_items.is_empty()
2219 || arg_items.len() == 1 && arg_item_strs[0].len() <= one_line_budget);
2221 for (item, arg) in arg_items.iter_mut().zip(arg_item_strs) {
2222 item.item = Some(arg);
2225 let last_line_ends_with_comment = arg_items
2228 .and_then(|item| item.post_comment.as_ref())
2229 .map_or(false, |s| s.trim().starts_with("//"));
2231 let (indent, trailing_comma) = match context.config.indent_style() {
2232 IndentStyle::Block if fits_in_one_line => {
2233 (indent.block_indent(context.config), SeparatorTactic::Never)
2235 IndentStyle::Block => (
2236 indent.block_indent(context.config),
2237 context.config.trailing_comma(),
2239 IndentStyle::Visual if last_line_ends_with_comment => {
2240 (arg_indent, context.config.trailing_comma())
2242 IndentStyle::Visual => (arg_indent, SeparatorTactic::Never),
2245 let tactic = definitive_tactic(
2247 context.config.fn_args_density().to_list_tactic(),
2251 let budget = match tactic {
2252 DefinitiveListTactic::Horizontal => one_line_budget,
2253 _ => multi_line_budget,
2256 debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);
2258 let fmt = ListFormatting {
2261 trailing_separator: if variadic {
2262 SeparatorTactic::Never
2266 separator_place: SeparatorPlace::Back,
2267 shape: Shape::legacy(budget, indent),
2268 ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
2269 preserve_newline: true,
2270 config: context.config,
2273 write_list(&arg_items, &fmt)
2276 fn arg_has_pattern(arg: &ast::Arg) -> bool {
2277 if let ast::PatKind::Ident(_, ident, _) = arg.pat.node {
2278 ident != symbol::keywords::Invalid.ident()
2284 fn compute_budgets_for_args(
2285 context: &RewriteContext,
2289 newline_brace: bool,
2291 force_vertical_layout: bool,
2292 ) -> Option<((usize, usize, Indent))> {
2294 "compute_budgets_for_args {} {:?}, {}, {}",
2300 // Try keeping everything on the same line.
2301 if !result.contains('\n') && !force_vertical_layout {
2302 // 2 = `()`, 3 = `() `, space is before ret_string.
2303 let overhead = if ret_str_len == 0 { 2 } else { 3 };
2304 let mut used_space = indent.width() + result.len() + ret_str_len + overhead;
2314 let one_line_budget = context.budget(used_space);
2316 if one_line_budget > 0 {
2318 let (indent, multi_line_budget) = match context.config.indent_style() {
2319 IndentStyle::Block => {
2320 let indent = indent.block_indent(context.config);
2321 (indent, context.budget(indent.width() + 1))
2323 IndentStyle::Visual => {
2324 let indent = indent + result.len() + 1;
2325 let multi_line_overhead = indent.width() + if newline_brace { 2 } else { 4 };
2326 (indent, context.budget(multi_line_overhead))
2330 return Some((one_line_budget, multi_line_budget, indent));
2334 // Didn't work. we must force vertical layout and put args on a newline.
2335 let new_indent = indent.block_indent(context.config);
2336 let used_space = match context.config.indent_style() {
2338 IndentStyle::Block => new_indent.width() + 1,
2339 // Account for `)` and possibly ` {`.
2340 IndentStyle::Visual => new_indent.width() + if ret_str_len == 0 { 1 } else { 3 },
2342 Some((0, context.budget(used_space), new_indent))
2345 fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> bool {
2346 let predicate_count = where_clause.predicates.len();
2348 if config.where_single_line() && predicate_count == 1 {
2351 let brace_style = config.brace_style();
2353 brace_style == BraceStyle::AlwaysNextLine
2354 || (brace_style == BraceStyle::SameLineWhere && predicate_count > 0)
2357 fn rewrite_generics(
2358 context: &RewriteContext,
2360 generics: &ast::Generics,
2363 ) -> Option<String> {
2364 // FIXME: convert bounds to where clauses where they get too big or if
2365 // there is a where clause at all.
2367 if generics.params.is_empty() {
2368 return Some(ident.to_owned());
2371 let params = &generics.params.iter().map(|e| &*e).collect::<Vec<_>>();
2372 overflow::rewrite_with_angle_brackets(context, ident, params, shape, span)
2375 pub fn generics_shape_from_config(config: &Config, shape: Shape, offset: usize) -> Option<Shape> {
2376 match config.indent_style() {
2377 IndentStyle::Visual => shape.visual_indent(1 + offset).sub_width(offset + 2),
2378 IndentStyle::Block => {
2382 .block_indent(config.tab_spaces())
2383 .with_max_width(config)
2389 fn rewrite_where_clause_rfc_style(
2390 context: &RewriteContext,
2391 where_clause: &ast::WhereClause,
2394 span_end: Option<BytePos>,
2395 span_end_before_where: BytePos,
2396 where_clause_option: WhereClauseOption,
2397 is_args_multi_line: bool,
2398 ) -> Option<String> {
2399 let block_shape = shape.block().with_max_width(context.config);
2401 let (span_before, span_after) =
2402 missing_span_before_after_where(span_end_before_where, where_clause);
2403 let (comment_before, comment_after) =
2404 rewrite_comments_before_after_where(context, span_before, span_after, shape)?;
2406 let starting_newline = if where_clause_option.snuggle && comment_before.is_empty() {
2409 block_shape.indent.to_string_with_newline(context.config)
2412 let clause_shape = block_shape.block_left(context.config.tab_spaces())?;
2414 let clause_shape = clause_shape.sub_width(1)?;
2415 // each clause on one line, trailing comma (except if suppress_comma)
2416 let span_start = where_clause.predicates[0].span().lo();
2417 // If we don't have the start of the next span, then use the end of the
2418 // predicates, but that means we miss comments.
2419 let len = where_clause.predicates.len();
2420 let end_of_preds = where_clause.predicates[len - 1].span().hi();
2421 let span_end = span_end.unwrap_or(end_of_preds);
2422 let items = itemize_list(
2423 context.snippet_provider,
2424 where_clause.predicates.iter(),
2427 |pred| pred.span().lo(),
2428 |pred| pred.span().hi(),
2429 |pred| pred.rewrite(context, clause_shape),
2434 let where_single_line = context.config.where_single_line() && len == 1 && !is_args_multi_line;
2435 let comma_tactic = if where_clause_option.suppress_comma || where_single_line {
2436 SeparatorTactic::Never
2438 context.config.trailing_comma()
2441 // shape should be vertical only and only if we have `where_single_line` option enabled
2442 // and the number of items of the where clause is equal to 1
2443 let shape_tactic = if where_single_line {
2444 DefinitiveListTactic::Horizontal
2446 DefinitiveListTactic::Vertical
2449 let fmt = ListFormatting {
2450 tactic: shape_tactic,
2452 trailing_separator: comma_tactic,
2453 separator_place: SeparatorPlace::Back,
2454 shape: clause_shape,
2455 ends_with_newline: true,
2456 preserve_newline: true,
2457 config: context.config,
2459 let preds_str = write_list(&items.collect::<Vec<_>>(), &fmt)?;
2461 let comment_separator = |comment: &str, shape: Shape| {
2462 if comment.is_empty() {
2465 shape.indent.to_string_with_newline(context.config)
2468 let newline_before_where = comment_separator(&comment_before, shape);
2469 let newline_after_where = comment_separator(&comment_after, clause_shape);
2472 let clause_sep = if where_clause_option.compress_where && comment_before.is_empty()
2473 && comment_after.is_empty() && !preds_str.contains('\n')
2474 && 6 + preds_str.len() <= shape.width || where_single_line
2478 clause_shape.indent.to_string_with_newline(context.config)
2481 "{}{}{}where{}{}{}{}",
2484 newline_before_where,
2485 newline_after_where,
2492 fn rewrite_where_clause(
2493 context: &RewriteContext,
2494 where_clause: &ast::WhereClause,
2495 brace_style: BraceStyle,
2499 span_end: Option<BytePos>,
2500 span_end_before_where: BytePos,
2501 where_clause_option: WhereClauseOption,
2502 is_args_multi_line: bool,
2503 ) -> Option<String> {
2504 if where_clause.predicates.is_empty() {
2505 return Some(String::new());
2508 if context.config.indent_style() == IndentStyle::Block {
2509 return rewrite_where_clause_rfc_style(
2515 span_end_before_where,
2516 where_clause_option,
2521 let extra_indent = Indent::new(context.config.tab_spaces(), 0);
2523 let offset = match context.config.indent_style() {
2524 IndentStyle::Block => shape.indent + extra_indent.block_indent(context.config),
2525 // 6 = "where ".len()
2526 IndentStyle::Visual => shape.indent + extra_indent + 6,
2528 // FIXME: if indent_style != Visual, then the budgets below might
2529 // be out by a char or two.
2531 let budget = context.config.max_width() - offset.width();
2532 let span_start = where_clause.predicates[0].span().lo();
2533 // If we don't have the start of the next span, then use the end of the
2534 // predicates, but that means we miss comments.
2535 let len = where_clause.predicates.len();
2536 let end_of_preds = where_clause.predicates[len - 1].span().hi();
2537 let span_end = span_end.unwrap_or(end_of_preds);
2538 let items = itemize_list(
2539 context.snippet_provider,
2540 where_clause.predicates.iter(),
2543 |pred| pred.span().lo(),
2544 |pred| pred.span().hi(),
2545 |pred| pred.rewrite(context, Shape::legacy(budget, offset)),
2550 let item_vec = items.collect::<Vec<_>>();
2551 // FIXME: we don't need to collect here
2552 let tactic = definitive_tactic(&item_vec, ListTactic::Vertical, Separator::Comma, budget);
2554 let mut comma_tactic = context.config.trailing_comma();
2555 // Kind of a hack because we don't usually have trailing commas in where clauses.
2556 if comma_tactic == SeparatorTactic::Vertical || where_clause_option.suppress_comma {
2557 comma_tactic = SeparatorTactic::Never;
2560 let fmt = ListFormatting {
2563 trailing_separator: comma_tactic,
2564 separator_place: SeparatorPlace::Back,
2565 shape: Shape::legacy(budget, offset),
2566 ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
2567 preserve_newline: true,
2568 config: context.config,
2570 let preds_str = write_list(&item_vec, &fmt)?;
2572 let end_length = if terminator == "{" {
2573 // If the brace is on the next line we don't need to count it otherwise it needs two
2576 BraceStyle::AlwaysNextLine | BraceStyle::SameLineWhere => 0,
2577 BraceStyle::PreferSameLine => 2,
2579 } else if terminator == "=" {
2584 if density == Density::Tall || preds_str.contains('\n')
2585 || shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
2589 (shape.indent + extra_indent).to_string(context.config),
2593 Some(format!(" where {}", preds_str))
2597 fn missing_span_before_after_where(
2598 before_item_span_end: BytePos,
2599 where_clause: &ast::WhereClause,
2601 let missing_span_before = mk_sp(before_item_span_end, where_clause.span.lo());
2603 let pos_after_where = where_clause.span.lo() + BytePos(5);
2604 let missing_span_after = mk_sp(pos_after_where, where_clause.predicates[0].span().lo());
2605 (missing_span_before, missing_span_after)
2608 fn rewrite_comments_before_after_where(
2609 context: &RewriteContext,
2610 span_before_where: Span,
2611 span_after_where: Span,
2613 ) -> Option<(String, String)> {
2614 let before_comment = rewrite_missing_comment(span_before_where, shape, context)?;
2615 let after_comment = rewrite_missing_comment(
2617 shape.block_indent(context.config.tab_spaces()),
2620 Some((before_comment, after_comment))
2623 fn format_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
2624 format!("{}{}{}", format_visibility(vis), item_name, ident)
2627 #[derive(PartialEq, Eq, Clone, Copy)]
2635 context: &RewriteContext,
2636 generics: &ast::Generics,
2637 brace_style: BraceStyle,
2638 brace_pos: BracePos,
2642 ) -> Option<String> {
2643 let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
2644 let mut result = rewrite_generics(context, "", generics, shape, span)?;
2646 let same_line_brace = if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
2647 let budget = context.budget(last_line_used_width(&result, offset.width()));
2648 let mut option = WhereClauseOption::snuggled(&result);
2649 if brace_pos == BracePos::None {
2650 option.suppress_comma = true;
2652 // If the generics are not parameterized then generics.span.hi() == 0,
2653 // so we use span.lo(), which is the position after `struct Foo`.
2654 let span_end_before_where = if generics.is_parameterized() {
2659 let where_clause_str = rewrite_where_clause(
2661 &generics.where_clause,
2663 Shape::legacy(budget, offset.block_only()),
2667 span_end_before_where,
2671 result.push_str(&where_clause_str);
2672 brace_pos == BracePos::ForceSameLine || brace_style == BraceStyle::PreferSameLine
2673 || (generics.where_clause.predicates.is_empty()
2674 && trimmed_last_line_width(&result) == 1)
2676 brace_pos == BracePos::ForceSameLine || trimmed_last_line_width(&result) == 1
2677 || brace_style != BraceStyle::AlwaysNextLine
2679 if brace_pos == BracePos::None {
2680 return Some(result);
2682 let total_used_width = last_line_used_width(&result, used_width);
2683 let remaining_budget = context.budget(total_used_width);
2684 // If the same line brace if forced, it indicates that we are rewriting an item with empty body,
2685 // and hence we take the closer into account as well for one line budget.
2686 // We assume that the closer has the same length as the opener.
2687 let overhead = if brace_pos == BracePos::ForceSameLine {
2694 let forbid_same_line_brace = overhead > remaining_budget;
2695 if !forbid_same_line_brace && same_line_brace {
2699 result.push_str(&offset.block_only().to_string(context.config));
2706 impl Rewrite for ast::ForeignItem {
2707 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
2708 let attrs_str = self.attrs.rewrite(context, shape)?;
2709 // Drop semicolon or it will be interpreted as comment.
2710 // FIXME: this may be a faulty span from libsyntax.
2711 let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
2713 let item_str = match self.node {
2714 ast::ForeignItemKind::Fn(ref fn_decl, ref generics) => {
2719 &FnSig::new(fn_decl, generics, self.vis.clone()),
2723 ).map(|(s, _)| format!("{};", s))
2725 ast::ForeignItemKind::Static(ref ty, is_mutable) => {
2726 // FIXME(#21): we're dropping potential comments in between the
2727 // function keywords here.
2728 let vis = format_visibility(&self.vis);
2729 let mut_str = if is_mutable { "mut " } else { "" };
2730 let prefix = format!("{}static {}{}:", vis, mut_str, self.ident);
2732 rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
2734 ast::ForeignItemKind::Ty => {
2735 let vis = format_visibility(&self.vis);
2736 Some(format!("{}type {};", vis, self.ident))
2738 ast::ForeignItemKind::Macro(ref mac) => {
2739 rewrite_macro(mac, None, context, shape, MacroPosition::Item)
2743 let missing_span = if self.attrs.is_empty() {
2744 mk_sp(self.span.lo(), self.span.lo())
2746 mk_sp(self.attrs[self.attrs.len() - 1].span.hi(), self.span.lo())
2748 combine_strs_with_missing_comments(
2759 impl Rewrite for ast::GenericParam {
2760 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
2762 ast::GenericParam::Lifetime(ref lifetime_def) => lifetime_def.rewrite(context, shape),
2763 ast::GenericParam::Type(ref ty) => ty.rewrite(context, shape),
2768 /// Rewrite an inline mod.
2769 pub fn rewrite_mod(item: &ast::Item) -> String {
2770 let mut result = String::with_capacity(32);
2771 result.push_str(&*format_visibility(&item.vis));
2772 result.push_str("mod ");
2773 result.push_str(&item.ident.to_string());
2778 /// Rewrite `extern crate foo;` WITHOUT attributes.
2779 pub fn rewrite_extern_crate(context: &RewriteContext, item: &ast::Item) -> Option<String> {
2780 assert!(is_extern_crate(item));
2781 let new_str = context.snippet(item.span);
2782 Some(if contains_comment(new_str) {
2785 let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
2786 String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
2790 /// Returns true for `mod foo;`, false for `mod foo { .. }`.
2791 pub fn is_mod_decl(item: &ast::Item) -> bool {
2793 ast::ItemKind::Mod(ref m) => m.inner.hi() != item.span.hi(),
2798 pub fn is_use_item(item: &ast::Item) -> bool {
2800 ast::ItemKind::Use(_) => true,
2805 pub fn is_extern_crate(item: &ast::Item) -> bool {
2807 ast::ItemKind::ExternCrate(..) => true,