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-09-29 01:08:48 -05:00
|
|
|
use std::borrow::Cow;
|
2018-04-04 00:57:55 -05:00
|
|
|
use std::cmp::{min, Ordering};
|
2017-07-20 17:17:49 -05:00
|
|
|
|
2018-02-07 07:48:05 -06:00
|
|
|
use config::lists::*;
|
2018-02-23 07:37:10 -06:00
|
|
|
use regex::Regex;
|
2018-04-28 02:08:58 -05:00
|
|
|
use rustc_target::spec::abi;
|
2018-03-06 04:47:28 -06:00
|
|
|
use syntax::codemap::{self, BytePos, Span};
|
2017-09-28 22:45:42 -05:00
|
|
|
use syntax::visit;
|
2018-04-28 02:08:58 -05:00
|
|
|
use syntax::{ast, ptr, symbol};
|
2017-07-13 04:42:14 -05:00
|
|
|
|
2017-07-28 22:51:45 -05:00
|
|
|
use codemap::{LineRangeUtils, SpanUtils};
|
2018-04-29 07:22:48 -05:00
|
|
|
use comment::{
|
|
|
|
combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
|
|
|
|
recover_missing_comment_in_span, rewrite_missing_comment, FindUncommented,
|
|
|
|
};
|
2017-11-23 18:19:36 -06:00
|
|
|
use config::{BraceStyle, Config, Density, IndentStyle};
|
2018-04-29 07:22:48 -05:00
|
|
|
use expr::{
|
|
|
|
format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, rewrite_assign_rhs_with,
|
|
|
|
ExprType, RhsTactics,
|
|
|
|
};
|
2018-02-07 07:48:05 -06:00
|
|
|
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
|
2018-04-06 09:09:45 -05:00
|
|
|
use macros::{rewrite_macro, MacroPosition};
|
2018-03-07 00:40:52 -06:00
|
|
|
use overflow;
|
2018-03-11 23:24:04 -05:00
|
|
|
use rewrite::{Rewrite, RewriteContext};
|
2017-09-17 01:23:25 -05:00
|
|
|
use shape::{Indent, Shape};
|
2017-12-23 09:28:58 -06:00
|
|
|
use spanned::Spanned;
|
2018-04-27 23:24:05 -05:00
|
|
|
use utils::*;
|
2017-07-03 04:54:41 -05:00
|
|
|
use vertical::rewrite_with_alignment;
|
2017-07-13 04:42:14 -05:00
|
|
|
use visitor::FmtVisitor;
|
2015-04-20 23:47:15 -05:00
|
|
|
|
2018-03-06 04:47:28 -06:00
|
|
|
const DEFAULT_VISIBILITY: ast::Visibility = codemap::Spanned {
|
|
|
|
node: ast::VisibilityKind::Inherited,
|
|
|
|
span: codemap::DUMMY_SP,
|
|
|
|
};
|
|
|
|
|
2017-06-05 20:11:17 -05:00
|
|
|
fn type_annotation_separator(config: &Config) -> &str {
|
2017-11-14 08:25:36 -06:00
|
|
|
colon_spaces(config.space_before_colon(), config.space_after_colon())
|
2017-06-05 20:11:17 -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> {
|
2017-06-11 22:58:58 -05:00
|
|
|
debug!(
|
|
|
|
"Local::rewrite {:?} {} {:?}",
|
2017-12-02 20:38:16 -06:00
|
|
|
self, shape.width, shape.indent
|
2017-06-11 22:58:58 -05:00
|
|
|
);
|
2017-07-28 22:51:45 -05:00
|
|
|
|
|
|
|
skip_out_of_file_lines_range!(context, self.span);
|
|
|
|
|
2017-08-30 05:27:36 -05:00
|
|
|
if contains_skip(&self.attrs) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
let attrs_str = self.attrs.rewrite(context, shape)?;
|
2017-08-30 05:27:36 -05:00
|
|
|
let mut result = if attrs_str.is_empty() {
|
|
|
|
"let ".to_owned()
|
|
|
|
} else {
|
2017-10-05 06:50:19 -05:00
|
|
|
combine_strs_with_missing_comments(
|
2017-08-30 05:27:36 -05:00
|
|
|
context,
|
|
|
|
&attrs_str,
|
|
|
|
"let ",
|
2017-08-30 21:11:52 -05:00
|
|
|
mk_sp(
|
|
|
|
self.attrs.last().map(|a| a.span.hi()).unwrap(),
|
|
|
|
self.span.lo(),
|
|
|
|
),
|
2017-08-30 05:27:36 -05:00
|
|
|
shape,
|
|
|
|
false,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?
|
2017-08-30 05:27:36 -05:00
|
|
|
};
|
2015-10-24 05:19:58 -05:00
|
|
|
|
2017-04-19 08:48:45 -05:00
|
|
|
// 4 = "let ".len()
|
2017-10-05 06:50:19 -05:00
|
|
|
let pat_shape = shape.offset_left(4)?;
|
2017-03-20 17:23:59 -05:00
|
|
|
// 1 = ;
|
2017-10-05 06:50:19 -05:00
|
|
|
let pat_shape = pat_shape.sub_width(1)?;
|
|
|
|
let pat_str = 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 = {
|
2017-08-27 10:24:10 -05:00
|
|
|
let mut infix = String::with_capacity(32);
|
2015-10-24 05:19:58 -05:00
|
|
|
|
|
|
|
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-10-05 06:50:19 -05:00
|
|
|
let budget = shape.width.checked_sub(indent.width() + 1)?;
|
|
|
|
let rewrite = 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-10-05 06:50:19 -05:00
|
|
|
let nested_shape = shape.sub_width(1)?;
|
2015-10-24 05:19:58 -05:00
|
|
|
|
2017-11-15 20:26:36 -06:00
|
|
|
result = 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
|
|
|
|
2018-06-21 21:41:17 -05:00
|
|
|
// FIXME convert to using rewrite style rather than visitor
|
|
|
|
// FIXME 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,
|
2017-09-29 01:08:48 -05:00
|
|
|
abi: Cow<'static, str>,
|
2017-01-12 19:07:42 -06:00
|
|
|
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> {
|
|
|
|
Item {
|
|
|
|
keyword: "",
|
2017-09-29 01:08:48 -05:00
|
|
|
abi: format_abi(fm.abi, config.force_explicit_abi(), true),
|
2017-01-12 19:07:42 -06:00
|
|
|
vis: None,
|
2018-05-07 16:25:48 -05:00
|
|
|
body: fm
|
|
|
|
.items
|
2017-03-06 14:40:08 -06:00
|
|
|
.iter()
|
|
|
|
.map(|i| BodyElement::ForeignItem(i))
|
|
|
|
.collect(),
|
2018-01-21 22:05:18 -06:00
|
|
|
span,
|
2017-01-12 19:07:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum BodyElement<'a> {
|
|
|
|
// Stmt(&'a ast::Stmt),
|
|
|
|
// Field(&'a ast::Field),
|
|
|
|
// Variant(&'a ast::Variant),
|
|
|
|
// Item(&'a ast::Item),
|
|
|
|
ForeignItem(&'a ast::ForeignItem),
|
|
|
|
}
|
|
|
|
|
2017-09-28 22:45:42 -05:00
|
|
|
/// Represents a fn's signature.
|
|
|
|
pub struct FnSig<'a> {
|
|
|
|
decl: &'a ast::FnDecl,
|
|
|
|
generics: &'a ast::Generics,
|
|
|
|
abi: abi::Abi,
|
|
|
|
constness: ast::Constness,
|
|
|
|
defaultness: ast::Defaultness,
|
|
|
|
unsafety: ast::Unsafety,
|
|
|
|
visibility: ast::Visibility,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> FnSig<'a> {
|
|
|
|
pub fn new(
|
|
|
|
decl: &'a ast::FnDecl,
|
|
|
|
generics: &'a ast::Generics,
|
|
|
|
vis: ast::Visibility,
|
|
|
|
) -> FnSig<'a> {
|
|
|
|
FnSig {
|
2018-01-21 22:05:18 -06:00
|
|
|
decl,
|
|
|
|
generics,
|
2017-09-28 22:45:42 -05:00
|
|
|
abi: abi::Abi::Rust,
|
|
|
|
constness: ast::Constness::NotConst,
|
|
|
|
defaultness: ast::Defaultness::Final,
|
|
|
|
unsafety: ast::Unsafety::Normal,
|
|
|
|
visibility: vis,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-15 12:28:00 -05:00
|
|
|
pub fn from_method_sig(
|
|
|
|
method_sig: &'a ast::MethodSig,
|
|
|
|
generics: &'a ast::Generics,
|
|
|
|
) -> FnSig<'a> {
|
2017-09-28 22:45:42 -05:00
|
|
|
FnSig {
|
2018-06-28 02:26:10 -05:00
|
|
|
unsafety: method_sig.header.unsafety,
|
|
|
|
constness: method_sig.header.constness.node,
|
2017-09-28 22:45:42 -05:00
|
|
|
defaultness: ast::Defaultness::Final,
|
2018-06-28 02:26:10 -05:00
|
|
|
abi: method_sig.header.abi,
|
2017-09-28 22:45:42 -05:00
|
|
|
decl: &*method_sig.decl,
|
2018-01-21 22:05:18 -06:00
|
|
|
generics,
|
2018-03-06 04:47:28 -06:00
|
|
|
visibility: DEFAULT_VISIBILITY,
|
2017-09-28 22:45:42 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_fn_kind(
|
|
|
|
fn_kind: &'a visit::FnKind,
|
2017-10-15 12:28:00 -05:00
|
|
|
generics: &'a ast::Generics,
|
2017-09-28 22:45:42 -05:00
|
|
|
decl: &'a ast::FnDecl,
|
2018-04-01 07:47:55 -05:00
|
|
|
defaultness: ast::Defaultness,
|
2017-09-28 22:45:42 -05:00
|
|
|
) -> FnSig<'a> {
|
|
|
|
match *fn_kind {
|
2018-06-28 02:26:10 -05:00
|
|
|
visit::FnKind::ItemFn(_, fn_header, visibility, _) => FnSig {
|
2018-01-21 22:05:18 -06:00
|
|
|
decl,
|
|
|
|
generics,
|
2018-06-28 02:26:10 -05:00
|
|
|
abi: fn_header.abi,
|
|
|
|
constness: fn_header.constness.node,
|
2018-04-01 07:47:55 -05:00
|
|
|
defaultness,
|
2018-06-28 02:26:10 -05:00
|
|
|
unsafety: fn_header.unsafety,
|
2017-09-28 22:45:42 -05:00
|
|
|
visibility: visibility.clone(),
|
|
|
|
},
|
2017-10-26 02:54:41 -05:00
|
|
|
visit::FnKind::Method(_, method_sig, vis, _) => {
|
2017-10-15 12:28:00 -05:00
|
|
|
let mut fn_sig = FnSig::from_method_sig(method_sig, generics);
|
2018-04-01 07:47:55 -05:00
|
|
|
fn_sig.defaultness = defaultness;
|
2017-09-28 22:45:42 -05:00
|
|
|
if let Some(vis) = vis {
|
|
|
|
fn_sig.visibility = vis.clone();
|
|
|
|
}
|
|
|
|
fn_sig
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
2017-09-29 01:09:13 -05:00
|
|
|
|
|
|
|
fn to_str(&self, context: &RewriteContext) -> String {
|
|
|
|
let mut result = String::with_capacity(128);
|
|
|
|
// Vis defaultness constness unsafety abi.
|
2018-06-25 09:36:45 -05:00
|
|
|
result.push_str(&*format_visibility(context, &self.visibility));
|
2017-09-29 01:09:13 -05:00
|
|
|
result.push_str(format_defaultness(self.defaultness));
|
|
|
|
result.push_str(format_constness(self.constness));
|
|
|
|
result.push_str(format_unsafety(self.unsafety));
|
|
|
|
result.push_str(&format_abi(
|
|
|
|
self.abi,
|
|
|
|
context.config.force_explicit_abi(),
|
|
|
|
false,
|
|
|
|
));
|
|
|
|
result
|
|
|
|
}
|
2017-09-28 22:45:42 -05:00
|
|
|
}
|
|
|
|
|
2015-10-24 05:19:58 -05:00
|
|
|
impl<'a> FmtVisitor<'a> {
|
2017-12-11 22:48:12 -06:00
|
|
|
fn format_item(&mut self, item: &Item) {
|
|
|
|
self.buffer.push_str(&item.abi);
|
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();
|
|
|
|
|
2017-12-08 01:59:04 -06:00
|
|
|
self.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-08-19 13:47:40 -05: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() {
|
2017-08-19 13:47:40 -05:00
|
|
|
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-12-08 01:59:04 -06:00
|
|
|
let indent_str = self.block_indent.to_string(self.config);
|
|
|
|
self.push_str(&indent_str);
|
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-08-19 13:47:40 -05: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
|
|
|
}
|
|
|
|
|
2017-12-08 01:59:04 -06:00
|
|
|
self.push_str("}");
|
2017-08-19 13:47:40 -05:00
|
|
|
self.last_pos = item.span.hi();
|
2017-01-12 19:07:42 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn format_body_element(&mut self, element: &BodyElement) {
|
|
|
|
match *element {
|
2017-08-29 08:16:04 -05:00
|
|
|
BodyElement::ForeignItem(item) => self.format_foreign_item(item),
|
2017-01-12 19:07:42 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn format_foreign_mod(&mut self, fm: &ast::ForeignMod, span: Span) {
|
|
|
|
let item = Item::from_foreign_mod(fm, span, self.config);
|
2017-12-11 22:48:12 -06:00
|
|
|
self.format_item(&item);
|
2015-09-21 13:02:45 -05:00
|
|
|
}
|
|
|
|
|
2017-08-24 18:19:51 -05:00
|
|
|
fn format_foreign_item(&mut self, item: &ast::ForeignItem) {
|
2017-08-30 05:26:45 -05:00
|
|
|
let rewrite = item.rewrite(&self.get_context(), self.shape());
|
2017-08-24 18:19:51 -05:00
|
|
|
self.push_rewrite(item.span(), rewrite);
|
2017-08-19 13:47:40 -05:00
|
|
|
self.last_pos = item.span.hi();
|
2015-09-21 13:02:45 -05:00
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn rewrite_fn(
|
|
|
|
&mut self,
|
|
|
|
indent: Indent,
|
|
|
|
ident: ast::Ident,
|
2017-09-28 22:49:39 -05:00
|
|
|
fn_sig: &FnSig,
|
2017-06-11 22:58:58 -05:00
|
|
|
span: Span,
|
|
|
|
block: &ast::Block,
|
2017-10-21 00:09:45 -05:00
|
|
|
inner_attrs: Option<&[ast::Attribute]>,
|
2017-06-11 22:58:58 -05:00
|
|
|
) -> Option<String> {
|
2015-11-22 06:45:51 -06:00
|
|
|
let context = self.get_context();
|
|
|
|
|
2017-11-24 02:08:24 -06:00
|
|
|
let mut newline_brace = newline_for_brace(self.config, &fn_sig.generics.where_clause);
|
2017-06-11 22:58:58 -05:00
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
let (mut result, force_newline_brace) =
|
|
|
|
rewrite_fn_base(&context, indent, ident, fn_sig, span, newline_brace, true)?;
|
2015-05-03 17:12:39 -05:00
|
|
|
|
2017-11-05 00:03:30 -05:00
|
|
|
// 2 = ` {`
|
2018-05-06 01:22:29 -05:00
|
|
|
if self.config.brace_style() == BraceStyle::AlwaysNextLine
|
|
|
|
|| force_newline_brace
|
2017-11-05 00:03:30 -05:00
|
|
|
|| last_line_width(&result) + 2 > self.shape().width
|
|
|
|
{
|
2017-09-28 06:12:04 -05:00
|
|
|
newline_brace = true;
|
|
|
|
} else if !result.contains('\n') {
|
2017-06-03 16:29:08 -05:00
|
|
|
newline_brace = false;
|
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 {
|
2018-02-18 21:47:54 -06:00
|
|
|
result.push_str(&indent.to_string_with_newline(self.config));
|
2015-05-03 17:12:39 -05:00
|
|
|
} else {
|
|
|
|
result.push(' ');
|
|
|
|
}
|
|
|
|
|
2017-10-21 00:09:45 -05:00
|
|
|
self.single_line_fn(&result, block, inner_attrs)
|
|
|
|
.or_else(|| Some(result))
|
2015-05-03 17:12:39 -05:00
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn rewrite_required_fn(
|
|
|
|
&mut self,
|
|
|
|
indent: Indent,
|
|
|
|
ident: ast::Ident,
|
|
|
|
sig: &ast::MethodSig,
|
2017-10-15 12:28:00 -05:00
|
|
|
generics: &ast::Generics,
|
2017-06-11 22:58:58 -05:00
|
|
|
span: Span,
|
|
|
|
) -> Option<String> {
|
2015-12-01 15:09:37 -06:00
|
|
|
// Drop semicolon or it will be interpreted as comment.
|
2017-08-19 13:47:40 -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
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
let (mut result, _) = rewrite_fn_base(
|
2017-06-11 22:58:58 -05:00
|
|
|
&context,
|
|
|
|
indent,
|
|
|
|
ident,
|
2017-10-15 12:28:00 -05:00
|
|
|
&FnSig::from_method_sig(sig, generics),
|
2017-06-11 22:58:58 -05:00
|
|
|
span,
|
|
|
|
false,
|
|
|
|
false,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
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
|
|
|
}
|
|
|
|
|
2017-10-21 00:09:45 -05:00
|
|
|
fn single_line_fn(
|
|
|
|
&self,
|
|
|
|
fn_str: &str,
|
|
|
|
block: &ast::Block,
|
|
|
|
inner_attrs: Option<&[ast::Attribute]>,
|
|
|
|
) -> Option<String> {
|
|
|
|
if fn_str.contains('\n') || inner_attrs.map_or(false, |a| !a.is_empty()) {
|
2015-11-19 20:11:32 -06:00
|
|
|
return None;
|
|
|
|
}
|
2015-11-17 22:53:06 -06:00
|
|
|
|
|
|
|
let codemap = self.get_context().codemap;
|
|
|
|
|
2018-05-06 01:22:29 -05:00
|
|
|
if self.config.empty_item_single_line()
|
|
|
|
&& is_empty_block(block, None, codemap)
|
2017-09-14 22:10:58 -05:00
|
|
|
&& self.block_indent.width() + fn_str.len() + 2 <= self.config.max_width()
|
2017-06-11 22:58:58 -05:00
|
|
|
{
|
2015-11-19 20:11:32 -06:00
|
|
|
return Some(format!("{}{{}}", fn_str));
|
2015-11-17 22:53:06 -06:00
|
|
|
}
|
|
|
|
|
2017-10-21 00:09:45 -05:00
|
|
|
if self.config.fn_single_line() && is_simple_block_stmt(block, None, codemap) {
|
2015-11-17 22:53:06 -06:00
|
|
|
let rewrite = {
|
2017-08-29 08:16:04 -05:00
|
|
|
if let Some(stmt) = block.stmts.first() {
|
2016-09-15 22:19:18 -05:00
|
|
|
match stmt_expr(stmt) {
|
|
|
|
Some(e) => {
|
2017-07-11 08:41:38 -05:00
|
|
|
let suffix = if semicolon_for_expr(&self.get_context(), e) {
|
|
|
|
";"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
2016-09-15 22:19:18 -05:00
|
|
|
|
2017-09-15 03:09:30 -05:00
|
|
|
format_expr(e, ExprType::Statement, &self.get_context(), self.shape())
|
2017-08-30 05:26:45 -05:00
|
|
|
.map(|s| s + suffix)
|
2017-12-07 22:07:42 -06:00
|
|
|
.or_else(|| Some(self.snippet(e.span).to_owned()))
|
2016-09-15 22:19:18 -05:00
|
|
|
}
|
2017-08-30 05:26:45 -05:00
|
|
|
None => stmt.rewrite(&self.get_context(), self.shape()),
|
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;
|
2017-05-16 03:47:09 -05:00
|
|
|
if !res.contains('\n') && width <= self.config.max_width() {
|
2015-11-17 22:53:06 -06:00
|
|
|
return Some(format!("{}{{ {} }}", fn_str, res));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:59:33 -06:00
|
|
|
pub fn visit_static(&mut self, static_parts: &StaticParts) {
|
|
|
|
let rewrite = rewrite_static(&self.get_context(), static_parts, self.block_indent);
|
|
|
|
self.push_rewrite(static_parts.span, rewrite);
|
|
|
|
}
|
|
|
|
|
2017-11-05 05:30:28 -06:00
|
|
|
pub fn visit_struct(&mut self, struct_parts: &StructParts) {
|
|
|
|
let is_tuple = struct_parts.def.is_tuple();
|
|
|
|
let rewrite = format_struct(&self.get_context(), struct_parts, self.block_indent, None)
|
|
|
|
.map(|s| if is_tuple { s + ";" } else { s });
|
|
|
|
self.push_rewrite(struct_parts.span, rewrite);
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn visit_enum(
|
|
|
|
&mut self,
|
|
|
|
ident: ast::Ident,
|
|
|
|
vis: &ast::Visibility,
|
|
|
|
enum_def: &ast::EnumDef,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
span: Span,
|
|
|
|
) {
|
2018-06-25 09:36:45 -05:00
|
|
|
let enum_header = format_header(&self.get_context(), "enum ", ident, vis);
|
2017-12-08 01:59:04 -06:00
|
|
|
self.push_str(&enum_header);
|
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();
|
2017-08-19 13:47:40 -05:00
|
|
|
let body_start = span.lo() + BytePos(brace_pos as u32 + 1);
|
2017-06-11 22:58:58 -05:00
|
|
|
let generics_str = format_generics(
|
|
|
|
&self.get_context(),
|
|
|
|
generics,
|
2017-11-13 19:47:02 -06:00
|
|
|
self.config.brace_style(),
|
2017-11-12 20:06:53 -06:00
|
|
|
if enum_def.variants.is_empty() {
|
|
|
|
BracePos::ForceSameLine
|
|
|
|
} else {
|
|
|
|
BracePos::Auto
|
|
|
|
},
|
2017-06-11 22:58:58 -05:00
|
|
|
self.block_indent,
|
2017-08-19 13:47:40 -05:00
|
|
|
mk_sp(span.lo(), body_start),
|
2017-06-24 05:45:22 -05:00
|
|
|
last_line_width(&enum_header),
|
2017-06-11 22:58:58 -05:00
|
|
|
).unwrap();
|
2017-12-08 01:59:04 -06:00
|
|
|
self.push_str(&generics_str);
|
2015-05-29 05:41:26 -05:00
|
|
|
|
|
|
|
self.last_pos = body_start;
|
|
|
|
|
2018-04-28 00:03:14 -05:00
|
|
|
match self.format_variant_list(enum_def, body_start, span.hi()) {
|
|
|
|
Some(ref s) if enum_def.variants.is_empty() => self.push_str(s),
|
|
|
|
rw => {
|
|
|
|
self.push_rewrite(mk_sp(body_start, span.hi()), rw);
|
|
|
|
self.block_indent = self.block_indent.block_unindent(self.config);
|
|
|
|
}
|
2015-10-07 18:23:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Format the body of an enum definition
|
2017-06-11 22:58:58 -05:00
|
|
|
fn format_variant_list(
|
2018-04-28 00:03:14 -05:00
|
|
|
&mut self,
|
2017-06-11 22:58:58 -05:00
|
|
|
enum_def: &ast::EnumDef,
|
|
|
|
body_lo: BytePos,
|
|
|
|
body_hi: BytePos,
|
|
|
|
) -> Option<String> {
|
2015-10-07 18:23:07 -05:00
|
|
|
if enum_def.variants.is_empty() {
|
2018-04-28 00:03:14 -05:00
|
|
|
let mut buffer = String::with_capacity(128);
|
|
|
|
// 1 = "}"
|
|
|
|
let span = mk_sp(body_lo, body_hi - BytePos(1));
|
|
|
|
format_empty_struct_or_tuple(
|
|
|
|
&self.get_context(),
|
|
|
|
span,
|
|
|
|
self.block_indent,
|
|
|
|
&mut buffer,
|
|
|
|
"",
|
|
|
|
"}",
|
|
|
|
);
|
|
|
|
return Some(buffer);
|
2015-10-07 18:23:07 -05:00
|
|
|
}
|
|
|
|
let mut result = String::with_capacity(1024);
|
2018-04-28 00:03:14 -05:00
|
|
|
let original_offset = self.block_indent;
|
|
|
|
self.block_indent = self.block_indent.block_indent(self.config);
|
2015-10-07 18:23:07 -05:00
|
|
|
|
2017-11-16 01:02:01 -06:00
|
|
|
let itemize_list_with = |one_line_width: usize| {
|
|
|
|
itemize_list(
|
2018-02-18 21:41:43 -06:00
|
|
|
self.snippet_provider,
|
2017-11-16 01:02:01 -06:00
|
|
|
enum_def.variants.iter(),
|
|
|
|
"}",
|
2017-11-16 02:38:12 -06:00
|
|
|
",",
|
2017-12-10 06:54:26 -06:00
|
|
|
|f| {
|
|
|
|
if !f.node.attrs.is_empty() {
|
|
|
|
f.node.attrs[0].span.lo()
|
|
|
|
} else {
|
|
|
|
f.span.lo()
|
|
|
|
}
|
2017-11-16 01:02:01 -06:00
|
|
|
},
|
|
|
|
|f| f.span.hi(),
|
|
|
|
|f| self.format_variant(f, one_line_width),
|
|
|
|
body_lo,
|
|
|
|
body_hi,
|
|
|
|
false,
|
|
|
|
).collect()
|
|
|
|
};
|
2017-11-24 01:17:06 -06:00
|
|
|
let mut items: Vec<_> =
|
|
|
|
itemize_list_with(self.config.width_heuristics().struct_variant_width);
|
2017-11-16 01:02:01 -06:00
|
|
|
// If one of the variants use multiple lines, use multi-lined formatting for all variants.
|
2017-11-30 02:22:00 -06:00
|
|
|
let has_multiline_variant = items.iter().any(|item| item.inner_as_ref().contains('\n'));
|
|
|
|
let has_single_line_variant = items.iter().any(|item| !item.inner_as_ref().contains('\n'));
|
2017-11-16 01:02:01 -06:00
|
|
|
if has_multiline_variant && has_single_line_variant {
|
|
|
|
items = itemize_list_with(0);
|
|
|
|
}
|
2015-10-07 18:23:07 -05:00
|
|
|
|
2018-01-26 01:20:00 -06:00
|
|
|
let shape = self.shape().sub_width(2)?;
|
2015-10-07 18:23:07 -05:00
|
|
|
let fmt = ListFormatting {
|
|
|
|
tactic: DefinitiveListTactic::Vertical,
|
|
|
|
separator: ",",
|
2017-05-16 03:47:09 -05:00
|
|
|
trailing_separator: self.config.trailing_comma(),
|
2017-08-18 09:19:47 -05:00
|
|
|
separator_place: SeparatorPlace::Back,
|
2018-01-21 22:05:18 -06:00
|
|
|
shape,
|
2015-10-07 18:23:07 -05:00
|
|
|
ends_with_newline: true,
|
2017-07-26 08:43:36 -05:00
|
|
|
preserve_newline: true,
|
2018-06-19 10:28:58 -05:00
|
|
|
nested: false,
|
2015-10-07 18:23:07 -05:00
|
|
|
config: self.config,
|
|
|
|
};
|
|
|
|
|
2017-11-16 01:02:01 -06:00
|
|
|
let list = write_list(&items, &fmt)?;
|
2015-10-07 18:23:07 -05:00
|
|
|
result.push_str(&list);
|
2018-04-28 00:03:14 -05:00
|
|
|
result.push_str(&original_offset.to_string_with_newline(self.config));
|
|
|
|
result.push('}');
|
2015-10-07 18:23:07 -05:00
|
|
|
Some(result)
|
2015-05-29 05:41:26 -05:00
|
|
|
}
|
|
|
|
|
2015-09-21 13:02:45 -05:00
|
|
|
// Variant of an enum.
|
2017-11-16 01:02:01 -06:00
|
|
|
fn format_variant(&self, field: &ast::Variant, one_line_width: usize) -> Option<String> {
|
2015-10-07 18:23:07 -05:00
|
|
|
if contains_skip(&field.node.attrs) {
|
2017-08-19 13:47:40 -05:00
|
|
|
let lo = field.node.attrs[0].span.lo();
|
|
|
|
let span = mk_sp(lo, field.span.hi());
|
2017-12-07 22:07:42 -06:00
|
|
|
return Some(self.snippet(span).to_owned());
|
2015-05-29 05:41:26 -05:00
|
|
|
}
|
|
|
|
|
2017-06-04 09:27:21 -05:00
|
|
|
let context = self.get_context();
|
2017-11-27 05:54:55 -06:00
|
|
|
// 1 = ','
|
|
|
|
let shape = self.shape().sub_width(1)?;
|
2017-10-05 06:50:19 -05:00
|
|
|
let attrs_str = field.node.attrs.rewrite(&context, shape)?;
|
2017-08-11 03:52:49 -05:00
|
|
|
let lo = field
|
|
|
|
.node
|
|
|
|
.attrs
|
|
|
|
.last()
|
2017-08-19 13:47:40 -05:00
|
|
|
.map_or(field.span.lo(), |attr| attr.span.hi());
|
|
|
|
let span = mk_sp(lo, field.span.lo());
|
2015-05-29 05:41:26 -05:00
|
|
|
|
2015-11-12 14:38:41 -06:00
|
|
|
let variant_body = match field.node.data {
|
2017-11-27 06:00:27 -06:00
|
|
|
ast::VariantData::Tuple(..) | ast::VariantData::Struct(..) => format_struct(
|
|
|
|
&context,
|
|
|
|
&StructParts::from_variant(field),
|
|
|
|
self.block_indent,
|
|
|
|
Some(one_line_width),
|
|
|
|
)?,
|
2017-08-15 10:48:12 -05:00
|
|
|
ast::VariantData::Unit(..) => {
|
|
|
|
if let Some(ref expr) = field.node.disr_expr {
|
2018-06-25 09:36:45 -05:00
|
|
|
let lhs = format!("{} =", rewrite_ident(&context, field.node.ident));
|
2018-05-22 16:04:14 -05:00
|
|
|
rewrite_assign_rhs(&context, lhs, &*expr.value, shape)?
|
2017-08-15 10:48:12 -05:00
|
|
|
} else {
|
2018-06-25 09:36:45 -05:00
|
|
|
rewrite_ident(&context, field.node.ident).to_owned()
|
2017-08-15 10:48:12 -05:00
|
|
|
}
|
|
|
|
}
|
2015-07-01 13:49:45 -05:00
|
|
|
};
|
|
|
|
|
2018-01-26 01:20:00 -06:00
|
|
|
combine_strs_with_missing_comments(&context, &attrs_str, &variant_body, span, shape, false)
|
2015-06-23 08:58:58 -05:00
|
|
|
}
|
2018-04-30 02:13:51 -05:00
|
|
|
|
|
|
|
fn visit_impl_items(&mut self, items: &[ast::ImplItem]) {
|
|
|
|
if self.get_context().config.reorder_impl_items() {
|
|
|
|
// Create visitor for each items, then reorder them.
|
|
|
|
let mut buffer = vec![];
|
|
|
|
for item in items {
|
|
|
|
self.visit_impl_item(item);
|
|
|
|
buffer.push((self.buffer.clone(), item.clone()));
|
|
|
|
self.buffer.clear();
|
|
|
|
}
|
|
|
|
// type -> const -> macro -> method
|
|
|
|
use ast::ImplItemKind::*;
|
|
|
|
fn need_empty_line(a: &ast::ImplItemKind, b: &ast::ImplItemKind) -> bool {
|
|
|
|
match (a, b) {
|
|
|
|
(Type(..), Type(..)) | (Const(..), Const(..)) => false,
|
|
|
|
_ => true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
buffer.sort_by(|(_, a), (_, b)| match (&a.node, &b.node) {
|
|
|
|
(Type(..), _) => Ordering::Less,
|
|
|
|
(_, Type(..)) => Ordering::Greater,
|
|
|
|
(Const(..), _) => Ordering::Less,
|
|
|
|
(_, Const(..)) => Ordering::Greater,
|
|
|
|
(Macro(..), _) => Ordering::Less,
|
|
|
|
(_, Macro(..)) => Ordering::Greater,
|
2018-04-30 02:19:52 -05:00
|
|
|
_ => a.span.lo().cmp(&b.span.lo()),
|
2018-04-30 02:13:51 -05:00
|
|
|
});
|
|
|
|
let mut prev_kind = None;
|
|
|
|
for (buf, item) in buffer {
|
|
|
|
// Make sure that there are at least a single empty line between
|
|
|
|
// different impl items.
|
|
|
|
if prev_kind
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |prev_kind| need_empty_line(prev_kind, &item.node))
|
|
|
|
{
|
|
|
|
self.push_str("\n");
|
|
|
|
}
|
|
|
|
let indent_str = self.block_indent.to_string_with_newline(self.config);
|
|
|
|
self.push_str(&indent_str);
|
|
|
|
self.push_str(buf.trim());
|
|
|
|
prev_kind = Some(item.node.clone());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for item in items {
|
|
|
|
self.visit_impl_item(item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
2015-05-29 05:41:26 -05:00
|
|
|
|
2017-06-12 21:49:47 -05:00
|
|
|
pub fn format_impl(
|
|
|
|
context: &RewriteContext,
|
|
|
|
item: &ast::Item,
|
|
|
|
offset: Indent,
|
|
|
|
where_span_end: Option<BytePos>,
|
|
|
|
) -> Option<String> {
|
2017-07-10 00:35:50 -05:00
|
|
|
if let ast::ItemKind::Impl(_, _, _, ref generics, _, ref self_ty, ref items) = item.node {
|
2017-08-27 10:16:54 -05:00
|
|
|
let mut result = String::with_capacity(128);
|
2017-10-05 06:50:19 -05:00
|
|
|
let ref_and_type = format_impl_ref_and_type(context, item, offset)?;
|
2018-02-18 21:47:54 -06:00
|
|
|
let sep = offset.to_string_with_newline(context.config);
|
2016-09-06 00:11:56 -05:00
|
|
|
result.push_str(&ref_and_type);
|
2015-11-22 17:00:22 -06:00
|
|
|
|
2017-06-10 05:45:23 -05:00
|
|
|
let where_budget = if result.contains('\n') {
|
|
|
|
context.config.max_width()
|
|
|
|
} else {
|
2017-08-29 01:19:56 -05:00
|
|
|
context.budget(last_line_width(&result))
|
2017-06-10 05:45:23 -05:00
|
|
|
};
|
2018-05-31 04:58:48 -05:00
|
|
|
|
|
|
|
let mut option = WhereClauseOption::snuggled(&ref_and_type);
|
2018-05-31 05:33:45 -05:00
|
|
|
let snippet = context.snippet(item.span);
|
|
|
|
let open_pos = snippet.find_uncommented("{")? + 1;
|
|
|
|
if !contains_comment(&snippet[open_pos..])
|
|
|
|
&& items.is_empty()
|
|
|
|
&& generics.where_clause.predicates.len() == 1
|
2018-06-05 20:46:49 -05:00
|
|
|
&& !result.contains('\n')
|
2018-05-31 05:33:45 -05:00
|
|
|
{
|
2018-05-31 04:58:48 -05:00
|
|
|
option.suppress_comma();
|
2018-05-31 05:33:45 -05:00
|
|
|
option.snuggle();
|
|
|
|
option.compress_where();
|
2018-05-31 04:58:48 -05:00
|
|
|
}
|
|
|
|
|
2018-07-22 21:52:02 -05:00
|
|
|
let where_clause_str = rewrite_where_clause(
|
2017-06-12 21:49:47 -05:00
|
|
|
context,
|
|
|
|
&generics.where_clause,
|
2017-11-13 19:47:02 -06:00
|
|
|
context.config.brace_style(),
|
2017-06-12 21:49:47 -05:00
|
|
|
Shape::legacy(where_budget, offset.block_only()),
|
2017-11-24 02:08:24 -06:00
|
|
|
Density::Vertical,
|
2017-06-12 21:49:47 -05:00
|
|
|
"{",
|
|
|
|
where_span_end,
|
2017-08-19 13:47:40 -05:00
|
|
|
self_ty.span.hi(),
|
2017-08-02 09:26:35 -05:00
|
|
|
option,
|
2017-11-05 16:53:17 -06:00
|
|
|
false,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2017-06-12 21:49:47 -05:00
|
|
|
|
2017-08-27 10:16:54 -05:00
|
|
|
// If there is no where clause, we may have missing comments between the trait name and
|
|
|
|
// the opening brace.
|
|
|
|
if generics.where_clause.predicates.is_empty() {
|
|
|
|
if let Some(hi) = where_span_end {
|
|
|
|
match recover_missing_comment_in_span(
|
2017-08-30 21:11:52 -05:00
|
|
|
mk_sp(self_ty.span.hi(), hi),
|
2017-08-27 10:16:54 -05:00
|
|
|
Shape::indented(offset, context.config),
|
|
|
|
context,
|
|
|
|
last_line_width(&result),
|
|
|
|
) {
|
|
|
|
Some(ref missing_comment) if !missing_comment.is_empty() => {
|
|
|
|
result.push_str(missing_comment);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
if is_impl_single_line(context, items, &result, &where_clause_str, item)? {
|
2016-01-09 23:45:58 -06:00
|
|
|
result.push_str(&where_clause_str);
|
2017-08-27 10:16:54 -05:00
|
|
|
if where_clause_str.contains('\n') || last_line_contains_single_line_comment(&result) {
|
2018-06-04 18:35:51 -05:00
|
|
|
// if the where_clause contains extra comments AND
|
|
|
|
// there is only one where clause predicate
|
2018-06-04 06:10:09 -05:00
|
|
|
// recover the suppressed comma in single line where_clause formatting
|
|
|
|
if generics.where_clause.predicates.len() == 1 {
|
|
|
|
result.push_str(",");
|
|
|
|
}
|
2017-08-27 10:16:54 -05:00
|
|
|
result.push_str(&format!("{}{{{}}}", &sep, &sep));
|
2016-01-09 23:45:58 -06:00
|
|
|
} else {
|
|
|
|
result.push_str(" {}");
|
|
|
|
}
|
|
|
|
return Some(result);
|
|
|
|
}
|
|
|
|
|
2015-11-22 17:00:22 -06:00
|
|
|
result.push_str(&where_clause_str);
|
|
|
|
|
2018-03-08 04:08:38 -06:00
|
|
|
let need_newline = last_line_contains_single_line_comment(&result) || result.contains('\n');
|
2017-11-13 19:47:02 -06:00
|
|
|
match context.config.brace_style() {
|
2018-01-01 22:52:19 -06:00
|
|
|
_ if need_newline => result.push_str(&sep),
|
2017-08-27 10:16:54 -05:00
|
|
|
BraceStyle::AlwaysNextLine => result.push_str(&sep),
|
2015-11-22 17:00:22 -06:00
|
|
|
BraceStyle::PreferSameLine => result.push(' '),
|
2017-08-15 10:48:12 -05:00
|
|
|
BraceStyle::SameLineWhere => {
|
|
|
|
if !where_clause_str.is_empty() {
|
|
|
|
result.push_str(&sep);
|
|
|
|
} else {
|
|
|
|
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);
|
2017-10-05 06:50:19 -05:00
|
|
|
let open_pos = snippet.find_uncommented("{")? + 1;
|
2015-11-23 22:54:44 -06:00
|
|
|
|
|
|
|
if !items.is_empty() || contains_comment(&snippet[open_pos..]) {
|
2017-12-06 22:53:10 -06:00
|
|
|
let mut visitor = FmtVisitor::from_context(context);
|
2018-04-04 00:57:55 -05:00
|
|
|
let item_indent = offset.block_only().block_indent(context.config);
|
|
|
|
visitor.block_indent = item_indent;
|
2017-08-19 13:47:40 -05:00
|
|
|
visitor.last_pos = item.span.lo() + BytePos(open_pos as u32);
|
2015-11-22 17:00:22 -06:00
|
|
|
|
2017-07-24 10:55:55 -05:00
|
|
|
visitor.visit_attrs(&item.attrs, ast::AttrStyle::Inner);
|
2018-04-30 02:13:51 -05:00
|
|
|
visitor.visit_impl_items(items);
|
2015-11-22 17:00:22 -06:00
|
|
|
|
2017-08-19 13:47:40 -05:00
|
|
|
visitor.format_missing(item.span.hi() - BytePos(1));
|
2015-11-23 22:54:44 -06:00
|
|
|
|
2018-02-18 21:47:54 -06:00
|
|
|
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
|
|
|
|
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
|
2015-11-23 22:54:44 -06:00
|
|
|
|
|
|
|
result.push_str(&inner_indent_str);
|
2018-01-01 22:04:39 -06:00
|
|
|
result.push_str(visitor.buffer.to_string().trim());
|
2015-11-23 22:54:44 -06:00
|
|
|
result.push_str(&outer_indent_str);
|
2015-11-22 17:00:22 -06:00
|
|
|
}
|
|
|
|
|
2018-06-05 20:55:25 -05:00
|
|
|
if result.ends_with('{') && !context.config.empty_item_single_line() {
|
2017-08-27 10:16:54 -05:00
|
|
|
result.push_str(&sep);
|
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!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
fn is_impl_single_line(
|
|
|
|
context: &RewriteContext,
|
2018-03-06 04:47:28 -06:00
|
|
|
items: &[ast::ImplItem],
|
2017-06-11 22:58:58 -05:00
|
|
|
result: &str,
|
|
|
|
where_clause_str: &str,
|
|
|
|
item: &ast::Item,
|
|
|
|
) -> Option<bool> {
|
2016-01-09 23:45:58 -06:00
|
|
|
let snippet = context.snippet(item.span);
|
2017-10-05 06:50:19 -05:00
|
|
|
let open_pos = snippet.find_uncommented("{")? + 1;
|
2016-01-09 23:45:58 -06:00
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
Some(
|
2018-05-06 01:22:29 -05:00
|
|
|
context.config.empty_item_single_line()
|
|
|
|
&& items.is_empty()
|
|
|
|
&& !result.contains('\n')
|
2017-09-14 22:10:58 -05:00
|
|
|
&& result.len() + where_clause_str.len() <= context.config.max_width()
|
|
|
|
&& !contains_comment(&snippet[open_pos..]),
|
2017-06-11 22:58:58 -05:00
|
|
|
)
|
2016-01-09 23:45:58 -06:00
|
|
|
}
|
|
|
|
|
2017-06-12 21:49:47 -05:00
|
|
|
fn format_impl_ref_and_type(
|
|
|
|
context: &RewriteContext,
|
|
|
|
item: &ast::Item,
|
|
|
|
offset: Indent,
|
|
|
|
) -> Option<String> {
|
2017-06-15 02:26:41 -05:00
|
|
|
if let ast::ItemKind::Impl(
|
|
|
|
unsafety,
|
|
|
|
polarity,
|
2017-07-26 19:43:35 -05:00
|
|
|
defaultness,
|
2017-06-15 02:26:41 -05:00
|
|
|
ref generics,
|
|
|
|
ref trait_ref,
|
|
|
|
ref self_ty,
|
|
|
|
_,
|
|
|
|
) = item.node
|
2017-06-11 22:58:58 -05:00
|
|
|
{
|
2017-08-27 10:24:10 -05:00
|
|
|
let mut result = String::with_capacity(128);
|
2016-09-06 00:11:56 -05:00
|
|
|
|
2018-06-25 09:36:45 -05:00
|
|
|
result.push_str(&format_visibility(context, &item.vis));
|
2017-08-29 08:16:04 -05:00
|
|
|
result.push_str(format_defaultness(defaultness));
|
2016-09-06 00:11:56 -05:00
|
|
|
result.push_str(format_unsafety(unsafety));
|
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
let shape = generics_shape_from_config(
|
2017-06-12 21:49:47 -05:00
|
|
|
context.config,
|
|
|
|
Shape::indented(offset + last_line_width(&result), context.config),
|
|
|
|
0,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2018-07-24 10:34:46 -05:00
|
|
|
let generics_str = rewrite_generics(context, "impl", generics, shape)?;
|
2018-03-07 00:48:47 -06:00
|
|
|
result.push_str(&generics_str);
|
2016-09-06 00:11:56 -05:00
|
|
|
|
2017-06-11 00:32:44 -05:00
|
|
|
let polarity_str = if polarity == ast::ImplPolarity::Negative {
|
|
|
|
"!"
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
};
|
2016-09-06 00:11:56 -05:00
|
|
|
|
|
|
|
if let Some(ref trait_ref) = *trait_ref {
|
2018-03-07 00:48:47 -06:00
|
|
|
let result_len = last_line_width(&result);
|
|
|
|
result.push_str(&rewrite_trait_ref(
|
2017-07-09 12:24:59 -05:00
|
|
|
context,
|
2017-08-29 08:16:04 -05:00
|
|
|
trait_ref,
|
2017-07-09 12:24:59 -05:00
|
|
|
offset,
|
|
|
|
polarity_str,
|
|
|
|
result_len,
|
2018-03-07 00:48:47 -06:00
|
|
|
)?);
|
2016-09-06 00:11:56 -05:00
|
|
|
}
|
|
|
|
|
2017-06-11 00:32:44 -05:00
|
|
|
// Try to put the self type in a single line.
|
|
|
|
// ` for`
|
|
|
|
let trait_ref_overhead = if trait_ref.is_some() { 4 } else { 0 };
|
|
|
|
let curly_brace_overhead = if generics.where_clause.predicates.is_empty() {
|
2016-09-06 00:11:56 -05:00
|
|
|
// If there is no where clause adapt budget for type formatting to take space and curly
|
|
|
|
// brace into account.
|
2017-11-13 19:47:02 -06:00
|
|
|
match context.config.brace_style() {
|
2017-06-11 00:32:44 -05:00
|
|
|
BraceStyle::AlwaysNextLine => 0,
|
|
|
|
_ => 2,
|
2016-09-06 00:11:56 -05:00
|
|
|
}
|
2017-06-11 00:32:44 -05:00
|
|
|
} else {
|
|
|
|
0
|
|
|
|
};
|
|
|
|
let used_space = last_line_width(&result) + trait_ref_overhead + curly_brace_overhead;
|
2017-01-16 18:08:36 -06:00
|
|
|
// 1 = space before the type.
|
2017-08-29 01:19:56 -05:00
|
|
|
let budget = context.budget(used_space + 1);
|
2017-06-11 00:32:44 -05:00
|
|
|
if let Some(self_ty_str) = self_ty.rewrite(context, Shape::legacy(budget, offset)) {
|
|
|
|
if !self_ty_str.contains('\n') {
|
|
|
|
if trait_ref.is_some() {
|
|
|
|
result.push_str(" for ");
|
|
|
|
} else {
|
|
|
|
result.push(' ');
|
|
|
|
}
|
|
|
|
result.push_str(&self_ty_str);
|
|
|
|
return Some(result);
|
|
|
|
}
|
2017-01-16 18:08:36 -06:00
|
|
|
}
|
2016-09-06 00:11:56 -05:00
|
|
|
|
2017-06-11 00:32:44 -05:00
|
|
|
// Couldn't fit the self type on a single line, put it on a new line.
|
|
|
|
result.push('\n');
|
|
|
|
// Add indentation of one additional tab.
|
|
|
|
let new_line_offset = offset.block_indent(context.config);
|
|
|
|
result.push_str(&new_line_offset.to_string(context.config));
|
|
|
|
if trait_ref.is_some() {
|
|
|
|
result.push_str("for ");
|
|
|
|
}
|
2017-08-29 01:19:56 -05:00
|
|
|
let budget = context.budget(last_line_width(&result));
|
2017-11-13 01:42:29 -06:00
|
|
|
let type_offset = match context.config.indent_style() {
|
|
|
|
IndentStyle::Visual => new_line_offset + trait_ref_overhead,
|
|
|
|
IndentStyle::Block => new_line_offset,
|
2017-06-11 00:32:44 -05:00
|
|
|
};
|
2017-11-29 02:37:51 -06:00
|
|
|
result.push_str(&*self_ty.rewrite(context, Shape::legacy(budget, type_offset))?);
|
2016-09-06 00:11:56 -05:00
|
|
|
Some(result)
|
|
|
|
} else {
|
|
|
|
unreachable!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-12 21:49:47 -05:00
|
|
|
fn rewrite_trait_ref(
|
|
|
|
context: &RewriteContext,
|
|
|
|
trait_ref: &ast::TraitRef,
|
|
|
|
offset: Indent,
|
|
|
|
polarity_str: &str,
|
|
|
|
result_len: usize,
|
|
|
|
) -> Option<String> {
|
2017-06-11 00:32:44 -05:00
|
|
|
// 1 = space between generics and trait_ref
|
2018-03-07 00:48:47 -06:00
|
|
|
let used_space = 1 + polarity_str.len() + result_len;
|
2017-06-11 00:32:44 -05:00
|
|
|
let shape = Shape::indented(offset + used_space, context.config);
|
|
|
|
if let Some(trait_ref_str) = trait_ref.rewrite(context, shape) {
|
2018-03-07 00:48:47 -06:00
|
|
|
if !trait_ref_str.contains('\n') {
|
|
|
|
return Some(format!(" {}{}", polarity_str, &trait_ref_str));
|
2017-06-10 06:37:34 -05:00
|
|
|
}
|
|
|
|
}
|
2017-06-11 00:32:44 -05:00
|
|
|
// We could not make enough space for trait_ref, so put it on new line.
|
2018-03-07 00:48:47 -06:00
|
|
|
let offset = offset.block_indent(context.config);
|
|
|
|
let shape = Shape::indented(offset, context.config);
|
|
|
|
let trait_ref_str = trait_ref.rewrite(context, shape)?;
|
|
|
|
Some(format!(
|
|
|
|
"{}{}{}",
|
|
|
|
&offset.to_string_with_newline(context.config),
|
|
|
|
polarity_str,
|
|
|
|
&trait_ref_str
|
|
|
|
))
|
2017-06-10 06:37:34 -05:00
|
|
|
}
|
|
|
|
|
2017-11-05 05:30:28 -06:00
|
|
|
pub struct StructParts<'a> {
|
|
|
|
prefix: &'a str,
|
2017-06-12 21:49:47 -05:00
|
|
|
ident: ast::Ident,
|
2017-11-05 05:30:28 -06:00
|
|
|
vis: &'a ast::Visibility,
|
|
|
|
def: &'a ast::VariantData,
|
|
|
|
generics: Option<&'a ast::Generics>,
|
2017-06-12 21:49:47 -05:00
|
|
|
span: Span,
|
2017-11-05 05:30:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> StructParts<'a> {
|
2018-06-25 09:36:45 -05:00
|
|
|
fn format_header(&self, context: &RewriteContext) -> String {
|
|
|
|
format_header(context, self.prefix, self.ident, self.vis)
|
2017-11-05 05:30:28 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn from_variant(variant: &'a ast::Variant) -> Self {
|
|
|
|
StructParts {
|
|
|
|
prefix: "",
|
2018-04-08 09:18:18 -05:00
|
|
|
ident: variant.node.ident,
|
2018-03-06 04:47:28 -06:00
|
|
|
vis: &DEFAULT_VISIBILITY,
|
2017-11-05 05:30:28 -06:00
|
|
|
def: &variant.node.data,
|
|
|
|
generics: None,
|
|
|
|
span: variant.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_item(item: &'a ast::Item) -> Self {
|
|
|
|
let (prefix, def, generics) = match item.node {
|
|
|
|
ast::ItemKind::Struct(ref def, ref generics) => ("struct ", def, generics),
|
|
|
|
ast::ItemKind::Union(ref def, ref generics) => ("union ", def, generics),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
StructParts {
|
2018-01-21 22:05:18 -06:00
|
|
|
prefix,
|
2017-11-05 05:30:28 -06:00
|
|
|
ident: item.ident,
|
|
|
|
vis: &item.vis,
|
2018-01-21 22:05:18 -06:00
|
|
|
def,
|
2017-11-05 05:30:28 -06:00
|
|
|
generics: Some(generics),
|
|
|
|
span: item.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn format_struct(
|
|
|
|
context: &RewriteContext,
|
|
|
|
struct_parts: &StructParts,
|
2017-06-12 21:49:47 -05:00
|
|
|
offset: Indent,
|
|
|
|
one_line_width: Option<usize>,
|
|
|
|
) -> Option<String> {
|
2017-11-05 05:30:28 -06:00
|
|
|
match *struct_parts.def {
|
2017-11-12 17:02:06 -06:00
|
|
|
ast::VariantData::Unit(..) => format_unit_struct(context, struct_parts, offset),
|
2017-11-05 05:30:28 -06:00
|
|
|
ast::VariantData::Tuple(ref fields, _) => {
|
|
|
|
format_tuple_struct(context, struct_parts, fields, offset)
|
|
|
|
}
|
|
|
|
ast::VariantData::Struct(ref fields, _) => {
|
|
|
|
format_struct_struct(context, struct_parts, fields, offset, one_line_width)
|
|
|
|
}
|
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> {
|
2018-04-20 09:31:31 -05:00
|
|
|
if let ast::ItemKind::Trait(
|
|
|
|
is_auto,
|
|
|
|
unsafety,
|
|
|
|
ref generics,
|
2018-06-25 01:24:00 -05:00
|
|
|
ref generic_bounds,
|
2018-04-20 09:31:31 -05:00
|
|
|
ref trait_items,
|
|
|
|
) = item.node
|
2017-06-11 22:58:58 -05:00
|
|
|
{
|
2017-08-27 10:17:19 -05:00
|
|
|
let mut result = String::with_capacity(128);
|
2017-06-11 22:58:58 -05:00
|
|
|
let header = format!(
|
2018-04-20 09:31:31 -05:00
|
|
|
"{}{}{}trait ",
|
2018-06-25 09:36:45 -05:00
|
|
|
format_visibility(context, &item.vis),
|
2017-06-11 22:58:58 -05:00
|
|
|
format_unsafety(unsafety),
|
2018-05-18 16:25:33 -05:00
|
|
|
format_auto(is_auto),
|
2017-06-11 22:58:58 -05:00
|
|
|
);
|
2016-03-11 15:18:30 -06:00
|
|
|
result.push_str(&header);
|
|
|
|
|
2018-02-18 21:41:43 -06:00
|
|
|
let body_lo = context.snippet_provider.span_after(item.span, "{");
|
2016-03-11 15:18:30 -06:00
|
|
|
|
2018-01-01 22:51:25 -06:00
|
|
|
let shape = Shape::indented(offset, context.config).offset_left(result.len())?;
|
2018-07-24 10:34:46 -05:00
|
|
|
let generics_str =
|
|
|
|
rewrite_generics(context, rewrite_ident(context, item.ident), generics, shape)?;
|
2016-03-11 15:18:30 -06:00
|
|
|
result.push_str(&generics_str);
|
|
|
|
|
2017-11-10 06:21:53 -06:00
|
|
|
// FIXME(#2055): rustfmt fails to format when there are comments between trait bounds.
|
2018-06-25 01:24:00 -05:00
|
|
|
if !generic_bounds.is_empty() {
|
2017-11-20 17:52:43 -06:00
|
|
|
let ident_hi = context
|
2018-02-18 21:41:43 -06:00
|
|
|
.snippet_provider
|
2018-06-25 09:36:45 -05:00
|
|
|
.span_after(item.span, &item.ident.as_str());
|
2018-06-25 01:24:00 -05:00
|
|
|
let bound_hi = generic_bounds.last().unwrap().span().hi();
|
2017-11-10 06:21:53 -06:00
|
|
|
let snippet = context.snippet(mk_sp(ident_hi, bound_hi));
|
2017-12-11 22:48:12 -06:00
|
|
|
if contains_comment(snippet) {
|
2017-11-10 06:21:53 -06:00
|
|
|
return None;
|
|
|
|
}
|
2018-06-25 01:24:00 -05:00
|
|
|
|
2018-03-09 23:54:13 -06:00
|
|
|
result = rewrite_assign_rhs_with(
|
|
|
|
context,
|
|
|
|
result + ":",
|
2018-06-25 01:24:00 -05:00
|
|
|
generic_bounds,
|
2018-03-09 23:54:13 -06:00
|
|
|
shape,
|
2018-06-25 01:24:00 -05:00
|
|
|
RhsTactics::ForceNextLineWithoutIndent,
|
2018-03-09 23:54:13 -06:00
|
|
|
)?;
|
2016-03-11 23:32:08 -06:00
|
|
|
}
|
2016-03-11 15:18:30 -06:00
|
|
|
|
2018-03-09 23:55:30 -06:00
|
|
|
// Rewrite where clause.
|
|
|
|
if !generics.where_clause.predicates.is_empty() {
|
|
|
|
let where_density = if context.config.indent_style() == IndentStyle::Block {
|
2017-11-24 02:08:24 -06:00
|
|
|
Density::Compressed
|
|
|
|
} else {
|
|
|
|
Density::Tall
|
|
|
|
};
|
2016-03-15 15:08:12 -05:00
|
|
|
|
2018-03-09 23:55:30 -06:00
|
|
|
let where_budget = context.budget(last_line_width(&result));
|
2018-06-25 01:24:00 -05:00
|
|
|
let pos_before_where = if generic_bounds.is_empty() {
|
2018-03-09 23:55:30 -06:00
|
|
|
generics.where_clause.span.lo()
|
|
|
|
} else {
|
2018-06-25 01:24:00 -05:00
|
|
|
generic_bounds[generic_bounds.len() - 1].span().hi()
|
2018-03-09 23:55:30 -06:00
|
|
|
};
|
|
|
|
let option = WhereClauseOption::snuggled(&generics_str);
|
|
|
|
let where_clause_str = rewrite_where_clause(
|
|
|
|
context,
|
|
|
|
&generics.where_clause,
|
|
|
|
context.config.brace_style(),
|
|
|
|
Shape::legacy(where_budget, offset.block_only()),
|
|
|
|
where_density,
|
|
|
|
"{",
|
|
|
|
None,
|
|
|
|
pos_before_where,
|
|
|
|
option,
|
|
|
|
false,
|
|
|
|
)?;
|
|
|
|
// If the where clause cannot fit on the same line,
|
|
|
|
// put the where clause on a new line
|
|
|
|
if !where_clause_str.contains('\n')
|
|
|
|
&& last_line_width(&result) + where_clause_str.len() + offset.width()
|
|
|
|
> context.config.comment_width()
|
|
|
|
{
|
|
|
|
let width = offset.block_indent + context.config.tab_spaces() - 1;
|
|
|
|
let where_indent = Indent::new(0, width);
|
|
|
|
result.push_str(&where_indent.to_string_with_newline(context.config));
|
|
|
|
}
|
|
|
|
result.push_str(&where_clause_str);
|
2017-07-10 00:35:50 -05:00
|
|
|
} else {
|
2017-08-27 10:17:19 -05:00
|
|
|
let item_snippet = context.snippet(item.span);
|
2018-03-21 18:32:42 -05:00
|
|
|
if let Some(lo) = item_snippet.find('/') {
|
2017-08-27 10:17:19 -05:00
|
|
|
// 1 = `{`
|
|
|
|
let comment_hi = body_lo - BytePos(1);
|
2017-08-30 21:11:52 -05:00
|
|
|
let comment_lo = item.span.lo() + BytePos(lo as u32);
|
2017-08-27 10:17:19 -05:00
|
|
|
if comment_lo < comment_hi {
|
|
|
|
match recover_missing_comment_in_span(
|
|
|
|
mk_sp(comment_lo, comment_hi),
|
|
|
|
Shape::indented(offset, context.config),
|
|
|
|
context,
|
|
|
|
last_line_width(&result),
|
|
|
|
) {
|
|
|
|
Some(ref missing_comment) if !missing_comment.is_empty() => {
|
|
|
|
result.push_str(missing_comment);
|
|
|
|
}
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 19:47:02 -06:00
|
|
|
match context.config.brace_style() {
|
2018-03-09 23:55:30 -06:00
|
|
|
_ if last_line_contains_single_line_comment(&result)
|
|
|
|
|| last_line_width(&result) + 2 > context.budget(offset.width()) =>
|
|
|
|
{
|
2018-02-18 21:47:54 -06:00
|
|
|
result.push_str(&offset.to_string_with_newline(context.config));
|
2017-08-27 10:17:19 -05:00
|
|
|
}
|
2016-03-11 23:32:08 -06:00
|
|
|
BraceStyle::AlwaysNextLine => {
|
2018-02-18 21:47:54 -06:00
|
|
|
result.push_str(&offset.to_string_with_newline(context.config));
|
2016-03-11 23:41:22 -06:00
|
|
|
}
|
2016-03-11 23:32:08 -06:00
|
|
|
BraceStyle::PreferSameLine => result.push(' '),
|
2017-08-15 10:48:12 -05:00
|
|
|
BraceStyle::SameLineWhere => {
|
2018-03-09 23:55:30 -06:00
|
|
|
if result.contains('\n')
|
|
|
|
|| (!generics.where_clause.predicates.is_empty() && !trait_items.is_empty())
|
2017-08-15 10:48:12 -05:00
|
|
|
{
|
2018-02-18 21:47:54 -06:00
|
|
|
result.push_str(&offset.to_string_with_newline(context.config));
|
2017-08-15 10:48:12 -05:00
|
|
|
} else {
|
|
|
|
result.push(' ');
|
|
|
|
}
|
|
|
|
}
|
2016-03-11 23:32:08 -06:00
|
|
|
}
|
|
|
|
result.push('{');
|
|
|
|
|
|
|
|
let snippet = context.snippet(item.span);
|
2017-10-05 06:50:19 -05:00
|
|
|
let open_pos = snippet.find_uncommented("{")? + 1;
|
2016-03-11 23:32:08 -06:00
|
|
|
|
|
|
|
if !trait_items.is_empty() || contains_comment(&snippet[open_pos..]) {
|
2017-12-06 22:53:10 -06:00
|
|
|
let mut visitor = FmtVisitor::from_context(context);
|
2017-03-27 17:25:59 -05:00
|
|
|
visitor.block_indent = offset.block_only().block_indent(context.config);
|
2017-08-19 13:47:40 -05:00
|
|
|
visitor.last_pos = item.span.lo() + BytePos(open_pos as u32);
|
2016-03-11 23:32:08 -06:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-08-19 13:47:40 -05:00
|
|
|
visitor.format_missing(item.span.hi() - BytePos(1));
|
2016-03-11 23:32:08 -06:00
|
|
|
|
2018-02-18 21:47:54 -06:00
|
|
|
let inner_indent_str = visitor.block_indent.to_string_with_newline(context.config);
|
|
|
|
let outer_indent_str = offset.block_only().to_string_with_newline(context.config);
|
2016-03-11 23:32:08 -06:00
|
|
|
|
|
|
|
result.push_str(&inner_indent_str);
|
2018-01-01 22:04:39 -06:00
|
|
|
result.push_str(visitor.buffer.to_string().trim());
|
2016-03-11 23:32:08 -06:00
|
|
|
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!();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-14 22:47:52 -06:00
|
|
|
pub fn format_trait_alias(
|
|
|
|
context: &RewriteContext,
|
|
|
|
ident: ast::Ident,
|
|
|
|
generics: &ast::Generics,
|
2018-06-25 01:24:00 -05:00
|
|
|
generic_bounds: &ast::GenericBounds,
|
2017-12-14 22:47:52 -06:00
|
|
|
shape: Shape,
|
|
|
|
) -> Option<String> {
|
2018-06-25 09:36:45 -05:00
|
|
|
let alias = rewrite_ident(context, ident);
|
2017-12-14 22:47:52 -06:00
|
|
|
// 6 = "trait ", 2 = " ="
|
2018-03-07 00:48:47 -06:00
|
|
|
let g_shape = shape.offset_left(6)?.sub_width(2)?;
|
2018-07-24 10:34:46 -05:00
|
|
|
let generics_str = rewrite_generics(context, &alias, generics, g_shape)?;
|
2018-03-07 00:48:47 -06:00
|
|
|
let lhs = format!("trait {} =", generics_str);
|
2017-12-14 22:47:52 -06:00
|
|
|
// 1 = ";"
|
2018-06-25 01:24:00 -05:00
|
|
|
rewrite_assign_rhs(context, lhs, generic_bounds, shape.sub_width(1)?).map(|s| s + ";")
|
2017-12-14 22:47:52 -06:00
|
|
|
}
|
|
|
|
|
2017-11-12 17:02:06 -06:00
|
|
|
fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent) -> Option<String> {
|
2018-06-25 09:36:45 -05:00
|
|
|
let header_str = format_header(context, p.prefix, p.ident, p.vis);
|
2017-11-12 17:02:06 -06:00
|
|
|
let generics_str = if let Some(generics) = p.generics {
|
2017-11-12 20:07:09 -06:00
|
|
|
let hi = if generics.where_clause.predicates.is_empty() {
|
|
|
|
generics.span.hi()
|
|
|
|
} else {
|
|
|
|
generics.where_clause.span.hi()
|
|
|
|
};
|
|
|
|
format_generics(
|
|
|
|
context,
|
|
|
|
generics,
|
2017-11-13 19:47:02 -06:00
|
|
|
context.config.brace_style(),
|
2017-11-12 20:07:09 -06:00
|
|
|
BracePos::None,
|
|
|
|
offset,
|
|
|
|
mk_sp(generics.span.lo(), hi),
|
|
|
|
last_line_width(&header_str),
|
|
|
|
)?
|
2017-11-12 17:02:06 -06:00
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
|
|
|
Some(format!("{}{};", header_str, generics_str))
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
2015-10-18 17:25:38 -05:00
|
|
|
|
2017-07-10 03:26:55 -05:00
|
|
|
pub fn format_struct_struct(
|
2017-06-11 22:58:58 -05:00
|
|
|
context: &RewriteContext,
|
2017-11-05 05:30:28 -06:00
|
|
|
struct_parts: &StructParts,
|
2017-06-11 22:58:58 -05:00
|
|
|
fields: &[ast::StructField],
|
|
|
|
offset: Indent,
|
|
|
|
one_line_width: Option<usize>,
|
|
|
|
) -> Option<String> {
|
2015-11-22 06:45:51 -06:00
|
|
|
let mut result = String::with_capacity(1024);
|
2017-11-05 05:30:28 -06:00
|
|
|
let span = struct_parts.span;
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2018-06-25 09:36:45 -05:00
|
|
|
let header_str = struct_parts.format_header(context);
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&header_str);
|
|
|
|
|
2017-11-04 14:24:41 -05:00
|
|
|
let header_hi = span.lo() + BytePos(header_str.len() as u32);
|
2018-02-18 21:41:43 -06:00
|
|
|
let body_lo = context.snippet_provider.span_after(span, "{");
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-11-05 05:30:28 -06:00
|
|
|
let generics_str = match struct_parts.generics {
|
2017-10-05 06:50:19 -05:00
|
|
|
Some(g) => format_generics(
|
2017-07-11 07:53:10 -05:00
|
|
|
context,
|
|
|
|
g,
|
2017-11-13 19:47:02 -06:00
|
|
|
context.config.brace_style(),
|
2017-11-12 20:06:53 -06:00
|
|
|
if fields.is_empty() {
|
|
|
|
BracePos::ForceSameLine
|
|
|
|
} else {
|
|
|
|
BracePos::Auto
|
|
|
|
},
|
2017-07-11 07:53:10 -05:00
|
|
|
offset,
|
2017-11-04 14:24:41 -05:00
|
|
|
mk_sp(header_hi, body_lo),
|
2017-07-11 07:53:10 -05:00
|
|
|
last_line_width(&result),
|
2017-10-05 06:50:19 -05:00
|
|
|
)?,
|
2015-11-22 06:45:51 -06:00
|
|
|
None => {
|
2017-06-24 05:45:22 -05:00
|
|
|
// 3 = ` {}`, 2 = ` {`.
|
|
|
|
let overhead = if fields.is_empty() { 3 } else { 2 };
|
2017-11-13 19:47:02 -06:00
|
|
|
if (context.config.brace_style() == BraceStyle::AlwaysNextLine && !fields.is_empty())
|
2017-09-14 22:10:58 -05:00
|
|
|
|| context.config.max_width() < overhead + result.len()
|
2017-06-11 22:58:58 -05:00
|
|
|
{
|
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
|
|
|
};
|
2017-06-24 05:45:22 -05:00
|
|
|
// 1 = `}`
|
|
|
|
let overhead = if fields.is_empty() { 1 } else { 0 };
|
2017-08-29 00:29:13 -05:00
|
|
|
let total_width = result.len() + generics_str.len() + overhead;
|
2018-05-06 01:22:29 -05:00
|
|
|
if !generics_str.is_empty()
|
|
|
|
&& !generics_str.contains('\n')
|
2017-09-14 22:10:58 -05:00
|
|
|
&& total_width > context.config.max_width()
|
2017-08-29 00:29:13 -05:00
|
|
|
{
|
2017-06-24 05:45:22 -05:00
|
|
|
result.push('\n');
|
2017-07-25 04:51:14 -05:00
|
|
|
result.push_str(&offset.to_string(context.config));
|
2017-08-29 08:16:04 -05:00
|
|
|
result.push_str(generics_str.trim_left());
|
2017-06-24 05:45:22 -05:00
|
|
|
} else {
|
|
|
|
result.push_str(&generics_str);
|
|
|
|
}
|
2015-05-24 18:03:26 -05:00
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
if fields.is_empty() {
|
2018-04-27 23:59:54 -05:00
|
|
|
let inner_span = mk_sp(body_lo, span.hi() - BytePos(1));
|
|
|
|
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "", "}");
|
2015-11-22 06:45:51 -06:00
|
|
|
return Some(result);
|
2015-04-21 05:50:43 -05:00
|
|
|
}
|
|
|
|
|
2017-07-20 17:17:49 -05:00
|
|
|
// 3 = ` ` and ` }`
|
2017-08-29 01:19:56 -05:00
|
|
|
let one_line_budget = context.budget(result.len() + 3 + offset.width());
|
2017-07-25 01:19:09 -05:00
|
|
|
let one_line_budget =
|
|
|
|
one_line_width.map_or(0, |one_line_width| min(one_line_width, one_line_budget));
|
2017-07-20 17:17:49 -05:00
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
let items_str = rewrite_with_alignment(
|
2017-07-03 04:54:41 -05:00
|
|
|
fields,
|
|
|
|
context,
|
2017-11-18 00:43:33 -06:00
|
|
|
Shape::indented(offset, context.config).sub_width(1)?,
|
2017-08-19 13:47:40 -05:00
|
|
|
mk_sp(body_lo, span.hi()),
|
2017-07-25 01:19:09 -05:00
|
|
|
one_line_budget,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2016-05-18 15:38:49 -05:00
|
|
|
|
2018-05-06 01:22:29 -05:00
|
|
|
if !items_str.contains('\n')
|
|
|
|
&& !result.contains('\n')
|
|
|
|
&& items_str.len() <= one_line_budget
|
2018-02-16 21:52:08 -06:00
|
|
|
&& !last_line_contains_single_line_comment(&items_str)
|
2018-02-16 14:09:41 -06:00
|
|
|
{
|
2016-05-18 15:38:49 -05:00
|
|
|
Some(format!("{} {} }}", result, items_str))
|
|
|
|
} else {
|
2017-06-11 22:58:58 -05:00
|
|
|
Some(format!(
|
|
|
|
"{}\n{}{}\n{}}}",
|
|
|
|
result,
|
2017-06-15 18:49:49 -05:00
|
|
|
offset
|
|
|
|
.block_indent(context.config)
|
|
|
|
.to_string(context.config),
|
2017-06-11 22:58:58 -05:00
|
|
|
items_str,
|
|
|
|
offset.to_string(context.config)
|
|
|
|
))
|
2016-05-18 15:38:49 -05:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
2015-04-22 23:22:48 -05:00
|
|
|
|
2018-03-06 04:47:28 -06:00
|
|
|
fn get_bytepos_after_visibility(vis: &ast::Visibility, default_span: Span) -> BytePos {
|
|
|
|
match vis.node {
|
|
|
|
ast::VisibilityKind::Crate(..) | ast::VisibilityKind::Restricted { .. } => vis.span.hi(),
|
2017-11-03 09:51:19 -05:00
|
|
|
_ => default_span.lo(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-27 23:59:03 -05:00
|
|
|
// Format tuple or struct without any fields. We need to make sure that the comments
|
|
|
|
// inside the delimiters are preserved.
|
|
|
|
fn format_empty_struct_or_tuple(
|
|
|
|
context: &RewriteContext,
|
|
|
|
span: Span,
|
|
|
|
offset: Indent,
|
|
|
|
result: &mut String,
|
|
|
|
opener: &str,
|
|
|
|
closer: &str,
|
|
|
|
) {
|
|
|
|
// 3 = " {}" or "();"
|
|
|
|
let used_width = last_line_used_width(&result, offset.width()) + 3;
|
|
|
|
if used_width > context.config.max_width() {
|
|
|
|
result.push_str(&offset.to_string_with_newline(context.config))
|
|
|
|
}
|
|
|
|
result.push_str(opener);
|
|
|
|
match rewrite_missing_comment(span, Shape::indented(offset, context.config), context) {
|
|
|
|
Some(ref s) if s.is_empty() => (),
|
|
|
|
Some(ref s) => {
|
|
|
|
if !is_single_line(s) || first_line_contains_single_line_comment(s) {
|
|
|
|
let nested_indent_str = offset
|
|
|
|
.block_indent(context.config)
|
|
|
|
.to_string_with_newline(context.config);
|
|
|
|
result.push_str(&nested_indent_str);
|
|
|
|
}
|
|
|
|
result.push_str(s);
|
|
|
|
if last_line_contains_single_line_comment(s) {
|
|
|
|
result.push_str(&offset.to_string_with_newline(context.config));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => result.push_str(context.snippet(span)),
|
|
|
|
}
|
|
|
|
result.push_str(closer);
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
fn format_tuple_struct(
|
|
|
|
context: &RewriteContext,
|
2017-11-05 05:30:28 -06:00
|
|
|
struct_parts: &StructParts,
|
2017-06-11 22:58:58 -05:00
|
|
|
fields: &[ast::StructField],
|
|
|
|
offset: Indent,
|
|
|
|
) -> Option<String> {
|
2015-11-22 06:45:51 -06:00
|
|
|
let mut result = String::with_capacity(1024);
|
2017-11-05 05:30:28 -06:00
|
|
|
let span = struct_parts.span;
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2018-06-25 09:36:45 -05:00
|
|
|
let header_str = struct_parts.format_header(context);
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&header_str);
|
|
|
|
|
2016-04-11 14:05:54 -05:00
|
|
|
let body_lo = if fields.is_empty() {
|
2018-03-06 04:47:28 -06:00
|
|
|
let lo = get_bytepos_after_visibility(struct_parts.vis, span);
|
2018-02-18 21:41:43 -06:00
|
|
|
context
|
|
|
|
.snippet_provider
|
|
|
|
.span_after(mk_sp(lo, span.hi()), "(")
|
2016-04-11 14:05:54 -05:00
|
|
|
} else {
|
2017-08-19 13:47:40 -05:00
|
|
|
fields[0].span.lo()
|
2016-04-11 14:05:54 -05:00
|
|
|
};
|
2017-07-10 00:35:50 -05:00
|
|
|
let body_hi = if fields.is_empty() {
|
2018-02-18 21:41:43 -06:00
|
|
|
context
|
|
|
|
.snippet_provider
|
|
|
|
.span_after(mk_sp(body_lo, span.hi()), ")")
|
2017-07-10 00:35:50 -05:00
|
|
|
} else {
|
|
|
|
// This is a dirty hack to work around a missing `)` from the span of the last field.
|
|
|
|
let last_arg_span = fields[fields.len() - 1].span;
|
2018-06-30 05:21:15 -05:00
|
|
|
context
|
|
|
|
.snippet_provider
|
|
|
|
.opt_span_after(mk_sp(last_arg_span.hi(), span.hi()), ")")
|
|
|
|
.unwrap_or(last_arg_span.hi())
|
2017-07-10 00:35:50 -05:00
|
|
|
};
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-11-05 05:30:28 -06:00
|
|
|
let where_clause_str = match struct_parts.generics {
|
2016-08-23 09:14:45 -05:00
|
|
|
Some(generics) => {
|
2017-06-24 05:45:22 -05:00
|
|
|
let budget = context.budget(last_line_width(&header_str));
|
|
|
|
let shape = Shape::legacy(budget, offset);
|
2018-07-24 10:34:46 -05:00
|
|
|
let generics_str = rewrite_generics(context, "", generics, shape)?;
|
2015-11-27 02:25:31 -06:00
|
|
|
result.push_str(&generics_str);
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-06-24 05:45:22 -05:00
|
|
|
let where_budget = context.budget(last_line_width(&result));
|
2017-08-02 09:26:35 -05:00
|
|
|
let option = WhereClauseOption::new(true, false);
|
2017-10-05 06:50:19 -05:00
|
|
|
rewrite_where_clause(
|
2017-06-11 22:58:58 -05:00
|
|
|
context,
|
|
|
|
&generics.where_clause,
|
2017-11-13 19:47:02 -06:00
|
|
|
context.config.brace_style(),
|
2017-06-11 22:58:58 -05:00
|
|
|
Shape::legacy(where_budget, offset.block_only()),
|
|
|
|
Density::Compressed,
|
|
|
|
";",
|
|
|
|
None,
|
2017-07-10 00:35:50 -05:00
|
|
|
body_hi,
|
2017-08-02 09:26:35 -05:00
|
|
|
option,
|
2017-11-05 16:53:17 -06:00
|
|
|
false,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?
|
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-04-14 06:25:47 -05:00
|
|
|
if fields.is_empty() {
|
2018-04-27 23:59:54 -05:00
|
|
|
let body_hi = context
|
|
|
|
.snippet_provider
|
|
|
|
.span_before(mk_sp(body_lo, span.hi()), ")");
|
|
|
|
let inner_span = mk_sp(body_lo, body_hi);
|
|
|
|
format_empty_struct_or_tuple(context, inner_span, offset, &mut result, "(", ")");
|
2017-04-14 06:25:47 -05:00
|
|
|
} else {
|
2017-11-27 05:54:55 -06:00
|
|
|
let shape = Shape::indented(offset, context.config).sub_width(1)?;
|
2018-04-01 04:12:50 -05:00
|
|
|
let fields = &fields.iter().collect::<Vec<_>>();
|
2018-03-07 00:40:52 -06:00
|
|
|
result = overflow::rewrite_with_parens(
|
|
|
|
context,
|
|
|
|
&result,
|
|
|
|
fields,
|
|
|
|
shape,
|
|
|
|
span,
|
|
|
|
context.config.width_heuristics().fn_call_width,
|
2018-03-07 04:29:42 -06:00
|
|
|
None,
|
2018-03-07 00:40:52 -06:00
|
|
|
)?;
|
2017-03-09 20:30:47 -06:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2018-07-05 18:58:22 -05:00
|
|
|
if !where_clause_str.is_empty()
|
|
|
|
&& !where_clause_str.contains('\n')
|
2017-09-14 22:10:58 -05:00
|
|
|
&& (result.contains('\n')
|
|
|
|
|| offset.block_indent + result.len() + where_clause_str.len() + 1
|
|
|
|
> context.config.max_width())
|
2017-06-11 22:58:58 -05:00
|
|
|
{
|
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');
|
2018-05-31 05:33:45 -05:00
|
|
|
result.push_str(
|
|
|
|
&(offset.block_only() + (context.config.tab_spaces() - 1)).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
|
|
|
|
2018-07-24 10:30:01 -05:00
|
|
|
fn rewrite_type_prefix(
|
2017-06-11 22:58:58 -05:00
|
|
|
context: &RewriteContext,
|
|
|
|
indent: Indent,
|
2018-07-24 10:30:01 -05:00
|
|
|
prefix: &str,
|
2017-06-11 22:58:58 -05:00
|
|
|
ident: ast::Ident,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
) -> Option<String> {
|
2017-08-27 10:24:10 -05:00
|
|
|
let mut result = String::with_capacity(128);
|
2018-07-24 10:30:01 -05:00
|
|
|
result.push_str(prefix);
|
|
|
|
let ident_str = rewrite_ident(context, ident);
|
2015-11-22 12:21:01 -06:00
|
|
|
|
2017-06-11 09:26:49 -05:00
|
|
|
// 2 = `= `
|
2018-07-24 10:30:01 -05:00
|
|
|
if generics.params.is_empty() {
|
|
|
|
result.push_str(ident_str)
|
|
|
|
} else {
|
|
|
|
let g_shape = Shape::indented(indent, context.config)
|
|
|
|
.offset_left(result.len())?
|
|
|
|
.sub_width(2)?;
|
2018-07-24 10:34:46 -05:00
|
|
|
let generics_str = rewrite_generics(context, ident_str, generics, g_shape)?;
|
2018-07-24 10:30:01 -05:00
|
|
|
result.push_str(&generics_str);
|
|
|
|
}
|
2015-12-02 12:39:45 -06:00
|
|
|
|
2017-08-29 01:19:56 -05:00
|
|
|
let where_budget = context.budget(last_line_width(&result));
|
2017-08-02 09:26:35 -05:00
|
|
|
let option = WhereClauseOption::snuggled(&result);
|
2017-10-05 06:50:19 -05:00
|
|
|
let where_clause_str = rewrite_where_clause(
|
2017-06-11 22:58:58 -05:00
|
|
|
context,
|
|
|
|
&generics.where_clause,
|
2017-11-13 19:47:02 -06:00
|
|
|
context.config.brace_style(),
|
2017-06-11 22:58:58 -05:00
|
|
|
Shape::legacy(where_budget, indent),
|
2017-11-24 02:08:24 -06:00
|
|
|
Density::Vertical,
|
2017-06-11 22:58:58 -05:00
|
|
|
"=",
|
2018-07-24 10:30:01 -05:00
|
|
|
None,
|
2017-08-19 13:47:40 -05:00
|
|
|
generics.span.hi(),
|
2017-08-02 09:26:35 -05:00
|
|
|
option,
|
2017-11-05 16:53:17 -06:00
|
|
|
false,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2015-12-02 12:39:45 -06:00
|
|
|
result.push_str(&where_clause_str);
|
2018-07-24 10:30:01 -05:00
|
|
|
|
|
|
|
Some(result)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_type_item<R: Rewrite>(
|
|
|
|
context: &RewriteContext,
|
|
|
|
indent: Indent,
|
|
|
|
prefix: &str,
|
|
|
|
suffix: &str,
|
|
|
|
ident: ast::Ident,
|
|
|
|
rhs: &R,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
vis: &ast::Visibility,
|
|
|
|
) -> Option<String> {
|
|
|
|
let mut result = String::with_capacity(128);
|
|
|
|
result.push_str(&rewrite_type_prefix(
|
|
|
|
context,
|
|
|
|
indent,
|
|
|
|
&format!("{}{} ", format_visibility(context, vis), prefix),
|
|
|
|
ident,
|
|
|
|
generics,
|
|
|
|
)?);
|
|
|
|
|
|
|
|
if generics.where_clause.predicates.is_empty() {
|
|
|
|
result.push_str(suffix);
|
2017-08-02 09:27:33 -05:00
|
|
|
} else {
|
2018-07-24 10:30:01 -05:00
|
|
|
result.push_str(&indent.to_string_with_newline(context.config));
|
|
|
|
result.push_str(suffix.trim_left());
|
2017-08-02 09:27:33 -05:00
|
|
|
}
|
2015-11-22 12:21:01 -06:00
|
|
|
|
2017-12-20 18:58:13 -06:00
|
|
|
// 1 = ";"
|
2018-07-24 10:30:01 -05:00
|
|
|
let rhs_shape = Shape::indented(indent, context.config).sub_width(1)?;
|
|
|
|
rewrite_assign_rhs(context, result, rhs, rhs_shape).map(|s| s + ";")
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rewrite_type_alias(
|
|
|
|
context: &RewriteContext,
|
|
|
|
indent: Indent,
|
|
|
|
ident: ast::Ident,
|
|
|
|
ty: &ast::Ty,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
vis: &ast::Visibility,
|
|
|
|
) -> Option<String> {
|
|
|
|
rewrite_type_item(context, indent, "type", " =", ident, ty, generics, vis)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn rewrite_existential_type(
|
|
|
|
context: &RewriteContext,
|
|
|
|
indent: Indent,
|
|
|
|
ident: ast::Ident,
|
|
|
|
generic_bounds: &ast::GenericBounds,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
vis: &ast::Visibility,
|
|
|
|
) -> Option<String> {
|
|
|
|
rewrite_type_item(
|
|
|
|
context,
|
|
|
|
indent,
|
|
|
|
"existential type",
|
|
|
|
":",
|
|
|
|
ident,
|
|
|
|
generic_bounds,
|
|
|
|
generics,
|
|
|
|
vis,
|
|
|
|
)
|
2015-11-22 12:21:01 -06:00
|
|
|
}
|
|
|
|
|
2016-09-16 18:44:51 -05:00
|
|
|
fn type_annotation_spacing(config: &Config) -> (&str, &str) {
|
2017-06-11 22:58:58 -05:00
|
|
|
(
|
2017-11-14 08:25:36 -06:00
|
|
|
if config.space_before_colon() { " " } else { "" },
|
|
|
|
if config.space_after_colon() { " " } else { "" },
|
2017-06-11 22:58:58 -05:00
|
|
|
)
|
2016-08-02 20:07:56 -05:00
|
|
|
}
|
|
|
|
|
2017-07-03 04:54:41 -05:00
|
|
|
pub fn rewrite_struct_field_prefix(
|
|
|
|
context: &RewriteContext,
|
|
|
|
field: &ast::StructField,
|
|
|
|
) -> Option<String> {
|
2018-06-25 09:36:45 -05:00
|
|
|
let vis = format_visibility(context, &field.vis);
|
2017-07-03 04:54:41 -05:00
|
|
|
let type_annotation_spacing = type_annotation_spacing(context.config);
|
|
|
|
Some(match field.ident {
|
2018-06-25 09:36:45 -05:00
|
|
|
Some(name) => format!(
|
|
|
|
"{}{}{}:",
|
|
|
|
vis,
|
|
|
|
rewrite_ident(context, name),
|
|
|
|
type_annotation_spacing.0
|
|
|
|
),
|
2017-08-11 03:52:49 -05:00
|
|
|
None => format!("{}", vis),
|
2017-07-03 04:54:41 -05:00
|
|
|
})
|
|
|
|
}
|
2015-10-18 17:25:38 -05:00
|
|
|
|
2017-06-24 05:49:01 -05:00
|
|
|
impl Rewrite for ast::StructField {
|
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
|
|
|
rewrite_struct_field(context, self, shape, 0)
|
|
|
|
}
|
|
|
|
}
|
2015-10-18 17:25:38 -05:00
|
|
|
|
2017-07-03 04:54:41 -05:00
|
|
|
pub fn rewrite_struct_field(
|
|
|
|
context: &RewriteContext,
|
|
|
|
field: &ast::StructField,
|
|
|
|
shape: Shape,
|
|
|
|
lhs_max_width: usize,
|
|
|
|
) -> Option<String> {
|
|
|
|
if contains_skip(&field.attrs) {
|
2017-12-23 22:57:29 -06:00
|
|
|
return Some(context.snippet(field.span()).to_owned());
|
2017-07-03 04:54:41 -05:00
|
|
|
}
|
2017-03-29 06:15:27 -05:00
|
|
|
|
2017-07-03 04:54:41 -05:00
|
|
|
let type_annotation_spacing = type_annotation_spacing(context.config);
|
2017-10-05 06:50:19 -05:00
|
|
|
let prefix = rewrite_struct_field_prefix(context, field)?;
|
2017-07-03 04:54:41 -05:00
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
let attrs_str = field.attrs.rewrite(context, shape)?;
|
2018-01-11 01:55:50 -06:00
|
|
|
let attrs_extendable = field.ident.is_none() && is_attributes_extendable(&attrs_str);
|
2017-08-11 03:52:49 -05:00
|
|
|
let missing_span = if field.attrs.is_empty() {
|
2017-08-19 13:47:40 -05:00
|
|
|
mk_sp(field.span.lo(), field.span.lo())
|
2017-08-11 03:52:49 -05:00
|
|
|
} else {
|
2017-08-19 13:47:40 -05:00
|
|
|
mk_sp(field.attrs.last().unwrap().span.hi(), field.span.lo())
|
2017-08-11 03:52:49 -05:00
|
|
|
};
|
2017-07-03 04:54:41 -05:00
|
|
|
let mut spacing = String::from(if field.ident.is_some() {
|
|
|
|
type_annotation_spacing.1
|
|
|
|
} else {
|
|
|
|
""
|
|
|
|
});
|
2017-08-11 03:52:49 -05:00
|
|
|
// Try to put everything on a single line.
|
2017-10-05 06:50:19 -05:00
|
|
|
let attr_prefix = combine_strs_with_missing_comments(
|
2017-08-11 03:52:49 -05:00
|
|
|
context,
|
|
|
|
&attrs_str,
|
|
|
|
&prefix,
|
|
|
|
missing_span,
|
|
|
|
shape,
|
2017-08-19 13:51:24 -05:00
|
|
|
attrs_extendable,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2017-08-11 03:52:49 -05:00
|
|
|
let overhead = last_line_width(&attr_prefix);
|
2018-05-14 07:58:57 -05:00
|
|
|
let lhs_offset = lhs_max_width.saturating_sub(overhead);
|
2017-07-03 04:54:41 -05:00
|
|
|
for _ in 0..lhs_offset {
|
|
|
|
spacing.push(' ');
|
|
|
|
}
|
2017-08-11 03:52:49 -05:00
|
|
|
// In this extreme case we will be missing a space betweeen an attribute and a field.
|
2017-08-19 13:51:24 -05:00
|
|
|
if prefix.is_empty() && !attrs_str.is_empty() && attrs_extendable && spacing.is_empty() {
|
2017-08-11 03:52:49 -05:00
|
|
|
spacing.push(' ');
|
|
|
|
}
|
2017-12-23 22:57:29 -06:00
|
|
|
let orig_ty = shape
|
|
|
|
.offset_left(overhead + spacing.len())
|
|
|
|
.and_then(|ty_shape| field.ty.rewrite(context, ty_shape));
|
2017-11-18 00:43:33 -06:00
|
|
|
if let Some(ref ty) = orig_ty {
|
2017-07-03 04:54:41 -05:00
|
|
|
if !ty.contains('\n') {
|
2017-10-08 08:35:45 -05:00
|
|
|
return Some(attr_prefix + &spacing + ty);
|
2017-07-03 04:54:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-23 22:57:29 -06:00
|
|
|
let is_prefix_empty = prefix.is_empty();
|
2017-11-18 00:43:33 -06:00
|
|
|
// We must use multiline. We are going to put attributes and a field on different lines.
|
2017-12-23 22:57:29 -06:00
|
|
|
let field_str = rewrite_assign_rhs(context, prefix, &*field.ty, shape)?;
|
|
|
|
// Remove a leading white-space from `rewrite_assign_rhs()` when rewriting a tuple struct.
|
|
|
|
let field_str = if is_prefix_empty {
|
|
|
|
field_str.trim_left()
|
2017-10-08 08:36:03 -05:00
|
|
|
} else {
|
2017-12-23 22:57:29 -06:00
|
|
|
&field_str
|
2017-08-11 03:52:49 -05:00
|
|
|
};
|
2017-12-23 22:57:29 -06:00
|
|
|
combine_strs_with_missing_comments(context, &attrs_str, field_str, missing_span, shape, false)
|
2015-10-18 17:25:38 -05:00
|
|
|
}
|
|
|
|
|
2017-11-05 00:44:52 -05:00
|
|
|
pub struct StaticParts<'a> {
|
2017-11-05 06:59:33 -06:00
|
|
|
prefix: &'a str,
|
2017-11-05 00:44:52 -05:00
|
|
|
vis: &'a ast::Visibility,
|
2017-06-11 22:58:58 -05:00
|
|
|
ident: ast::Ident,
|
2017-11-05 00:44:52 -05:00
|
|
|
ty: &'a ast::Ty,
|
2017-06-11 22:58:58 -05:00
|
|
|
mutability: ast::Mutability,
|
2017-11-05 00:44:52 -05:00
|
|
|
expr_opt: Option<&'a ptr::P<ast::Expr>>,
|
2017-11-30 04:38:05 -06:00
|
|
|
defaultness: Option<ast::Defaultness>,
|
2017-11-05 06:59:33 -06:00
|
|
|
span: Span,
|
2017-11-05 00:44:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> StaticParts<'a> {
|
2017-11-05 06:59:33 -06:00
|
|
|
pub fn from_item(item: &'a ast::Item) -> Self {
|
|
|
|
let (prefix, ty, mutability, expr) = match item.node {
|
|
|
|
ast::ItemKind::Static(ref ty, mutability, ref expr) => ("static", ty, mutability, expr),
|
|
|
|
ast::ItemKind::Const(ref ty, ref expr) => {
|
|
|
|
("const", ty, ast::Mutability::Immutable, expr)
|
|
|
|
}
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
2017-11-05 00:44:52 -05:00
|
|
|
StaticParts {
|
2018-01-21 22:05:18 -06:00
|
|
|
prefix,
|
2017-11-05 06:59:33 -06:00
|
|
|
vis: &item.vis,
|
|
|
|
ident: item.ident,
|
2018-01-21 22:05:18 -06:00
|
|
|
ty,
|
|
|
|
mutability,
|
2017-11-05 06:59:33 -06:00
|
|
|
expr_opt: Some(expr),
|
2017-11-30 04:38:05 -06:00
|
|
|
defaultness: None,
|
2017-11-05 06:59:33 -06:00
|
|
|
span: item.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_trait_item(ti: &'a ast::TraitItem) -> Self {
|
|
|
|
let (ty, expr_opt) = match ti.node {
|
|
|
|
ast::TraitItemKind::Const(ref ty, ref expr_opt) => (ty, expr_opt),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
StaticParts {
|
|
|
|
prefix: "const",
|
2018-03-06 04:47:28 -06:00
|
|
|
vis: &DEFAULT_VISIBILITY,
|
2017-11-05 06:59:33 -06:00
|
|
|
ident: ti.ident,
|
2018-01-21 22:05:18 -06:00
|
|
|
ty,
|
2017-11-05 06:59:33 -06:00
|
|
|
mutability: ast::Mutability::Immutable,
|
|
|
|
expr_opt: expr_opt.as_ref(),
|
2017-11-30 04:38:05 -06:00
|
|
|
defaultness: None,
|
2017-11-05 06:59:33 -06:00
|
|
|
span: ti.span,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn from_impl_item(ii: &'a ast::ImplItem) -> Self {
|
|
|
|
let (ty, expr) = match ii.node {
|
|
|
|
ast::ImplItemKind::Const(ref ty, ref expr) => (ty, expr),
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
StaticParts {
|
|
|
|
prefix: "const",
|
|
|
|
vis: &ii.vis,
|
|
|
|
ident: ii.ident,
|
2018-01-21 22:05:18 -06:00
|
|
|
ty,
|
2017-11-05 06:59:33 -06:00
|
|
|
mutability: ast::Mutability::Immutable,
|
|
|
|
expr_opt: Some(expr),
|
2017-11-30 04:38:05 -06:00
|
|
|
defaultness: Some(ii.defaultness),
|
2017-11-05 06:59:33 -06:00
|
|
|
span: ii.span,
|
2017-11-05 00:44:52 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-05 06:59:33 -06:00
|
|
|
fn rewrite_static(
|
|
|
|
context: &RewriteContext,
|
2017-11-05 00:44:52 -05:00
|
|
|
static_parts: &StaticParts,
|
2017-06-11 22:58:58 -05:00
|
|
|
offset: Indent,
|
|
|
|
) -> Option<String> {
|
2017-07-05 04:25:50 -05:00
|
|
|
let colon = colon_spaces(
|
2017-11-14 08:25:36 -06:00
|
|
|
context.config.space_before_colon(),
|
|
|
|
context.config.space_after_colon(),
|
2017-07-05 04:25:50 -05:00
|
|
|
);
|
2018-01-10 08:23:42 -06:00
|
|
|
let mut prefix = format!(
|
2017-11-30 04:38:05 -06:00
|
|
|
"{}{}{} {}{}{}",
|
2018-06-25 09:36:45 -05:00
|
|
|
format_visibility(context, static_parts.vis),
|
2017-11-30 04:38:05 -06:00
|
|
|
static_parts.defaultness.map_or("", format_defaultness),
|
2017-11-06 06:46:45 -06:00
|
|
|
static_parts.prefix,
|
|
|
|
format_mutability(static_parts.mutability),
|
|
|
|
static_parts.ident,
|
2017-07-05 04:25:50 -05:00
|
|
|
colon,
|
2017-06-11 22:58:58 -05:00
|
|
|
);
|
2015-10-18 14:25:30 -05:00
|
|
|
// 2 = " =".len()
|
2017-11-06 06:46:45 -06:00
|
|
|
let ty_shape =
|
|
|
|
Shape::indented(offset.block_only(), context.config).offset_left(prefix.len() + 2)?;
|
2018-01-10 08:23:42 -06:00
|
|
|
let ty_str = match static_parts.ty.rewrite(context, ty_shape) {
|
|
|
|
Some(ty_str) => ty_str,
|
|
|
|
None => {
|
|
|
|
if prefix.ends_with(' ') {
|
|
|
|
prefix.pop();
|
|
|
|
}
|
|
|
|
let nested_indent = offset.block_indent(context.config);
|
|
|
|
let nested_shape = Shape::indented(nested_indent, context.config);
|
|
|
|
let ty_str = static_parts.ty.rewrite(context, nested_shape)?;
|
2018-02-18 21:47:54 -06:00
|
|
|
format!(
|
|
|
|
"{}{}",
|
|
|
|
nested_indent.to_string_with_newline(context.config),
|
|
|
|
ty_str
|
|
|
|
)
|
2018-01-10 08:23:42 -06:00
|
|
|
}
|
|
|
|
};
|
2015-10-18 14:25:30 -05:00
|
|
|
|
2017-11-06 06:46:45 -06:00
|
|
|
if let Some(expr) = static_parts.expr_opt {
|
2016-03-15 15:08:12 -05:00
|
|
|
let lhs = format!("{}{} =", prefix, ty_str);
|
|
|
|
// 1 = ;
|
2017-08-29 01:19:56 -05:00
|
|
|
let remaining_width = context.budget(offset.block_indent + 1);
|
2017-06-11 22:58:58 -05:00
|
|
|
rewrite_assign_rhs(
|
|
|
|
context,
|
|
|
|
lhs,
|
2017-11-15 20:26:36 -06:00
|
|
|
&**expr,
|
2017-06-11 22:58:58 -05:00
|
|
|
Shape::legacy(remaining_width, offset.block_only()),
|
2017-11-29 02:37:51 -06:00
|
|
|
).and_then(|res| recover_comment_removed(res, static_parts.span, context))
|
2018-07-11 04:35:10 -05:00
|
|
|
.map(|s| if s.ends_with(';') { s } else { s + ";" })
|
2016-03-15 15:08:12 -05:00
|
|
|
} else {
|
2017-07-05 04:25:50 -05:00
|
|
|
Some(format!("{}{};", prefix, ty_str))
|
2016-03-15 15:08:12 -05:00
|
|
|
}
|
2015-10-18 14:25:30 -05:00
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn rewrite_associated_type(
|
|
|
|
ident: ast::Ident,
|
|
|
|
ty_opt: Option<&ptr::P<ast::Ty>>,
|
2018-06-25 01:24:00 -05:00
|
|
|
generic_bounds_opt: Option<&ast::GenericBounds>,
|
2017-06-11 22:58:58 -05:00
|
|
|
context: &RewriteContext,
|
|
|
|
indent: Indent,
|
|
|
|
) -> Option<String> {
|
2018-06-25 09:36:45 -05:00
|
|
|
let prefix = format!("type {}", rewrite_ident(context, ident));
|
2016-03-14 20:52:07 -05:00
|
|
|
|
2018-06-25 01:24:00 -05:00
|
|
|
let type_bounds_str = if let Some(bounds) = generic_bounds_opt {
|
2018-03-09 23:54:13 -06:00
|
|
|
if bounds.is_empty() {
|
2016-03-14 20:52:07 -05:00
|
|
|
String::new()
|
2018-03-09 23:54:13 -06:00
|
|
|
} else {
|
|
|
|
// 2 = ": ".len()
|
|
|
|
let shape = Shape::indented(indent, context.config).offset_left(prefix.len() + 2)?;
|
|
|
|
bounds.rewrite(context, shape).map(|s| format!(": {}", s))?
|
2017-05-23 08:13:29 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
String::new()
|
|
|
|
};
|
2016-03-14 20:52:07 -05:00
|
|
|
|
|
|
|
if let Some(ty) = ty_opt {
|
2017-11-15 20:27:11 -06:00
|
|
|
// 1 = `;`
|
|
|
|
let shape = Shape::indented(indent, context.config).sub_width(1)?;
|
|
|
|
let lhs = format!("{}{} =", prefix, type_bounds_str);
|
|
|
|
rewrite_assign_rhs(context, lhs, &**ty, shape).map(|s| s + ";")
|
2016-03-14 20:52:07 -05:00
|
|
|
} else {
|
|
|
|
Some(format!("{}{};", prefix, type_bounds_str))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-24 10:30:01 -05:00
|
|
|
pub fn rewrite_existential_impl_type(
|
|
|
|
context: &RewriteContext,
|
|
|
|
ident: ast::Ident,
|
|
|
|
generic_bounds: &ast::GenericBounds,
|
|
|
|
indent: Indent,
|
|
|
|
) -> Option<String> {
|
|
|
|
rewrite_associated_type(ident, None, Some(generic_bounds), context, indent)
|
|
|
|
.map(|s| format!("existential {}", s))
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
pub fn rewrite_associated_impl_type(
|
|
|
|
ident: ast::Ident,
|
|
|
|
defaultness: ast::Defaultness,
|
|
|
|
ty_opt: Option<&ptr::P<ast::Ty>>,
|
2018-06-25 01:24:00 -05:00
|
|
|
generic_bounds_opt: Option<&ast::GenericBounds>,
|
2017-06-11 22:58:58 -05:00
|
|
|
context: &RewriteContext,
|
|
|
|
indent: Indent,
|
|
|
|
) -> Option<String> {
|
2018-06-25 01:24:00 -05:00
|
|
|
let result = rewrite_associated_type(ident, ty_opt, generic_bounds_opt, context, indent)?;
|
2017-02-26 15:43:49 -06:00
|
|
|
|
|
|
|
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-10-05 06:50:19 -05:00
|
|
|
let inner_width = 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
|
|
|
|
2017-06-16 16:25:58 -05:00
|
|
|
fn is_empty_infer(context: &RewriteContext, ty: &ast::Ty) -> bool {
|
|
|
|
match ty.node {
|
|
|
|
ast::TyKind::Infer => {
|
|
|
|
let original = context.snippet(ty.span);
|
|
|
|
original != "_"
|
|
|
|
}
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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) {
|
2018-05-07 16:25:48 -05:00
|
|
|
let mut result = self
|
|
|
|
.pat
|
2017-10-05 06:50:19 -05:00
|
|
|
.rewrite(context, Shape::legacy(shape.width, shape.indent))?;
|
2015-12-25 13:59:46 -06:00
|
|
|
|
2017-06-16 16:25:58 -05:00
|
|
|
if !is_empty_infer(context, &*self.ty) {
|
2017-11-14 08:25:36 -06:00
|
|
|
if context.config.space_before_colon() {
|
2016-08-02 20:07:56 -05:00
|
|
|
result.push_str(" ");
|
|
|
|
}
|
2016-09-16 18:44:51 -05:00
|
|
|
result.push_str(":");
|
2017-11-14 08:25:36 -06:00
|
|
|
if context.config.space_after_colon() {
|
2016-09-16 18:44:51 -05:00
|
|
|
result.push_str(" ");
|
|
|
|
}
|
2017-09-25 21:20:47 -05:00
|
|
|
let overhead = last_line_width(&result);
|
2017-10-05 06:50:19 -05:00
|
|
|
let max_width = shape.width.checked_sub(overhead)?;
|
2018-05-07 16:25:48 -05:00
|
|
|
let ty_str = self
|
|
|
|
.ty
|
2017-10-05 06:50:19 -05:00
|
|
|
.rewrite(context, Shape::legacy(max_width, shape.indent))?;
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05: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-10-05 06:50:19 -05:00
|
|
|
let lifetime_str = l.rewrite(
|
2017-06-11 22:58:58 -05:00
|
|
|
context,
|
2017-06-13 10:06:48 -05:00
|
|
|
Shape::legacy(context.config.max_width(), Indent::empty()),
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
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-10-05 06:50:19 -05:00
|
|
|
let type_str = ty.rewrite(
|
2017-06-11 22:58:58 -05:00
|
|
|
context,
|
2017-06-13 10:06:48 -05:00
|
|
|
Shape::legacy(context.config.max_width(), Indent::empty()),
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2017-06-11 22:58:58 -05: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) {
|
2017-08-19 13:47:40 -05:00
|
|
|
arg.pat.span.lo()
|
2015-07-19 07:33:02 -05:00
|
|
|
} else {
|
2017-08-19 13:47:40 -05:00
|
|
|
arg.ty.span.lo()
|
2015-07-19 07:33:02 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-16 16:25:58 -05:00
|
|
|
pub fn span_hi_for_arg(context: &RewriteContext, arg: &ast::Arg) -> BytePos {
|
2015-08-19 15:39:45 -05:00
|
|
|
match arg.ty.node {
|
2017-08-19 13:47:40 -05:00
|
|
|
ast::TyKind::Infer if context.snippet(arg.ty.span) == "_" => arg.ty.span.hi(),
|
|
|
|
ast::TyKind::Infer if is_named_arg(arg) => arg.pat.span.hi(),
|
|
|
|
_ => arg.ty.span.hi(),
|
2015-08-19 15:39:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-04-08 09:18:18 -05:00
|
|
|
ident != 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
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
// Return type is (result, force_new_line_for_brace)
|
2017-06-11 22:58:58 -05:00
|
|
|
fn rewrite_fn_base(
|
|
|
|
context: &RewriteContext,
|
|
|
|
indent: Indent,
|
|
|
|
ident: ast::Ident,
|
2017-09-28 22:49:39 -05:00
|
|
|
fn_sig: &FnSig,
|
2017-06-11 22:58:58 -05:00
|
|
|
span: Span,
|
|
|
|
newline_brace: bool,
|
|
|
|
has_body: bool,
|
|
|
|
) -> Option<(String, bool)> {
|
2015-11-22 06:45:51 -06:00
|
|
|
let mut force_new_line_for_brace = false;
|
|
|
|
|
2017-09-28 22:49:39 -05:00
|
|
|
let where_clause = &fn_sig.generics.where_clause;
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
let mut result = String::with_capacity(1024);
|
2017-09-29 01:09:13 -05:00
|
|
|
result.push_str(&fn_sig.to_str(context));
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
// fn foo
|
|
|
|
result.push_str("fn ");
|
|
|
|
|
|
|
|
// Generics.
|
2017-09-28 22:50:35 -05:00
|
|
|
let overhead = if has_body && !newline_brace {
|
2017-06-14 06:37:54 -05:00
|
|
|
// 4 = `() {`
|
|
|
|
4
|
|
|
|
} else {
|
|
|
|
// 2 = `()`
|
|
|
|
2
|
|
|
|
};
|
2017-08-01 08:23:12 -05:00
|
|
|
let used_width = last_line_used_width(&result, indent.width());
|
2017-08-29 01:19:56 -05:00
|
|
|
let one_line_budget = context.budget(used_width + overhead);
|
2017-08-01 08:23:12 -05:00
|
|
|
let shape = Shape {
|
|
|
|
width: one_line_budget,
|
2018-01-21 22:05:18 -06:00
|
|
|
indent,
|
2017-08-01 08:23:12 -05:00
|
|
|
offset: used_width,
|
|
|
|
};
|
2017-09-28 22:49:39 -05:00
|
|
|
let fd = fn_sig.decl;
|
2018-06-25 09:36:45 -05:00
|
|
|
let generics_str = rewrite_generics(
|
|
|
|
context,
|
|
|
|
rewrite_ident(context, ident),
|
|
|
|
fn_sig.generics,
|
|
|
|
shape,
|
|
|
|
)?;
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&generics_str);
|
|
|
|
|
2017-06-17 02:56:54 -05:00
|
|
|
let snuggle_angle_bracket = generics_str
|
|
|
|
.lines()
|
|
|
|
.last()
|
|
|
|
.map_or(false, |l| l.trim_left().len() == 1);
|
2017-03-09 19:29:45 -06:00
|
|
|
|
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.
|
2018-05-07 16:25:48 -05:00
|
|
|
let ret_str = fd
|
|
|
|
.output
|
2017-10-05 06:50:19 -05:00
|
|
|
.rewrite(context, Shape::indented(indent, context.config))?;
|
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.
|
2017-10-05 06:50:19 -05:00
|
|
|
let (one_line_budget, multi_line_budget, mut arg_indent) = compute_budgets_for_args(
|
2017-08-01 08:25:26 -05:00
|
|
|
context,
|
|
|
|
&result,
|
|
|
|
indent,
|
|
|
|
ret_str_len,
|
|
|
|
newline_brace,
|
2017-09-28 22:50:35 -05:00
|
|
|
has_body,
|
2017-08-01 08:25:26 -05:00
|
|
|
multi_line_ret_str,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2016-04-01 18:25:35 -05:00
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
debug!(
|
|
|
|
"rewrite_fn_base: one_line_budget: {}, multi_line_budget: {}, arg_indent: {:?}",
|
2017-12-02 20:38:16 -06:00
|
|
|
one_line_budget, multi_line_budget, arg_indent
|
2017-06-11 22:58:58 -05:00
|
|
|
);
|
2015-11-22 06:45:51 -06:00
|
|
|
|
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 {
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push('(');
|
|
|
|
} else {
|
2017-08-29 08:16:04 -05:00
|
|
|
result.push_str("(");
|
2017-11-13 01:42:29 -06:00
|
|
|
if context.config.indent_style() == IndentStyle::Visual {
|
2018-02-18 21:47:54 -06:00
|
|
|
result.push_str(&arg_indent.to_string_with_newline(context.config));
|
2017-06-03 09:18:39 -05:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.push('(');
|
2017-06-03 09:18:39 -05:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-10-24 10:51:04 -05:00
|
|
|
// Skip `pub(crate)`.
|
2018-03-06 04:47:28 -06:00
|
|
|
let lo_after_visibility = get_bytepos_after_visibility(&fn_sig.visibility, span);
|
2015-11-22 06:45:51 -06:00
|
|
|
// A conservative estimation, to goal is to be over all parens in generics
|
2017-09-28 22:49:39 -05:00
|
|
|
let args_start = fn_sig
|
|
|
|
.generics
|
2017-12-21 19:52:22 -06:00
|
|
|
.params
|
|
|
|
.iter()
|
2017-06-17 02:56:54 -05:00
|
|
|
.last()
|
2017-12-23 09:29:59 -06:00
|
|
|
.map_or(lo_after_visibility, |param| param.span().hi());
|
2017-07-10 00:35:50 -05:00
|
|
|
let args_end = if fd.inputs.is_empty() {
|
2017-08-19 13:47:40 -05:00
|
|
|
context
|
2018-02-18 21:41:43 -06:00
|
|
|
.snippet_provider
|
2017-08-19 13:47:40 -05:00
|
|
|
.span_after(mk_sp(args_start, span.hi()), ")")
|
2017-07-10 00:35:50 -05:00
|
|
|
} else {
|
2017-08-19 13:47:40 -05:00
|
|
|
let last_span = mk_sp(fd.inputs[fd.inputs.len() - 1].span().hi(), span.hi());
|
2018-02-18 21:41:43 -06:00
|
|
|
context.snippet_provider.span_after(last_span, ")")
|
2017-07-10 00:35:50 -05:00
|
|
|
};
|
2017-06-11 22:58:58 -05:00
|
|
|
let args_span = mk_sp(
|
2017-08-19 13:47:40 -05:00
|
|
|
context
|
2018-02-18 21:41:43 -06:00
|
|
|
.snippet_provider
|
2017-08-19 13:47:40 -05:00
|
|
|
.span_after(mk_sp(args_start, span.hi()), "("),
|
2017-07-10 00:35:50 -05:00
|
|
|
args_end,
|
2017-06-11 22:58:58 -05:00
|
|
|
);
|
2017-10-05 06:50:19 -05:00
|
|
|
let arg_str = rewrite_args(
|
2017-06-11 22:58:58 -05:00
|
|
|
context,
|
|
|
|
&fd.inputs,
|
|
|
|
fd.get_self().as_ref(),
|
|
|
|
one_line_budget,
|
|
|
|
multi_line_budget,
|
|
|
|
indent,
|
|
|
|
arg_indent,
|
|
|
|
args_span,
|
|
|
|
fd.variadic,
|
|
|
|
generics_str.contains('\n'),
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2016-04-01 18:25:35 -05:00
|
|
|
|
2017-11-13 01:42:29 -06:00
|
|
|
let put_args_in_block = match context.config.indent_style() {
|
2017-08-01 08:25:26 -05:00
|
|
|
IndentStyle::Block => arg_str.contains('\n') || arg_str.len() > one_line_budget,
|
2017-03-12 17:25:46 -05:00
|
|
|
_ => false,
|
|
|
|
} && !fd.inputs.is_empty();
|
2016-04-01 18:25:35 -05:00
|
|
|
|
2017-05-25 23:14:36 -05:00
|
|
|
let mut args_last_line_contains_comment = false;
|
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);
|
2018-02-18 21:47:54 -06:00
|
|
|
result.push_str(&arg_indent.to_string_with_newline(context.config));
|
2016-04-01 18:25:35 -05:00
|
|
|
result.push_str(&arg_str);
|
2018-02-18 21:47:54 -06:00
|
|
|
result.push_str(&indent.to_string_with_newline(context.config));
|
2016-04-01 18:25:35 -05:00
|
|
|
result.push(')');
|
|
|
|
} else {
|
|
|
|
result.push_str(&arg_str);
|
2017-08-01 08:25:26 -05:00
|
|
|
let used_width = last_line_used_width(&result, indent.width()) + first_line_width(&ret_str);
|
|
|
|
// Put the closing brace on the next line if it overflows the max width.
|
|
|
|
// 1 = `)`
|
2017-08-29 08:16:04 -05:00
|
|
|
if fd.inputs.is_empty() && used_width + 1 > context.config.max_width() {
|
2017-08-01 08:25:26 -05:00
|
|
|
result.push('\n');
|
|
|
|
}
|
2017-05-25 23:14:36 -05:00
|
|
|
// If the last line of args contains comment, we cannot put the closing paren
|
|
|
|
// on the same line.
|
2017-06-17 02:56:54 -05:00
|
|
|
if arg_str
|
|
|
|
.lines()
|
|
|
|
.last()
|
|
|
|
.map_or(false, |last_line| last_line.contains("//"))
|
2017-06-11 22:58:58 -05:00
|
|
|
{
|
2017-05-25 23:14:36 -05:00
|
|
|
args_last_line_contains_comment = true;
|
2018-02-18 21:47:54 -06:00
|
|
|
result.push_str(&arg_indent.to_string_with_newline(context.config));
|
2017-05-25 23:14:36 -05:00
|
|
|
}
|
2016-04-01 18:25:35 -05:00
|
|
|
result.push(')');
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return type.
|
2017-08-01 08:25:26 -05:00
|
|
|
if let ast::FunctionRetTy::Ty(..) = fd.output {
|
2017-11-13 01:42:29 -06:00
|
|
|
let ret_should_indent = match context.config.indent_style() {
|
2016-04-07 14:47:43 -05:00
|
|
|
// If our args are block layout then we surely must have space.
|
2017-08-29 08:16:04 -05:00
|
|
|
IndentStyle::Block if put_args_in_block || fd.inputs.is_empty() => false,
|
2017-08-01 08:25:26 -05:00
|
|
|
_ if args_last_line_contains_comment => false,
|
|
|
|
_ if result.contains('\n') || multi_line_ret_str => true,
|
2016-04-07 14:01:16 -05:00
|
|
|
_ => {
|
2017-08-01 08:25:26 -05:00
|
|
|
// If 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.
|
2016-07-31 23:25:00 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-08-01 08:25:26 -05:00
|
|
|
sig_length > context.config.max_width()
|
2016-04-07 14:01:16 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let ret_indent = if ret_should_indent {
|
2017-11-23 18:19:36 -06:00
|
|
|
let indent = if arg_str.is_empty() {
|
2015-11-22 06:45:51 -06:00
|
|
|
// Aligning with non-existent args looks silly.
|
2017-11-23 18:19:36 -06:00
|
|
|
force_new_line_for_brace = true;
|
|
|
|
indent + 4
|
|
|
|
} else {
|
2015-11-22 06:45:51 -06:00
|
|
|
// 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.
|
2017-11-23 18:19:36 -06:00
|
|
|
arg_indent
|
2015-11-22 06:45:51 -06:00
|
|
|
};
|
|
|
|
|
2018-02-18 21:47:54 -06:00
|
|
|
result.push_str(&indent.to_string_with_newline(context.config));
|
2015-11-22 06:45:51 -06:00
|
|
|
indent
|
|
|
|
} else {
|
|
|
|
result.push(' ');
|
2017-06-12 02:23:10 -05:00
|
|
|
Indent::new(indent.block_indent, last_line_width(&result))
|
2015-11-22 06:45:51 -06:00
|
|
|
};
|
|
|
|
|
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.
|
2018-05-07 16:25:48 -05:00
|
|
|
let ret_str = fd
|
|
|
|
.output
|
2017-10-05 06:50:19 -05:00
|
|
|
.rewrite(context, Shape::indented(ret_indent, context.config))?;
|
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.
|
2017-08-19 13:47:40 -05:00
|
|
|
let snippet_lo = fd.output.span().hi();
|
2015-11-22 06:45:51 -06:00
|
|
|
if where_clause.predicates.is_empty() {
|
2017-08-19 13:47:40 -05:00
|
|
|
let snippet_hi = span.hi();
|
2015-11-22 06:45:51 -06:00
|
|
|
let snippet = context.snippet(mk_sp(snippet_lo, snippet_hi));
|
2017-06-03 16:29:08 -05:00
|
|
|
// Try to preserve the layout of the original snippet.
|
2017-06-17 02:56:54 -05:00
|
|
|
let original_starts_with_newline = snippet
|
|
|
|
.find(|c| c != ' ')
|
2017-10-04 10:23:17 -05:00
|
|
|
.map_or(false, |i| starts_with_newline(&snippet[i..]));
|
2017-06-17 02:56:54 -05:00
|
|
|
let original_ends_with_newline = snippet
|
|
|
|
.rfind(|c| c != ' ')
|
|
|
|
.map_or(false, |i| snippet[i..].ends_with('\n'));
|
2015-11-22 06:45:51 -06:00
|
|
|
let snippet = snippet.trim();
|
|
|
|
if !snippet.is_empty() {
|
2017-06-11 23:01:41 -05:00
|
|
|
result.push(if original_starts_with_newline {
|
|
|
|
'\n'
|
|
|
|
} else {
|
|
|
|
' '
|
|
|
|
});
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(snippet);
|
2017-06-03 16:29:08 -05:00
|
|
|
if original_ends_with_newline {
|
|
|
|
force_new_line_for_brace = true;
|
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 00:35:50 -05:00
|
|
|
let pos_before_where = match fd.output {
|
2017-08-19 13:47:40 -05:00
|
|
|
ast::FunctionRetTy::Default(..) => args_span.hi(),
|
|
|
|
ast::FunctionRetTy::Ty(ref ty) => ty.span.hi(),
|
2017-07-10 00:35:50 -05:00
|
|
|
};
|
2017-08-01 08:26:22 -05:00
|
|
|
|
2017-11-05 16:53:17 -06:00
|
|
|
let is_args_multi_lined = arg_str.contains('\n');
|
|
|
|
|
2017-09-28 22:50:35 -05:00
|
|
|
let option = WhereClauseOption::new(!has_body, put_args_in_block && ret_str.is_empty());
|
2017-10-05 06:50:19 -05:00
|
|
|
let where_clause_str = rewrite_where_clause(
|
2017-06-11 22:58:58 -05:00
|
|
|
context,
|
|
|
|
where_clause,
|
2017-11-13 19:47:02 -06:00
|
|
|
context.config.brace_style(),
|
2017-06-11 22:58:58 -05:00
|
|
|
Shape::indented(indent, context.config),
|
|
|
|
Density::Tall,
|
|
|
|
"{",
|
2017-08-19 13:47:40 -05:00
|
|
|
Some(span.hi()),
|
2017-07-10 00:35:50 -05:00
|
|
|
pos_before_where,
|
2017-08-02 09:26:35 -05:00
|
|
|
option,
|
2017-11-05 16:53:17 -06:00
|
|
|
is_args_multi_lined,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2017-08-10 09:10:41 -05:00
|
|
|
// If there are neither where clause nor return type, we may be missing comments between
|
|
|
|
// args and `{`.
|
|
|
|
if where_clause_str.is_empty() {
|
|
|
|
if let ast::FunctionRetTy::Default(ret_span) = fd.output {
|
2017-08-27 10:13:42 -05:00
|
|
|
match recover_missing_comment_in_span(
|
2017-08-19 13:47:40 -05:00
|
|
|
mk_sp(args_span.hi(), ret_span.hi()),
|
2017-08-27 10:13:42 -05:00
|
|
|
shape,
|
|
|
|
context,
|
|
|
|
last_line_width(&result),
|
|
|
|
) {
|
|
|
|
Some(ref missing_comment) if !missing_comment.is_empty() => {
|
|
|
|
result.push_str(missing_comment);
|
|
|
|
force_new_line_for_brace = true;
|
|
|
|
}
|
|
|
|
_ => (),
|
2017-08-10 09:10:41 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-01-10 23:06:06 -06:00
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&where_clause_str);
|
|
|
|
|
2017-06-09 02:15:31 -05:00
|
|
|
force_new_line_for_brace |= last_line_contains_single_line_comment(&result);
|
2017-11-05 16:53:17 -06:00
|
|
|
force_new_line_for_brace |= is_args_multi_lined && context.config.where_single_line();
|
2017-08-29 08:16:04 -05:00
|
|
|
Some((result, force_new_line_for_brace))
|
2017-06-09 02:15:31 -05:00
|
|
|
}
|
|
|
|
|
2017-08-29 08:16:04 -05:00
|
|
|
#[derive(Copy, Clone)]
|
2017-08-02 09:26:35 -05:00
|
|
|
struct WhereClauseOption {
|
|
|
|
suppress_comma: bool, // Force no trailing comma
|
|
|
|
snuggle: bool, // Do not insert newline before `where`
|
|
|
|
compress_where: bool, // Try single line where clause instead of vertical layout
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WhereClauseOption {
|
|
|
|
pub fn new(suppress_comma: bool, snuggle: bool) -> WhereClauseOption {
|
|
|
|
WhereClauseOption {
|
2018-01-21 22:05:18 -06:00
|
|
|
suppress_comma,
|
|
|
|
snuggle,
|
2017-08-02 09:26:35 -05:00
|
|
|
compress_where: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn snuggled(current: &str) -> WhereClauseOption {
|
|
|
|
WhereClauseOption {
|
|
|
|
suppress_comma: false,
|
2018-06-05 20:54:40 -05:00
|
|
|
snuggle: last_line_width(current) == 1,
|
2017-08-02 09:26:35 -05:00
|
|
|
compress_where: false,
|
|
|
|
}
|
|
|
|
}
|
2018-05-31 04:58:48 -05:00
|
|
|
|
|
|
|
pub fn suppress_comma(&mut self) {
|
|
|
|
self.suppress_comma = true
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn compress_where(&mut self) {
|
|
|
|
self.compress_where = true
|
|
|
|
}
|
2018-05-31 05:33:45 -05:00
|
|
|
|
|
|
|
pub fn snuggle(&mut self) {
|
|
|
|
self.snuggle = true
|
|
|
|
}
|
2017-08-02 09:26:35 -05:00
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
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,
|
|
|
|
generics_str_contains_newline: bool,
|
|
|
|
) -> Option<String> {
|
2018-05-07 16:25:48 -05:00
|
|
|
let mut arg_item_strs = args
|
|
|
|
.iter()
|
2017-11-29 02:37:51 -06:00
|
|
|
.map(|arg| arg.rewrite(context, Shape::legacy(multi_line_budget, arg_indent)))
|
2017-10-05 06:50:19 -05:00
|
|
|
.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
|
2017-11-29 02:37:51 -06:00
|
|
|
.and_then(|explicit_self| rewrite_explicit_self(explicit_self, args, context))
|
2017-03-27 16:58:41 -05:00
|
|
|
.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]) {
|
2017-08-19 13:47:40 -05:00
|
|
|
args[1].pat.span.lo()
|
2016-03-27 06:07:28 -05:00
|
|
|
} else {
|
2017-08-19 13:47:40 -05:00
|
|
|
args[1].ty.span.lo()
|
2016-03-27 06:07:28 -05:00
|
|
|
};
|
2017-08-19 13:47:40 -05:00
|
|
|
let reduced_span = mk_sp(span.lo(), second_arg_start);
|
2016-03-27 06:07:28 -05:00
|
|
|
|
2018-02-18 21:41:43 -06:00
|
|
|
context.snippet_provider.span_after_last(reduced_span, ",")
|
2015-11-22 06:45:51 -06:00
|
|
|
} else {
|
2017-08-19 13:47:40 -05:00
|
|
|
span.lo()
|
2015-11-22 06:45:51 -06:00
|
|
|
};
|
|
|
|
|
|
|
|
enum ArgumentKind<'a> {
|
|
|
|
Regular(&'a ast::Arg),
|
|
|
|
Variadic(BytePos),
|
|
|
|
}
|
|
|
|
|
|
|
|
let variadic_arg = if variadic {
|
2017-08-19 13:47:40 -05:00
|
|
|
let variadic_span = mk_sp(args.last().unwrap().ty.span.hi(), span.hi());
|
2018-02-18 21:41:43 -06:00
|
|
|
let variadic_start =
|
|
|
|
context.snippet_provider.span_after(variadic_span, "...") - BytePos(3);
|
2015-11-22 06:45:51 -06:00
|
|
|
Some(ArgumentKind::Variadic(variadic_start))
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
let more_items = itemize_list(
|
2018-02-18 21:41:43 -06:00
|
|
|
context.snippet_provider,
|
2017-06-11 22:58:58 -05:00
|
|
|
args[min_args - 1..]
|
|
|
|
.iter()
|
|
|
|
.map(ArgumentKind::Regular)
|
|
|
|
.chain(variadic_arg),
|
|
|
|
")",
|
2017-11-16 02:38:12 -06:00
|
|
|
",",
|
2017-06-11 22:58:58 -05:00
|
|
|
|arg| match *arg {
|
|
|
|
ArgumentKind::Regular(arg) => span_lo_for_arg(arg),
|
|
|
|
ArgumentKind::Variadic(start) => start,
|
|
|
|
},
|
|
|
|
|arg| match *arg {
|
2017-08-19 13:47:40 -05:00
|
|
|
ArgumentKind::Regular(arg) => arg.ty.span.hi(),
|
2017-06-11 22:58:58 -05:00
|
|
|
ArgumentKind::Variadic(start) => start + BytePos(3),
|
|
|
|
},
|
|
|
|
|arg| match *arg {
|
|
|
|
ArgumentKind::Regular(..) => None,
|
|
|
|
ArgumentKind::Variadic(..) => Some("...".to_owned()),
|
|
|
|
},
|
|
|
|
comment_span_start,
|
2017-08-19 13:47:40 -05:00
|
|
|
span.hi(),
|
2017-08-07 03:29:55 -05:00
|
|
|
false,
|
2017-06-11 22:58:58 -05:00
|
|
|
);
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
arg_items.extend(more_items);
|
|
|
|
}
|
|
|
|
|
2017-09-14 22:10:58 -05:00
|
|
|
let fits_in_one_line = !generics_str_contains_newline
|
|
|
|
&& (arg_items.is_empty()
|
|
|
|
|| arg_items.len() == 1 && arg_item_strs[0].len() <= one_line_budget);
|
2017-05-17 02:31:09 -05:00
|
|
|
|
2015-11-22 06:45:51 -06:00
|
|
|
for (item, arg) in arg_items.iter_mut().zip(arg_item_strs) {
|
|
|
|
item.item = Some(arg);
|
|
|
|
}
|
|
|
|
|
2017-05-26 17:33:06 -05:00
|
|
|
let last_line_ends_with_comment = arg_items
|
|
|
|
.iter()
|
|
|
|
.last()
|
|
|
|
.and_then(|item| item.post_comment.as_ref())
|
|
|
|
.map_or(false, |s| s.trim().starts_with("//"));
|
|
|
|
|
2017-11-13 01:42:29 -06:00
|
|
|
let (indent, trailing_comma) = match context.config.indent_style() {
|
2017-07-13 06:32:46 -05:00
|
|
|
IndentStyle::Block if fits_in_one_line => {
|
|
|
|
(indent.block_indent(context.config), SeparatorTactic::Never)
|
|
|
|
}
|
2017-07-11 07:53:10 -05:00
|
|
|
IndentStyle::Block => (
|
|
|
|
indent.block_indent(context.config),
|
|
|
|
context.config.trailing_comma(),
|
|
|
|
),
|
2017-05-26 17:33:06 -05:00
|
|
|
IndentStyle::Visual if last_line_ends_with_comment => {
|
2017-07-13 06:32:46 -05:00
|
|
|
(arg_indent, context.config.trailing_comma())
|
2017-05-26 17:33:06 -05:00
|
|
|
}
|
2017-07-13 06:32:46 -05:00
|
|
|
IndentStyle::Visual => (arg_indent, SeparatorTactic::Never),
|
2015-11-22 06:45:51 -06:00
|
|
|
};
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
let tactic = definitive_tactic(
|
|
|
|
&arg_items,
|
|
|
|
context.config.fn_args_density().to_list_tactic(),
|
2017-07-31 02:23:42 -05:00
|
|
|
Separator::Comma,
|
2017-06-11 22:58:58 -05:00
|
|
|
one_line_budget,
|
|
|
|
);
|
2015-11-22 06:45:51 -06:00
|
|
|
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 {
|
2018-01-21 22:05:18 -06:00
|
|
|
tactic,
|
2015-11-22 06:45:51 -06:00
|
|
|
separator: ",",
|
2017-06-03 01:01:04 -05:00
|
|
|
trailing_separator: if variadic {
|
|
|
|
SeparatorTactic::Never
|
|
|
|
} else {
|
|
|
|
trailing_comma
|
|
|
|
},
|
2017-08-18 09:19:47 -05:00
|
|
|
separator_place: SeparatorPlace::Back,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape::legacy(budget, indent),
|
2017-11-13 01:42:29 -06:00
|
|
|
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
|
2017-07-26 08:43:36 -05:00
|
|
|
preserve_newline: true,
|
2018-06-19 10:28:58 -05:00
|
|
|
nested: false,
|
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 {
|
2018-04-08 09:18:18 -05:00
|
|
|
ident != symbol::keywords::Invalid.ident()
|
2016-03-27 06:07:28 -05:00
|
|
|
} else {
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
fn compute_budgets_for_args(
|
|
|
|
context: &RewriteContext,
|
|
|
|
result: &str,
|
|
|
|
indent: Indent,
|
|
|
|
ret_str_len: usize,
|
|
|
|
newline_brace: bool,
|
|
|
|
has_braces: bool,
|
2017-08-01 08:25:26 -05:00
|
|
|
force_vertical_layout: bool,
|
2017-06-11 22:58:58 -05:00
|
|
|
) -> Option<((usize, usize, Indent))> {
|
|
|
|
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.
|
2017-08-01 08:25:26 -05:00
|
|
|
if !result.contains('\n') && !force_vertical_layout {
|
2017-05-25 08:55:11 -05:00
|
|
|
// 2 = `()`, 3 = `() `, space is before ret_string.
|
|
|
|
let overhead = if ret_str_len == 0 { 2 } else { 3 };
|
|
|
|
let mut used_space = indent.width() + result.len() + ret_str_len + overhead;
|
|
|
|
if has_braces {
|
|
|
|
if !newline_brace {
|
|
|
|
// 2 = `{}`
|
|
|
|
used_space += 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// 1 = `;`
|
|
|
|
used_space += 1;
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
2017-08-29 01:19:56 -05:00
|
|
|
let one_line_budget = context.budget(used_space);
|
2015-12-01 15:09:37 -06:00
|
|
|
|
|
|
|
if one_line_budget > 0 {
|
2016-05-26 18:49:26 -05:00
|
|
|
// 4 = "() {".len()
|
2017-11-13 01:42:29 -06:00
|
|
|
let (indent, multi_line_budget) = match context.config.indent_style() {
|
2017-08-01 08:25:26 -05:00
|
|
|
IndentStyle::Block => {
|
|
|
|
let indent = indent.block_indent(context.config);
|
2017-08-29 01:19:56 -05:00
|
|
|
(indent, context.budget(indent.width() + 1))
|
2017-08-01 08:25:26 -05:00
|
|
|
}
|
|
|
|
IndentStyle::Visual => {
|
|
|
|
let indent = indent + result.len() + 1;
|
2017-08-27 00:57:21 -05:00
|
|
|
let multi_line_overhead = indent.width() + if newline_brace { 2 } else { 4 };
|
2017-08-29 01:19:56 -05:00
|
|
|
(indent, context.budget(multi_line_overhead))
|
2017-08-01 08:25:26 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return Some((one_line_budget, multi_line_budget, indent));
|
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);
|
2017-11-13 01:42:29 -06:00
|
|
|
let used_space = match context.config.indent_style() {
|
2017-08-01 08:25:26 -05:00
|
|
|
// 1 = `,`
|
|
|
|
IndentStyle::Block => new_indent.width() + 1,
|
|
|
|
// Account for `)` and possibly ` {`.
|
|
|
|
IndentStyle::Visual => new_indent.width() + if ret_str_len == 0 { 1 } else { 3 },
|
|
|
|
};
|
2017-08-29 01:19:56 -05:00
|
|
|
Some((0, context.budget(used_space), new_indent))
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
|
2017-11-24 02:08:24 -06:00
|
|
|
fn newline_for_brace(config: &Config, where_clause: &ast::WhereClause) -> bool {
|
2017-11-05 16:53:17 -06:00
|
|
|
let predicate_count = where_clause.predicates.len();
|
|
|
|
|
|
|
|
if config.where_single_line() && predicate_count == 1 {
|
|
|
|
return false;
|
|
|
|
}
|
2017-11-24 02:08:24 -06:00
|
|
|
let brace_style = config.brace_style();
|
|
|
|
|
|
|
|
brace_style == BraceStyle::AlwaysNextLine
|
|
|
|
|| (brace_style == BraceStyle::SameLineWhere && predicate_count > 0)
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
|
2017-06-12 21:49:47 -05:00
|
|
|
fn rewrite_generics(
|
|
|
|
context: &RewriteContext,
|
2018-03-07 00:48:47 -06:00
|
|
|
ident: &str,
|
2017-06-12 21:49:47 -05:00
|
|
|
generics: &ast::Generics,
|
|
|
|
shape: Shape,
|
|
|
|
) -> Option<String> {
|
2015-11-22 06:45:51 -06:00
|
|
|
// FIXME: convert bounds to where clauses where they get too big or if
|
|
|
|
// there is a where clause at all.
|
|
|
|
|
2017-12-21 19:52:22 -06:00
|
|
|
if generics.params.is_empty() {
|
2018-03-07 00:48:47 -06:00
|
|
|
return Some(ident.to_owned());
|
2017-09-04 11:04:08 -05:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2018-04-01 04:12:50 -05:00
|
|
|
let params = &generics.params.iter().map(|e| &*e).collect::<Vec<_>>();
|
2018-07-24 10:34:46 -05:00
|
|
|
overflow::rewrite_with_angle_brackets(context, ident, params, shape, generics.span)
|
2017-06-10 04:24:23 -05:00
|
|
|
}
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-06-17 12:00:58 -05:00
|
|
|
pub fn generics_shape_from_config(config: &Config, shape: Shape, offset: usize) -> Option<Shape> {
|
2017-11-13 01:42:29 -06:00
|
|
|
match config.indent_style() {
|
2017-06-17 12:00:58 -05:00
|
|
|
IndentStyle::Visual => shape.visual_indent(1 + offset).sub_width(offset + 2),
|
|
|
|
IndentStyle::Block => {
|
|
|
|
// 1 = ","
|
|
|
|
shape
|
|
|
|
.block()
|
|
|
|
.block_indent(config.tab_spaces())
|
|
|
|
.with_max_width(config)
|
|
|
|
.sub_width(1)
|
|
|
|
}
|
2017-06-10 04:24:23 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
fn rewrite_where_clause_rfc_style(
|
|
|
|
context: &RewriteContext,
|
|
|
|
where_clause: &ast::WhereClause,
|
|
|
|
shape: Shape,
|
|
|
|
terminator: &str,
|
|
|
|
span_end: Option<BytePos>,
|
2017-07-10 00:35:50 -05:00
|
|
|
span_end_before_where: BytePos,
|
2017-08-02 09:26:35 -05:00
|
|
|
where_clause_option: WhereClauseOption,
|
2017-11-05 16:53:17 -06:00
|
|
|
is_args_multi_line: bool,
|
2017-06-11 22:58:58 -05:00
|
|
|
) -> Option<String> {
|
2017-06-17 12:00:58 -05:00
|
|
|
let block_shape = shape.block().with_max_width(context.config);
|
2017-03-08 19:21:20 -06:00
|
|
|
|
2017-07-10 00:35:50 -05:00
|
|
|
let (span_before, span_after) =
|
2017-08-01 04:36:36 -05:00
|
|
|
missing_span_before_after_where(span_end_before_where, where_clause);
|
2017-10-05 06:50:19 -05:00
|
|
|
let (comment_before, comment_after) =
|
|
|
|
rewrite_comments_before_after_where(context, span_before, span_after, shape)?;
|
2017-07-10 00:35:50 -05:00
|
|
|
|
2017-08-02 09:26:35 -05:00
|
|
|
let starting_newline = if where_clause_option.snuggle && comment_before.is_empty() {
|
2018-02-18 21:47:54 -06:00
|
|
|
Cow::from(" ")
|
2017-03-08 19:21:20 -06:00
|
|
|
} else {
|
2018-02-18 21:47:54 -06:00
|
|
|
block_shape.indent.to_string_with_newline(context.config)
|
2017-03-08 19:21:20 -06:00
|
|
|
};
|
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
let clause_shape = block_shape.block_left(context.config.tab_spaces())?;
|
2017-10-01 10:00:48 -05:00
|
|
|
// 1 = `,`
|
2017-10-05 06:50:19 -05:00
|
|
|
let clause_shape = clause_shape.sub_width(1)?;
|
2017-03-08 19:21:20 -06:00
|
|
|
// each clause on one line, trailing comma (except if suppress_comma)
|
2017-08-19 13:47:40 -05:00
|
|
|
let span_start = where_clause.predicates[0].span().lo();
|
2017-03-08 19:21:20 -06:00
|
|
|
// 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();
|
2017-08-19 13:47:40 -05:00
|
|
|
let end_of_preds = where_clause.predicates[len - 1].span().hi();
|
2017-03-08 19:21:20 -06:00
|
|
|
let span_end = span_end.unwrap_or(end_of_preds);
|
2017-06-12 21:49:47 -05:00
|
|
|
let items = itemize_list(
|
2018-02-18 21:41:43 -06:00
|
|
|
context.snippet_provider,
|
2017-06-12 21:49:47 -05:00
|
|
|
where_clause.predicates.iter(),
|
|
|
|
terminator,
|
2017-11-16 02:38:12 -06:00
|
|
|
",",
|
2017-08-19 13:47:40 -05:00
|
|
|
|pred| pred.span().lo(),
|
|
|
|
|pred| pred.span().hi(),
|
2017-10-01 10:00:48 -05:00
|
|
|
|pred| pred.rewrite(context, clause_shape),
|
2017-06-12 21:49:47 -05:00
|
|
|
span_start,
|
|
|
|
span_end,
|
2017-08-07 03:29:55 -05:00
|
|
|
false,
|
2017-06-12 21:49:47 -05:00
|
|
|
);
|
2017-11-05 16:53:17 -06:00
|
|
|
let where_single_line = context.config.where_single_line() && len == 1 && !is_args_multi_line;
|
|
|
|
let comma_tactic = if where_clause_option.suppress_comma || where_single_line {
|
2017-03-08 19:21:20 -06:00
|
|
|
SeparatorTactic::Never
|
|
|
|
} else {
|
2017-06-19 02:00:04 -05:00
|
|
|
context.config.trailing_comma()
|
2017-03-08 19:21:20 -06:00
|
|
|
};
|
|
|
|
|
2017-11-05 16:53:17 -06:00
|
|
|
// shape should be vertical only and only if we have `where_single_line` option enabled
|
|
|
|
// and the number of items of the where clause is equal to 1
|
|
|
|
let shape_tactic = if where_single_line {
|
|
|
|
DefinitiveListTactic::Horizontal
|
|
|
|
} else {
|
|
|
|
DefinitiveListTactic::Vertical
|
|
|
|
};
|
|
|
|
|
2017-03-08 19:21:20 -06:00
|
|
|
let fmt = ListFormatting {
|
2017-11-05 16:53:17 -06:00
|
|
|
tactic: shape_tactic,
|
2017-03-08 19:21:20 -06:00
|
|
|
separator: ",",
|
|
|
|
trailing_separator: comma_tactic,
|
2017-08-18 09:19:47 -05:00
|
|
|
separator_place: SeparatorPlace::Back,
|
2017-03-08 19:21:20 -06:00
|
|
|
shape: clause_shape,
|
|
|
|
ends_with_newline: true,
|
2017-07-26 08:43:36 -05:00
|
|
|
preserve_newline: true,
|
2018-06-19 10:28:58 -05:00
|
|
|
nested: false,
|
2017-03-08 19:21:20 -06:00
|
|
|
config: context.config,
|
|
|
|
};
|
2017-10-05 06:50:19 -05:00
|
|
|
let preds_str = write_list(&items.collect::<Vec<_>>(), &fmt)?;
|
2017-03-08 19:21:20 -06:00
|
|
|
|
2017-12-10 06:54:26 -06:00
|
|
|
let comment_separator = |comment: &str, shape: Shape| {
|
|
|
|
if comment.is_empty() {
|
2018-02-18 21:47:54 -06:00
|
|
|
Cow::from("")
|
2017-12-10 06:54:26 -06:00
|
|
|
} else {
|
2018-02-18 21:47:54 -06:00
|
|
|
shape.indent.to_string_with_newline(context.config)
|
2017-12-10 06:54:26 -06:00
|
|
|
}
|
2017-07-10 00:35:50 -05:00
|
|
|
};
|
2017-08-02 09:27:19 -05:00
|
|
|
let newline_before_where = comment_separator(&comment_before, shape);
|
|
|
|
let newline_after_where = comment_separator(&comment_after, clause_shape);
|
|
|
|
|
2017-08-02 09:26:35 -05:00
|
|
|
// 6 = `where `
|
2018-05-06 01:22:29 -05:00
|
|
|
let clause_sep = if where_clause_option.compress_where
|
|
|
|
&& comment_before.is_empty()
|
|
|
|
&& comment_after.is_empty()
|
|
|
|
&& !preds_str.contains('\n')
|
2018-07-05 18:58:22 -05:00
|
|
|
&& 6 + preds_str.len() <= shape.width
|
|
|
|
|| where_single_line
|
2017-08-01 08:26:22 -05:00
|
|
|
{
|
2018-02-18 21:47:54 -06:00
|
|
|
Cow::from(" ")
|
2017-08-01 08:26:22 -05:00
|
|
|
} else {
|
2018-02-18 21:47:54 -06:00
|
|
|
clause_shape.indent.to_string_with_newline(context.config)
|
2017-08-01 08:26:22 -05:00
|
|
|
};
|
2017-06-11 22:58:58 -05:00
|
|
|
Some(format!(
|
2017-08-01 08:26:22 -05:00
|
|
|
"{}{}{}where{}{}{}{}",
|
2017-06-11 22:58:58 -05:00
|
|
|
starting_newline,
|
2017-07-10 00:35:50 -05:00
|
|
|
comment_before,
|
|
|
|
newline_before_where,
|
|
|
|
newline_after_where,
|
|
|
|
comment_after,
|
2017-08-01 08:26:22 -05:00
|
|
|
clause_sep,
|
2017-06-11 22:58:58 -05:00
|
|
|
preds_str
|
|
|
|
))
|
2017-03-08 19:21:20 -06:00
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
fn rewrite_where_clause(
|
|
|
|
context: &RewriteContext,
|
|
|
|
where_clause: &ast::WhereClause,
|
|
|
|
brace_style: BraceStyle,
|
|
|
|
shape: Shape,
|
|
|
|
density: Density,
|
|
|
|
terminator: &str,
|
|
|
|
span_end: Option<BytePos>,
|
2017-07-10 00:35:50 -05:00
|
|
|
span_end_before_where: BytePos,
|
2017-08-02 09:26:35 -05:00
|
|
|
where_clause_option: WhereClauseOption,
|
2017-11-05 16:53:17 -06:00
|
|
|
is_args_multi_line: bool,
|
2017-06-11 22:58:58 -05:00
|
|
|
) -> Option<String> {
|
2015-11-22 06:45:51 -06:00
|
|
|
if where_clause.predicates.is_empty() {
|
|
|
|
return Some(String::new());
|
|
|
|
}
|
|
|
|
|
2017-11-13 01:42:29 -06:00
|
|
|
if context.config.indent_style() == IndentStyle::Block {
|
2017-06-11 22:58:58 -05:00
|
|
|
return rewrite_where_clause_rfc_style(
|
|
|
|
context,
|
|
|
|
where_clause,
|
|
|
|
shape,
|
|
|
|
terminator,
|
|
|
|
span_end,
|
2017-07-10 00:35:50 -05:00
|
|
|
span_end_before_where,
|
2017-08-02 09:26:35 -05:00
|
|
|
where_clause_option,
|
2017-11-05 16:53:17 -06:00
|
|
|
is_args_multi_line,
|
2017-06-11 22:58:58 -05:00
|
|
|
);
|
2017-03-08 19:21:20 -06:00
|
|
|
}
|
|
|
|
|
2017-05-16 03:47:09 -05:00
|
|
|
let extra_indent = Indent::new(context.config.tab_spaces(), 0);
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-11-13 01:42:29 -06:00
|
|
|
let offset = match context.config.indent_style() {
|
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
|
|
|
};
|
2017-11-13 01:42:29 -06:00
|
|
|
// FIXME: if indent_style != Visual, then the budgets below might
|
2015-11-22 06:45:51 -06:00
|
|
|
// be out by a char or two.
|
|
|
|
|
2017-05-16 03:47:09 -05:00
|
|
|
let budget = context.config.max_width() - offset.width();
|
2017-08-19 13:47:40 -05:00
|
|
|
let span_start = where_clause.predicates[0].span().lo();
|
2015-11-22 06:45:51 -06:00
|
|
|
// 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();
|
2017-08-19 13:47:40 -05:00
|
|
|
let end_of_preds = where_clause.predicates[len - 1].span().hi();
|
2015-11-22 06:45:51 -06:00
|
|
|
let span_end = span_end.unwrap_or(end_of_preds);
|
2017-06-11 22:58:58 -05:00
|
|
|
let items = itemize_list(
|
2018-02-18 21:41:43 -06:00
|
|
|
context.snippet_provider,
|
2017-06-11 22:58:58 -05:00
|
|
|
where_clause.predicates.iter(),
|
|
|
|
terminator,
|
2017-11-16 02:38:12 -06:00
|
|
|
",",
|
2017-08-19 13:47:40 -05:00
|
|
|
|pred| pred.span().lo(),
|
|
|
|
|pred| pred.span().hi(),
|
2017-06-11 22:58:58 -05:00
|
|
|
|pred| pred.rewrite(context, Shape::legacy(budget, offset)),
|
|
|
|
span_start,
|
|
|
|
span_end,
|
2017-08-07 03:29:55 -05:00
|
|
|
false,
|
2017-06-11 22:58:58 -05:00
|
|
|
);
|
2015-11-22 06:45:51 -06:00
|
|
|
let item_vec = items.collect::<Vec<_>>();
|
2017-11-24 02:08:24 -06:00
|
|
|
// FIXME: we don't need to collect here
|
|
|
|
let tactic = definitive_tactic(&item_vec, ListTactic::Vertical, Separator::Comma, budget);
|
2017-02-23 15:31:23 -06:00
|
|
|
|
2017-05-16 03:47:09 -05:00
|
|
|
let mut comma_tactic = context.config.trailing_comma();
|
2017-02-23 15:31:23 -06:00
|
|
|
// Kind of a hack because we don't usually have trailing commas in where clauses.
|
2017-08-02 09:26:35 -05:00
|
|
|
if comma_tactic == SeparatorTactic::Vertical || where_clause_option.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 {
|
2018-01-21 22:05:18 -06:00
|
|
|
tactic,
|
2015-11-22 06:45:51 -06:00
|
|
|
separator: ",",
|
2017-02-23 15:31:23 -06:00
|
|
|
trailing_separator: comma_tactic,
|
2017-08-18 09:19:47 -05:00
|
|
|
separator_place: SeparatorPlace::Back,
|
2017-01-30 13:28:48 -06:00
|
|
|
shape: Shape::legacy(budget, offset),
|
2017-11-13 01:42:29 -06:00
|
|
|
ends_with_newline: tactic.ends_with_newline(context.config.indent_style()),
|
2017-07-26 08:43:36 -05:00
|
|
|
preserve_newline: true,
|
2018-06-19 10:28:58 -05:00
|
|
|
nested: false,
|
2015-11-22 06:45:51 -06:00
|
|
|
config: context.config,
|
|
|
|
};
|
2017-10-05 06:50:19 -05:00
|
|
|
let preds_str = write_list(&item_vec, &fmt)?;
|
2015-11-22 06:45:51 -06:00
|
|
|
|
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 {
|
2017-07-09 12:24:59 -05:00
|
|
|
BraceStyle::AlwaysNextLine | 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
|
|
|
};
|
2018-05-06 01:22:29 -05:00
|
|
|
if density == Density::Tall
|
|
|
|
|| preds_str.contains('\n')
|
2017-09-14 22:10:58 -05:00
|
|
|
|| shape.indent.width() + " where ".len() + preds_str.len() + end_length > shape.width
|
2017-06-11 22:58:58 -05:00
|
|
|
{
|
|
|
|
Some(format!(
|
|
|
|
"\n{}where {}",
|
|
|
|
(shape.indent + extra_indent).to_string(context.config),
|
|
|
|
preds_str
|
|
|
|
))
|
2015-11-22 06:45:51 -06:00
|
|
|
} else {
|
|
|
|
Some(format!(" where {}", preds_str))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-10 00:35:50 -05:00
|
|
|
fn missing_span_before_after_where(
|
|
|
|
before_item_span_end: BytePos,
|
|
|
|
where_clause: &ast::WhereClause,
|
|
|
|
) -> (Span, Span) {
|
2017-08-19 13:47:40 -05:00
|
|
|
let missing_span_before = mk_sp(before_item_span_end, where_clause.span.lo());
|
2017-07-10 00:35:50 -05:00
|
|
|
// 5 = `where`
|
2017-08-19 13:47:40 -05:00
|
|
|
let pos_after_where = where_clause.span.lo() + BytePos(5);
|
|
|
|
let missing_span_after = mk_sp(pos_after_where, where_clause.predicates[0].span().lo());
|
2017-07-10 00:35:50 -05:00
|
|
|
(missing_span_before, missing_span_after)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rewrite_comments_before_after_where(
|
|
|
|
context: &RewriteContext,
|
|
|
|
span_before_where: Span,
|
|
|
|
span_after_where: Span,
|
|
|
|
shape: Shape,
|
|
|
|
) -> Option<(String, String)> {
|
2017-10-05 06:50:19 -05:00
|
|
|
let before_comment = rewrite_missing_comment(span_before_where, shape, context)?;
|
|
|
|
let after_comment = rewrite_missing_comment(
|
2017-08-27 10:13:42 -05:00
|
|
|
span_after_where,
|
2017-07-10 00:35:50 -05:00
|
|
|
shape.block_indent(context.config.tab_spaces()),
|
2017-08-27 10:13:42 -05:00
|
|
|
context,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2017-07-10 00:35:50 -05:00
|
|
|
Some((before_comment, after_comment))
|
|
|
|
}
|
|
|
|
|
2018-06-25 09:36:45 -05:00
|
|
|
fn format_header(
|
|
|
|
context: &RewriteContext,
|
|
|
|
item_name: &str,
|
|
|
|
ident: ast::Ident,
|
|
|
|
vis: &ast::Visibility,
|
|
|
|
) -> String {
|
|
|
|
format!(
|
|
|
|
"{}{}{}",
|
|
|
|
format_visibility(context, vis),
|
|
|
|
item_name,
|
|
|
|
rewrite_ident(context, ident)
|
|
|
|
)
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
|
|
|
|
2017-12-11 22:48:12 -06:00
|
|
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
2017-11-12 20:06:53 -06:00
|
|
|
enum BracePos {
|
|
|
|
None,
|
|
|
|
Auto,
|
|
|
|
ForceSameLine,
|
|
|
|
}
|
|
|
|
|
2017-06-11 22:58:58 -05:00
|
|
|
fn format_generics(
|
|
|
|
context: &RewriteContext,
|
|
|
|
generics: &ast::Generics,
|
|
|
|
brace_style: BraceStyle,
|
2017-11-12 20:06:53 -06:00
|
|
|
brace_pos: BracePos,
|
2017-06-11 22:58:58 -05:00
|
|
|
offset: Indent,
|
|
|
|
span: Span,
|
2017-06-24 05:46:36 -05:00
|
|
|
used_width: usize,
|
2017-06-11 22:58:58 -05:00
|
|
|
) -> Option<String> {
|
2017-06-24 05:46:36 -05:00
|
|
|
let shape = Shape::legacy(context.budget(used_width + offset.width()), offset);
|
2018-07-24 10:34:46 -05:00
|
|
|
let mut result = rewrite_generics(context, "", generics, shape)?;
|
2015-11-22 06:45:51 -06:00
|
|
|
|
2017-07-26 02:30:58 -05:00
|
|
|
let same_line_brace = if !generics.where_clause.predicates.is_empty() || result.contains('\n') {
|
2017-08-29 01:19:56 -05:00
|
|
|
let budget = context.budget(last_line_used_width(&result, offset.width()));
|
2017-11-12 20:06:53 -06:00
|
|
|
let mut option = WhereClauseOption::snuggled(&result);
|
|
|
|
if brace_pos == BracePos::None {
|
|
|
|
option.suppress_comma = true;
|
|
|
|
}
|
2017-11-04 14:24:41 -05:00
|
|
|
// If the generics are not parameterized then generics.span.hi() == 0,
|
|
|
|
// so we use span.lo(), which is the position after `struct Foo`.
|
2018-06-25 01:24:00 -05:00
|
|
|
let span_end_before_where = if !generics.params.is_empty() {
|
2017-11-04 14:24:41 -05:00
|
|
|
generics.span.hi()
|
|
|
|
} else {
|
|
|
|
span.lo()
|
|
|
|
};
|
2017-10-05 06:50:19 -05:00
|
|
|
let where_clause_str = rewrite_where_clause(
|
2017-06-11 22:58:58 -05:00
|
|
|
context,
|
|
|
|
&generics.where_clause,
|
|
|
|
brace_style,
|
|
|
|
Shape::legacy(budget, offset.block_only()),
|
|
|
|
Density::Tall,
|
2017-11-05 00:29:40 -05:00
|
|
|
"{",
|
2017-08-19 13:47:40 -05:00
|
|
|
Some(span.hi()),
|
2017-11-04 14:24:41 -05:00
|
|
|
span_end_before_where,
|
2017-08-02 09:26:35 -05:00
|
|
|
option,
|
2017-11-05 16:53:17 -06:00
|
|
|
false,
|
2017-10-05 06:50:19 -05:00
|
|
|
)?;
|
2015-11-22 06:45:51 -06:00
|
|
|
result.push_str(&where_clause_str);
|
2018-07-05 18:58:22 -05:00
|
|
|
brace_pos == BracePos::ForceSameLine
|
|
|
|
|| brace_style == BraceStyle::PreferSameLine
|
2017-09-14 22:10:58 -05:00
|
|
|
|| (generics.where_clause.predicates.is_empty()
|
|
|
|
&& trimmed_last_line_width(&result) == 1)
|
2015-11-22 06:45:51 -06:00
|
|
|
} else {
|
2018-05-06 01:22:29 -05:00
|
|
|
brace_pos == BracePos::ForceSameLine
|
|
|
|
|| trimmed_last_line_width(&result) == 1
|
2017-09-14 22:10:58 -05:00
|
|
|
|| brace_style != BraceStyle::AlwaysNextLine
|
2017-07-25 04:51:14 -05:00
|
|
|
};
|
2017-11-12 20:06:53 -06:00
|
|
|
if brace_pos == BracePos::None {
|
|
|
|
return Some(result);
|
|
|
|
}
|
2017-08-01 08:23:12 -05:00
|
|
|
let total_used_width = last_line_used_width(&result, used_width);
|
2017-08-29 01:19:56 -05:00
|
|
|
let remaining_budget = context.budget(total_used_width);
|
2017-07-25 04:51:14 -05:00
|
|
|
// If the same line brace if forced, it indicates that we are rewriting an item with empty body,
|
|
|
|
// and hence we take the closer into account as well for one line budget.
|
|
|
|
// We assume that the closer has the same length as the opener.
|
2017-11-12 20:06:53 -06:00
|
|
|
let overhead = if brace_pos == BracePos::ForceSameLine {
|
2017-11-05 00:29:40 -05:00
|
|
|
// 3 = ` {}`
|
|
|
|
3
|
2017-07-25 04:51:14 -05:00
|
|
|
} else {
|
2017-11-05 00:29:40 -05:00
|
|
|
// 2 = ` {`
|
|
|
|
2
|
2017-07-25 04:51:14 -05:00
|
|
|
};
|
|
|
|
let forbid_same_line_brace = overhead > remaining_budget;
|
|
|
|
if !forbid_same_line_brace && same_line_brace {
|
|
|
|
result.push(' ');
|
|
|
|
} else {
|
|
|
|
result.push('\n');
|
|
|
|
result.push_str(&offset.block_only().to_string(context.config));
|
2015-11-22 06:45:51 -06:00
|
|
|
}
|
2017-11-05 00:29:40 -05:00
|
|
|
result.push('{');
|
2015-11-22 06:45:51 -06:00
|
|
|
|
|
|
|
Some(result)
|
|
|
|
}
|
2017-08-24 18:19:51 -05:00
|
|
|
|
|
|
|
impl Rewrite for ast::ForeignItem {
|
|
|
|
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
|
2017-10-05 06:50:19 -05:00
|
|
|
let attrs_str = self.attrs.rewrite(context, shape)?;
|
2017-08-24 18:19:51 -05:00
|
|
|
// Drop semicolon or it will be interpreted as comment.
|
|
|
|
// FIXME: this may be a faulty span from libsyntax.
|
2017-08-19 13:47:40 -05:00
|
|
|
let span = mk_sp(self.span.lo(), self.span.hi() - BytePos(1));
|
2017-08-24 18:19:51 -05:00
|
|
|
|
2017-10-05 06:50:19 -05:00
|
|
|
let item_str = match self.node {
|
2018-07-11 04:35:10 -05:00
|
|
|
ast::ForeignItemKind::Fn(ref fn_decl, ref generics) => rewrite_fn_base(
|
|
|
|
context,
|
|
|
|
shape.indent,
|
|
|
|
self.ident,
|
|
|
|
&FnSig::new(fn_decl, generics, self.vis.clone()),
|
|
|
|
span,
|
|
|
|
false,
|
|
|
|
false,
|
|
|
|
).map(|(s, _)| format!("{};", s)),
|
2017-08-24 18:19:51 -05:00
|
|
|
ast::ForeignItemKind::Static(ref ty, is_mutable) => {
|
|
|
|
// FIXME(#21): we're dropping potential comments in between the
|
|
|
|
// function keywords here.
|
2018-06-25 09:36:45 -05:00
|
|
|
let vis = format_visibility(context, &self.vis);
|
2017-08-24 18:19:51 -05:00
|
|
|
let mut_str = if is_mutable { "mut " } else { "" };
|
2018-06-25 09:36:45 -05:00
|
|
|
let prefix = format!(
|
|
|
|
"{}static {}{}:",
|
|
|
|
vis,
|
|
|
|
mut_str,
|
|
|
|
rewrite_ident(context, self.ident)
|
|
|
|
);
|
2017-08-24 18:19:51 -05:00
|
|
|
// 1 = ;
|
2018-04-24 17:21:23 -05:00
|
|
|
rewrite_assign_rhs(context, prefix, &**ty, shape.sub_width(1)?).map(|s| s + ";")
|
2017-08-24 18:19:51 -05:00
|
|
|
}
|
2017-09-04 20:22:17 -05:00
|
|
|
ast::ForeignItemKind::Ty => {
|
2018-06-25 09:36:45 -05:00
|
|
|
let vis = format_visibility(context, &self.vis);
|
|
|
|
Some(format!(
|
|
|
|
"{}type {};",
|
|
|
|
vis,
|
|
|
|
rewrite_ident(context, self.ident)
|
|
|
|
))
|
2017-09-04 20:22:17 -05:00
|
|
|
}
|
2018-04-06 09:09:45 -05:00
|
|
|
ast::ForeignItemKind::Macro(ref mac) => {
|
|
|
|
rewrite_macro(mac, None, context, shape, MacroPosition::Item)
|
|
|
|
}
|
2017-10-05 06:50:19 -05:00
|
|
|
}?;
|
2017-08-24 18:19:51 -05:00
|
|
|
|
|
|
|
let missing_span = if self.attrs.is_empty() {
|
2017-08-19 13:47:40 -05:00
|
|
|
mk_sp(self.span.lo(), self.span.lo())
|
2017-08-24 18:19:51 -05:00
|
|
|
} else {
|
2017-08-19 13:47:40 -05:00
|
|
|
mk_sp(self.attrs[self.attrs.len() - 1].span.hi(), self.span.lo())
|
2017-08-24 18:19:51 -05:00
|
|
|
};
|
|
|
|
combine_strs_with_missing_comments(
|
|
|
|
context,
|
|
|
|
&attrs_str,
|
|
|
|
&item_str,
|
|
|
|
missing_span,
|
|
|
|
shape,
|
|
|
|
false,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
2017-12-23 09:29:59 -06:00
|
|
|
|
2018-02-17 10:11:43 -06:00
|
|
|
/// Rewrite an inline mod.
|
2018-06-25 09:36:45 -05:00
|
|
|
pub fn rewrite_mod(context: &RewriteContext, item: &ast::Item) -> String {
|
2018-02-17 10:11:43 -06:00
|
|
|
let mut result = String::with_capacity(32);
|
2018-06-25 09:36:45 -05:00
|
|
|
result.push_str(&*format_visibility(context, &item.vis));
|
2018-02-17 10:11:43 -06:00
|
|
|
result.push_str("mod ");
|
2018-06-25 09:36:45 -05:00
|
|
|
result.push_str(rewrite_ident(context, item.ident));
|
2018-02-17 10:11:43 -06:00
|
|
|
result.push(';');
|
|
|
|
result
|
|
|
|
}
|
2018-02-23 07:37:10 -06:00
|
|
|
|
|
|
|
/// Rewrite `extern crate foo;` WITHOUT attributes.
|
|
|
|
pub fn rewrite_extern_crate(context: &RewriteContext, item: &ast::Item) -> Option<String> {
|
|
|
|
assert!(is_extern_crate(item));
|
|
|
|
let new_str = context.snippet(item.span);
|
|
|
|
Some(if contains_comment(new_str) {
|
|
|
|
new_str.to_owned()
|
|
|
|
} else {
|
|
|
|
let no_whitespace = &new_str.split_whitespace().collect::<Vec<&str>>().join(" ");
|
|
|
|
String::from(&*Regex::new(r"\s;").unwrap().replace(no_whitespace, ";"))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Returns true for `mod foo;`, false for `mod foo { .. }`.
|
|
|
|
pub fn is_mod_decl(item: &ast::Item) -> bool {
|
|
|
|
match item.node {
|
|
|
|
ast::ItemKind::Mod(ref m) => m.inner.hi() != item.span.hi(),
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_use_item(item: &ast::Item) -> bool {
|
|
|
|
match item.node {
|
|
|
|
ast::ItemKind::Use(_) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_extern_crate(item: &ast::Item) -> bool {
|
|
|
|
match item.node {
|
|
|
|
ast::ItemKind::ExternCrate(..) => true,
|
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|