2015-04-20 23:47:15 -05:00
|
|
|
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2015-05-24 18:03:26 -05:00
|
|
|
// Formatting top-level items - functions, structs, enums, traits, impls.
|
|
|
|
|
2017-01-30 13:28:48 -06:00
|
|
|
use {Indent, Shape};
|
2016-05-25 13:41:26 -05:00
|
|
|
use codemap::SpanUtils;
|
|
|
|
use utils::{format_mutability, format_visibility, contains_skip, end_typaram, wrap_str,
|
2017-03-09 19:29:45 -06:00
|
|
|
last_line_width, format_unsafety, trim_newlines, stmt_expr, semicolon_for_expr,
|
|
|
|
trimmed_last_line_width};
|
2017-03-09 20:30:47 -06:00
|
|
|
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, list_helper,
|
2016-05-18 15:38:49 -05:00
|
|
|
DefinitiveListTactic, ListTactic, definitive_tactic, format_item_list};
|
2016-08-03 23:17:47 -05:00
|
|
|
use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, type_annotation_separator};
|
2015-11-23 22:54:44 -06:00
|
|
|
use comment::{FindUncommented, contains_comment};
|
2015-04-21 04:01:19 -05:00
|
|
|
use visitor::FmtVisitor;
|
2015-09-08 13:56:33 -05:00
|
|
|
use rewrite::{Rewrite, RewriteContext};
|
2017-03-19 17:42:20 -05:00
|
|
|
use config::{Config, IndentStyle, Density, ReturnIndent, BraceStyle, Style};
|
2016-06-06 16:30:40 -05:00
|
|
|
use itertools::Itertools;
|
2015-06-23 08:58:58 -05:00
|
|
|
|
2016-12-23 13:13:00 -06:00
|
|
|
use syntax::{ast, abi, codemap, ptr, symbol};
|
2015-11-19 01:53:25 -06:00
|
|
|
use syntax::codemap::{Span, BytePos, mk_sp};
|
2016-01-09 23:45:58 -06:00
|
|
|
use syntax::ast::ImplItem;
|
2015-04-20 23:47:15 -05:00
|
|
|
|
2015-10-24 05:19:58 -05:00
|
|
|
// Statements of the form
|
|
|
|
// let pat: ty = init;
|
|
|
|
impl Rewrite for ast::Local {
|
2017-01-30 13:28:48 -06:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
|
|
|
debug!("Local::rewrite {:?} {} {:?}",
|
|
|
|
self,
|
|
|
|
shape.width,
|
|
|
|
shape.indent);
|
2015-10-24 05:19:58 -05:00
|
|
|
let mut result = "let ".to_owned();
|
|
|
|
|
2017-03-20 17:23:59 -05:00
|
|
|
let pat_shape = try_opt!(shape.offset_left(result.len()));
|
|
|
|
// 1 = ;
|
|
|
|
let pat_shape = try_opt!(pat_shape.sub_width(1));
|
|
|
|
let pat_str = try_opt!(self.pat.rewrite(&context, pat_shape));
|
2015-10-24 05:19:58 -05:00
|
|
|
result.push_str(&pat_str);
|
|
|
|
|
|
|
|
// String that is placed within the assignment pattern and expression.
|
|
|
|
let infix = {
|
|
|
|
let mut infix = String::new();
|
|
|
|
|
|
|
|
if let Some(ref ty) = self.ty {
|
2016-08-03 23:17:47 -05:00
|
|
|
let separator = type_annotation_separator(context.config);
|
2017-01-30 13:28:48 -06:00
|
|
|
let indent = shape.indent + last_line_width(&result) + separator.len();
|
2015-10-24 05:19:58 -05:00
|
|
|
// 1 = ;
|
2017-01-30 13:28:48 -06:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(indent.width() + 1));
|
|
|
|
let rewrite = try_opt!(ty.rewrite(context, Shape::legacy(budget, indent)));
|
2015-10-24 05:19:58 -05:00
|
|
|
|
2016-08-02 20:07:56 -05:00
|
|
|
infix.push_str(separator);
|
2015-10-24 05:19:58 -05:00
|
|
|
infix.push_str(&rewrite);
|
2015-08-21 06:31:09 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 05:19:58 -05:00
|
|
|
if self.init.is_some() {
|
|
|
|
infix.push_str(" =");
|
2015-08-21 06:31:09 -05:00
|
|
|
}
|
2015-10-24 05:19:58 -05:00
|
|
|
|
|
|
|
infix
|
2015-08-21 06:31:09 -05:00
|
|
|
};
|
|
|
|
|
2015-10-24 05:19:58 -05:00
|
|
|
result.push_str(&infix);
|
|
|
|
|
|
|
|
if let Some(ref ex) = self.init {
|
2017-01-26 14:14:26 -06:00
|
|
|
// 1 = trailing semicolon;
|
2017-03-20 17:23:59 -05:00
|
|
|
//let budget = try_opt!(shape.width.checked_sub(shape.indent.block_only().width() + 1));
|
|
|
|
let nested_shape = try_opt!(shape.sub_width(1));
|
2015-10-24 05:19:58 -05:00
|
|
|
|
2017-03-21 15:05:50 -05:00
|
|
|
result = try_opt!(rewrite_assign_rhs(&context, result, ex, nested_shape));
|
2015-10-24 05:19:58 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
result.push(';');
|
|
|
|
Some(result)
|
2015-08-21 06:31:09 -05:00
|
|
|
}
|
2015-10-24 05:19:58 -05:00
|
|
|
}
|
2015-08-21 06:31:09 -05:00
|
|
|
|
2017-01-12 19:07:42 -06:00
|
|
|
// TODO convert to using rewrite style rather than visitor
|
|
|
|
// TODO format modules in this style
|
2017-01-26 12:42:34 -06:00
|
|
|
#[allow(dead_code)]
|
2017-01-12 19:07:42 -06:00
|
|
|
struct Item<'a> {
|
|
|
|
keyword: &'static str,
|
|
|
|
abi: String,
|
|
|
|
vis: Option<&'a ast::Visibility>,
|
|
|
|
body: Vec<BodyElement<'a>>,
|
|
|
|
span: Span,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Item<'a> {
|
|
|
|
fn from_foreign_mod(fm: &'a ast::ForeignMod, span: Span, config: &Config) -> Item<'a> {
|
|
|
|
let abi = if fm.abi == abi::Abi::C && !config.force_explicit_abi {
|
|
|
|
"extern".into()
|
|
|
|
} else {
|
|
|
|
format!("extern {}", fm.abi)
|
|
|
|
};
|
|
|
|
Item {
|
|
|
|
keyword: "",
|
|
|
|
abi: abi,
|
|
|
|
vis: None,
|
2017-03-06 14:40:08 -06:00
|
|
|
body: fm.items
|
|
|
|
.iter()
|
|
|
|
.map(|i| BodyElement::ForeignItem(i))
|
|
|
|
.collect(),
|
2017-01-12 19:07:42 -06:00
|
|
|
span: span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum BodyElement<'a> {
|
|
|
|
// Stmt(&'a ast::Stmt),
|
|
|
|
// Field(&'a ast::Field),
|
|
|
|
// Variant(&'a ast::Variant),
|
|
|
|
// Item(&'a ast::Item),
|
|
|
|
ForeignItem(&'a ast::ForeignItem),
|
|
|
|
}
|
|
|
|
|
2015-10-24 05:19:58 -05:00
|
|
|
impl<'a> FmtVisitor<'a> {
|
2017-01-12 19:07:42 -06:00
|
|
|
fn format_item(&mut self, item: Item) {
|
|
|
|
self.buffer.push_str(&item.abi);
|
|
|
|
self.buffer.push_str(" ");
|
2015-09-21 13:02:45 -05:00
|
|
|
|
2017-01-12 19:07:42 -06:00
|
|
|
let snippet = self.snippet(item.span);
|
2016-03-14 00:55:55 -05:00
|
|
|
let brace_pos = snippet.find_uncommented("{").unwrap();
|
|
|
|
|
2016-08-23 07:00:43 -05:00
|
|
|
self.buffer.push_str("{");
|
2017-01-12 19:07:42 -06:00
|
|
|
if !item.body.is_empty() || contains_comment(&snippet[brace_pos..]) {
|
2016-03-14 00:55:55 -05:00
|
|
|
// FIXME: this skips comments between the extern keyword and the opening
|
|
|
|
// brace.
|
2017-01-12 19:07:42 -06:00
|
|
|
self.last_pos = item.span.lo + BytePos(brace_pos as u32 + 1);
|
2016-03-14 00:55:55 -05:00
|
|
|
self.block_indent = self.block_indent.block_indent(self.config);
|
|
|
|
|
2017-01-12 19:07:42 -06:00
|
|
|
if item.body.is_empty() {
|
|
|
|
self.format_missing_no_indent(item.span.hi - BytePos(1));
|
2016-08-23 07:00:43 -05:00
|
|
|
self.block_indent = self.block_indent.block_unindent(self.config);
|
|
|
|
|
2017-03-27 17:14:47 -05:00
|
|
|
self.buffer
|
|
|
|
.push_str(&self.block_indent.to_string(self.config));
|
2016-08-23 07:00:43 -05:00
|
|
|
} else {
|
2017-01-12 19:07:42 -06:00
|
|
|
for item in &item.body {
|
|
|
|
self.format_body_element(item);
|
2016-08-23 07:00:43 -05:00
|
|
|
}
|
2015-09-21 13:02:45 -05:00
|
|
|
|
2016-08-23 07:00:43 -05:00
|
|
|
self.block_indent = self.block_indent.block_unindent(self.config);
|
2017-01-12 19:07:42 -06:00
|
|
|
self.format_missing_with_indent(item.span.hi - BytePos(1));
|
2016-08-23 07:00:43 -05:00
|
|
|
}
|
2015-09-21 13:02:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
self.buffer.push_str("}");
|
2017-01-12 19:07:42 -06:00
|
|
|
self.last_pos = item.span.hi;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_body_element(&mut self, element: &BodyElement) {
|
|
|
|
match *element {
|
|
|
|
BodyElement::ForeignItem(ref item) => self.format_foreign_item(item),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
|
|
|
|
let item = Item::from_foreign_mod(fm, span, self.config);
|
|
|
|
self.format_item(item);
|
2015-09-21 13:02:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
|
|
|
|
self.format_missing_with_indent(item.span.lo);
|
|
|
|
// Drop semicolon or it will be interpreted as comment.
|
|
|
|
// FIXME: this may be a faulty span from libsyntax.
|
2015-10-18 17:25:38 -05:00
|
|
|
let span = mk_sp(item.span.lo, item.span.hi - BytePos(1));
|
2015-09-21 13:02:45 -05:00
|
|
|
|
|
|
|
match item.node {
|
2016-03-01 16:27:19 -06:00
|
|
|
ast::ForeignItemKind::Fn(ref fn_decl, ref generics) => {
|
2015-09-21 13:02:45 -05:00
|
|
|
let indent = self.block_indent;
|
2015-11-22 06:45:51 -06:00
|
|
|
let rewrite = rewrite_fn_base(&self.get_context(),
|
|
|
|
indent,
|
|
|
|
item.ident,
|
|
|
|
fn_decl,
|
|
|
|
generics,
|
|
|
|
ast::Unsafety::Normal,
|
|
|
|
ast::Constness::NotConst,
|
2016-05-31 12:48:49 -05:00
|
|
|
ast::Defaultness::Final,
|
2015-11-22 06:45:51 -06:00
|
|
|
// These are not actually rust functions,
|
|
|
|
// but we format them as such.
|
|
|
|
abi::Abi::Rust,
|
2016-05-01 22:01:46 -05:00
|
|
|
&item.vis,
|
2015-11-22 06:45:51 -06:00
|
|
|
span,
|
|
|
|
false,
|
2017-03-12 17:42:41 -05:00
|
|
|
false,
|
2015-11-22 06:45:51 -06:00
|
|
|
false);
|
2015-09-21 13:02:45 -05:00
|
|
|
|
|
|
|
match rewrite {
|
2015-10-08 22:07:14 -05:00
|
|
|
Some((new_fn, _)) => {
|
2015-09-21 13:02:45 -05:00
|
|
|
self.buffer.push_str(&new_fn);
|
|
|
|
self.buffer.push_str(";");
|
|
|
|
}
|
|
|
|
None => self.format_missing(item.span.hi),
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
ast::ForeignItemKind::Static(ref ty, is_mutable) => {
|
2015-09-21 13:02:45 -05:00
|
|
|
// FIXME(#21): we're dropping potential comments in between the
|
|
|
|
// function keywords here.
|
2016-05-27 17:58:25 -05:00
|
|
|
let vis = format_visibility(&item.vis);
|
2016-05-29 10:58:38 -05:00
|
|
|
let mut_str = if is_mutable { "mut " } else { "" };
|
2016-05-02 00:05:23 -05:00
|
|
|
let prefix = format!("{}static {}{}: ", vis, mut_str, item.ident);
|
2015-09-21 13:02:45 -05:00
|
|
|
let offset = self.block_indent + prefix.len();
|
|
|
|
// 1 = ;
|
|
|
|
let width = self.config.max_width - offset.width() - 1;
|
2017-01-30 13:28:48 -06:00
|
|
|
let rewrite = ty.rewrite(&self.get_context(), Shape::legacy(width, offset));
|
2015-09-21 13:02:45 -05:00
|
|
|
|
|
|
|
match rewrite {
|
|
|
|
Some(result) => {
|
|
|
|
self.buffer.push_str(&prefix);
|
|
|
|
self.buffer.push_str(&result);
|
|
|
|
self.buffer.push_str(";");
|
|
|
|
}
|
|
|
|
None => self.format_missing(item.span.hi),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.last_pos = item.span.hi;
|
|
|
|
}
|
|
|
|
|
2015-04-20 23:47:15 -05:00
|
|
|
pub fn rewrite_fn(&mut self,
|
2015-09-06 00:39:28 -05:00
|
|
|
indent: Indent,
|
2015-04-20 23:47:15 -05:00
|
|
|
ident: ast::Ident,
|
|
|
|
fd: &ast::FnDecl,
|
|
|
|
generics: &ast::Generics,
|
2015-09-21 13:02:45 -05:00
|
|
|
unsafety: ast::Unsafety,
|
|
|
|
constness: ast::Constness,
|
2016-05-31 12:48:49 -05:00
|
|
|
defaultness: ast::Defaultness,
|
2015-09-21 13:02:45 -05:00
|
|
|
abi: abi::Abi,
|
2016-05-01 22:01:46 -05:00
|
|
|
vis: &ast::Visibility,
|
2015-11-19 20:49:24 -06:00
|
|
|
span: Span,
|
|
|
|
block: &ast::Block)
|
2015-09-04 11:09:05 -05:00
|
|
|
-> Option<String> {
|
2015-11-22 06:45:51 -06:00
|
|
|
let mut newline_brace = newline_for_brace(self.config, &generics.where_clause);
|
|
|
|
let context = self.get_context();
|
|
|
|
|
2016-01-10 23:06:06 -06:00
|
|
|
let block_snippet = self.snippet(codemap::mk_sp(block.span.lo, block.span.hi));
|
2017-03-27 17:14:47 -05:00
|
|
|
let has_body = !block_snippet[1..block_snippet.len() - 1]
|
|
|
|
.trim()
|
|
|
|
.is_empty() ||
|
2016-01-11 15:47:56 -06:00
|
|
|
!context.config.fn_empty_single_line;
|
2016-01-10 23:06:06 -06:00
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
let (mut result, force_newline_brace) = try_opt!(rewrite_fn_base(&context,
|
|
|
|
indent,
|
|
|
|
ident,
|
|
|
|
fd,
|
|
|
|
generics,
|
|
|
|
unsafety,
|
|
|
|
constness,
|
2016-05-31 12:48:49 -05:00
|
|
|
defaultness,
|
2015-11-22 06:45:51 -06:00
|
|
|
abi,
|
|
|
|
vis,
|
|
|
|
span,
|
|
|
|
newline_brace,
|
2017-03-12 17:42:41 -05:00
|
|
|
has_body,
|
|
|
|
true));
|
2015-05-03 17:12:39 -05:00
|
|
|
|
2015-09-01 02:36:00 -05:00
|
|
|
if self.config.fn_brace_style != BraceStyle::AlwaysNextLine && !result.contains('\n') {
|
|
|
|
newline_brace = false;
|
2015-10-08 22:07:14 -05:00
|
|
|
} else if force_newline_brace {
|
|
|
|
newline_brace = true;
|
2015-09-01 02:36:00 -05:00
|
|
|
}
|
|
|
|
|
2015-08-21 06:31:09 -05:00
|
|
|
// Prepare for the function body by possibly adding a newline and
|
|
|
|
// indent.
|
|
|
|
// FIXME we'll miss anything between the end of the signature and the
|
|
|
|
// start of the body, but we need more spans from the compiler to solve
|
|
|
|
// this.
|
2015-05-03 17:12:39 -05:00
|
|
|
if newline_brace {
|
|
|
|
result.push('\n');
|
2015-09-18 23:50:44 -05:00
|
|
|
result.push_str(&indent.to_string(self.config));
|
2015-05-03 17:12:39 -05:00
|
|
|
} else {
|
|
|
|
result.push(' ');
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:14:47 -05:00
|
|
|
self.single_line_fn(&result, block)
|
|
|
|
.or_else(|| Some(result))
|
2015-05-03 17:12:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rewrite_required_fn(&mut self,
|
2015-09-06 00:39:28 -05:00
|
|
|
indent: Indent,
|
2015-05-03 17:12:39 -05:00
|
|
|
ident: ast::Ident,
|
|
|
|
sig: &ast::MethodSig,
|
|
|
|
span: Span)
|
2015-09-04 11:09:05 -05:00
|
|
|
-> Option<String> {
|
2015-12-01 15:09:37 -06:00
|
|
|
// Drop semicolon or it will be interpreted as comment.
|
2015-10-18 17:25:38 -05:00
|
|
|
let span = mk_sp(span.lo, span.hi - BytePos(1));
|
2015-11-22 06:45:51 -06:00
|
|
|
let context = self.get_context();
|
2015-05-03 17:12:39 -05:00
|
|
|
|
2015-12-01 15:09:37 -06:00
|
|
|
let (mut result, _) = try_opt!(rewrite_fn_base(&context,
|
|
|
|
indent,
|
|
|
|
ident,
|
|
|
|
&sig.decl,
|
|
|
|
&sig.generics,
|
|
|
|
sig.unsafety,
|
2016-09-15 22:19:18 -05:00
|
|
|
sig.constness.node,
|
2016-05-31 12:48:49 -05:00
|
|
|
ast::Defaultness::Final,
|
2015-12-01 15:09:37 -06:00
|
|
|
sig.abi,
|
2016-05-01 22:01:46 -05:00
|
|
|
&ast::Visibility::Inherited,
|
2015-12-01 15:09:37 -06:00
|
|
|
span,
|
|
|
|
false,
|
2017-03-12 17:42:41 -05:00
|
|
|
false,
|
2015-12-01 15:09:37 -06:00
|
|
|
false));
|
2015-05-03 17:12:39 -05:00
|
|
|
|
|
|
|
// Re-attach semicolon
|
|
|
|
result.push(';');
|
|
|
|
|
2015-09-04 11:09:05 -05:00
|
|
|
Some(result)
|
2015-05-03 17:12:39 -05:00
|
|
|
}
|
|
|
|
|
2015-11-19 20:49:24 -06:00
|
|
|
fn single_line_fn(&self, fn_str: &str, block: &ast::Block) -> Option<String> {
|
2015-11-19 20:11:32 -06:00
|
|
|
if fn_str.contains('\n') {
|
|
|
|
return None;
|
|
|
|
}
|
2015-11-17 22:53:06 -06:00
|
|
|
|
|
|
|
let codemap = self.get_context().codemap;
|
|
|
|
|
2015-11-19 20:11:32 -06:00
|
|
|
if self.config.fn_empty_single_line && is_empty_block(block, codemap) &&
|
|
|
|
self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width {
|
|
|
|
return Some(format!("{}{{}}", fn_str));
|
2015-11-17 22:53:06 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if self.config.fn_single_line && is_simple_block_stmt(block, codemap) {
|
|
|
|
let rewrite = {
|
2016-09-15 22:19:18 -05:00
|
|
|
if let Some(ref stmt) = block.stmts.first() {
|
|
|
|
match stmt_expr(stmt) {
|
|
|
|
Some(e) => {
|
|
|
|
let suffix = if semicolon_for_expr(e) { ";" } else { "" };
|
|
|
|
|
|
|
|
e.rewrite(&self.get_context(),
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(self.config.max_width -
|
|
|
|
self.block_indent.width(),
|
|
|
|
self.block_indent))
|
2016-09-15 22:19:18 -05:00
|
|
|
.map(|s| s + suffix)
|
|
|
|
.or_else(|| Some(self.snippet(e.span)))
|
|
|
|
}
|
|
|
|
None => {
|
|
|
|
stmt.rewrite(&self.get_context(),
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(self.config.max_width -
|
|
|
|
self.block_indent.width(),
|
|
|
|
self.block_indent))
|
2016-09-15 22:19:18 -05:00
|
|
|
}
|
|
|
|
}
|
2015-11-17 22:53:06 -06:00
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(res) = rewrite {
|
2015-11-19 20:11:32 -06:00
|
|
|
let width = self.block_indent.width() + fn_str.len() + res.len() + 4;
|
2015-11-17 22:53:06 -06:00
|
|
|
if !res.contains('\n') && width <= self.config.max_width {
|
|
|
|
return Some(format!("{}{{ {} }}", fn_str, res));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2015-05-29 05:41:26 -05:00
|
|
|
pub fn visit_enum(&mut self,
|
|
|
|
ident: ast::Ident,
|
2016-05-01 22:01:46 -05:00
|
|
|
vis: &ast::Visibility,
|
2015-05-29 05:41:26 -05:00
|
|
|
enum_def: &ast::EnumDef,
|
|
|
|
generics: &ast::Generics,
|
2015-07-03 04:13:28 -05:00
|
|
|
span: Span) {
|
2017-03-27 17:25:59 -05:00
|
|
|
self.buffer.push_str(&format_header("enum ", ident, vis));
|
2015-05-29 05:41:26 -05:00
|
|
|
|
|
|
|
let enum_snippet = self.snippet(span);
|
2016-08-23 07:00:43 -05:00
|
|
|
let brace_pos = enum_snippet.find_uncommented("{").unwrap();
|
|
|
|
let body_start = span.lo + BytePos(brace_pos as u32 + 1);
|
2015-11-22 06:45:51 -06:00
|
|
|
let generics_str = format_generics(&self.get_context(),
|
|
|
|
generics,
|
|
|
|
"{",
|
|
|
|
"{",
|
|
|
|
self.config.item_brace_style,
|
|
|
|
enum_def.variants.is_empty(),
|
|
|
|
self.block_indent,
|
|
|
|
self.block_indent.block_indent(self.config),
|
|
|
|
mk_sp(span.lo, body_start))
|
2017-02-21 21:20:50 -06:00
|
|
|
.unwrap();
|
2015-07-26 07:05:43 -05:00
|
|
|
self.buffer.push_str(&generics_str);
|
2015-05-29 05:41:26 -05:00
|
|
|
|
|
|
|
self.last_pos = body_start;
|
|
|
|
|
2015-10-07 18:23:07 -05:00
|
|
|
self.block_indent = self.block_indent.block_indent(self.config);
|
|
|
|
let variant_list = self.format_variant_list(enum_def, body_start, span.hi - BytePos(1));
|
|
|
|
match variant_list {
|
2016-08-23 09:14:45 -05:00
|
|
|
Some(ref body_str) => self.buffer.push_str(body_str),
|
2016-08-23 07:00:43 -05:00
|
|
|
None => {
|
|
|
|
if contains_comment(&enum_snippet[brace_pos..]) {
|
|
|
|
self.format_missing_no_indent(span.hi - BytePos(1))
|
|
|
|
} else {
|
|
|
|
self.format_missing(span.hi - BytePos(1))
|
|
|
|
}
|
|
|
|
}
|
2015-05-29 05:41:26 -05:00
|
|
|
}
|
2015-09-18 23:50:44 -05:00
|
|
|
self.block_indent = self.block_indent.block_unindent(self.config);
|
2015-05-29 05:41:26 -05:00
|
|
|
|
2016-08-23 07:00:43 -05:00
|
|
|
if variant_list.is_some() || contains_comment(&enum_snippet[brace_pos..]) {
|
2017-03-27 17:14:47 -05:00
|
|
|
self.buffer
|
|
|
|
.push_str(&self.block_indent.to_string(self.config));
|
2015-10-07 18:23:07 -05:00
|
|
|
}
|
2015-07-26 07:05:43 -05:00
|
|
|
self.buffer.push_str("}");
|
2015-10-07 18:23:07 -05:00
|
|
|
self.last_pos = span.hi;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the body of an enum definition
|
|
|
|
fn format_variant_list(&self,
|
|
|
|
enum_def: &ast::EnumDef,
|
|
|
|
body_lo: BytePos,
|
|
|
|
body_hi: BytePos)
|
|
|
|
-> Option<String> {
|
|
|
|
if enum_def.variants.is_empty() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let mut result = String::with_capacity(1024);
|
|
|
|
result.push('\n');
|
|
|
|
let indentation = self.block_indent.to_string(self.config);
|
|
|
|
result.push_str(&indentation);
|
|
|
|
|
|
|
|
let items = itemize_list(self.codemap,
|
|
|
|
enum_def.variants.iter(),
|
|
|
|
"}",
|
2016-11-20 13:37:35 -06:00
|
|
|
|f| if !f.node.attrs.is_empty() {
|
|
|
|
f.node.attrs[0].span.lo
|
|
|
|
} else {
|
|
|
|
f.span.lo
|
|
|
|
},
|
2015-10-07 18:23:07 -05:00
|
|
|
|f| f.span.hi,
|
|
|
|
|f| self.format_variant(f),
|
|
|
|
body_lo,
|
|
|
|
body_hi);
|
|
|
|
|
|
|
|
let budget = self.config.max_width - self.block_indent.width() - 2;
|
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: DefinitiveListTactic::Vertical,
|
|
|
|
separator: ",",
|
2017-02-23 15:31:23 -06:00
|
|
|
trailing_separator: self.config.trailing_comma,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape::legacy(budget, self.block_indent),
|
2015-10-07 18:23:07 -05:00
|
|
|
ends_with_newline: true,
|
|
|
|
config: self.config,
|
|
|
|
};
|
|
|
|
|
|
|
|
let list = try_opt!(write_list(items, &fmt));
|
|
|
|
result.push_str(&list);
|
|
|
|
result.push('\n');
|
|
|
|
Some(result)
|
2015-05-29 05:41:26 -05:00
|
|
|
}
|
|
|
|
|
2015-09-21 13:02:45 -05:00
|
|
|
// Variant of an enum.
|
2015-10-07 18:23:07 -05:00
|
|
|
fn format_variant(&self, field: &ast::Variant) -> Option<String> {
|
|
|
|
if contains_skip(&field.node.attrs) {
|
|
|
|
let lo = field.node.attrs[0].span.lo;
|
2015-10-18 17:25:38 -05:00
|
|
|
let span = mk_sp(lo, field.span.hi);
|
2015-10-07 18:23:07 -05:00
|
|
|
return Some(self.snippet(span));
|
2015-05-29 05:41:26 -05:00
|
|
|
}
|
|
|
|
|
2015-10-07 18:23:07 -05:00
|
|
|
let indent = self.block_indent;
|
2017-03-27 17:14:47 -05:00
|
|
|
let mut result = try_opt!(field
|
|
|
|
.node
|
|
|
|
.attrs
|
|
|
|
.rewrite(&self.get_context(),
|
|
|
|
Shape::legacy(self.config.max_width -
|
|
|
|
indent.width(),
|
|
|
|
indent)));
|
2015-10-07 18:23:07 -05:00
|
|
|
if !result.is_empty() {
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&indent.to_string(self.config));
|
|
|
|
}
|
2015-05-29 05:41:26 -05:00
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
let context = self.get_context();
|
2015-11-12 14:38:41 -06:00
|
|
|
let variant_body = match field.node.data {
|
2015-10-18 17:25:38 -05:00
|
|
|
ast::VariantData::Tuple(..) |
|
2015-10-15 13:37:36 -05:00
|
|
|
ast::VariantData::Struct(..) => {
|
2015-10-08 22:07:14 -05:00
|
|
|
// FIXME: Should limit the width, as we have a trailing comma
|
2015-11-22 06:45:51 -06:00
|
|
|
format_struct(&context,
|
|
|
|
"",
|
|
|
|
field.node.name,
|
2016-05-01 22:01:46 -05:00
|
|
|
&ast::Visibility::Inherited,
|
2015-11-22 06:45:51 -06:00
|
|
|
&field.node.data,
|
|
|
|
None,
|
|
|
|
field.span,
|
2016-05-18 15:38:49 -05:00
|
|
|
indent,
|
|
|
|
Some(self.config.struct_variant_width))
|
2015-05-29 05:41:26 -05:00
|
|
|
}
|
2015-10-15 13:37:36 -05:00
|
|
|
ast::VariantData::Unit(..) => {
|
|
|
|
let tag = if let Some(ref expr) = field.node.disr_expr {
|
|
|
|
format!("{} = {}", field.node.name, self.snippet(expr.span))
|
|
|
|
} else {
|
|
|
|
field.node.name.to_string()
|
|
|
|
};
|
|
|
|
|
|
|
|
wrap_str(tag,
|
|
|
|
self.config.max_width,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(self.config.max_width - indent.width(), indent))
|
2015-10-15 13:37:36 -05:00
|
|
|
}
|
2015-07-01 13:49:45 -05:00
|
|
|
};
|
|
|
|
|
2015-10-07 18:23:07 -05:00
|
|
|
if let Some(variant_str) = variant_body {
|
|
|
|
result.push_str(&variant_str);
|
|
|
|
Some(result)
|
|
|
|
} else {
|
|
|
|
None
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
2015-05-29 05:41:26 -05:00
|
|
|
|
2015-11-22 17:00:22 -06:00
|
|
|
pub fn format_impl(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> {
|
2016-09-06 00:11:56 -05:00
|
|
|
if let ast::ItemKind::Impl(_, _, ref generics, ref trait_ref, _, ref items) = item.node {
|
2015-11-22 17:00:22 -06:00
|
|
|
let mut result = String::new();
|
2016-09-06 00:11:56 -05:00
|
|
|
// First try to format the ref and type without a split at the 'for'.
|
|
|
|
let mut ref_and_type = try_opt!(format_impl_ref_and_type(context, item, offset, false));
|
2015-11-22 17:00:22 -06:00
|
|
|
|
2016-09-06 00:11:56 -05:00
|
|
|
// If there is a line break present in the first result format it again
|
|
|
|
// with a split at the 'for'. Skip this if there is no trait ref and
|
|
|
|
// therefore no 'for'.
|
2017-01-16 18:08:36 -06:00
|
|
|
if ref_and_type.contains('\n') && trait_ref.is_some() {
|
|
|
|
ref_and_type = try_opt!(format_impl_ref_and_type(context, item, offset, true));
|
2016-07-26 00:34:11 -05:00
|
|
|
}
|
2016-09-06 00:11:56 -05:00
|
|
|
result.push_str(&ref_and_type);
|
2015-11-22 17:00:22 -06:00
|
|
|
|
2017-03-27 17:14:47 -05:00
|
|
|
let where_budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(last_line_width(&result)));
|
2015-11-22 17:00:22 -06:00
|
|
|
let where_clause_str = try_opt!(rewrite_where_clause(context,
|
|
|
|
&generics.where_clause,
|
2015-12-01 12:51:49 -06:00
|
|
|
context.config.item_brace_style,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(where_budget,
|
2017-02-20 19:43:43 -06:00
|
|
|
offset.block_only()),
|
2015-11-22 17:00:22 -06:00
|
|
|
context.config.where_density,
|
|
|
|
"{",
|
2017-03-01 19:25:57 -06:00
|
|
|
false,
|
2017-03-09 20:58:35 -06:00
|
|
|
last_line_width(&ref_and_type) == 1,
|
2015-11-22 17:00:22 -06:00
|
|
|
None));
|
2016-01-09 23:45:58 -06:00
|
|
|
|
|
|
|
if try_opt!(is_impl_single_line(context, &items, &result, &where_clause_str, &item)) {
|
|
|
|
result.push_str(&where_clause_str);
|
|
|
|
if where_clause_str.contains('\n') {
|
2016-04-14 13:42:38 -05:00
|
|
|
let white_space = offset.to_string(context.config);
|
|
|
|
result.push_str(&format!("\n{}{{\n{}}}", &white_space, &white_space));
|
2016-01-09 23:45:58 -06:00
|
|
|
} else {
|
|
|
|
result.push_str(" {}");
|
|
|
|
}
|
|
|
|
return Some(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
if !where_clause_str.is_empty() && !where_clause_str.contains('\n') {
|
2015-11-22 17:00:22 -06:00
|
|
|
result.push('\n');
|
2017-02-20 19:43:43 -06:00
|
|
|
let width = offset.block_indent + context.config.tab_spaces - 1;
|
2015-11-22 17:00:22 -06:00
|
|
|
let where_indent = Indent::new(0, width);
|
|
|
|
result.push_str(&where_indent.to_string(context.config));
|
|
|
|
}
|
|
|
|
result.push_str(&where_clause_str);
|
|
|
|
|
|
|
|
match context.config.item_brace_style {
|
2016-11-27 18:47:07 -06:00
|
|
|
BraceStyle::AlwaysNextLine => {
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&offset.to_string(context.config));
|
|
|
|
}
|
2015-11-22 17:00:22 -06:00
|
|
|
BraceStyle::PreferSameLine => result.push(' '),
|
|
|
|
BraceStyle::SameLineWhere => {
|
2015-11-25 00:39:15 -06:00
|
|
|
if !where_clause_str.is_empty() {
|
2016-04-14 13:42:38 -05:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&offset.to_string(context.config));
|
2015-11-22 17:00:22 -06:00
|
|
|
} else {
|
2016-04-14 13:42:38 -05:00
|
|
|
result.push(' ');
|
2015-11-22 17:00:22 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-09 23:45:58 -06:00
|
|
|
|
2015-11-22 17:00:22 -06:00
|
|
|
result.push('{');
|
|
|
|
|
2015-11-23 22:54:44 -06:00
|
|
|
let snippet = context.snippet(item.span);
|
|
|
|
let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
|
|
|
|
|
|
|
|
if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
|
2016-02-05 14:59:41 -06:00
|
|
|
let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config);
|
2017-03-27 17:25:59 -05:00
|
|
|
visitor.block_indent = offset.block_only().block_indent(context.config);
|
2015-11-22 17:00:22 -06:00
|
|
|
visitor.last_pos = item.span.lo + BytePos(open_pos as u32);
|
|
|
|
|
|
|
|
for item in items {
|
2016-08-23 09:14:45 -05:00
|
|
|
visitor.visit_impl_item(item);
|
2015-11-22 17:00:22 -06:00
|
|
|
}
|
|
|
|
|
2015-11-23 22:54:44 -06:00
|
|
|
visitor.format_missing(item.span.hi - BytePos(1));
|
|
|
|
|
|
|
|
let inner_indent_str = visitor.block_indent.to_string(context.config);
|
2017-02-20 19:43:43 -06:00
|
|
|
let outer_indent_str = offset.block_only().to_string(context.config);
|
2015-11-23 22:54:44 -06:00
|
|
|
|
2015-11-22 20:49:21 -06:00
|
|
|
result.push('\n');
|
2015-11-23 22:54:44 -06:00
|
|
|
result.push_str(&inner_indent_str);
|
2016-08-23 09:14:45 -05:00
|
|
|
result.push_str(trim_newlines(visitor.buffer.to_string().trim()));
|
2015-11-22 17:00:22 -06:00
|
|
|
result.push('\n');
|
2015-11-23 22:54:44 -06:00
|
|
|
result.push_str(&outer_indent_str);
|
2015-11-22 17:00:22 -06:00
|
|
|
}
|
|
|
|
|
2016-01-09 23:45:58 -06:00
|
|
|
if result.chars().last().unwrap() == '{' {
|
|
|
|
result.push('\n');
|
2017-01-03 14:20:02 -06:00
|
|
|
result.push_str(&offset.to_string(context.config));
|
2016-01-09 23:45:58 -06:00
|
|
|
}
|
2015-11-23 22:54:44 -06:00
|
|
|
result.push('}');
|
2016-01-09 23:45:58 -06:00
|
|
|
|
2015-11-22 17:00:22 -06:00
|
|
|
Some(result)
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-09 23:45:58 -06:00
|
|
|
fn is_impl_single_line(context: &RewriteContext,
|
2016-08-23 09:14:45 -05:00
|
|
|
items: &[ImplItem],
|
2016-01-09 23:45:58 -06:00
|
|
|
result: &str,
|
|
|
|
where_clause_str: &str,
|
|
|
|
item: &ast::Item)
|
|
|
|
-> Option<bool> {
|
|
|
|
let snippet = context.snippet(item.span);
|
|
|
|
let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
|
|
|
|
|
|
|
|
Some(context.config.impl_empty_single_line && items.is_empty() &&
|
|
|
|
result.len() + where_clause_str.len() <= context.config.max_width &&
|
|
|
|
!contains_comment(&snippet[open_pos..]))
|
|
|
|
}
|
|
|
|
|
2016-09-06 00:11:56 -05:00
|
|
|
fn format_impl_ref_and_type(context: &RewriteContext,
|
|
|
|
item: &ast::Item,
|
|
|
|
offset: Indent,
|
|
|
|
split_at_for: bool)
|
|
|
|
-> Option<String> {
|
|
|
|
if let ast::ItemKind::Impl(unsafety, polarity, ref generics, ref trait_ref, ref self_ty, _) =
|
2016-09-09 09:20:16 -05:00
|
|
|
item.node {
|
2016-09-06 00:11:56 -05:00
|
|
|
let mut result = String::new();
|
|
|
|
|
2017-03-09 20:58:35 -06:00
|
|
|
result.push_str(&format_visibility(&item.vis));
|
2016-09-06 00:11:56 -05:00
|
|
|
result.push_str(format_unsafety(unsafety));
|
|
|
|
result.push_str("impl");
|
|
|
|
|
|
|
|
let lo = context.codemap.span_after(item.span, "impl");
|
|
|
|
let hi = match *trait_ref {
|
|
|
|
Some(ref tr) => tr.path.span.lo,
|
|
|
|
None => self_ty.span.lo,
|
|
|
|
};
|
|
|
|
let generics_str = try_opt!(rewrite_generics(context,
|
|
|
|
generics,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(context.config.max_width,
|
|
|
|
offset),
|
2016-09-06 00:11:56 -05:00
|
|
|
offset + result.len(),
|
|
|
|
mk_sp(lo, hi)));
|
|
|
|
result.push_str(&generics_str);
|
|
|
|
|
|
|
|
if polarity == ast::ImplPolarity::Negative {
|
2017-01-16 18:08:36 -06:00
|
|
|
result.push_str(" !");
|
2016-09-06 00:11:56 -05:00
|
|
|
}
|
|
|
|
if let Some(ref trait_ref) = *trait_ref {
|
2017-01-16 18:08:36 -06:00
|
|
|
if polarity != ast::ImplPolarity::Negative {
|
|
|
|
result.push_str(" ");
|
|
|
|
}
|
2017-03-09 20:58:35 -06:00
|
|
|
let used_space = last_line_width(&result);
|
2017-03-27 17:25:59 -05:00
|
|
|
let budget = try_opt!(context.config.max_width.checked_sub(used_space));
|
2017-03-09 20:58:35 -06:00
|
|
|
let indent = offset + used_space;
|
2017-01-30 13:28:48 -06:00
|
|
|
result.push_str(&*try_opt!(trait_ref.rewrite(context, Shape::legacy(budget, indent))));
|
2016-09-06 00:11:56 -05:00
|
|
|
|
|
|
|
if split_at_for {
|
|
|
|
result.push('\n');
|
|
|
|
|
|
|
|
// Add indentation of one additional tab.
|
2017-02-20 19:43:43 -06:00
|
|
|
let width = offset.block_indent + context.config.tab_spaces;
|
2016-09-06 00:11:56 -05:00
|
|
|
let for_indent = Indent::new(0, width);
|
|
|
|
result.push_str(&for_indent.to_string(context.config));
|
|
|
|
|
2017-01-16 18:08:36 -06:00
|
|
|
result.push_str("for");
|
2016-09-06 00:11:56 -05:00
|
|
|
} else {
|
2017-01-16 18:08:36 -06:00
|
|
|
result.push_str(" for");
|
2016-09-06 00:11:56 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut used_space = last_line_width(&result);
|
|
|
|
if generics.where_clause.predicates.is_empty() {
|
|
|
|
// If there is no where clause adapt budget for type formatting to take space and curly
|
|
|
|
// brace into account.
|
|
|
|
match context.config.item_brace_style {
|
|
|
|
BraceStyle::AlwaysNextLine => {}
|
|
|
|
BraceStyle::PreferSameLine => used_space += 2,
|
|
|
|
BraceStyle::SameLineWhere => used_space += 2,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-16 18:08:36 -06:00
|
|
|
// 1 = space before the type.
|
2017-03-27 17:25:59 -05:00
|
|
|
let budget = try_opt!(context.config.max_width.checked_sub(used_space + 1));
|
2017-01-16 18:08:36 -06:00
|
|
|
let indent = offset + result.len() + 1;
|
2017-01-30 13:28:48 -06:00
|
|
|
let self_ty_str = self_ty.rewrite(context, Shape::legacy(budget, indent));
|
2017-01-16 18:08:36 -06:00
|
|
|
if let Some(self_ty_str) = self_ty_str {
|
|
|
|
result.push_str(" ");
|
|
|
|
result.push_str(&self_ty_str);
|
|
|
|
return Some(result);
|
|
|
|
}
|
2016-09-06 00:11:56 -05:00
|
|
|
|
2017-01-16 18:08:36 -06:00
|
|
|
// Can't fit the self type on what's left of the line, so start a new one.
|
|
|
|
let indent = offset.block_indent(context.config);
|
|
|
|
result.push_str(&format!("\n{}", indent.to_string(context.config)));
|
2017-03-27 17:25:59 -05:00
|
|
|
let budget = try_opt!(context.config.max_width.checked_sub(indent.width()));
|
2017-01-30 13:28:48 -06:00
|
|
|
result.push_str(&*try_opt!(self_ty.rewrite(context, Shape::legacy(budget, indent))));
|
2016-09-06 00:11:56 -05:00
|
|
|
Some(result)
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
pub fn format_struct(context: &RewriteContext,
|
|
|
|
item_name: &str,
|
|
|
|
ident: ast::Ident,
|
2016-05-01 22:01:46 -05:00
|
|
|
vis: &ast::Visibility,
|
2015-11-22 06:45:51 -06:00
|
|
|
struct_def: &ast::VariantData,
|
|
|
|
generics: Option<&ast::Generics>,
|
|
|
|
span: Span,
|
2016-05-18 15:38:49 -05:00
|
|
|
offset: Indent,
|
|
|
|
one_line_width: Option<usize>)
|
2015-11-22 06:45:51 -06:00
|
|
|
-> Option<String> {
|
|
|
|
match *struct_def {
|
2016-05-27 17:58:25 -05:00
|
|
|
ast::VariantData::Unit(..) => Some(format_unit_struct(item_name, ident, vis)),
|
2015-11-22 06:45:51 -06:00
|
|
|
ast::VariantData::Tuple(ref fields, _) => {
|
|
|
|
format_tuple_struct(context,
|
|
|
|
item_name,
|
|
|
|
ident,
|
|
|
|
vis,
|
|
|
|
fields,
|
|
|
|
generics,
|
|
|
|
span,
|
|
|
|
offset)
|
2015-11-09 00:00:04 -06:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
ast::VariantData::Struct(ref fields, _) => {
|
|
|
|
format_struct_struct(context,
|
|
|
|
item_name,
|
|
|
|
ident,
|
|
|
|
vis,
|
|
|
|
fields,
|
|
|
|
generics,
|
|
|
|
span,
|
2016-05-18 15:38:49 -05:00
|
|
|
offset,
|
|
|
|
one_line_width)
|
2015-10-18 17:25:38 -05:00
|
|
|
}
|
2015-11-09 00:00:04 -06:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
2015-11-09 00:00:04 -06:00
|
|
|
|
2016-03-11 15:18:30 -06:00
|
|
|
pub fn format_trait(context: &RewriteContext, item: &ast::Item, offset: Indent) -> Option<String> {
|
2016-03-12 12:09:27 -06:00
|
|
|
if let ast::ItemKind::Trait(unsafety, ref generics, ref type_param_bounds, ref trait_items) =
|
2016-09-09 09:20:16 -05:00
|
|
|
item.node {
|
2016-03-11 15:18:30 -06:00
|
|
|
let mut result = String::new();
|
|
|
|
let header = format!("{}{}trait {}",
|
2016-05-27 17:58:25 -05:00
|
|
|
format_visibility(&item.vis),
|
2016-03-11 15:18:30 -06:00
|
|
|
format_unsafety(unsafety),
|
|
|
|
item.ident);
|
|
|
|
|
|
|
|
result.push_str(&header);
|
|
|
|
|
2016-03-14 21:51:41 -05:00
|
|
|
let body_lo = context.codemap.span_after(item.span, "{");
|
2016-03-11 15:18:30 -06:00
|
|
|
|
|
|
|
let generics_str = try_opt!(rewrite_generics(context,
|
|
|
|
generics,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(context.config.max_width,
|
|
|
|
offset),
|
2016-03-11 15:18:30 -06:00
|
|
|
offset + result.len(),
|
|
|
|
mk_sp(item.span.lo, body_lo)));
|
|
|
|
result.push_str(&generics_str);
|
|
|
|
|
2017-01-30 13:28:48 -06:00
|
|
|
let trait_bound_str =
|
|
|
|
try_opt!(rewrite_trait_bounds(context,
|
|
|
|
type_param_bounds,
|
|
|
|
Shape::legacy(context.config.max_width, offset)));
|
2016-03-15 15:08:12 -05:00
|
|
|
// If the trait, generics, and trait bound cannot fit on the same line,
|
|
|
|
// put the trait bounds on an indented new line
|
|
|
|
if offset.width() + last_line_width(&result) + trait_bound_str.len() >
|
2017-03-12 15:07:33 -05:00
|
|
|
context.config.comment_width {
|
2016-03-11 23:32:08 -06:00
|
|
|
result.push('\n');
|
2017-03-27 17:25:59 -05:00
|
|
|
let trait_indent = offset.block_only().block_indent(context.config);
|
2016-03-11 23:32:08 -06:00
|
|
|
result.push_str(&trait_indent.to_string(context.config));
|
|
|
|
}
|
2016-03-11 15:18:30 -06:00
|
|
|
result.push_str(&trait_bound_str);
|
|
|
|
|
2016-03-15 15:08:12 -05:00
|
|
|
let has_body = !trait_items.is_empty();
|
|
|
|
|
2016-05-12 14:50:43 -05:00
|
|
|
let where_density =
|
|
|
|
if (context.config.where_density == Density::Compressed &&
|
2017-03-19 17:42:20 -05:00
|
|
|
(!result.contains('\n') || context.config.fn_args_layout == IndentStyle::Block)) ||
|
|
|
|
(context.config.fn_args_layout == IndentStyle::Block && result.is_empty()) ||
|
2016-05-12 14:50:43 -05:00
|
|
|
(context.config.where_density == Density::CompressedIfEmpty && !has_body &&
|
|
|
|
!result.contains('\n')) {
|
|
|
|
Density::Compressed
|
|
|
|
} else {
|
|
|
|
Density::Tall
|
|
|
|
};
|
2016-03-15 15:08:12 -05:00
|
|
|
|
2017-03-27 17:14:47 -05:00
|
|
|
let where_budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(last_line_width(&result)));
|
2016-03-11 15:49:11 -06:00
|
|
|
let where_clause_str = try_opt!(rewrite_where_clause(context,
|
|
|
|
&generics.where_clause,
|
|
|
|
context.config.item_brace_style,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(where_budget,
|
2017-02-20 19:43:43 -06:00
|
|
|
offset.block_only()),
|
2016-03-15 15:08:12 -05:00
|
|
|
where_density,
|
2016-03-11 15:49:11 -06:00
|
|
|
"{",
|
2017-03-01 19:25:57 -06:00
|
|
|
!has_body,
|
2017-03-09 21:06:05 -06:00
|
|
|
trait_bound_str.is_empty() &&
|
|
|
|
last_line_width(&generics_str) == 1,
|
2016-03-11 15:49:11 -06:00
|
|
|
None));
|
2016-03-15 15:08:12 -05:00
|
|
|
// If the where clause cannot fit on the same line,
|
|
|
|
// put the where clause on a new line
|
2016-03-11 15:49:11 -06:00
|
|
|
if !where_clause_str.contains('\n') &&
|
2016-03-15 15:08:12 -05:00
|
|
|
last_line_width(&result) + where_clause_str.len() + offset.width() >
|
2017-03-12 15:07:33 -05:00
|
|
|
context.config.comment_width {
|
2016-03-11 15:49:11 -06:00
|
|
|
result.push('\n');
|
2017-02-20 19:43:43 -06:00
|
|
|
let width = offset.block_indent + context.config.tab_spaces - 1;
|
2016-03-11 15:49:11 -06:00
|
|
|
let where_indent = Indent::new(0, width);
|
|
|
|
result.push_str(&where_indent.to_string(context.config));
|
|
|
|
}
|
|
|
|
result.push_str(&where_clause_str);
|
|
|
|
|
2016-03-11 23:32:08 -06:00
|
|
|
match context.config.item_brace_style {
|
|
|
|
BraceStyle::AlwaysNextLine => {
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&offset.to_string(context.config));
|
2016-03-11 23:41:22 -06:00
|
|
|
}
|
2016-03-11 23:32:08 -06:00
|
|
|
BraceStyle::PreferSameLine => result.push(' '),
|
|
|
|
BraceStyle::SameLineWhere => {
|
2016-03-15 15:08:12 -05:00
|
|
|
if !where_clause_str.is_empty() &&
|
2016-08-23 09:14:45 -05:00
|
|
|
(!trait_items.is_empty() || result.contains('\n')) {
|
2016-03-11 23:32:08 -06:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&offset.to_string(context.config));
|
|
|
|
} else {
|
|
|
|
result.push(' ');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.push('{');
|
|
|
|
|
|
|
|
let snippet = context.snippet(item.span);
|
|
|
|
let open_pos = try_opt!(snippet.find_uncommented("{")) + 1;
|
|
|
|
|
|
|
|
if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
|
2016-03-12 12:09:27 -06:00
|
|
|
let mut visitor = FmtVisitor::from_codemap(context.parse_session, context.config);
|
2017-03-27 17:25:59 -05:00
|
|
|
visitor.block_indent = offset.block_only().block_indent(context.config);
|
2016-03-11 23:32:08 -06:00
|
|
|
visitor.last_pos = item.span.lo + BytePos(open_pos as u32);
|
|
|
|
|
|
|
|
for item in trait_items {
|
2016-08-23 09:14:45 -05:00
|
|
|
visitor.visit_trait_item(item);
|
2016-03-11 23:32:08 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
visitor.format_missing(item.span.hi - BytePos(1));
|
|
|
|
|
|
|
|
let inner_indent_str = visitor.block_indent.to_string(context.config);
|
2017-02-20 19:43:43 -06:00
|
|
|
let outer_indent_str = offset.block_only().to_string(context.config);
|
2016-03-11 23:32:08 -06:00
|
|
|
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&inner_indent_str);
|
2016-08-23 09:14:45 -05:00
|
|
|
result.push_str(trim_newlines(visitor.buffer.to_string().trim()));
|
2016-03-11 23:32:08 -06:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&outer_indent_str);
|
2016-03-15 15:08:12 -05:00
|
|
|
} else if result.contains('\n') {
|
|
|
|
result.push('\n');
|
2016-03-11 15:18:30 -06:00
|
|
|
}
|
|
|
|
|
2016-03-11 23:32:08 -06:00
|
|
|
result.push('}');
|
2016-03-11 15:18:30 -06:00
|
|
|
Some(result)
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:58:25 -05:00
|
|
|
fn format_unit_struct(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
|
|
|
|
format!("{};", format_header(item_name, ident, vis))
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
2015-10-18 17:25:38 -05:00
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
fn format_struct_struct(context: &RewriteContext,
|
|
|
|
item_name: &str,
|
|
|
|
ident: ast::Ident,
|
2016-05-01 22:01:46 -05:00
|
|
|
vis: &ast::Visibility,
|
2015-11-22 06:45:51 -06:00
|
|
|
fields: &[ast::StructField],
|
|
|
|
generics: Option<&ast::Generics>,
|
|
|
|
span: Span,
|
2016-05-18 15:38:49 -05:00
|
|
|
offset: Indent,
|
|
|
|
one_line_width: Option<usize>)
|
2015-11-22 06:45:51 -06:00
|
|
|
-> Option<String> {
|
|
|
|
let mut result = String::with_capacity(1024);
|
|
|
|
|
2016-05-27 17:58:25 -05:00
|
|
|
let header_str = format_header(item_name, ident, vis);
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&header_str);
|
|
|
|
|
2016-03-07 12:41:32 -06:00
|
|
|
let body_lo = context.codemap.span_after(span, "{");
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
let generics_str = match generics {
|
|
|
|
Some(g) => {
|
|
|
|
try_opt!(format_generics(context,
|
|
|
|
g,
|
|
|
|
"{",
|
|
|
|
"{",
|
|
|
|
context.config.item_brace_style,
|
|
|
|
fields.is_empty(),
|
|
|
|
offset,
|
|
|
|
offset + header_str.len(),
|
|
|
|
mk_sp(span.lo, body_lo)))
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
None => {
|
|
|
|
if context.config.item_brace_style == BraceStyle::AlwaysNextLine && !fields.is_empty() {
|
2017-02-20 19:43:43 -06:00
|
|
|
format!("\n{}{{", offset.block_only().to_string(context.config))
|
2015-11-19 00:34:14 -06:00
|
|
|
} else {
|
2015-11-22 06:45:51 -06:00
|
|
|
" {".to_owned()
|
2015-11-19 00:34:14 -06:00
|
|
|
}
|
2015-05-29 05:41:26 -05:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
};
|
|
|
|
result.push_str(&generics_str);
|
2015-05-24 18:03:26 -05:00
|
|
|
|
2016-04-11 14:05:54 -05:00
|
|
|
// FIXME(#919): properly format empty structs and their comments.
|
2015-11-22 06:45:51 -06:00
|
|
|
if fields.is_empty() {
|
2017-01-08 21:11:12 -06:00
|
|
|
let snippet = context.snippet(mk_sp(body_lo, span.hi - BytePos(1)));
|
|
|
|
if snippet.trim().is_empty() {
|
|
|
|
// `struct S {}`
|
2017-03-27 17:14:47 -05:00
|
|
|
} else if snippet
|
|
|
|
.trim_right_matches(&[' ', '\t'][..])
|
|
|
|
.ends_with('\n') {
|
2017-01-08 21:11:12 -06:00
|
|
|
// fix indent
|
|
|
|
result.push_str(&snippet.trim_right());
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&offset.to_string(context.config));
|
|
|
|
} else {
|
|
|
|
result.push_str(&snippet);
|
|
|
|
}
|
|
|
|
result.push('}');
|
2015-11-22 06:45:51 -06:00
|
|
|
return Some(result);
|
2015-04-21 05:50:43 -05:00
|
|
|
}
|
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
let item_indent = offset.block_indent(context.config);
|
2016-01-14 01:26:15 -06:00
|
|
|
// 1 = ","
|
2017-03-27 17:14:47 -05:00
|
|
|
let item_budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(item_indent.width() + 1));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-01-30 13:28:48 -06:00
|
|
|
let items =
|
|
|
|
itemize_list(context.codemap,
|
|
|
|
fields.iter(),
|
|
|
|
"}",
|
|
|
|
|field| {
|
2017-03-21 15:05:50 -05:00
|
|
|
// Include attributes and doc comments, if present
|
|
|
|
if !field.attrs.is_empty() {
|
|
|
|
field.attrs[0].span.lo
|
|
|
|
} else {
|
|
|
|
field.span.lo
|
|
|
|
}
|
|
|
|
},
|
2017-01-30 13:28:48 -06:00
|
|
|
|field| field.ty.span.hi,
|
|
|
|
|field| field.rewrite(context, Shape::legacy(item_budget, item_indent)),
|
|
|
|
context.codemap.span_after(span, "{"),
|
|
|
|
span.hi)
|
2017-02-21 21:20:50 -06:00
|
|
|
.collect::<Vec<_>>();
|
2015-11-22 06:45:51 -06:00
|
|
|
// 1 = ,
|
|
|
|
let budget = context.config.max_width - offset.width() + context.config.tab_spaces - 1;
|
2016-05-18 15:38:49 -05:00
|
|
|
|
|
|
|
let tactic = match one_line_width {
|
|
|
|
Some(w) => definitive_tactic(&items, ListTactic::LimitedHorizontalVertical(w), budget),
|
|
|
|
None => DefinitiveListTactic::Vertical,
|
|
|
|
};
|
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
let fmt = ListFormatting {
|
2016-05-18 15:38:49 -05:00
|
|
|
tactic: tactic,
|
2015-11-22 06:45:51 -06:00
|
|
|
separator: ",",
|
2017-02-23 15:31:23 -06:00
|
|
|
trailing_separator: context.config.trailing_comma,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape::legacy(budget, item_indent),
|
2015-11-22 06:45:51 -06:00
|
|
|
ends_with_newline: true,
|
|
|
|
config: context.config,
|
|
|
|
};
|
2016-05-18 15:38:49 -05:00
|
|
|
let items_str = try_opt!(write_list(&items, &fmt));
|
|
|
|
if one_line_width.is_some() && !items_str.contains('\n') {
|
|
|
|
Some(format!("{} {} }}", result, items_str))
|
|
|
|
} else {
|
|
|
|
Some(format!("{}\n{}{}\n{}}}",
|
|
|
|
result,
|
2017-03-27 17:14:47 -05:00
|
|
|
offset
|
|
|
|
.block_indent(context.config)
|
|
|
|
.to_string(context.config),
|
2016-05-18 15:38:49 -05:00
|
|
|
items_str,
|
|
|
|
offset.to_string(context.config)))
|
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
2015-04-22 23:22:48 -05:00
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
fn format_tuple_struct(context: &RewriteContext,
|
|
|
|
item_name: &str,
|
|
|
|
ident: ast::Ident,
|
2016-05-01 22:01:46 -05:00
|
|
|
vis: &ast::Visibility,
|
2015-11-22 06:45:51 -06:00
|
|
|
fields: &[ast::StructField],
|
|
|
|
generics: Option<&ast::Generics>,
|
|
|
|
span: Span,
|
|
|
|
offset: Indent)
|
|
|
|
-> Option<String> {
|
|
|
|
let mut result = String::with_capacity(1024);
|
|
|
|
|
2016-05-27 17:58:25 -05:00
|
|
|
let header_str = format_header(item_name, ident, vis);
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&header_str);
|
|
|
|
|
2016-04-11 14:05:54 -05:00
|
|
|
// FIXME(#919): don't lose comments on empty tuple structs.
|
|
|
|
let body_lo = if fields.is_empty() {
|
|
|
|
span.hi
|
|
|
|
} else {
|
|
|
|
fields[0].span.lo
|
|
|
|
};
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2015-11-27 02:25:31 -06:00
|
|
|
let where_clause_str = match generics {
|
2016-08-23 09:14:45 -05:00
|
|
|
Some(generics) => {
|
2015-11-22 06:45:51 -06:00
|
|
|
let generics_str = try_opt!(rewrite_generics(context,
|
|
|
|
generics,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(context.config.max_width,
|
|
|
|
offset),
|
2015-11-22 06:45:51 -06:00
|
|
|
offset + header_str.len(),
|
|
|
|
mk_sp(span.lo, body_lo)));
|
2015-11-27 02:25:31 -06:00
|
|
|
result.push_str(&generics_str);
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-03-27 17:14:47 -05:00
|
|
|
let where_budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(last_line_width(&result)));
|
2016-01-07 00:43:45 -06:00
|
|
|
try_opt!(rewrite_where_clause(context,
|
|
|
|
&generics.where_clause,
|
|
|
|
context.config.item_brace_style,
|
2017-02-20 19:43:43 -06:00
|
|
|
Shape::legacy(where_budget, offset.block_only()),
|
2016-01-07 00:43:45 -06:00
|
|
|
Density::Compressed,
|
|
|
|
";",
|
2017-03-01 19:25:57 -06:00
|
|
|
true,
|
2017-03-08 19:21:20 -06:00
|
|
|
false,
|
2016-01-07 00:43:45 -06:00
|
|
|
None))
|
2015-09-01 02:36:00 -05:00
|
|
|
}
|
2015-11-27 02:25:31 -06:00
|
|
|
None => "".to_owned(),
|
2015-11-22 06:45:51 -06:00
|
|
|
};
|
|
|
|
|
2017-03-09 20:30:47 -06:00
|
|
|
let (tactic, item_indent) = match context.config.fn_args_layout {
|
2017-03-19 17:42:20 -05:00
|
|
|
IndentStyle::Visual => {
|
2017-03-12 17:25:46 -05:00
|
|
|
// 1 = `(`
|
|
|
|
(ListTactic::HorizontalVertical, offset.block_only() + result.len() + 1)
|
2017-03-09 20:30:47 -06:00
|
|
|
}
|
2017-03-19 17:42:20 -05:00
|
|
|
IndentStyle::Block => {
|
2017-03-27 17:25:59 -05:00
|
|
|
(ListTactic::HorizontalVertical, offset.block_only().block_indent(&context.config))
|
2017-03-09 20:30:47 -06:00
|
|
|
}
|
|
|
|
};
|
2017-03-12 17:25:46 -05:00
|
|
|
// 3 = `();`
|
2017-03-27 17:14:47 -05:00
|
|
|
let item_budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(item_indent.width() + 3));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-01-30 13:28:48 -06:00
|
|
|
let items =
|
|
|
|
itemize_list(context.codemap,
|
|
|
|
fields.iter(),
|
|
|
|
")",
|
|
|
|
|field| {
|
2017-03-21 15:05:50 -05:00
|
|
|
// Include attributes and doc comments, if present
|
|
|
|
if !field.attrs.is_empty() {
|
|
|
|
field.attrs[0].span.lo
|
|
|
|
} else {
|
|
|
|
field.span.lo
|
|
|
|
}
|
|
|
|
},
|
2017-01-30 13:28:48 -06:00
|
|
|
|field| field.ty.span.hi,
|
|
|
|
|field| field.rewrite(context, Shape::legacy(item_budget, item_indent)),
|
|
|
|
context.codemap.span_after(span, "("),
|
|
|
|
span.hi);
|
2017-03-27 17:14:47 -05:00
|
|
|
let body_budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(offset.block_only().width() + result.len() +
|
|
|
|
3));
|
2017-03-09 20:30:47 -06:00
|
|
|
let body = try_opt!(list_helper(items,
|
2017-03-12 17:25:46 -05:00
|
|
|
// TODO budget is wrong in block case
|
|
|
|
Shape::legacy(body_budget, item_indent),
|
2017-03-09 20:30:47 -06:00
|
|
|
context.config,
|
|
|
|
tactic));
|
2016-10-12 20:34:08 -05:00
|
|
|
|
2017-03-19 17:42:20 -05:00
|
|
|
if context.config.fn_args_layout == IndentStyle::Visual || !body.contains('\n') {
|
2017-03-12 17:25:46 -05:00
|
|
|
result.push('(');
|
|
|
|
if context.config.spaces_within_parens && body.len() > 0 {
|
|
|
|
result.push(' ');
|
|
|
|
}
|
2016-10-12 20:34:08 -05:00
|
|
|
|
2017-03-12 17:25:46 -05:00
|
|
|
result.push_str(&body);
|
2016-10-12 20:34:08 -05:00
|
|
|
|
2017-03-12 17:25:46 -05:00
|
|
|
if context.config.spaces_within_parens && body.len() > 0 {
|
|
|
|
result.push(' ');
|
2017-03-09 20:30:47 -06:00
|
|
|
}
|
2017-03-12 17:25:46 -05:00
|
|
|
result.push(')');
|
|
|
|
} else {
|
|
|
|
result.push_str("(\n");
|
|
|
|
result.push_str(&item_indent.to_string(&context.config));
|
|
|
|
result.push_str(&body);
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&offset.block_only().to_string(&context.config));
|
|
|
|
result.push(')');
|
2017-03-09 20:30:47 -06:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2015-11-25 00:39:15 -06:00
|
|
|
if !where_clause_str.is_empty() && !where_clause_str.contains('\n') &&
|
2015-11-22 06:45:51 -06:00
|
|
|
(result.contains('\n') ||
|
2017-02-20 19:43:43 -06:00
|
|
|
offset.block_indent + result.len() + where_clause_str.len() + 1 >
|
2015-11-22 06:45:51 -06:00
|
|
|
context.config.max_width) {
|
2017-03-09 20:30:47 -06:00
|
|
|
// We need to put the where clause on a new line, but we didn't
|
2015-11-22 06:45:51 -06:00
|
|
|
// know that earlier, so the where clause will not be indented properly.
|
|
|
|
result.push('\n');
|
2017-02-20 19:43:43 -06:00
|
|
|
result.push_str(&(offset.block_only() + (context.config.tab_spaces - 1))
|
2017-02-21 21:20:50 -06:00
|
|
|
.to_string(context.config));
|
2015-04-21 02:59:48 -05:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&where_clause_str);
|
|
|
|
|
|
|
|
Some(result)
|
2015-09-08 13:56:33 -05:00
|
|
|
}
|
2015-04-21 02:59:48 -05:00
|
|
|
|
2015-11-22 12:21:01 -06:00
|
|
|
pub fn rewrite_type_alias(context: &RewriteContext,
|
|
|
|
indent: Indent,
|
|
|
|
ident: ast::Ident,
|
|
|
|
ty: &ast::Ty,
|
|
|
|
generics: &ast::Generics,
|
2016-05-01 22:01:46 -05:00
|
|
|
vis: &ast::Visibility,
|
2015-11-22 12:21:01 -06:00
|
|
|
span: Span)
|
|
|
|
-> Option<String> {
|
|
|
|
let mut result = String::new();
|
|
|
|
|
2016-08-23 09:14:45 -05:00
|
|
|
result.push_str(&format_visibility(vis));
|
2015-11-22 12:21:01 -06:00
|
|
|
result.push_str("type ");
|
|
|
|
result.push_str(&ident.to_string());
|
|
|
|
|
|
|
|
let generics_indent = indent + result.len();
|
2016-03-07 12:41:32 -06:00
|
|
|
let generics_span = mk_sp(context.codemap.span_after(span, "type"), ty.span.lo);
|
2015-12-02 12:38:05 -06:00
|
|
|
let generics_width = context.config.max_width - " =".len();
|
2015-11-22 12:21:01 -06:00
|
|
|
let generics_str = try_opt!(rewrite_generics(context,
|
|
|
|
generics,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(generics_width, indent),
|
2015-11-22 12:21:01 -06:00
|
|
|
generics_indent,
|
|
|
|
generics_span));
|
|
|
|
|
|
|
|
result.push_str(&generics_str);
|
2015-12-02 12:39:45 -06:00
|
|
|
|
2017-03-27 17:14:47 -05:00
|
|
|
let where_budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(last_line_width(&result)));
|
2015-12-02 12:39:45 -06:00
|
|
|
let where_clause_str = try_opt!(rewrite_where_clause(context,
|
|
|
|
&generics.where_clause,
|
|
|
|
context.config.item_brace_style,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(where_budget, indent),
|
2015-12-02 12:39:45 -06:00
|
|
|
context.config.where_density,
|
|
|
|
"=",
|
2017-03-01 19:25:57 -06:00
|
|
|
true,
|
2017-03-09 20:58:35 -06:00
|
|
|
true,
|
2015-12-02 12:39:45 -06:00
|
|
|
Some(span.hi)));
|
|
|
|
result.push_str(&where_clause_str);
|
2015-11-22 12:21:01 -06:00
|
|
|
result.push_str(" = ");
|
|
|
|
|
2015-11-26 14:20:04 -06:00
|
|
|
let line_width = last_line_width(&result);
|
2015-12-02 12:38:05 -06:00
|
|
|
// This checked_sub may fail as the extra space after '=' is not taken into account
|
|
|
|
// In that case the budget is set to 0 which will make ty.rewrite retry on a new line
|
2017-03-27 16:58:41 -05:00
|
|
|
let budget = context
|
|
|
|
.config
|
2016-04-22 02:03:36 -05:00
|
|
|
.max_width
|
|
|
|
.checked_sub(indent.width() + line_width + ";".len())
|
|
|
|
.unwrap_or(0);
|
2015-11-26 14:20:04 -06:00
|
|
|
let type_indent = indent + line_width;
|
2015-11-22 12:21:01 -06:00
|
|
|
// Try to fit the type on the same line
|
2017-03-27 17:14:47 -05:00
|
|
|
let ty_str = try_opt!(ty.rewrite(context, Shape::legacy(budget, type_indent))
|
|
|
|
.or_else(|| {
|
2017-02-21 21:20:50 -06:00
|
|
|
// The line was too short, try to put the type on the next line
|
2016-04-22 02:03:36 -05:00
|
|
|
|
2017-02-21 21:20:50 -06:00
|
|
|
// Remove the space after '='
|
|
|
|
result.pop();
|
|
|
|
let type_indent = indent.block_indent(context.config);
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&type_indent.to_string(context.config));
|
2017-03-27 17:14:47 -05:00
|
|
|
let budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(type_indent.width() + ";".len()));
|
2017-02-21 21:20:50 -06:00
|
|
|
ty.rewrite(context, Shape::legacy(budget, type_indent))
|
|
|
|
}));
|
2015-11-22 12:21:01 -06:00
|
|
|
result.push_str(&ty_str);
|
|
|
|
result.push_str(";");
|
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
|
2016-09-16 18:44:51 -05:00
|
|
|
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
|
|
|
|
(if config.space_before_type_annotation {
|
2016-09-18 21:48:56 -05:00
|
|
|
" "
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
},
|
2016-09-16 18:44:51 -05:00
|
|
|
if config.space_after_type_annotation_colon {
|
2016-09-18 21:48:56 -05:00
|
|
|
" "
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
})
|
2016-08-02 20:07:56 -05:00
|
|
|
}
|
|
|
|
|
2015-10-18 17:25:38 -05:00
|
|
|
impl Rewrite for ast::StructField {
|
2017-01-30 13:28:48 -06:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2016-05-01 22:01:46 -05:00
|
|
|
if contains_skip(&self.attrs) {
|
|
|
|
let span = context.snippet(mk_sp(self.attrs[0].span.lo, self.span.hi));
|
2017-01-30 13:28:48 -06:00
|
|
|
return wrap_str(span, context.config.max_width, shape);
|
2015-10-18 17:25:38 -05:00
|
|
|
}
|
|
|
|
|
2016-05-01 22:01:46 -05:00
|
|
|
let name = self.ident;
|
2016-05-27 17:58:25 -05:00
|
|
|
let vis = format_visibility(&self.vis);
|
2017-03-27 17:14:47 -05:00
|
|
|
let mut attr_str = try_opt!(self.attrs
|
|
|
|
.rewrite(context,
|
|
|
|
Shape::legacy(context.config.max_width -
|
|
|
|
shape.indent.width(),
|
|
|
|
shape.indent)));
|
2015-10-18 17:25:38 -05:00
|
|
|
if !attr_str.is_empty() {
|
|
|
|
attr_str.push('\n');
|
2017-01-30 13:28:48 -06:00
|
|
|
attr_str.push_str(&shape.indent.to_string(context.config));
|
2015-10-18 17:25:38 -05:00
|
|
|
}
|
|
|
|
|
2016-08-02 20:07:56 -05:00
|
|
|
let type_annotation_spacing = type_annotation_spacing(context.config);
|
2015-10-18 17:25:38 -05:00
|
|
|
let result = match name {
|
2016-09-16 18:44:51 -05:00
|
|
|
Some(name) => {
|
|
|
|
format!("{}{}{}{}:{}",
|
|
|
|
attr_str,
|
|
|
|
vis,
|
|
|
|
name,
|
|
|
|
type_annotation_spacing.0,
|
|
|
|
type_annotation_spacing.1)
|
|
|
|
}
|
2015-10-18 17:25:38 -05:00
|
|
|
None => format!("{}{}", attr_str, vis),
|
|
|
|
};
|
|
|
|
|
|
|
|
let last_line_width = last_line_width(&result);
|
2017-01-30 13:28:48 -06:00
|
|
|
let budget = try_opt!(shape.width.checked_sub(last_line_width));
|
2017-03-27 17:14:47 -05:00
|
|
|
let rewrite = try_opt!(self.ty
|
|
|
|
.rewrite(context,
|
|
|
|
Shape::legacy(budget,
|
|
|
|
shape.indent + last_line_width)));
|
2015-10-18 17:25:38 -05:00
|
|
|
Some(result + &rewrite)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-18 14:25:30 -05:00
|
|
|
pub fn rewrite_static(prefix: &str,
|
2016-05-01 22:01:46 -05:00
|
|
|
vis: &ast::Visibility,
|
2015-10-18 14:25:30 -05:00
|
|
|
ident: ast::Ident,
|
|
|
|
ty: &ast::Ty,
|
|
|
|
mutability: ast::Mutability,
|
2016-03-15 15:08:12 -05:00
|
|
|
expr_opt: Option<&ptr::P<ast::Expr>>,
|
2017-02-20 19:43:43 -06:00
|
|
|
offset: Indent,
|
2015-10-18 14:25:30 -05:00
|
|
|
context: &RewriteContext)
|
|
|
|
-> Option<String> {
|
2016-08-02 20:07:56 -05:00
|
|
|
let type_annotation_spacing = type_annotation_spacing(context.config);
|
2016-09-16 18:44:51 -05:00
|
|
|
let prefix = format!("{}{} {}{}{}:{}",
|
2016-05-27 17:58:25 -05:00
|
|
|
format_visibility(vis),
|
2015-10-19 13:08:03 -05:00
|
|
|
prefix,
|
|
|
|
format_mutability(mutability),
|
2016-08-02 20:07:56 -05:00
|
|
|
ident,
|
2016-09-16 18:44:51 -05:00
|
|
|
type_annotation_spacing.0,
|
|
|
|
type_annotation_spacing.1);
|
2015-10-18 14:25:30 -05:00
|
|
|
// 2 = " =".len()
|
|
|
|
let ty_str = try_opt!(ty.rewrite(context,
|
2017-02-21 21:20:50 -06:00
|
|
|
Shape::legacy(context.config.max_width - offset.block_indent -
|
2017-01-30 13:28:48 -06:00
|
|
|
prefix.len() -
|
|
|
|
2,
|
2017-02-20 19:43:43 -06:00
|
|
|
offset.block_only())));
|
2015-10-18 14:25:30 -05:00
|
|
|
|
2016-08-23 09:14:45 -05:00
|
|
|
if let Some(expr) = expr_opt {
|
2016-03-15 15:08:12 -05:00
|
|
|
let lhs = format!("{}{} =", prefix, ty_str);
|
|
|
|
// 1 = ;
|
2017-02-20 19:43:43 -06:00
|
|
|
let remaining_width = context.config.max_width - offset.block_indent - 1;
|
2017-01-30 13:28:48 -06:00
|
|
|
rewrite_assign_rhs(context,
|
|
|
|
lhs,
|
|
|
|
expr,
|
2017-02-20 19:43:43 -06:00
|
|
|
Shape::legacy(remaining_width, offset.block_only()))
|
2017-02-21 21:20:50 -06:00
|
|
|
.map(|s| s + ";")
|
2016-03-15 15:08:12 -05:00
|
|
|
} else {
|
|
|
|
let lhs = format!("{}{};", prefix, ty_str);
|
|
|
|
Some(lhs)
|
|
|
|
}
|
2015-10-18 14:25:30 -05:00
|
|
|
}
|
|
|
|
|
2016-03-15 15:08:12 -05:00
|
|
|
pub fn rewrite_associated_type(ident: ast::Ident,
|
2016-03-14 20:52:07 -05:00
|
|
|
ty_opt: Option<&ptr::P<ast::Ty>>,
|
|
|
|
ty_param_bounds_opt: Option<&ast::TyParamBounds>,
|
|
|
|
context: &RewriteContext,
|
|
|
|
indent: Indent)
|
|
|
|
-> Option<String> {
|
2016-03-15 15:08:12 -05:00
|
|
|
let prefix = format!("type {}", ident);
|
2016-03-14 20:52:07 -05:00
|
|
|
|
|
|
|
let type_bounds_str = if let Some(ty_param_bounds) = ty_param_bounds_opt {
|
2016-08-23 09:14:45 -05:00
|
|
|
let bounds: &[_] = ty_param_bounds;
|
2016-06-06 18:03:25 -05:00
|
|
|
let bound_str = try_opt!(bounds.iter()
|
2017-02-26 15:44:05 -06:00
|
|
|
.map(|ty_bound| {
|
|
|
|
ty_bound.rewrite(context, Shape::legacy(context.config.max_width, indent))
|
|
|
|
})
|
|
|
|
.intersperse(Some(" + ".to_string()))
|
|
|
|
.collect::<Option<String>>());
|
2016-03-14 20:52:07 -05:00
|
|
|
if bounds.len() > 0 {
|
|
|
|
format!(": {}", bound_str)
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(ty) = ty_opt {
|
|
|
|
let ty_str = try_opt!(ty.rewrite(context,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(context.config.max_width -
|
2017-02-20 19:43:43 -06:00
|
|
|
indent.block_indent -
|
2017-01-30 13:28:48 -06:00
|
|
|
prefix.len() -
|
|
|
|
2,
|
2017-02-20 19:43:43 -06:00
|
|
|
indent.block_only())));
|
2016-03-14 20:52:07 -05:00
|
|
|
Some(format!("{} = {};", prefix, ty_str))
|
|
|
|
} else {
|
|
|
|
Some(format!("{}{};", prefix, type_bounds_str))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-26 15:43:49 -06:00
|
|
|
pub fn rewrite_associated_impl_type(ident: ast::Ident,
|
|
|
|
defaultness: ast::Defaultness,
|
|
|
|
ty_opt: Option<&ptr::P<ast::Ty>>,
|
|
|
|
ty_param_bounds_opt: Option<&ast::TyParamBounds>,
|
|
|
|
context: &RewriteContext,
|
|
|
|
indent: Indent)
|
|
|
|
-> Option<String> {
|
|
|
|
let result =
|
|
|
|
try_opt!(rewrite_associated_type(ident, ty_opt, ty_param_bounds_opt, context, indent));
|
|
|
|
|
|
|
|
match defaultness {
|
|
|
|
ast::Defaultness::Default => Some(format!("default {}", result)),
|
|
|
|
_ => Some(result),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-08 13:56:33 -05:00
|
|
|
impl Rewrite for ast::FunctionRetTy {
|
2017-01-30 13:28:48 -06:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-09-08 13:56:33 -05:00
|
|
|
match *self {
|
2016-03-01 16:27:19 -06:00
|
|
|
ast::FunctionRetTy::Default(_) => Some(String::new()),
|
|
|
|
ast::FunctionRetTy::Ty(ref ty) => {
|
2017-01-30 13:28:48 -06:00
|
|
|
let inner_width = try_opt!(shape.width.checked_sub(3));
|
2017-03-27 17:14:47 -05:00
|
|
|
ty.rewrite(context, Shape::legacy(inner_width, shape.indent + 3))
|
|
|
|
.map(|r| format!("-> {}", r))
|
2015-09-02 15:57:22 -05:00
|
|
|
}
|
2015-04-23 01:10:43 -05:00
|
|
|
}
|
2015-04-21 02:59:48 -05:00
|
|
|
}
|
2015-08-19 15:39:45 -05:00
|
|
|
}
|
2015-04-21 02:59:48 -05:00
|
|
|
|
2015-09-11 05:24:13 -05:00
|
|
|
impl Rewrite for ast::Arg {
|
2017-01-30 13:28:48 -06:00
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2015-09-11 05:24:13 -05:00
|
|
|
if is_named_arg(self) {
|
2017-03-27 17:14:47 -05:00
|
|
|
let mut result = try_opt!(self.pat
|
|
|
|
.rewrite(context,
|
|
|
|
Shape::legacy(shape.width, shape.indent)));
|
2015-12-25 13:59:46 -06:00
|
|
|
|
2016-03-01 16:27:19 -06:00
|
|
|
if self.ty.node != ast::TyKind::Infer {
|
2016-08-02 20:07:56 -05:00
|
|
|
if context.config.space_before_type_annotation {
|
|
|
|
result.push_str(" ");
|
|
|
|
}
|
2016-09-16 18:44:51 -05:00
|
|
|
result.push_str(":");
|
2017-03-27 17:25:59 -05:00
|
|
|
if context.config.space_after_type_annotation_colon {
|
2016-09-16 18:44:51 -05:00
|
|
|
result.push_str(" ");
|
|
|
|
}
|
2017-01-30 13:28:48 -06:00
|
|
|
let max_width = try_opt!(shape.width.checked_sub(result.len()));
|
2017-03-27 17:14:47 -05:00
|
|
|
let ty_str = try_opt!(self.ty
|
|
|
|
.rewrite(context,
|
|
|
|
Shape::legacy(max_width,
|
|
|
|
shape.indent + result.len())));
|
2015-09-11 05:24:13 -05:00
|
|
|
result.push_str(&ty_str);
|
|
|
|
}
|
2015-12-25 13:59:46 -06:00
|
|
|
|
|
|
|
Some(result)
|
2015-07-19 07:33:02 -05:00
|
|
|
} else {
|
2017-01-30 13:28:48 -06:00
|
|
|
self.ty.rewrite(context, shape)
|
2015-07-19 07:33:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 13:59:46 -06:00
|
|
|
fn rewrite_explicit_self(explicit_self: &ast::ExplicitSelf,
|
|
|
|
args: &[ast::Arg],
|
|
|
|
context: &RewriteContext)
|
|
|
|
-> Option<String> {
|
2015-07-24 12:54:38 -05:00
|
|
|
match explicit_self.node {
|
2016-05-30 07:53:04 -05:00
|
|
|
ast::SelfKind::Region(lt, m) => {
|
2015-07-24 12:54:38 -05:00
|
|
|
let mut_str = format_mutability(m);
|
|
|
|
match lt {
|
2015-12-25 13:59:46 -06:00
|
|
|
Some(ref l) => {
|
2017-02-22 13:48:16 -06:00
|
|
|
let lifetime_str = try_opt!(l.rewrite(context,
|
|
|
|
Shape::legacy(usize::max_value(),
|
|
|
|
Indent::empty())));
|
2015-12-25 13:59:46 -06:00
|
|
|
Some(format!("&{} {}self", lifetime_str, mut_str))
|
|
|
|
}
|
2015-07-24 12:54:38 -05:00
|
|
|
None => Some(format!("&{}self", mut_str)),
|
|
|
|
}
|
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
ast::SelfKind::Explicit(ref ty, _) => {
|
2015-12-25 12:07:51 -06:00
|
|
|
assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
|
|
|
|
|
|
|
|
let mutability = explicit_self_mutability(&args[0]);
|
2017-02-21 21:20:50 -06:00
|
|
|
let type_str = try_opt!(ty.rewrite(context,
|
|
|
|
Shape::legacy(usize::max_value(), Indent::empty())));
|
2015-12-25 12:07:51 -06:00
|
|
|
|
2015-12-25 13:59:46 -06:00
|
|
|
Some(format!("{}self: {}", format_mutability(mutability), type_str))
|
2015-07-24 12:54:38 -05:00
|
|
|
}
|
2016-03-01 16:27:19 -06:00
|
|
|
ast::SelfKind::Value(_) => {
|
2015-12-25 12:07:51 -06:00
|
|
|
assert!(!args.is_empty(), "&[ast::Arg] shouldn't be empty.");
|
2015-07-24 12:54:38 -05:00
|
|
|
|
2015-12-25 12:07:51 -06:00
|
|
|
let mutability = explicit_self_mutability(&args[0]);
|
2015-07-24 12:54:38 -05:00
|
|
|
|
2015-12-25 12:07:51 -06:00
|
|
|
Some(format!("{}self", format_mutability(mutability)))
|
2015-07-24 12:54:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-25 12:07:51 -06:00
|
|
|
// Hacky solution caused by absence of `Mutability` in `SelfValue` and
|
|
|
|
// `SelfExplicit` variants of `ast::ExplicitSelf_`.
|
|
|
|
fn explicit_self_mutability(arg: &ast::Arg) -> ast::Mutability {
|
2016-03-01 16:27:19 -06:00
|
|
|
if let ast::PatKind::Ident(ast::BindingMode::ByValue(mutability), _, _) = arg.pat.node {
|
2015-12-25 12:07:51 -06:00
|
|
|
mutability
|
|
|
|
} else {
|
|
|
|
unreachable!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 15:39:45 -05:00
|
|
|
pub fn span_lo_for_arg(arg: &ast::Arg) -> BytePos {
|
2015-07-19 07:33:02 -05:00
|
|
|
if is_named_arg(arg) {
|
|
|
|
arg.pat.span.lo
|
|
|
|
} else {
|
|
|
|
arg.ty.span.lo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-19 15:39:45 -05:00
|
|
|
pub fn span_hi_for_arg(arg: &ast::Arg) -> BytePos {
|
|
|
|
match arg.ty.node {
|
2016-03-01 16:27:19 -06:00
|
|
|
ast::TyKind::Infer if is_named_arg(arg) => arg.pat.span.hi,
|
2015-08-19 15:39:45 -05:00
|
|
|
_ => arg.ty.span.hi,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-05 18:11:26 -06:00
|
|
|
pub fn is_named_arg(arg: &ast::Arg) -> bool {
|
2016-03-01 16:27:19 -06:00
|
|
|
if let ast::PatKind::Ident(_, ident, _) = arg.pat.node {
|
2016-12-23 13:13:00 -06:00
|
|
|
ident.node != symbol::keywords::Invalid.ident()
|
2015-07-19 07:33:02 -05:00
|
|
|
} else {
|
|
|
|
true
|
2015-04-20 23:47:15 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-21 05:50:43 -05:00
|
|
|
|
|
|
|
fn span_for_return(ret: &ast::FunctionRetTy) -> Span {
|
|
|
|
match *ret {
|
2016-03-01 16:27:19 -06:00
|
|
|
ast::FunctionRetTy::Default(ref span) => span.clone(),
|
|
|
|
ast::FunctionRetTy::Ty(ref ty) => ty.span,
|
2015-04-21 05:50:43 -05:00
|
|
|
}
|
|
|
|
}
|
2015-04-22 23:22:48 -05:00
|
|
|
|
|
|
|
fn span_for_ty_param(ty: &ast::TyParam) -> Span {
|
|
|
|
// Note that ty.span is the span for ty.ident, not the whole item.
|
|
|
|
let lo = ty.span.lo;
|
|
|
|
if let Some(ref def) = ty.default {
|
2015-10-18 17:25:38 -05:00
|
|
|
return mk_sp(lo, def.span.hi);
|
2015-04-22 23:22:48 -05:00
|
|
|
}
|
2015-08-24 16:54:47 -05:00
|
|
|
if ty.bounds.is_empty() {
|
2015-04-22 23:22:48 -05:00
|
|
|
return ty.span;
|
|
|
|
}
|
|
|
|
let hi = match ty.bounds[ty.bounds.len() - 1] {
|
|
|
|
ast::TyParamBound::TraitTyParamBound(ref ptr, _) => ptr.span.hi,
|
|
|
|
ast::TyParamBound::RegionTyParamBound(ref l) => l.span.hi,
|
|
|
|
};
|
2015-10-18 17:25:38 -05:00
|
|
|
mk_sp(lo, hi)
|
2015-04-22 23:22:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn span_for_where_pred(pred: &ast::WherePredicate) -> Span {
|
|
|
|
match *pred {
|
|
|
|
ast::WherePredicate::BoundPredicate(ref p) => p.span,
|
|
|
|
ast::WherePredicate::RegionPredicate(ref p) => p.span,
|
|
|
|
ast::WherePredicate::EqPredicate(ref p) => p.span,
|
|
|
|
}
|
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
// Return type is (result, force_new_line_for_brace)
|
|
|
|
fn rewrite_fn_base(context: &RewriteContext,
|
|
|
|
indent: Indent,
|
|
|
|
ident: ast::Ident,
|
|
|
|
fd: &ast::FnDecl,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
unsafety: ast::Unsafety,
|
|
|
|
constness: ast::Constness,
|
2016-05-31 12:48:49 -05:00
|
|
|
defaultness: ast::Defaultness,
|
2015-11-22 06:45:51 -06:00
|
|
|
abi: abi::Abi,
|
2016-05-01 22:01:46 -05:00
|
|
|
vis: &ast::Visibility,
|
2015-11-22 06:45:51 -06:00
|
|
|
span: Span,
|
|
|
|
newline_brace: bool,
|
2017-03-12 17:42:41 -05:00
|
|
|
has_body: bool,
|
|
|
|
has_braces: bool)
|
2015-11-22 06:45:51 -06:00
|
|
|
-> Option<(String, bool)> {
|
|
|
|
let mut force_new_line_for_brace = false;
|
|
|
|
|
|
|
|
let where_clause = &generics.where_clause;
|
|
|
|
|
|
|
|
let mut result = String::with_capacity(1024);
|
|
|
|
// Vis unsafety abi.
|
2016-05-27 17:58:25 -05:00
|
|
|
result.push_str(&*format_visibility(vis));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2016-05-31 12:48:49 -05:00
|
|
|
if let ast::Defaultness::Default = defaultness {
|
|
|
|
result.push_str("default ");
|
|
|
|
}
|
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
if let ast::Constness::Const = constness {
|
|
|
|
result.push_str("const ");
|
|
|
|
}
|
2015-11-22 09:07:38 -06:00
|
|
|
|
2016-03-02 09:37:20 -06:00
|
|
|
result.push_str(::utils::format_unsafety(unsafety));
|
|
|
|
|
2016-03-01 16:27:19 -06:00
|
|
|
if abi != abi::Abi::Rust {
|
2016-04-18 11:39:40 -05:00
|
|
|
result.push_str(&::utils::format_abi(abi, context.config.force_explicit_abi));
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// fn foo
|
|
|
|
result.push_str("fn ");
|
|
|
|
result.push_str(&ident.to_string());
|
|
|
|
|
|
|
|
// Generics.
|
|
|
|
let generics_indent = indent + result.len();
|
|
|
|
let generics_span = mk_sp(span.lo, span_for_return(&fd.output).lo);
|
|
|
|
let generics_str = try_opt!(rewrite_generics(context,
|
|
|
|
generics,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(context.config.max_width, indent),
|
2015-11-22 06:45:51 -06:00
|
|
|
generics_indent,
|
|
|
|
generics_span));
|
|
|
|
result.push_str(&generics_str);
|
|
|
|
|
2017-03-09 19:29:45 -06:00
|
|
|
let snuggle_angle_bracket = last_line_width(&generics_str) == 1;
|
|
|
|
|
2017-03-09 14:26:14 -06:00
|
|
|
// Note that the width and indent don't really matter, we'll re-layout the
|
2015-11-22 06:45:51 -06:00
|
|
|
// return type later anyway.
|
2017-03-27 17:14:47 -05:00
|
|
|
let ret_str = try_opt!(fd.output
|
|
|
|
.rewrite(&context,
|
|
|
|
Shape::legacy(context.config.max_width -
|
|
|
|
indent.width(),
|
|
|
|
indent)));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
let multi_line_ret_str = ret_str.contains('\n');
|
2016-05-29 10:58:38 -05:00
|
|
|
let ret_str_len = if multi_line_ret_str { 0 } else { ret_str.len() };
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
// Args.
|
2016-04-07 14:24:30 -05:00
|
|
|
let (mut one_line_budget, mut multi_line_budget, mut arg_indent) =
|
2016-05-26 18:49:26 -05:00
|
|
|
try_opt!(compute_budgets_for_args(context, &result, indent, ret_str_len, newline_brace));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-03-19 17:42:20 -05:00
|
|
|
if context.config.fn_args_layout == IndentStyle::Block {
|
2016-04-01 18:25:35 -05:00
|
|
|
arg_indent = indent.block_indent(context.config);
|
2016-04-07 14:24:30 -05:00
|
|
|
multi_line_budget = context.config.max_width - arg_indent.width();
|
2016-04-01 18:25:35 -05:00
|
|
|
}
|
|
|
|
|
2017-02-20 19:43:43 -06:00
|
|
|
debug!("rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, arg_indent: {:?}",
|
2015-11-22 06:45:51 -06:00
|
|
|
one_line_budget,
|
|
|
|
multi_line_budget,
|
|
|
|
arg_indent);
|
|
|
|
|
2017-01-25 20:44:54 -06:00
|
|
|
// Check if vertical layout was forced.
|
2015-12-01 15:09:37 -06:00
|
|
|
if one_line_budget == 0 {
|
2017-03-09 19:29:45 -06:00
|
|
|
if snuggle_angle_bracket {
|
|
|
|
result.push_str("(");
|
|
|
|
} else if context.config.fn_args_paren_newline {
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&arg_indent.to_string(context.config));
|
|
|
|
arg_indent = arg_indent + 1; // extra space for `(`
|
|
|
|
result.push('(');
|
2016-10-12 20:34:08 -05:00
|
|
|
if context.config.spaces_within_parens && fd.inputs.len() > 0 {
|
|
|
|
result.push(' ')
|
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
} else {
|
|
|
|
result.push_str("(\n");
|
|
|
|
result.push_str(&arg_indent.to_string(context.config));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push('(');
|
2016-10-12 20:34:08 -05:00
|
|
|
if context.config.spaces_within_parens && fd.inputs.len() > 0 {
|
|
|
|
result.push(' ')
|
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
if multi_line_ret_str {
|
|
|
|
one_line_budget = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A conservative estimation, to goal is to be over all parens in generics
|
2017-03-27 17:14:47 -05:00
|
|
|
let args_start = generics
|
|
|
|
.ty_params
|
|
|
|
.last()
|
|
|
|
.map_or(span.lo, |tp| end_typaram(tp));
|
|
|
|
let args_span = mk_sp(context
|
|
|
|
.codemap
|
|
|
|
.span_after(mk_sp(args_start, span.hi), "("),
|
2015-11-22 06:45:51 -06:00
|
|
|
span_for_return(&fd.output).lo);
|
|
|
|
let arg_str = try_opt!(rewrite_args(context,
|
|
|
|
&fd.inputs,
|
2016-05-30 07:53:04 -05:00
|
|
|
fd.get_self().as_ref(),
|
2015-11-22 06:45:51 -06:00
|
|
|
one_line_budget,
|
|
|
|
multi_line_budget,
|
|
|
|
indent,
|
|
|
|
arg_indent,
|
|
|
|
args_span,
|
|
|
|
fd.variadic));
|
2016-04-01 18:25:35 -05:00
|
|
|
|
|
|
|
let multi_line_arg_str = arg_str.contains('\n');
|
|
|
|
|
2017-03-12 17:25:46 -05:00
|
|
|
let put_args_in_block = match context.config.fn_args_layout {
|
2017-03-19 17:42:20 -05:00
|
|
|
IndentStyle::Block => multi_line_arg_str || generics_str.contains('\n'),
|
2017-03-12 17:25:46 -05:00
|
|
|
_ => false,
|
|
|
|
} && !fd.inputs.is_empty();
|
2016-04-01 18:25:35 -05:00
|
|
|
|
2016-04-07 14:01:16 -05:00
|
|
|
if put_args_in_block {
|
2016-04-01 18:25:35 -05:00
|
|
|
arg_indent = indent.block_indent(context.config);
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&arg_indent.to_string(context.config));
|
|
|
|
result.push_str(&arg_str);
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push('\n');
|
2016-04-01 16:59:35 -05:00
|
|
|
result.push_str(&indent.to_string(context.config));
|
2016-04-01 18:25:35 -05:00
|
|
|
result.push(')');
|
|
|
|
} else {
|
|
|
|
result.push_str(&arg_str);
|
2016-10-12 20:34:08 -05:00
|
|
|
if context.config.spaces_within_parens && fd.inputs.len() > 0 {
|
|
|
|
result.push(' ')
|
|
|
|
}
|
2016-04-01 18:25:35 -05:00
|
|
|
result.push(')');
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return type.
|
|
|
|
if !ret_str.is_empty() {
|
2016-04-07 14:01:16 -05:00
|
|
|
let ret_should_indent = match context.config.fn_args_layout {
|
2016-04-07 14:47:43 -05:00
|
|
|
// If our args are block layout then we surely must have space.
|
2017-03-19 17:42:20 -05:00
|
|
|
IndentStyle::Block if put_args_in_block => false,
|
2016-04-07 14:01:16 -05:00
|
|
|
_ => {
|
2016-07-31 23:25:00 -05:00
|
|
|
// If we've already gone multi-line, or the return type would push over the max
|
|
|
|
// width, then put the return type on a new line. With the +1 for the signature
|
|
|
|
// length an additional space between the closing parenthesis of the argument and
|
|
|
|
// the arrow '->' is considered.
|
|
|
|
let mut sig_length = result.len() + indent.width() + ret_str_len + 1;
|
|
|
|
|
|
|
|
// If there is no where clause, take into account the space after the return type
|
|
|
|
// and the brace.
|
|
|
|
if where_clause.predicates.is_empty() {
|
|
|
|
sig_length += 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
let overlong_sig = sig_length > context.config.max_width;
|
|
|
|
|
2016-08-23 09:14:45 -05:00
|
|
|
result.contains('\n') || multi_line_ret_str || overlong_sig
|
2016-04-07 14:01:16 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let ret_indent = if ret_should_indent {
|
2015-11-22 06:45:51 -06:00
|
|
|
let indent = match context.config.fn_return_indent {
|
|
|
|
ReturnIndent::WithWhereClause => indent + 4,
|
|
|
|
// Aligning with non-existent args looks silly.
|
2015-11-25 00:39:15 -06:00
|
|
|
_ if arg_str.is_empty() => {
|
2015-11-22 06:45:51 -06:00
|
|
|
force_new_line_for_brace = true;
|
|
|
|
indent + 4
|
|
|
|
}
|
|
|
|
// FIXME: we might want to check that using the arg indent
|
|
|
|
// doesn't blow our budget, and if it does, then fallback to
|
|
|
|
// the where clause indent.
|
|
|
|
_ => arg_indent,
|
|
|
|
};
|
|
|
|
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&indent.to_string(context.config));
|
|
|
|
indent
|
|
|
|
} else {
|
|
|
|
result.push(' ');
|
|
|
|
Indent::new(indent.width(), result.len())
|
|
|
|
};
|
|
|
|
|
2017-03-09 14:26:14 -06:00
|
|
|
if multi_line_ret_str || ret_should_indent {
|
2015-11-22 06:45:51 -06:00
|
|
|
// Now that we know the proper indent and width, we need to
|
|
|
|
// re-layout the return type.
|
2017-03-27 17:25:59 -05:00
|
|
|
let budget = try_opt!(context.config.max_width.checked_sub(ret_indent.width()));
|
2017-03-27 17:14:47 -05:00
|
|
|
let ret_str = try_opt!(fd.output
|
|
|
|
.rewrite(context, Shape::legacy(budget, ret_indent)));
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&ret_str);
|
|
|
|
} else {
|
|
|
|
result.push_str(&ret_str);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Comment between return type and the end of the decl.
|
|
|
|
let snippet_lo = fd.output.span().hi;
|
|
|
|
if where_clause.predicates.is_empty() {
|
|
|
|
let snippet_hi = span.hi;
|
|
|
|
let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
|
|
|
|
let snippet = snippet.trim();
|
|
|
|
if !snippet.is_empty() {
|
|
|
|
result.push(' ');
|
|
|
|
result.push_str(snippet);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// FIXME it would be nice to catch comments between the return type
|
|
|
|
// and the where clause, but we don't have a span for the where
|
|
|
|
// clause.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-07 14:47:43 -05:00
|
|
|
let should_compress_where = match context.config.where_density {
|
2016-04-07 14:01:16 -05:00
|
|
|
Density::Compressed => !result.contains('\n') || put_args_in_block,
|
|
|
|
Density::CompressedIfEmpty => !has_body && !result.contains('\n'),
|
|
|
|
_ => false,
|
|
|
|
} || (put_args_in_block && ret_str.is_empty());
|
|
|
|
|
2017-03-09 14:26:14 -06:00
|
|
|
if where_clause.predicates.len() == 1 && should_compress_where {
|
2017-03-27 17:14:47 -05:00
|
|
|
let budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(last_line_width(&result)));
|
2017-03-09 14:26:14 -06:00
|
|
|
if let Some(where_clause_str) =
|
|
|
|
rewrite_where_clause(context,
|
|
|
|
where_clause,
|
|
|
|
context.config.fn_brace_style,
|
|
|
|
Shape::legacy(budget, indent),
|
|
|
|
Density::Compressed,
|
|
|
|
"{",
|
2017-03-12 17:42:41 -05:00
|
|
|
!has_braces,
|
2017-03-09 14:26:14 -06:00
|
|
|
put_args_in_block && ret_str.is_empty(),
|
|
|
|
Some(span.hi)) {
|
2017-03-09 14:45:26 -06:00
|
|
|
if !where_clause_str.contains('\n') {
|
|
|
|
if last_line_width(&result) + where_clause_str.len() > context.config.max_width {
|
|
|
|
result.push('\n');
|
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-03-09 14:45:26 -06:00
|
|
|
result.push_str(&where_clause_str);
|
2017-03-09 14:26:14 -06:00
|
|
|
|
2017-03-09 14:45:26 -06:00
|
|
|
return Some((result, force_new_line_for_brace));
|
|
|
|
}
|
2017-03-09 14:26:14 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 17:14:47 -05:00
|
|
|
let budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(indent.block_indent));
|
2015-11-22 06:45:51 -06:00
|
|
|
let where_clause_str = try_opt!(rewrite_where_clause(context,
|
|
|
|
where_clause,
|
2015-12-01 12:51:49 -06:00
|
|
|
context.config.fn_brace_style,
|
2017-03-09 14:26:14 -06:00
|
|
|
Shape::legacy(budget, indent),
|
|
|
|
Density::Tall,
|
2015-11-22 06:45:51 -06:00
|
|
|
"{",
|
2017-03-12 17:42:41 -05:00
|
|
|
!has_braces,
|
2017-03-08 19:21:20 -06:00
|
|
|
put_args_in_block && ret_str.is_empty(),
|
2015-11-22 06:45:51 -06:00
|
|
|
Some(span.hi)));
|
2016-01-10 23:06:06 -06:00
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&where_clause_str);
|
|
|
|
|
|
|
|
Some((result, force_new_line_for_brace))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_args(context: &RewriteContext,
|
|
|
|
args: &[ast::Arg],
|
|
|
|
explicit_self: Option<&ast::ExplicitSelf>,
|
|
|
|
one_line_budget: usize,
|
|
|
|
multi_line_budget: usize,
|
|
|
|
indent: Indent,
|
|
|
|
arg_indent: Indent,
|
|
|
|
span: Span,
|
|
|
|
variadic: bool)
|
|
|
|
-> Option<String> {
|
2017-02-22 13:52:52 -06:00
|
|
|
let mut arg_item_strs =
|
|
|
|
try_opt!(args.iter()
|
|
|
|
.map(|arg| {
|
|
|
|
arg.rewrite(&context, Shape::legacy(multi_line_budget, arg_indent))
|
|
|
|
})
|
|
|
|
.collect::<Option<Vec<_>>>());
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
// Account for sugary self.
|
|
|
|
// FIXME: the comment for the self argument is dropped. This is blocked
|
|
|
|
// on rust issue #27522.
|
2017-03-27 16:58:41 -05:00
|
|
|
let min_args = explicit_self
|
|
|
|
.and_then(|explicit_self| rewrite_explicit_self(explicit_self, args, context))
|
|
|
|
.map_or(1, |self_str| {
|
|
|
|
arg_item_strs[0] = self_str;
|
|
|
|
2
|
|
|
|
});
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
// Comments between args.
|
|
|
|
let mut arg_items = Vec::new();
|
|
|
|
if min_args == 2 {
|
|
|
|
arg_items.push(ListItem::from_str(""));
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME(#21): if there are no args, there might still be a comment, but
|
|
|
|
// without spans for the comment or parens, there is no chance of
|
|
|
|
// getting it right. You also don't get to put a comment on self, unless
|
|
|
|
// it is explicit.
|
|
|
|
if args.len() >= min_args || variadic {
|
|
|
|
let comment_span_start = if min_args == 2 {
|
2016-03-27 06:07:28 -05:00
|
|
|
let second_arg_start = if arg_has_pattern(&args[1]) {
|
|
|
|
args[1].pat.span.lo
|
|
|
|
} else {
|
|
|
|
args[1].ty.span.lo
|
|
|
|
};
|
|
|
|
let reduced_span = mk_sp(span.lo, second_arg_start);
|
|
|
|
|
2017-03-27 17:25:59 -05:00
|
|
|
context.codemap.span_after_last(reduced_span, ",")
|
2015-11-22 06:45:51 -06:00
|
|
|
} else {
|
|
|
|
span.lo
|
|
|
|
};
|
|
|
|
|
|
|
|
enum ArgumentKind<'a> {
|
|
|
|
Regular(&'a ast::Arg),
|
|
|
|
Variadic(BytePos),
|
|
|
|
}
|
|
|
|
|
|
|
|
let variadic_arg = if variadic {
|
2017-03-27 17:14:47 -05:00
|
|
|
let variadic_span = mk_sp(args.last().unwrap().ty.span.hi, span.hi);
|
2017-03-27 17:25:59 -05:00
|
|
|
let variadic_start = context.codemap.span_after(variadic_span, "...") - BytePos(3);
|
2015-11-22 06:45:51 -06:00
|
|
|
Some(ArgumentKind::Variadic(variadic_start))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
let more_items = itemize_list(context.codemap,
|
|
|
|
args[min_args - 1..]
|
|
|
|
.iter()
|
|
|
|
.map(ArgumentKind::Regular)
|
|
|
|
.chain(variadic_arg),
|
|
|
|
")",
|
2016-11-20 13:37:35 -06:00
|
|
|
|arg| match *arg {
|
|
|
|
ArgumentKind::Regular(arg) => span_lo_for_arg(arg),
|
|
|
|
ArgumentKind::Variadic(start) => start,
|
2015-11-22 06:45:51 -06:00
|
|
|
},
|
2016-11-20 13:37:35 -06:00
|
|
|
|arg| match *arg {
|
|
|
|
ArgumentKind::Regular(arg) => arg.ty.span.hi,
|
|
|
|
ArgumentKind::Variadic(start) => start + BytePos(3),
|
2015-11-22 06:45:51 -06:00
|
|
|
},
|
2016-11-20 13:37:35 -06:00
|
|
|
|arg| match *arg {
|
|
|
|
ArgumentKind::Regular(..) => None,
|
|
|
|
ArgumentKind::Variadic(..) => Some("...".to_owned()),
|
2015-11-22 06:45:51 -06:00
|
|
|
},
|
|
|
|
comment_span_start,
|
|
|
|
span.hi);
|
|
|
|
|
|
|
|
arg_items.extend(more_items);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (item, arg) in arg_items.iter_mut().zip(arg_item_strs) {
|
|
|
|
item.item = Some(arg);
|
|
|
|
}
|
|
|
|
|
2017-03-19 17:59:08 -05:00
|
|
|
let (indent, trailing_comma, end_with_newline) = match context.config.fn_args_layout {
|
|
|
|
IndentStyle::Block => {
|
|
|
|
(indent.block_indent(context.config), SeparatorTactic::Vertical, true)
|
|
|
|
}
|
|
|
|
IndentStyle::Visual => (arg_indent, SeparatorTactic::Never, false),
|
2015-11-22 06:45:51 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
let tactic = definitive_tactic(&arg_items,
|
2017-03-27 17:25:59 -05:00
|
|
|
context.config.fn_args_density.to_list_tactic(),
|
2015-11-22 06:45:51 -06:00
|
|
|
one_line_budget);
|
|
|
|
let budget = match tactic {
|
|
|
|
DefinitiveListTactic::Horizontal => one_line_budget,
|
|
|
|
_ => multi_line_budget,
|
|
|
|
};
|
|
|
|
|
2016-04-07 14:24:30 -05:00
|
|
|
debug!("rewrite_args: budget: {}, tactic: {:?}", budget, tactic);
|
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: tactic,
|
|
|
|
separator: ",",
|
2017-03-08 19:47:29 -06:00
|
|
|
trailing_separator: trailing_comma,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape::legacy(budget, indent),
|
2016-03-13 16:27:13 -05:00
|
|
|
ends_with_newline: end_with_newline,
|
2015-11-22 06:45:51 -06:00
|
|
|
config: context.config,
|
|
|
|
};
|
|
|
|
|
|
|
|
write_list(&arg_items, &fmt)
|
|
|
|
}
|
|
|
|
|
2016-03-27 06:07:28 -05:00
|
|
|
fn arg_has_pattern(arg: &ast::Arg) -> bool {
|
2016-12-23 13:13:00 -06:00
|
|
|
if let ast::PatKind::Ident(_, ident, _) = arg.pat.node {
|
|
|
|
ident.node != symbol::keywords::Invalid.ident()
|
2016-03-27 06:07:28 -05:00
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
fn compute_budgets_for_args(context: &RewriteContext,
|
|
|
|
result: &str,
|
|
|
|
indent: Indent,
|
|
|
|
ret_str_len: usize,
|
|
|
|
newline_brace: bool)
|
2016-05-26 18:49:26 -05:00
|
|
|
-> Option<((usize, usize, Indent))> {
|
2017-01-25 20:44:54 -06:00
|
|
|
debug!("compute_budgets_for_args {} {:?}, {}, {}",
|
|
|
|
result.len(),
|
|
|
|
indent,
|
|
|
|
ret_str_len,
|
|
|
|
newline_brace);
|
2015-12-01 15:09:37 -06:00
|
|
|
// Try keeping everything on the same line.
|
2016-08-23 09:14:45 -05:00
|
|
|
if !result.contains('\n') {
|
2015-12-01 15:09:37 -06:00
|
|
|
// 3 = `() `, space is before ret_string.
|
2015-11-22 06:45:51 -06:00
|
|
|
let mut used_space = indent.width() + result.len() + ret_str_len + 3;
|
|
|
|
if !newline_brace {
|
|
|
|
used_space += 2;
|
|
|
|
}
|
2017-03-27 16:58:41 -05:00
|
|
|
let one_line_budget = context
|
|
|
|
.config
|
2017-03-06 14:40:08 -06:00
|
|
|
.max_width
|
|
|
|
.checked_sub(used_space)
|
|
|
|
.unwrap_or(0);
|
2015-12-01 15:09:37 -06:00
|
|
|
|
|
|
|
if one_line_budget > 0 {
|
2016-05-26 18:49:26 -05:00
|
|
|
// 4 = "() {".len()
|
2017-03-27 17:14:47 -05:00
|
|
|
let multi_line_budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(indent.width() + result.len() +
|
|
|
|
4));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2016-05-26 18:49:26 -05:00
|
|
|
return Some((one_line_budget, multi_line_budget, indent + result.len() + 1));
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Didn't work. we must force vertical layout and put args on a newline.
|
|
|
|
let new_indent = indent.block_indent(context.config);
|
2016-05-26 18:49:26 -05:00
|
|
|
let used_space = new_indent.width() + 4; // Account for `(` and `)` and possibly ` {`.
|
2015-11-22 06:45:51 -06:00
|
|
|
let max_space = context.config.max_width;
|
|
|
|
if used_space <= max_space {
|
2016-05-26 18:49:26 -05:00
|
|
|
Some((0, max_space - used_space, new_indent))
|
2015-11-22 06:45:51 -06:00
|
|
|
} else {
|
|
|
|
// Whoops! bankrupt.
|
2016-05-26 18:49:26 -05:00
|
|
|
None
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> bool {
|
|
|
|
match config.fn_brace_style {
|
|
|
|
BraceStyle::AlwaysNextLine => true,
|
|
|
|
BraceStyle::SameLineWhere if !where_clause.predicates.is_empty() => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_generics(context: &RewriteContext,
|
|
|
|
generics: &ast::Generics,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape,
|
|
|
|
// TODO shouldn't need this
|
2015-11-22 06:45:51 -06:00
|
|
|
generics_offset: Indent,
|
|
|
|
span: Span)
|
|
|
|
-> Option<String> {
|
|
|
|
// FIXME: convert bounds to where clauses where they get too big or if
|
|
|
|
// there is a where clause at all.
|
|
|
|
let lifetimes: &[_] = &generics.lifetimes;
|
|
|
|
let tys: &[_] = &generics.ty_params;
|
|
|
|
if lifetimes.is_empty() && tys.is_empty() {
|
|
|
|
return Some(String::new());
|
|
|
|
}
|
|
|
|
|
|
|
|
let offset = match context.config.generics_indent {
|
2017-03-19 17:42:20 -05:00
|
|
|
IndentStyle::Block => shape.indent.block_indent(context.config),
|
2015-11-22 06:45:51 -06:00
|
|
|
// 1 = <
|
2017-03-19 17:42:20 -05:00
|
|
|
IndentStyle::Visual => generics_offset + 1,
|
2015-11-22 06:45:51 -06:00
|
|
|
};
|
|
|
|
|
2017-03-27 17:25:59 -05:00
|
|
|
let h_budget = try_opt!(shape.width.checked_sub(generics_offset.width() + 2));
|
2015-11-22 06:45:51 -06:00
|
|
|
// FIXME: might need to insert a newline if the generics are really long.
|
|
|
|
|
|
|
|
// Strings for the generics.
|
2017-03-27 17:14:47 -05:00
|
|
|
let lt_strs = lifetimes
|
|
|
|
.iter()
|
|
|
|
.map(|lt| lt.rewrite(context, Shape::legacy(h_budget, offset)));
|
|
|
|
let ty_strs = tys.iter()
|
|
|
|
.map(|ty_param| ty_param.rewrite(context, Shape::legacy(h_budget, offset)));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
// Extract comments between generics.
|
2017-03-27 17:14:47 -05:00
|
|
|
let lt_spans = lifetimes
|
|
|
|
.iter()
|
|
|
|
.map(|l| {
|
|
|
|
let hi = if l.bounds.is_empty() {
|
|
|
|
l.lifetime.span.hi
|
|
|
|
} else {
|
|
|
|
l.bounds[l.bounds.len() - 1].span.hi
|
|
|
|
};
|
|
|
|
mk_sp(l.lifetime.span.lo, hi)
|
|
|
|
});
|
2015-11-22 06:45:51 -06:00
|
|
|
let ty_spans = tys.iter().map(span_for_ty_param);
|
|
|
|
|
|
|
|
let items = itemize_list(context.codemap,
|
2017-03-27 17:25:59 -05:00
|
|
|
lt_spans.chain(ty_spans).zip(lt_strs.chain(ty_strs)),
|
2015-11-22 06:45:51 -06:00
|
|
|
">",
|
|
|
|
|&(sp, _)| sp.lo,
|
|
|
|
|&(sp, _)| sp.hi,
|
|
|
|
// FIXME: don't clone
|
|
|
|
|&(_, ref str)| str.clone(),
|
2016-03-07 12:41:32 -06:00
|
|
|
context.codemap.span_after(span, "<"),
|
2015-11-22 06:45:51 -06:00
|
|
|
span.hi);
|
2017-01-30 13:28:48 -06:00
|
|
|
let list_str =
|
|
|
|
try_opt!(format_item_list(items, Shape::legacy(h_budget, offset), context.config));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-03-19 17:42:20 -05:00
|
|
|
let result = if context.config.generics_indent != IndentStyle::Visual &&
|
2017-03-09 21:06:05 -06:00
|
|
|
list_str.contains('\n') {
|
|
|
|
format!("<\n{}{}\n{}>",
|
|
|
|
offset.to_string(context.config),
|
|
|
|
list_str,
|
|
|
|
shape.indent.to_string(context.config))
|
2017-03-09 19:29:45 -06:00
|
|
|
} else if context.config.spaces_within_angle_brackets {
|
|
|
|
format!("< {} >", list_str)
|
|
|
|
} else {
|
|
|
|
format!("<{}>", list_str)
|
|
|
|
};
|
|
|
|
Some(result)
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
|
2016-03-11 15:18:30 -06:00
|
|
|
fn rewrite_trait_bounds(context: &RewriteContext,
|
2016-03-11 23:41:22 -06:00
|
|
|
type_param_bounds: &ast::TyParamBounds,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape)
|
2016-03-11 23:41:22 -06:00
|
|
|
-> Option<String> {
|
2016-08-23 09:14:45 -05:00
|
|
|
let bounds: &[_] = type_param_bounds;
|
2016-03-11 15:18:30 -06:00
|
|
|
|
|
|
|
if bounds.is_empty() {
|
|
|
|
return Some(String::new());
|
|
|
|
}
|
|
|
|
|
2017-03-27 16:58:41 -05:00
|
|
|
let bound_str = try_opt!(bounds
|
|
|
|
.iter()
|
2017-02-21 21:20:50 -06:00
|
|
|
.map(|ty_bound| ty_bound.rewrite(&context, shape))
|
|
|
|
.intersperse(Some(" + ".to_string()))
|
|
|
|
.collect::<Option<String>>());
|
2016-03-11 15:18:30 -06:00
|
|
|
|
|
|
|
let mut result = String::new();
|
2016-03-12 12:09:27 -06:00
|
|
|
result.push_str(": ");
|
2016-03-11 15:18:30 -06:00
|
|
|
result.push_str(&bound_str);
|
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:21:20 -06:00
|
|
|
// fn reflow_list_node_with_rule(
|
|
|
|
// &self,
|
|
|
|
// node: &CompoundNode,
|
|
|
|
// rule: &Rule,
|
|
|
|
// args: &[Arg],
|
|
|
|
// shape: &Shape
|
|
|
|
// ) -> Result<String, ()>
|
|
|
|
// where
|
|
|
|
// T: Foo,
|
|
|
|
// {
|
|
|
|
|
|
|
|
|
|
|
|
fn rewrite_where_clause_rfc_style(context: &RewriteContext,
|
|
|
|
where_clause: &ast::WhereClause,
|
|
|
|
shape: Shape,
|
|
|
|
terminator: &str,
|
|
|
|
suppress_comma: bool,
|
|
|
|
// where clause can be kept on the current line.
|
|
|
|
snuggle: bool,
|
|
|
|
span_end: Option<BytePos>)
|
|
|
|
-> Option<String> {
|
|
|
|
let block_shape = shape.block();
|
|
|
|
|
|
|
|
let starting_newline = if snuggle {
|
|
|
|
" ".to_owned()
|
|
|
|
} else {
|
|
|
|
"\n".to_owned() + &block_shape.indent.to_string(context.config)
|
|
|
|
};
|
|
|
|
|
|
|
|
let clause_shape = block_shape.block_indent(context.config.tab_spaces);
|
|
|
|
// each clause on one line, trailing comma (except if suppress_comma)
|
|
|
|
let span_start = span_for_where_pred(&where_clause.predicates[0]).lo;
|
|
|
|
// If we don't have the start of the next span, then use the end of the
|
|
|
|
// predicates, but that means we miss comments.
|
|
|
|
let len = where_clause.predicates.len();
|
|
|
|
let end_of_preds = span_for_where_pred(&where_clause.predicates[len - 1]).hi;
|
|
|
|
let span_end = span_end.unwrap_or(end_of_preds);
|
|
|
|
let items = itemize_list(context.codemap,
|
|
|
|
where_clause.predicates.iter(),
|
|
|
|
terminator,
|
|
|
|
|pred| span_for_where_pred(pred).lo,
|
|
|
|
|pred| span_for_where_pred(pred).hi,
|
|
|
|
|pred| pred.rewrite(context, clause_shape),
|
|
|
|
span_start,
|
|
|
|
span_end);
|
|
|
|
let comma_tactic = if suppress_comma {
|
|
|
|
SeparatorTactic::Never
|
|
|
|
} else {
|
|
|
|
SeparatorTactic::Always
|
|
|
|
};
|
|
|
|
|
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: DefinitiveListTactic::Vertical,
|
|
|
|
separator: ",",
|
|
|
|
trailing_separator: comma_tactic,
|
|
|
|
shape: clause_shape,
|
|
|
|
ends_with_newline: true,
|
|
|
|
config: context.config,
|
|
|
|
};
|
|
|
|
let preds_str = try_opt!(write_list(items, &fmt));
|
|
|
|
|
|
|
|
Some(format!("{}where\n{}{}",
|
|
|
|
starting_newline,
|
|
|
|
clause_shape.indent.to_string(context.config),
|
|
|
|
preds_str))
|
|
|
|
}
|
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
fn rewrite_where_clause(context: &RewriteContext,
|
|
|
|
where_clause: &ast::WhereClause,
|
2015-12-01 12:51:49 -06:00
|
|
|
brace_style: BraceStyle,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape,
|
2015-11-22 06:45:51 -06:00
|
|
|
density: Density,
|
|
|
|
terminator: &str,
|
2017-03-01 19:25:57 -06:00
|
|
|
suppress_comma: bool,
|
2017-03-08 19:21:20 -06:00
|
|
|
snuggle: bool,
|
2015-11-22 06:45:51 -06:00
|
|
|
span_end: Option<BytePos>)
|
|
|
|
-> Option<String> {
|
|
|
|
if where_clause.predicates.is_empty() {
|
|
|
|
return Some(String::new());
|
|
|
|
}
|
|
|
|
|
2017-03-08 19:21:20 -06:00
|
|
|
if context.config.where_style == Style::Rfc {
|
|
|
|
return rewrite_where_clause_rfc_style(context,
|
|
|
|
where_clause,
|
|
|
|
shape,
|
|
|
|
terminator,
|
|
|
|
suppress_comma,
|
|
|
|
snuggle,
|
|
|
|
span_end);
|
|
|
|
}
|
|
|
|
|
2017-03-19 17:07:33 -05:00
|
|
|
let extra_indent = Indent::new(context.config.tab_spaces, 0);
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
let offset = match context.config.where_pred_indent {
|
2017-03-19 17:42:20 -05:00
|
|
|
IndentStyle::Block => shape.indent + extra_indent.block_indent(context.config),
|
2015-11-22 06:45:51 -06:00
|
|
|
// 6 = "where ".len()
|
2017-03-19 17:42:20 -05:00
|
|
|
IndentStyle::Visual => shape.indent + extra_indent + 6,
|
2015-11-22 06:45:51 -06:00
|
|
|
};
|
|
|
|
// FIXME: if where_pred_indent != Visual, then the budgets below might
|
|
|
|
// be out by a char or two.
|
|
|
|
|
|
|
|
let budget = context.config.max_width - offset.width();
|
|
|
|
let span_start = span_for_where_pred(&where_clause.predicates[0]).lo;
|
|
|
|
// If we don't have the start of the next span, then use the end of the
|
|
|
|
// predicates, but that means we miss comments.
|
|
|
|
let len = where_clause.predicates.len();
|
|
|
|
let end_of_preds = span_for_where_pred(&where_clause.predicates[len - 1]).hi;
|
|
|
|
let span_end = span_end.unwrap_or(end_of_preds);
|
|
|
|
let items = itemize_list(context.codemap,
|
|
|
|
where_clause.predicates.iter(),
|
|
|
|
terminator,
|
|
|
|
|pred| span_for_where_pred(pred).lo,
|
|
|
|
|pred| span_for_where_pred(pred).hi,
|
2017-01-30 13:28:48 -06:00
|
|
|
|pred| pred.rewrite(context, Shape::legacy(budget, offset)),
|
2015-11-22 06:45:51 -06:00
|
|
|
span_start,
|
|
|
|
span_end);
|
|
|
|
let item_vec = items.collect::<Vec<_>>();
|
|
|
|
// FIXME: we don't need to collect here if the where_layout isn't
|
|
|
|
// HorizontalVertical.
|
|
|
|
let tactic = definitive_tactic(&item_vec, context.config.where_layout, budget);
|
2017-02-23 15:31:23 -06:00
|
|
|
|
|
|
|
let mut comma_tactic = context.config.trailing_comma;
|
|
|
|
// Kind of a hack because we don't usually have trailing commas in where clauses.
|
2017-03-01 19:25:57 -06:00
|
|
|
if comma_tactic == SeparatorTactic::Vertical || suppress_comma {
|
2017-02-23 15:31:23 -06:00
|
|
|
comma_tactic = SeparatorTactic::Never;
|
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: tactic,
|
|
|
|
separator: ",",
|
2017-02-23 15:31:23 -06:00
|
|
|
trailing_separator: comma_tactic,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape::legacy(budget, offset),
|
2015-11-22 06:45:51 -06:00
|
|
|
ends_with_newline: true,
|
|
|
|
config: context.config,
|
|
|
|
};
|
|
|
|
let preds_str = try_opt!(write_list(&item_vec, &fmt));
|
|
|
|
|
2015-11-28 06:59:14 -06:00
|
|
|
let end_length = if terminator == "{" {
|
|
|
|
// If the brace is on the next line we don't need to count it otherwise it needs two
|
|
|
|
// characters " {"
|
2015-12-01 12:51:49 -06:00
|
|
|
match brace_style {
|
2016-08-23 09:14:45 -05:00
|
|
|
BraceStyle::AlwaysNextLine |
|
2015-11-28 06:59:14 -06:00
|
|
|
BraceStyle::SameLineWhere => 0,
|
2016-08-23 09:14:45 -05:00
|
|
|
BraceStyle::PreferSameLine => 2,
|
2015-11-28 06:59:14 -06:00
|
|
|
}
|
2015-12-02 12:38:05 -06:00
|
|
|
} else if terminator == "=" {
|
|
|
|
2
|
2015-11-27 02:25:31 -06:00
|
|
|
} else {
|
2015-11-28 06:59:14 -06:00
|
|
|
terminator.len()
|
2015-11-27 02:25:31 -06:00
|
|
|
};
|
2015-11-22 06:45:51 -06:00
|
|
|
if density == Density::Tall || preds_str.contains('\n') ||
|
2017-01-30 13:28:48 -06:00
|
|
|
shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width {
|
2015-11-22 06:45:51 -06:00
|
|
|
Some(format!("\n{}where {}",
|
2017-01-30 13:28:48 -06:00
|
|
|
(shape.indent + extra_indent).to_string(context.config),
|
2015-11-22 06:45:51 -06:00
|
|
|
preds_str))
|
|
|
|
} else {
|
|
|
|
Some(format!(" where {}", preds_str))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-27 17:58:25 -05:00
|
|
|
fn format_header(item_name: &str, ident: ast::Ident, vis: &ast::Visibility) -> String {
|
|
|
|
format!("{}{}{}", format_visibility(vis), item_name, ident)
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn format_generics(context: &RewriteContext,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
opener: &str,
|
|
|
|
terminator: &str,
|
|
|
|
brace_style: BraceStyle,
|
|
|
|
force_same_line_brace: bool,
|
|
|
|
offset: Indent,
|
|
|
|
generics_offset: Indent,
|
|
|
|
span: Span)
|
|
|
|
-> Option<String> {
|
2015-12-02 12:38:05 -06:00
|
|
|
let mut result = try_opt!(rewrite_generics(context,
|
|
|
|
generics,
|
2017-01-30 13:28:48 -06:00
|
|
|
Shape::legacy(context.config.max_width, offset),
|
2015-12-02 12:38:05 -06:00
|
|
|
generics_offset,
|
|
|
|
span));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
|
2017-03-27 17:14:47 -05:00
|
|
|
let budget = try_opt!(context
|
|
|
|
.config
|
|
|
|
.max_width
|
|
|
|
.checked_sub(last_line_width(&result)));
|
2017-03-09 21:06:05 -06:00
|
|
|
let where_clause_str =
|
|
|
|
try_opt!(rewrite_where_clause(context,
|
|
|
|
&generics.where_clause,
|
|
|
|
brace_style,
|
|
|
|
Shape::legacy(budget, offset.block_only()),
|
|
|
|
Density::Tall,
|
|
|
|
terminator,
|
|
|
|
false,
|
|
|
|
trimmed_last_line_width(&result) == 1,
|
|
|
|
Some(span.hi)));
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&where_clause_str);
|
2017-03-09 21:06:05 -06:00
|
|
|
let same_line_brace = force_same_line_brace ||
|
|
|
|
(generics.where_clause.predicates.is_empty() &&
|
|
|
|
trimmed_last_line_width(&result) == 1);
|
2017-03-09 19:29:45 -06:00
|
|
|
if !same_line_brace &&
|
2015-11-22 06:45:51 -06:00
|
|
|
(brace_style == BraceStyle::SameLineWhere || brace_style == BraceStyle::AlwaysNextLine) {
|
|
|
|
result.push('\n');
|
2017-02-20 19:43:43 -06:00
|
|
|
result.push_str(&offset.block_only().to_string(context.config));
|
2015-11-22 06:45:51 -06:00
|
|
|
} else {
|
|
|
|
result.push(' ');
|
|
|
|
}
|
|
|
|
result.push_str(opener);
|
|
|
|
} else {
|
2017-03-09 21:06:05 -06:00
|
|
|
if force_same_line_brace || trimmed_last_line_width(&result) == 1 ||
|
|
|
|
brace_style != BraceStyle::AlwaysNextLine {
|
2017-03-09 19:29:45 -06:00
|
|
|
result.push(' ');
|
|
|
|
} else {
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push('\n');
|
2017-02-20 19:43:43 -06:00
|
|
|
result.push_str(&offset.block_only().to_string(context.config));
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
result.push_str(opener);
|
|
|
|
}
|
|
|
|
|
|
|
|
Some(result)
|
|
|
|
}
|