1
Fork 0

Add Indent::none(), remove make_indent.

This commit is contained in:
Pavel Sountsov 2015-09-18 21:50:44 -07:00 committed by SiegeLord
parent d4108a3029
commit 03e1b27826
11 changed files with 82 additions and 92 deletions

View file

@ -21,7 +21,7 @@
use Indent; use Indent;
use rewrite::{Rewrite, RewriteContext}; use rewrite::{Rewrite, RewriteContext};
use utils::{first_line_width, make_indent}; use utils::first_line_width;
use expr::rewrite_call; use expr::rewrite_call;
use syntax::{ast, ptr}; use syntax::{ast, ptr};
@ -117,7 +117,7 @@ pub fn rewrite_chain(mut expr: &ast::Expr,
let connector = if fits_single_line { let connector = if fits_single_line {
String::new() String::new()
} else { } else {
format!("\n{}", make_indent(indent, context.config)) format!("\n{}", indent.to_string(context.config))
}; };
let first_connector = if extend { let first_connector = if extend {

View file

@ -15,7 +15,6 @@ use std::iter;
use Indent; use Indent;
use config::Config; use config::Config;
use string::{StringFormat, rewrite_string}; use string::{StringFormat, rewrite_string};
use utils::make_indent;
pub fn rewrite_comment(orig: &str, pub fn rewrite_comment(orig: &str,
block_style: bool, block_style: bool,
@ -45,7 +44,7 @@ pub fn rewrite_comment(orig: &str,
config: config, config: config,
}; };
let indent_str = make_indent(offset, config); let indent_str = offset.to_string(config);
let line_breaks = s.chars().filter(|&c| c == '\n').count(); let line_breaks = s.chars().filter(|&c| c == '\n').count();
let (_, mut s) = s.lines() let (_, mut s) = s.lines()
@ -304,7 +303,7 @@ mod test {
assert_eq!("/* test */", rewrite_comment(" //test", true, 100, Indent::new(0, 100), assert_eq!("/* test */", rewrite_comment(" //test", true, 100, Indent::new(0, 100),
&config)); &config));
assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10, assert_eq!("// comment\n// on a", rewrite_comment("// comment on a", false, 10,
Indent::new(0, 0), &config)); Indent::empty(), &config));
assert_eq!("// A multi line comment\n // between args.", assert_eq!("// A multi line comment\n // between args.",
rewrite_comment("// A multi line comment\n // between args.", rewrite_comment("// A multi line comment\n // between args.",

View file

@ -15,8 +15,7 @@ use Indent;
use rewrite::{Rewrite, RewriteContext}; use rewrite::{Rewrite, RewriteContext};
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic}; use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic};
use string::{StringFormat, rewrite_string}; use string::{StringFormat, rewrite_string};
use utils::{span_after, make_indent, extra_offset, first_line_width, last_line_width, wrap_str, use utils::{span_after, extra_offset, first_line_width, last_line_width, wrap_str, binary_search};
binary_search};
use visitor::FmtVisitor; use visitor::FmtVisitor;
use config::{StructLitStyle, MultilineStyle}; use config::{StructLitStyle, MultilineStyle};
use comment::{FindUncommented, rewrite_comment, contains_comment}; use comment::{FindUncommented, rewrite_comment, contains_comment};
@ -268,7 +267,7 @@ fn rewrite_closure(capture: ast::CaptureClause,
if !ret_str.is_empty() { if !ret_str.is_empty() {
if prefix.contains('\n') { if prefix.contains('\n') {
prefix.push('\n'); prefix.push('\n');
prefix.push_str(&make_indent(argument_offset, context.config)); prefix.push_str(&argument_offset.to_string(context.config));
} else { } else {
prefix.push(' '); prefix.push(' ');
} }
@ -311,12 +310,12 @@ fn rewrite_closure(capture: ast::CaptureClause,
.as_ref() .as_ref()
.and_then(|body_expr| { .and_then(|body_expr| {
if let ast::Expr_::ExprBlock(ref inner) = body_expr.node { if let ast::Expr_::ExprBlock(ref inner) = body_expr.node {
Some(inner.rewrite(&context, 2, Indent::new(0, 0))) Some(inner.rewrite(&context, 2, Indent::empty()))
} else { } else {
None None
} }
}) })
.unwrap_or_else(|| body.rewrite(&context, 2, Indent::new(0, 0))); .unwrap_or_else(|| body.rewrite(&context, 2, Indent::empty()));
Some(format!("{} {}", prefix, try_opt!(body_rewrite))) Some(format!("{} {}", prefix, try_opt!(body_rewrite)))
} }
@ -592,11 +591,11 @@ fn single_line_if_else(context: &RewriteContext,
let new_width = try_opt!(width.checked_sub(pat_expr_str.len() + fixed_cost)); let new_width = try_opt!(width.checked_sub(pat_expr_str.len() + fixed_cost));
let if_expr = if_node.expr.as_ref().unwrap(); let if_expr = if_node.expr.as_ref().unwrap();
let if_str = try_opt!(if_expr.rewrite(context, new_width, Indent::new(0, 0))); let if_str = try_opt!(if_expr.rewrite(context, new_width, Indent::empty()));
let new_width = try_opt!(new_width.checked_sub(if_str.len())); let new_width = try_opt!(new_width.checked_sub(if_str.len()));
let else_expr = else_node.expr.as_ref().unwrap(); let else_expr = else_node.expr.as_ref().unwrap();
let else_str = try_opt!(else_expr.rewrite(context, new_width, Indent::new(0, 0))); let else_str = try_opt!(else_expr.rewrite(context, new_width, Indent::empty()));
// FIXME: this check shouldn't be necessary. Rewrites should either fail // FIXME: this check shouldn't be necessary. Rewrites should either fail
// or wrap to a newline when the object does not fit the width. // or wrap to a newline when the object does not fit the width.
@ -638,7 +637,7 @@ fn rewrite_match(context: &RewriteContext,
let nested_context = context.nested_context(); let nested_context = context.nested_context();
let arm_indent = nested_context.block_indent + context.overflow_indent; let arm_indent = nested_context.block_indent + context.overflow_indent;
let arm_indent_str = make_indent(arm_indent, context.config); let arm_indent_str = arm_indent.to_string(context.config);
let open_brace_pos = span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])), let open_brace_pos = span_after(mk_sp(cond.span.hi, arm_start_pos(&arms[0])),
"{", "{",
@ -688,7 +687,7 @@ fn rewrite_match(context: &RewriteContext,
// match expression, but meh. // match expression, but meh.
result.push('\n'); result.push('\n');
result.push_str(&make_indent(context.block_indent + context.overflow_indent, context.config)); result.push_str(&(context.block_indent + context.overflow_indent).to_string(context.config));
result.push('}'); result.push('}');
Some(result) Some(result)
} }
@ -710,7 +709,7 @@ fn arm_end_pos(arm: &ast::Arm) -> BytePos {
impl Rewrite for ast::Arm { impl Rewrite for ast::Arm {
fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> { fn rewrite(&self, context: &RewriteContext, width: usize, offset: Indent) -> Option<String> {
let &ast::Arm { ref attrs, ref pats, ref guard, ref body } = self; let &ast::Arm { ref attrs, ref pats, ref guard, ref body } = self;
let indent_str = make_indent(offset, context.config); let indent_str = offset.to_string(context.config);
// FIXME this is all a bit grotty, would be nice to abstract out the // FIXME this is all a bit grotty, would be nice to abstract out the
// treatment of attributes. // treatment of attributes.
@ -738,7 +737,7 @@ impl Rewrite for ast::Arm {
.map(|p| { .map(|p| {
p.rewrite(context, p.rewrite(context,
pat_budget, pat_budget,
offset.block_indent(context.config.tab_spaces)) offset.block_indent(context.config))
}) })
.collect::<Option<Vec<_>>>()); .collect::<Option<Vec<_>>>());
@ -784,7 +783,7 @@ impl Rewrite for ast::Arm {
let mut line_indent = offset + pats_width; let mut line_indent = offset + pats_width;
if vertical { if vertical {
line_indent = line_indent.block_indent(context.config.tab_spaces); line_indent = line_indent.block_indent(context.config);
} }
let comma = if let ast::ExprBlock(_) = body.node { let comma = if let ast::ExprBlock(_) = body.node {
@ -819,7 +818,7 @@ impl Rewrite for ast::Arm {
Some(format!("{}{} =>\n{}{},", Some(format!("{}{} =>\n{}{},",
attr_str.trim_left(), attr_str.trim_left(),
pats_str, pats_str,
make_indent(offset.block_indent(context.config.tab_spaces), context.config), offset.block_indent(context.config).to_string(context.config),
body_str)) body_str))
} }
} }
@ -849,11 +848,10 @@ fn rewrite_guard(context: &RewriteContext,
if overhead < width { if overhead < width {
let cond_str = guard.rewrite(context, let cond_str = guard.rewrite(context,
width - overhead, width - overhead,
offset.block_indent(context.config.tab_spaces)); offset.block_indent(context.config));
if let Some(cond_str) = cond_str { if let Some(cond_str) = cond_str {
return Some(format!("\n{}if {}", return Some(format!("\n{}if {}",
make_indent(offset.block_indent(context.config.tab_spaces), offset.block_indent(context.config).to_string(context.config),
context.config),
cond_str)); cond_str));
} }
} }
@ -908,7 +906,7 @@ fn rewrite_pat_expr(context: &RewriteContext,
// The expression won't fit on the current line, jump to next. // The expression won't fit on the current line, jump to next.
result.push('\n'); result.push('\n');
result.push_str(&make_indent(pat_offset, context.config)); result.push_str(&pat_offset.to_string(context.config));
let expr_rewrite = expr.rewrite(context, let expr_rewrite = expr.rewrite(context,
context.config.max_width - pat_offset.width(), context.config.max_width - pat_offset.width(),
@ -1067,7 +1065,7 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
StructLitStyle::Block => { StructLitStyle::Block => {
// If we are all on one line, then we'll ignore the indent, and we // If we are all on one line, then we'll ignore the indent, and we
// have a smaller budget. // have a smaller budget.
let indent = context.block_indent.block_indent(context.config.tab_spaces); let indent = context.block_indent.block_indent(context.config);
let v_budget = context.config.max_width.checked_sub(indent.width()).unwrap_or(0); let v_budget = context.config.max_width.checked_sub(indent.width()).unwrap_or(0);
(indent, v_budget) (indent, v_budget)
} }
@ -1139,10 +1137,10 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
let fields_str = try_opt!(write_list(&items.collect::<Vec<_>>(), &fmt)); let fields_str = try_opt!(write_list(&items.collect::<Vec<_>>(), &fmt));
let format_on_newline = || { let format_on_newline = || {
let inner_indent = make_indent(context.block_indent let inner_indent = context.block_indent
.block_indent(context.config.tab_spaces), .block_indent(context.config)
context.config); .to_string(context.config);
let outer_indent = make_indent(context.block_indent, context.config); let outer_indent = context.block_indent.to_string(context.config);
Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent)) Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent))
}; };
@ -1255,7 +1253,7 @@ fn rewrite_binary_op(context: &RewriteContext,
Some(format!("{} {}\n{}{}", Some(format!("{} {}\n{}{}",
try_opt!(lhs.rewrite(context, budget, offset)), try_opt!(lhs.rewrite(context, budget, offset)),
operator_str, operator_str,
make_indent(offset, context.config), offset.to_string(context.config),
rhs_result)) rhs_result))
} }
@ -1319,8 +1317,8 @@ pub fn rewrite_assign_rhs<S: Into<String>>(context: &RewriteContext,
None => { None => {
// Expression did not fit on the same line as the identifier. Retry // Expression did not fit on the same line as the identifier. Retry
// on the next line. // on the next line.
let new_offset = offset.block_indent(context.config.tab_spaces); let new_offset = offset.block_indent(context.config);
result.push_str(&format!("\n{}", make_indent(new_offset, context.config))); result.push_str(&format!("\n{}", new_offset.to_string(context.config)));
// FIXME: we probably should related max_width to width instead of config.max_width // FIXME: we probably should related max_width to width instead of config.max_width
// where is the 1 coming from anyway? // where is the 1 coming from anyway?

View file

@ -11,8 +11,7 @@
// Formatting top-level items - functions, structs, enums, traits, impls. // Formatting top-level items - functions, structs, enums, traits, impls.
use Indent; use Indent;
use utils::{format_mutability, format_visibility, make_indent, contains_skip, span_after, use utils::{format_mutability, format_visibility, contains_skip, span_after, end_typaram, wrap_str};
end_typaram, wrap_str};
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic}; use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
use expr::rewrite_assign_rhs; use expr::rewrite_assign_rhs;
use comment::FindUncommented; use comment::FindUncommented;
@ -35,7 +34,7 @@ impl<'a> FmtVisitor<'a> {
if let Some(ref ty) = local.ty { if let Some(ref ty) = local.ty {
infix.push_str(": "); infix.push_str(": ");
infix.push_str(&ty.rewrite(&self.get_context(), 1000, Indent::new(0, 0)).unwrap()); infix.push_str(&ty.rewrite(&self.get_context(), 1000, Indent::empty()).unwrap());
} }
if local.init.is_some() { if local.init.is_some() {
@ -126,7 +125,7 @@ impl<'a> FmtVisitor<'a> {
// this. // this.
if newline_brace { if newline_brace {
result.push('\n'); result.push('\n');
result.push_str(&make_indent(indent, self.config)); result.push_str(&indent.to_string(self.config));
} else { } else {
result.push(' '); result.push(' ');
} }
@ -226,17 +225,17 @@ impl<'a> FmtVisitor<'a> {
if one_line_budget <= 0 { if one_line_budget <= 0 {
if self.config.fn_args_paren_newline { if self.config.fn_args_paren_newline {
result.push('\n'); result.push('\n');
result.push_str(&make_indent(arg_indent, self.config)); result.push_str(&arg_indent.to_string(self.config));
arg_indent = arg_indent + 1; // extra space for `(` arg_indent = arg_indent + 1; // extra space for `(`
result.push('('); result.push('(');
} else { } else {
result.push_str("(\n"); result.push_str("(\n");
result.push_str(&make_indent(arg_indent, self.config)); result.push_str(&arg_indent.to_string(self.config));
} }
} else if self.config.fn_args_layout == StructLitStyle::Block { } else if self.config.fn_args_layout == StructLitStyle::Block {
arg_indent = indent.block_indent(self.config.tab_spaces); arg_indent = indent.block_indent(self.config);
result.push_str("(\n"); result.push_str("(\n");
result.push_str(&make_indent(arg_indent, self.config)); result.push_str(&arg_indent.to_string(self.config));
} else { } else {
result.push('('); result.push('(');
} }
@ -281,7 +280,7 @@ impl<'a> FmtVisitor<'a> {
}; };
result.push('\n'); result.push('\n');
result.push_str(&make_indent(indent, self.config)); result.push_str(&indent.to_string(self.config));
} else { } else {
result.push(' '); result.push(' ');
} }
@ -390,7 +389,7 @@ impl<'a> FmtVisitor<'a> {
let indent = match self.config.fn_arg_indent { let indent = match self.config.fn_arg_indent {
BlockIndentStyle::Inherit => indent, BlockIndentStyle::Inherit => indent,
BlockIndentStyle::Tabbed => indent.block_indent(self.config.tab_spaces), BlockIndentStyle::Tabbed => indent.block_indent(self.config),
BlockIndentStyle::Visual => arg_indent, BlockIndentStyle::Visual => arg_indent,
}; };
@ -444,7 +443,7 @@ impl<'a> FmtVisitor<'a> {
// Didn't work. we must force vertical layout and put args on a newline. // Didn't work. we must force vertical layout and put args on a newline.
if let None = budgets { if let None = budgets {
let new_indent = indent.block_indent(self.config.tab_spaces); let new_indent = indent.block_indent(self.config);
let used_space = new_indent.width() + 2; // account for `(` and `)` let used_space = new_indent.width() + 2; // account for `(` and `)`
let max_space = self.config.ideal_width + self.config.leeway; let max_space = self.config.ideal_width + self.config.leeway;
if used_space > max_space { if used_space > max_space {
@ -480,14 +479,13 @@ impl<'a> FmtVisitor<'a> {
let generics_str = self.format_generics(generics, let generics_str = self.format_generics(generics,
" {", " {",
self.block_indent, self.block_indent,
self.block_indent self.block_indent.block_indent(self.config),
.block_indent(self.config.tab_spaces),
codemap::mk_sp(span.lo, body_start)) codemap::mk_sp(span.lo, body_start))
.unwrap(); .unwrap();
self.buffer.push_str(&generics_str); self.buffer.push_str(&generics_str);
self.last_pos = body_start; self.last_pos = body_start;
self.block_indent = self.block_indent.block_indent(self.config.tab_spaces); self.block_indent = self.block_indent.block_indent(self.config);
for (i, f) in enum_def.variants.iter().enumerate() { for (i, f) in enum_def.variants.iter().enumerate() {
let next_span_start: BytePos = if i == enum_def.variants.len() - 1 { let next_span_start: BytePos = if i == enum_def.variants.len() - 1 {
span.hi span.hi
@ -497,7 +495,7 @@ impl<'a> FmtVisitor<'a> {
self.visit_variant(f, i == enum_def.variants.len() - 1, next_span_start); self.visit_variant(f, i == enum_def.variants.len() - 1, next_span_start);
} }
self.block_indent = self.block_indent.block_unindent(self.config.tab_spaces); self.block_indent = self.block_indent.block_unindent(self.config);
self.format_missing_with_indent(span.lo + BytePos(enum_snippet.rfind('}').unwrap() as u32), self.format_missing_with_indent(span.lo + BytePos(enum_snippet.rfind('}').unwrap() as u32),
self.config); self.config);
@ -530,7 +528,7 @@ impl<'a> FmtVisitor<'a> {
arg.ty arg.ty
.rewrite(&self.get_context(), .rewrite(&self.get_context(),
1000, 1000,
Indent::new(0, 0)) Indent::empty())
.unwrap() .unwrap()
}, },
span_after(field.span, "(", self.codemap), span_after(field.span, "(", self.codemap),
@ -672,7 +670,7 @@ impl<'a> FmtVisitor<'a> {
single_line_cost as usize + used_budget > self.config.max_width; single_line_cost as usize + used_budget > self.config.max_width;
let tactic = if break_line { let tactic = if break_line {
let indentation = make_indent(offset.block_indent(self.config.tab_spaces), self.config); let indentation = offset.block_indent(self.config).to_string(self.config);
result.push('\n'); result.push('\n');
result.push_str(&indentation); result.push_str(&indentation);
@ -687,7 +685,7 @@ impl<'a> FmtVisitor<'a> {
tactic: tactic, tactic: tactic,
separator: ",", separator: ",",
trailing_separator: self.config.struct_trailing_comma, trailing_separator: self.config.struct_trailing_comma,
indent: offset.block_indent(self.config.tab_spaces), indent: offset.block_indent(self.config),
h_width: self.config.max_width, h_width: self.config.max_width,
v_width: budget, v_width: budget,
ends_with_newline: true, ends_with_newline: true,
@ -699,7 +697,7 @@ impl<'a> FmtVisitor<'a> {
if break_line { if break_line {
result.push('\n'); result.push('\n');
result.push_str(&make_indent(offset, self.config)); result.push_str(&offset.to_string(self.config));
} }
result.push_str(terminator); result.push_str(terminator);
@ -751,7 +749,7 @@ impl<'a> FmtVisitor<'a> {
Density::Tall, Density::Tall,
span.hi)); span.hi));
result.push_str(&where_clause_str); result.push_str(&where_clause_str);
result.push_str(&make_indent(self.block_indent, self.config)); result.push_str(&self.block_indent.to_string(self.config));
result.push('\n'); result.push('\n');
result.push_str(opener.trim()); result.push_str(opener.trim());
} else { } else {
@ -776,9 +774,9 @@ impl<'a> FmtVisitor<'a> {
ast::StructFieldKind::UnnamedField(vis) => format_visibility(vis), ast::StructFieldKind::UnnamedField(vis) => format_visibility(vis),
}; };
// FIXME silly width, indent // FIXME silly width, indent
let typ = field.node.ty.rewrite(&self.get_context(), 1000, Indent::new(0, 0)).unwrap(); let typ = field.node.ty.rewrite(&self.get_context(), 1000, Indent::empty()).unwrap();
let indent = self.block_indent + self.config.tab_spaces; let indent = self.block_indent.block_indent(self.config);
let mut attr_str = field.node let mut attr_str = field.node
.attrs .attrs
.rewrite(&self.get_context(), .rewrite(&self.get_context(),
@ -787,7 +785,7 @@ impl<'a> FmtVisitor<'a> {
.unwrap(); .unwrap();
if !attr_str.is_empty() { if !attr_str.is_empty() {
attr_str.push('\n'); attr_str.push('\n');
attr_str.push_str(&make_indent(indent, self.config)); attr_str.push_str(&indent.to_string(self.config));
} }
match name { match name {
@ -812,7 +810,7 @@ impl<'a> FmtVisitor<'a> {
let offset = match self.config.generics_indent { let offset = match self.config.generics_indent {
BlockIndentStyle::Inherit => offset, BlockIndentStyle::Inherit => offset,
BlockIndentStyle::Tabbed => offset.block_indent(self.config.tab_spaces), BlockIndentStyle::Tabbed => offset.block_indent(self.config),
// 1 = < // 1 = <
BlockIndentStyle::Visual => generics_offset + 1, BlockIndentStyle::Visual => generics_offset + 1,
}; };
@ -870,7 +868,7 @@ impl<'a> FmtVisitor<'a> {
} }
let extra_indent = match self.config.where_indent { let extra_indent = match self.config.where_indent {
BlockIndentStyle::Inherit => Indent::new(0, 0), BlockIndentStyle::Inherit => Indent::empty(),
BlockIndentStyle::Tabbed | BlockIndentStyle::Visual => Indent::new(config.tab_spaces, BlockIndentStyle::Tabbed | BlockIndentStyle::Visual => Indent::new(config.tab_spaces,
0), 0),
}; };
@ -879,7 +877,7 @@ impl<'a> FmtVisitor<'a> {
let offset = match self.config.where_pred_indent { let offset = match self.config.where_pred_indent {
BlockIndentStyle::Inherit => indent + extra_indent, BlockIndentStyle::Inherit => indent + extra_indent,
BlockIndentStyle::Tabbed => indent + extra_indent.block_indent(config.tab_spaces), BlockIndentStyle::Tabbed => indent + extra_indent.block_indent(config),
// 6 = "where ".len() // 6 = "where ".len()
BlockIndentStyle::Visual => indent + extra_indent + 6, BlockIndentStyle::Visual => indent + extra_indent + 6,
}; };
@ -915,9 +913,7 @@ impl<'a> FmtVisitor<'a> {
// 9 = " where ".len() + " {".len() // 9 = " where ".len() + " {".len()
if density == Density::Tall || preds_str.contains('\n') || if density == Density::Tall || preds_str.contains('\n') ||
indent.width() + 9 + preds_str.len() > self.config.max_width { indent.width() + 9 + preds_str.len() > self.config.max_width {
Some(format!("\n{}where {}", Some(format!("\n{}where {}", (indent + extra_indent).to_string(self.config), preds_str))
make_indent(indent + extra_indent, self.config),
preds_str))
} else { } else {
Some(format!(" where {}", preds_str)) Some(format!(" where {}", preds_str))
} }

View file

@ -83,7 +83,10 @@ const SKIP_ANNOTATION: &'static str = "rustfmt_skip";
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct Indent { pub struct Indent {
// Width of the block indent, in characters. Must be a multiple of
// Config::tab_spaces.
block_indent: usize, block_indent: usize,
// Alignment in characters.
alignment: usize, alignment: usize,
} }
@ -92,13 +95,17 @@ impl Indent {
Indent { block_indent: block_indent, alignment: alignment } Indent { block_indent: block_indent, alignment: alignment }
} }
pub fn block_indent(mut self, block_indent: usize) -> Indent { pub fn empty() -> Indent {
self.block_indent += block_indent; Indent::new(0, 0)
}
pub fn block_indent(mut self, config: &Config) -> Indent {
self.block_indent += config.tab_spaces;
self self
} }
pub fn block_unindent(mut self, block_indent: usize) -> Indent { pub fn block_unindent(mut self, config: &Config) -> Indent {
self.block_indent -= block_indent; self.block_indent -= config.tab_spaces;
self self
} }
@ -139,10 +146,7 @@ impl Sub for Indent {
type Output = Indent; type Output = Indent;
fn sub(self, rhs: Indent) -> Indent { fn sub(self, rhs: Indent) -> Indent {
Indent { Indent::new(self.block_indent - rhs.block_indent, self.alignment - rhs.alignment)
block_indent: self.block_indent - rhs.block_indent,
alignment: self.alignment - rhs.alignment,
}
} }
} }
@ -150,7 +154,7 @@ impl Add<usize> for Indent {
type Output = Indent; type Output = Indent;
fn add(self, rhs: usize) -> Indent { fn add(self, rhs: usize) -> Indent {
Indent { block_indent: self.block_indent, alignment: self.alignment + rhs } Indent::new(self.block_indent, self.alignment + rhs)
} }
} }

View file

@ -14,7 +14,7 @@ use std::iter::Peekable;
use syntax::codemap::{self, CodeMap, BytePos}; use syntax::codemap::{self, CodeMap, BytePos};
use Indent; use Indent;
use utils::{round_up_to_power_of_two, make_indent, wrap_str}; use utils::{round_up_to_power_of_two, wrap_str};
use comment::{FindUncommented, rewrite_comment, find_comment_end}; use comment::{FindUncommented, rewrite_comment, find_comment_end};
use config::Config; use config::Config;
@ -155,7 +155,7 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> Op
let mut result = String::with_capacity(round_up_to_power_of_two(alloc_width)); let mut result = String::with_capacity(round_up_to_power_of_two(alloc_width));
let mut line_len = 0; let mut line_len = 0;
let indent_str = &make_indent(formatting.indent, formatting.config); let indent_str = &formatting.indent.to_string(formatting.config);
for (i, item) in items.iter().enumerate() { for (i, item) in items.iter().enumerate() {
let first = i == 0; let first = i == 0;
let last = i == items.len() - 1; let last = i == items.len() - 1;
@ -221,7 +221,7 @@ pub fn write_list<'b>(items: &[ListItem], formatting: &ListFormatting<'b>) -> Op
let formatted_comment = rewrite_comment(comment, let formatted_comment = rewrite_comment(comment,
true, true,
formatting.v_width, formatting.v_width,
Indent::new(0, 0), Indent::empty(),
formatting.config); formatting.config);
result.push(' '); result.push(' ');

View file

@ -9,7 +9,6 @@
// except according to those terms. // except according to those terms.
use config::Config; use config::Config;
use utils::make_indent;
use visitor::FmtVisitor; use visitor::FmtVisitor;
use syntax::codemap::{self, BytePos}; use syntax::codemap::{self, BytePos};
@ -29,7 +28,7 @@ impl<'a> FmtVisitor<'a> {
// No new lines in the snippet. // No new lines in the snippet.
this.buffer.push_str("\n"); this.buffer.push_str("\n");
} }
let indent = make_indent(this.block_indent, config); let indent = this.block_indent.to_string(config);
this.buffer.push_str(&indent); this.buffer.push_str(&indent);
}) })
} }

View file

@ -44,7 +44,7 @@ impl<'a> RewriteContext<'a> {
RewriteContext { RewriteContext {
codemap: self.codemap, codemap: self.codemap,
config: self.config, config: self.config,
block_indent: self.block_indent.block_indent(self.config.tab_spaces), block_indent: self.block_indent.block_indent(self.config),
overflow_indent: self.overflow_indent, overflow_indent: self.overflow_indent,
} }
} }

View file

@ -15,7 +15,7 @@ use regex::Regex;
use Indent; use Indent;
use config::Config; use config::Config;
use utils::{make_indent, round_up_to_power_of_two}; use utils::round_up_to_power_of_two;
use MIN_STRING; use MIN_STRING;
@ -39,7 +39,7 @@ pub fn rewrite_string<'a>(s: &str, fmt: &StringFormat<'a>) -> String {
let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>(); let graphemes = UnicodeSegmentation::graphemes(&*stripped_str, false).collect::<Vec<&str>>();
let indent = make_indent(fmt.offset, fmt.config); let indent = fmt.offset.to_string(fmt.config);
let indent = &indent; let indent = &indent;
let mut cur_start = 0; let mut cur_start = 0;

View file

@ -15,7 +15,6 @@ use syntax::codemap::{CodeMap, Span, BytePos};
use Indent; use Indent;
use comment::FindUncommented; use comment::FindUncommented;
use config::Config;
use rewrite::{Rewrite, RewriteContext}; use rewrite::{Rewrite, RewriteContext};
use SKIP_ANNOTATION; use SKIP_ANNOTATION;
@ -37,11 +36,6 @@ pub fn span_after(original: Span, needle: &str, codemap: &CodeMap) -> BytePos {
original.lo + BytePos(snippet.find_uncommented(needle).unwrap() as u32 + 1) original.lo + BytePos(snippet.find_uncommented(needle).unwrap() as u32 + 1)
} }
#[inline]
pub fn make_indent(indent: Indent, config: &Config) -> String {
indent.to_string(config)
}
#[inline] #[inline]
pub fn format_visibility(vis: Visibility) -> &'static str { pub fn format_visibility(vis: Visibility) -> &'static str {
match vis { match vis {

View file

@ -102,7 +102,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
}; };
self.last_pos = self.last_pos + brace_compensation; self.last_pos = self.last_pos + brace_compensation;
self.block_indent = self.block_indent.block_indent(self.config.tab_spaces); self.block_indent = self.block_indent.block_indent(self.config);
self.buffer.push_str("{"); self.buffer.push_str("{");
for stmt in &b.stmts { for stmt in &b.stmts {
@ -117,7 +117,7 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
None => {} None => {}
} }
self.block_indent = self.block_indent.block_unindent(self.config.tab_spaces); self.block_indent = self.block_indent.block_unindent(self.config);
// TODO: we should compress any newlines here to just one // TODO: we should compress any newlines here to just one
self.format_missing_with_indent(b.span.hi - brace_compensation, self.config); self.format_missing_with_indent(b.span.hi - brace_compensation, self.config);
self.buffer.push_str("}"); self.buffer.push_str("}");
@ -198,9 +198,9 @@ impl<'a, 'v> visit::Visitor<'v> for FmtVisitor<'a> {
} }
ast::Item_::ItemImpl(..) | ast::Item_::ItemImpl(..) |
ast::Item_::ItemTrait(..) => { ast::Item_::ItemTrait(..) => {
self.block_indent = self.block_indent.block_indent(self.config.tab_spaces); self.block_indent = self.block_indent.block_indent(self.config);
visit::walk_item(self, item); visit::walk_item(self, item);
self.block_indent = self.block_indent.block_unindent(self.config.tab_spaces); self.block_indent = self.block_indent.block_unindent(self.config);
} }
ast::Item_::ItemExternCrate(_) => { ast::Item_::ItemExternCrate(_) => {
self.format_missing_with_indent(item.span.lo, self.config); self.format_missing_with_indent(item.span.lo, self.config);
@ -330,17 +330,17 @@ impl<'a> FmtVisitor<'a> {
if is_internal { if is_internal {
debug!("FmtVisitor::format_mod: internal mod"); debug!("FmtVisitor::format_mod: internal mod");
self.block_indent = self.block_indent.block_indent(self.config.tab_spaces); self.block_indent = self.block_indent.block_indent(self.config);
visit::walk_mod(self, m); visit::walk_mod(self, m);
debug!("... last_pos after: {:?}", self.last_pos); debug!("... last_pos after: {:?}", self.last_pos);
self.block_indent = self.block_indent.block_unindent(self.config.tab_spaces); self.block_indent = self.block_indent.block_unindent(self.config);
} }
} }
pub fn format_separate_mod(&mut self, m: &ast::Mod, filename: &str) { pub fn format_separate_mod(&mut self, m: &ast::Mod, filename: &str) {
let filemap = self.codemap.get_filemap(filename); let filemap = self.codemap.get_filemap(filename);
self.last_pos = filemap.start_pos; self.last_pos = filemap.start_pos;
self.block_indent = Indent::new(0, 0); self.block_indent = Indent::empty();
visit::walk_mod(self, m); visit::walk_mod(self, m);
self.format_missing(filemap.end_pos); self.format_missing(filemap.end_pos);
} }
@ -353,7 +353,7 @@ impl<'a> FmtVisitor<'a> {
codemap: self.codemap, codemap: self.codemap,
config: self.config, config: self.config,
block_indent: self.block_indent, block_indent: self.block_indent,
overflow_indent: Indent::new(0, 0), overflow_indent: Indent::empty(),
}; };
// 1 = ";" // 1 = ";"
match vp.rewrite(&context, self.config.max_width - offset.width() - 1, offset) { match vp.rewrite(&context, self.config.max_width - offset.width() - 1, offset) {
@ -385,7 +385,7 @@ impl<'a> FmtVisitor<'a> {
codemap: self.codemap, codemap: self.codemap,
config: self.config, config: self.config,
block_indent: self.block_indent, block_indent: self.block_indent,
overflow_indent: Indent::new(0, 0), overflow_indent: Indent::empty(),
} }
} }
} }
@ -396,7 +396,7 @@ impl<'a> Rewrite for [ast::Attribute] {
if self.is_empty() { if self.is_empty() {
return Some(result); return Some(result);
} }
let indent = utils::make_indent(offset, context.config); let indent = offset.to_string(context.config);
for (i, a) in self.iter().enumerate() { for (i, a) in self.iter().enumerate() {
let a_str = context.snippet(a.span); let a_str = context.snippet(a.span);