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.
13 use strings::string_buffer::StringBuffer;
14 use syntax::{ast, ptr, visit};
15 use syntax::attr::HasAttrs;
16 use syntax::codemap::{self, BytePos, CodeMap, Pos, Span};
17 use syntax::parse::ParseSess;
19 use {Indent, Shape, Spanned};
20 use codemap::{LineRangeUtils, SpanUtils};
21 use comment::{contains_comment, CodeCharKind, CommentCodeSlices, FindUncommented};
22 use comment::rewrite_comment;
23 use config::{BraceStyle, Config};
24 use expr::{format_expr, ExprType};
25 use items::{format_impl, format_trait, rewrite_associated_impl_type, rewrite_associated_type,
26 rewrite_static, rewrite_type_alias};
27 use lists::{itemize_list, write_list, DefinitiveListTactic, ListFormatting, SeparatorTactic};
28 use macros::{rewrite_macro, MacroPosition};
30 use rewrite::{Rewrite, RewriteContext};
31 use utils::{self, contains_skip, mk_sp};
33 fn is_use_item(item: &ast::Item) -> bool {
35 ast::ItemKind::Use(_) => true,
40 fn is_extern_crate(item: &ast::Item) -> bool {
42 ast::ItemKind::ExternCrate(..) => true,
47 pub struct FmtVisitor<'a> {
48 pub parse_session: &'a ParseSess,
49 pub codemap: &'a CodeMap,
50 pub buffer: StringBuffer,
51 pub last_pos: BytePos,
52 // FIXME: use an RAII util or closure for indenting
53 pub block_indent: Indent,
54 pub config: &'a Config,
55 pub is_if_else_block: bool,
58 impl<'a> FmtVisitor<'a> {
59 fn visit_stmt(&mut self, stmt: &ast::Stmt) {
61 "visit_stmt: {:?} {:?}",
62 self.codemap.lookup_char_pos(stmt.span.lo),
63 self.codemap.lookup_char_pos(stmt.span.hi)
67 ast::StmtKind::Item(ref item) => {
68 self.visit_item(item);
70 ast::StmtKind::Local(ref local) => {
71 let rewrite = if contains_skip(&local.attrs) {
76 Shape::indented(self.block_indent, self.config),
79 self.push_rewrite(stmt.span, rewrite);
81 ast::StmtKind::Expr(ref expr) => {
82 let rewrite = format_expr(
86 Shape::indented(self.block_indent, self.config),
88 let span = if expr.attrs.is_empty() {
91 mk_sp(expr.span().lo, stmt.span.hi)
93 self.push_rewrite(span, rewrite)
95 ast::StmtKind::Semi(ref expr) => {
96 let rewrite = stmt.rewrite(
98 Shape::indented(self.block_indent, self.config),
100 let span = if expr.attrs.is_empty() {
103 mk_sp(expr.span().lo, stmt.span.hi)
105 self.push_rewrite(span, rewrite)
107 ast::StmtKind::Mac(ref mac) => {
108 let (ref mac, _macro_style, ref attrs) = **mac;
109 if contains_skip(attrs) {
110 self.push_rewrite(mac.span, None);
112 self.visit_mac(mac, None, MacroPosition::Statement);
114 self.format_missing(stmt.span.hi);
119 pub fn visit_block(&mut self, b: &ast::Block, inner_attrs: Option<&[ast::Attribute]>) {
121 "visit_block: {:?} {:?}",
122 self.codemap.lookup_char_pos(b.span.lo),
123 self.codemap.lookup_char_pos(b.span.hi)
126 // Check if this block has braces.
127 let snippet = self.snippet(b.span);
128 let has_braces = snippet.starts_with('{') || snippet.starts_with("unsafe");
129 let brace_compensation = if has_braces { BytePos(1) } else { BytePos(0) };
131 self.last_pos = self.last_pos + brace_compensation;
132 self.block_indent = self.block_indent.block_indent(self.config);
133 self.buffer.push_str("{");
135 if self.config.remove_blank_lines_at_start_or_end_of_block() {
136 if let Some(first_stmt) = b.stmts.first() {
137 let attr_lo = inner_attrs
139 utils::inner_attributes(attrs)
141 .map(|attr| attr.span.lo)
144 // Attributes for an item in a statement position
145 // do not belong to the statement. (rust-lang/rust#34459)
146 if let ast::StmtKind::Item(ref item) = first_stmt.node {
149 first_stmt.attrs().first()
151 // Some stmts can have embedded attributes.
152 // e.g. `match { #![attr] ... }`
153 let attr_lo = attr.span.lo;
154 if attr_lo < first_stmt.span.lo {
163 self.snippet(mk_sp(self.last_pos, attr_lo.unwrap_or(first_stmt.span.lo)));
164 let len = CommentCodeSlices::new(&snippet).nth(0).and_then(
165 |(kind, _, s)| if kind == CodeCharKind::Normal {
171 if let Some(len) = len {
172 self.last_pos = self.last_pos + BytePos::from_usize(len);
177 // Format inner attributes if available.
178 if let Some(attrs) = inner_attrs {
179 self.visit_attrs(attrs, ast::AttrStyle::Inner);
182 for stmt in &b.stmts {
183 self.visit_stmt(stmt)
186 if !b.stmts.is_empty() {
187 if let Some(expr) = utils::stmt_expr(&b.stmts[b.stmts.len() - 1]) {
188 if utils::semicolon_for_expr(&self.get_context(), expr) {
189 self.buffer.push_str(";");
194 let mut remove_len = BytePos(0);
195 if self.config.remove_blank_lines_at_start_or_end_of_block() {
196 if let Some(stmt) = b.stmts.last() {
197 let snippet = self.snippet(mk_sp(
199 source!(self, b.span).hi - brace_compensation,
201 let len = CommentCodeSlices::new(&snippet)
203 .and_then(|(kind, _, s)| {
204 if kind == CodeCharKind::Normal && s.trim().is_empty() {
210 if let Some(len) = len {
211 remove_len = BytePos::from_usize(len);
216 let mut unindent_comment = self.is_if_else_block && !b.stmts.is_empty();
217 if unindent_comment {
218 let end_pos = source!(self, b.span).hi - brace_compensation - remove_len;
219 let snippet = self.get_context().snippet(mk_sp(self.last_pos, end_pos));
220 unindent_comment = snippet.contains("//") || snippet.contains("/*");
222 // FIXME: we should compress any newlines here to just one
223 if unindent_comment {
224 self.block_indent = self.block_indent.block_unindent(self.config);
226 self.format_missing_with_indent(source!(self, b.span).hi - brace_compensation - remove_len);
227 if unindent_comment {
228 self.block_indent = self.block_indent.block_indent(self.config);
230 self.close_block(unindent_comment);
231 self.last_pos = source!(self, b.span).hi;
234 // FIXME: this is a terrible hack to indent the comments between the last
235 // item in the block and the closing brace to the block's level.
236 // The closing brace itself, however, should be indented at a shallower
238 fn close_block(&mut self, unindent_comment: bool) {
239 let total_len = self.buffer.len;
240 let chars_too_many = if unindent_comment {
242 } else if self.config.hard_tabs() {
245 self.config.tab_spaces()
247 self.buffer.truncate(total_len - chars_too_many);
248 self.buffer.push_str("}");
249 self.block_indent = self.block_indent.block_unindent(self.config);
252 // Note that this only gets called for function definitions. Required methods
253 // on traits do not get handled here.
260 defaultness: ast::Defaultness,
261 inner_attrs: Option<&[ast::Attribute]>,
263 let indent = self.block_indent;
265 let rewrite = match fk {
266 visit::FnKind::ItemFn(ident, generics, unsafety, constness, abi, vis, b) => {
278 mk_sp(s.lo, b.span.lo),
282 visit::FnKind::Method(ident, sig, vis, b) => {
293 vis.unwrap_or(&ast::Visibility::Inherited),
294 mk_sp(s.lo, b.span.lo),
298 visit::FnKind::Closure(_) => unreachable!(),
301 if let Some(fn_str) = rewrite {
302 self.format_missing_with_indent(source!(self, s).lo);
303 self.buffer.push_str(&fn_str);
304 if let Some(c) = fn_str.chars().last() {
306 self.last_pos = source!(self, block.span).hi;
311 self.format_missing(source!(self, block.span).lo);
314 self.last_pos = source!(self, block.span).lo;
315 self.visit_block(block, inner_attrs)
318 pub fn visit_item(&mut self, item: &ast::Item) {
319 skip_out_of_file_lines_range_visitor!(self, item.span);
321 // This is where we bail out if there is a skip attribute. This is only
322 // complex in the module case. It is complex because the module could be
323 // in a separate file and there might be attributes in both files, but
324 // the AST lumps them all together.
325 let mut attrs = item.attrs.clone();
327 ast::ItemKind::Mod(ref m) => {
328 let outer_file = self.codemap.lookup_char_pos(item.span.lo).file;
329 let inner_file = self.codemap.lookup_char_pos(m.inner.lo).file;
330 if outer_file.name == inner_file.name {
331 // Module is inline, in this case we treat modules like any
333 if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
334 self.push_rewrite(item.span, None);
337 } else if utils::contains_skip(&item.attrs) {
338 // Module is not inline, but should be skipped.
341 // Module is not inline and should not be skipped. We want
342 // to process only the attributes in the current file.
343 let filterd_attrs = item.attrs
346 let attr_file = self.codemap.lookup_char_pos(a.span.lo).file;
347 if attr_file.name == outer_file.name {
353 .collect::<Vec<_>>();
354 // Assert because if we should skip it should be caught by
356 assert!(!self.visit_attrs(&filterd_attrs, ast::AttrStyle::Outer));
357 attrs = filterd_attrs;
360 _ => if self.visit_attrs(&item.attrs, ast::AttrStyle::Outer) {
361 self.push_rewrite(item.span, None);
367 ast::ItemKind::Use(ref vp) => {
368 self.format_import(&item.vis, vp, item.span, &item.attrs);
370 ast::ItemKind::Impl(..) => {
371 self.format_missing_with_indent(source!(self, item.span).lo);
372 let snippet = self.get_context().snippet(item.span);
373 let where_span_end = snippet
374 .find_uncommented("{")
375 .map(|x| (BytePos(x as u32)) + source!(self, item.span).lo);
376 if let Some(impl_str) =
377 format_impl(&self.get_context(), item, self.block_indent, where_span_end)
379 self.buffer.push_str(&impl_str);
380 self.last_pos = source!(self, item.span).hi;
383 ast::ItemKind::Trait(..) => {
384 self.format_missing_with_indent(item.span.lo);
385 if let Some(trait_str) = format_trait(&self.get_context(), item, self.block_indent)
387 self.buffer.push_str(&trait_str);
388 self.last_pos = source!(self, item.span).hi;
391 ast::ItemKind::ExternCrate(_) => {
392 self.format_missing_with_indent(source!(self, item.span).lo);
393 let new_str = self.snippet(item.span);
394 if contains_comment(&new_str) {
395 self.buffer.push_str(&new_str)
398 &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
400 .push_str(&Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"));
402 self.last_pos = source!(self, item.span).hi;
404 ast::ItemKind::Struct(ref def, ref generics) => {
406 let indent = self.block_indent;
407 let context = self.get_context();
408 ::items::format_struct(
418 ).map(|s| match *def {
419 ast::VariantData::Tuple(..) => s + ";",
423 self.push_rewrite(item.span, rewrite);
425 ast::ItemKind::Enum(ref def, ref generics) => {
426 self.format_missing_with_indent(source!(self, item.span).lo);
427 self.visit_enum(item.ident, &item.vis, def, generics, item.span);
428 self.last_pos = source!(self, item.span).hi;
430 ast::ItemKind::Mod(ref module) => {
431 self.format_missing_with_indent(source!(self, item.span).lo);
432 self.format_mod(module, &item.vis, item.span, item.ident, &attrs);
434 ast::ItemKind::Mac(ref mac) => {
435 self.visit_mac(mac, Some(item.ident), MacroPosition::Item);
437 ast::ItemKind::ForeignMod(ref foreign_mod) => {
438 self.format_missing_with_indent(source!(self, item.span).lo);
439 self.format_foreign_mod(foreign_mod, item.span);
441 ast::ItemKind::Static(ref ty, mutability, ref expr) => {
442 let rewrite = rewrite_static(
453 self.push_rewrite(item.span, rewrite);
455 ast::ItemKind::Const(ref ty, ref expr) => {
456 let rewrite = rewrite_static(
461 ast::Mutability::Immutable,
467 self.push_rewrite(item.span, rewrite);
469 ast::ItemKind::DefaultImpl(..) => {
470 // FIXME(#78): format impl definitions.
472 ast::ItemKind::Fn(ref decl, unsafety, constness, abi, ref generics, ref body) => {
474 visit::FnKind::ItemFn(
486 ast::Defaultness::Final,
490 ast::ItemKind::Ty(ref ty, ref generics) => {
491 let rewrite = rewrite_type_alias(
500 self.push_rewrite(item.span, rewrite);
502 ast::ItemKind::Union(ref def, ref generics) => {
503 let rewrite = ::items::format_struct_struct(
514 self.push_rewrite(item.span, rewrite);
516 ast::ItemKind::GlobalAsm(..) => {
517 let snippet = Some(self.snippet(item.span));
518 self.push_rewrite(item.span, snippet);
520 ast::ItemKind::MacroDef(..) => {
521 // FIXME(#1539): macros 2.0
522 let snippet = Some(self.snippet(item.span));
523 self.push_rewrite(item.span, snippet);
528 pub fn visit_trait_item(&mut self, ti: &ast::TraitItem) {
529 skip_out_of_file_lines_range_visitor!(self, ti.span);
531 if self.visit_attrs(&ti.attrs, ast::AttrStyle::Outer) {
532 self.push_rewrite(ti.span, None);
537 ast::TraitItemKind::Const(ref ty, ref expr_opt) => {
538 let rewrite = rewrite_static(
540 &ast::Visibility::Inherited,
543 ast::Mutability::Immutable,
549 self.push_rewrite(ti.span, rewrite);
551 ast::TraitItemKind::Method(ref sig, None) => {
552 let indent = self.block_indent;
553 let rewrite = self.rewrite_required_fn(indent, ti.ident, sig, ti.span);
554 self.push_rewrite(ti.span, rewrite);
556 ast::TraitItemKind::Method(ref sig, Some(ref body)) => {
558 visit::FnKind::Method(ti.ident, sig, None, body),
562 ast::Defaultness::Final,
566 ast::TraitItemKind::Type(ref type_param_bounds, ref type_default) => {
567 let rewrite = rewrite_associated_type(
569 type_default.as_ref(),
570 Some(type_param_bounds),
574 self.push_rewrite(ti.span, rewrite);
576 ast::TraitItemKind::Macro(ref mac) => {
577 self.visit_mac(mac, Some(ti.ident), MacroPosition::Item);
582 pub fn visit_impl_item(&mut self, ii: &ast::ImplItem) {
583 skip_out_of_file_lines_range_visitor!(self, ii.span);
585 if self.visit_attrs(&ii.attrs, ast::AttrStyle::Outer) {
586 self.push_rewrite(ii.span, None);
591 ast::ImplItemKind::Method(ref sig, ref body) => {
593 visit::FnKind::Method(ii.ident, sig, Some(&ii.vis), body),
601 ast::ImplItemKind::Const(ref ty, ref expr) => {
602 let rewrite = rewrite_static(
607 ast::Mutability::Immutable,
613 self.push_rewrite(ii.span, rewrite);
615 ast::ImplItemKind::Type(ref ty) => {
616 let rewrite = rewrite_associated_impl_type(
624 self.push_rewrite(ii.span, rewrite);
626 ast::ImplItemKind::Macro(ref mac) => {
627 self.visit_mac(mac, Some(ii.ident), MacroPosition::Item);
632 fn visit_mac(&mut self, mac: &ast::Mac, ident: Option<ast::Ident>, pos: MacroPosition) {
633 skip_out_of_file_lines_range_visitor!(self, mac.span);
636 let shape = Shape::indented(self.block_indent, self.config)
639 let rewrite = rewrite_macro(mac, ident, &self.get_context(), shape, pos);
640 self.push_rewrite(mac.span, rewrite);
643 fn push_rewrite(&mut self, span: Span, rewrite: Option<String>) {
644 self.format_missing_with_indent(source!(self, span).lo);
645 let result = rewrite.unwrap_or_else(|| self.snippet(span));
646 self.buffer.push_str(&result);
647 self.last_pos = source!(self, span).hi;
650 pub fn from_codemap(parse_session: &'a ParseSess, config: &'a Config) -> FmtVisitor<'a> {
652 parse_session: parse_session,
653 codemap: parse_session.codemap(),
654 buffer: StringBuffer::new(),
655 last_pos: BytePos(0),
656 block_indent: Indent::empty(),
658 is_if_else_block: false,
662 pub fn snippet(&self, span: Span) -> String {
663 match self.codemap.span_to_snippet(span) {
667 "Couldn't make snippet for span {:?}->{:?}",
668 self.codemap.lookup_char_pos(span.lo),
669 self.codemap.lookup_char_pos(span.hi)
676 // Returns true if we should skip the following item.
677 pub fn visit_attrs(&mut self, attrs: &[ast::Attribute], style: ast::AttrStyle) -> bool {
678 if utils::contains_skip(attrs) {
682 let attrs: Vec<_> = attrs.iter().filter(|a| a.style == style).cloned().collect();
683 if attrs.is_empty() {
687 let rewrite = attrs.rewrite(
689 Shape::indented(self.block_indent, self.config),
691 let span = mk_sp(attrs[0].span.lo, attrs[attrs.len() - 1].span.hi);
692 self.push_rewrite(span, rewrite);
699 items_left: &[ptr::P<ast::Item>],
704 F: Fn(&ast::Item) -> bool,
706 let mut last = self.codemap.lookup_line_range(items_left[0].span());
707 let item_length = items_left
710 is_item(&***ppi) && (!in_group || {
711 let current = self.codemap.lookup_line_range(ppi.span());
712 let in_same_group = current.lo < last.hi + 2;
718 let items = &items_left[..item_length];
720 let at_least_one_in_file_lines = items
722 .any(|item| !out_of_file_lines_range!(self, item.span));
724 if at_least_one_in_file_lines {
725 self.format_imports(items);
728 self.push_rewrite(item.span, None);
735 fn walk_mod_items(&mut self, m: &ast::Mod) {
736 let mut items_left: &[ptr::P<ast::Item>] = &m.items;
737 while !items_left.is_empty() {
738 // If the next item is a `use` declaration, then extract it and any subsequent `use`s
739 // to be potentially reordered within `format_imports`. Otherwise, just format the
740 // next item for output.
741 if self.config.reorder_imports() && is_use_item(&*items_left[0]) {
742 let used_items_len = self.reorder_items(
745 self.config.reorder_imports_in_group(),
747 let (_, rest) = items_left.split_at(used_items_len);
749 } else if self.config.reorder_extern_crates() && is_extern_crate(&*items_left[0]) {
750 let used_items_len = self.reorder_items(
753 self.config.reorder_extern_crates_in_group(),
755 let (_, rest) = items_left.split_at(used_items_len);
758 // `unwrap()` is safe here because we know `items_left`
759 // has elements from the loop condition
760 let (item, rest) = items_left.split_first().unwrap();
761 self.visit_item(item);
770 vis: &ast::Visibility,
773 attrs: &[ast::Attribute],
775 // Decide whether this is an inline mod or an external mod.
776 let local_file_name = self.codemap.span_to_filename(s);
777 let inner_span = source!(self, m.inner);
778 let is_internal = !(inner_span.lo.0 == 0 && inner_span.hi.0 == 0) &&
779 local_file_name == self.codemap.span_to_filename(inner_span);
781 self.buffer.push_str(&*utils::format_visibility(vis));
782 self.buffer.push_str("mod ");
783 self.buffer.push_str(&ident.to_string());
786 match self.config.item_brace_style() {
787 BraceStyle::AlwaysNextLine => self.buffer
788 .push_str(&format!("\n{}{{", self.block_indent.to_string(self.config))),
789 _ => self.buffer.push_str(" {"),
791 // Hackery to account for the closing }.
792 let mod_lo = self.codemap.span_after(source!(self, s), "{");
793 let body_snippet = self.snippet(mk_sp(mod_lo, source!(self, m.inner).hi - BytePos(1)));
794 let body_snippet = body_snippet.trim();
795 if body_snippet.is_empty() {
796 self.buffer.push_str("}");
798 self.last_pos = mod_lo;
799 self.block_indent = self.block_indent.block_indent(self.config);
800 self.visit_attrs(attrs, ast::AttrStyle::Inner);
801 self.walk_mod_items(m);
802 self.format_missing_with_indent(source!(self, m.inner).hi - BytePos(1));
803 self.close_block(false);
805 self.last_pos = source!(self, m.inner).hi;
807 self.buffer.push_str(";");
808 self.last_pos = source!(self, s).hi;
812 pub fn format_separate_mod(&mut self, m: &ast::Mod, filemap: &codemap::FileMap) {
813 self.block_indent = Indent::empty();
814 self.walk_mod_items(m);
815 self.format_missing_with_indent(filemap.end_pos);
818 pub fn get_context(&self) -> RewriteContext {
820 parse_session: self.parse_session,
821 codemap: self.codemap,
825 is_if_else_block: false,
826 force_one_line_chain: false,
831 impl Rewrite for ast::NestedMetaItem {
832 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
834 ast::NestedMetaItemKind::MetaItem(ref meta_item) => meta_item.rewrite(context, shape),
835 ast::NestedMetaItemKind::Literal(..) => Some(context.snippet(self.span)),
840 impl Rewrite for ast::MetaItem {
841 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
842 Some(match self.node {
843 ast::MetaItemKind::Word => String::from(&*self.name.as_str()),
844 ast::MetaItemKind::List(ref list) => {
845 let name = self.name.as_str();
846 // 3 = `#[` and `(`, 2 = `]` and `)`
847 let item_shape = try_opt!(
849 .shrink_left(name.len() + 3)
850 .and_then(|s| s.sub_width(2))
852 let items = itemize_list(
856 |nested_meta_item| nested_meta_item.span.lo,
857 |nested_meta_item| nested_meta_item.span.hi,
858 |nested_meta_item| nested_meta_item.rewrite(context, item_shape),
863 let item_vec = items.collect::<Vec<_>>();
864 let fmt = ListFormatting {
865 tactic: DefinitiveListTactic::Mixed,
867 trailing_separator: SeparatorTactic::Never,
869 ends_with_newline: false,
870 preserve_newline: false,
871 config: context.config,
873 format!("{}({})", name, try_opt!(write_list(&item_vec, &fmt)))
875 ast::MetaItemKind::NameValue(ref literal) => {
876 let name = self.name.as_str();
877 let value = context.snippet(literal.span);
878 if &*name == "doc" && contains_comment(&value) {
879 let doc_shape = Shape {
880 width: cmp::min(shape.width, context.config.comment_width())
881 .checked_sub(shape.indent.width())
887 try_opt!(rewrite_comment(&value, false, doc_shape, context.config))
890 format!("{} = {}", name, value)
897 impl Rewrite for ast::Attribute {
898 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
899 try_opt!(self.meta())
900 .rewrite(context, shape)
901 .map(|rw| if self.is_sugared_doc {
904 let original = context.snippet(self.span);
905 let prefix = match self.style {
906 ast::AttrStyle::Inner => "#!",
907 ast::AttrStyle::Outer => "#",
909 if contains_comment(&original) {
912 format!("{}[{}]", prefix, rw)
918 impl<'a> Rewrite for [ast::Attribute] {
919 fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
920 let mut result = String::new();
924 let indent = shape.indent.to_string(context.config);
926 for (i, a) in self.iter().enumerate() {
927 let a_str = try_opt!(a.rewrite(context, shape));
929 // Write comments and blank lines between attributes.
931 let comment = context.snippet(mk_sp(self[i - 1].span.hi, a.span.lo));
932 // This particular horror show is to preserve line breaks in between doc
933 // comments. An alternative would be to force such line breaks to start
934 // with the usual doc comment token.
935 let multi_line = a_str.starts_with("//") && comment.matches('\n').count() > 1;
936 let comment = comment.trim();
937 if !comment.is_empty() {
938 let comment = try_opt!(rewrite_comment(
942 context.config.comment_width() - shape.indent.width(),
947 result.push_str(&indent);
948 result.push_str(&comment);
950 } else if multi_line {
953 result.push_str(&indent);
956 // Write the attribute itself.
957 result.push_str(&a_str);
959 if i < self.len() - 1 {