Merge pull request #374 from nrc/fix-fn

Use max width for function decls, not ideal width
This commit is contained in:
Marcus Klaas de Vries 2015-09-27 14:52:08 +02:00
commit 0f485aabd6
6 changed files with 20 additions and 29 deletions

View File

@ -216,7 +216,6 @@ macro_rules! create_config {
create_config! {
max_width: usize, "Maximum width of each line",
ideal_width: usize, "Ideal width of each line (only used for comments)",
leeway: usize, "Leeway of line width (deprecated)",
tab_spaces: usize, "Number of spaces per tab",
fn_call_width: usize, "Maximum width of the args of a function call\
before faling back to vertical formatting",
@ -258,7 +257,6 @@ impl Default for Config {
Config {
max_width: 100,
ideal_width: 80,
leeway: 5,
tab_spaces: 4,
fn_call_width: 50,
struct_lit_width: 12,

View File

@ -513,8 +513,6 @@ impl<'a> FmtVisitor<'a> {
ret_str_len: usize,
newline_brace: bool)
-> (usize, usize, Indent) {
let mut budgets = None;
// Try keeping everything on the same line
if !result.contains("\n") {
// 3 = `() `, space is before ret_string
@ -530,31 +528,28 @@ impl<'a> FmtVisitor<'a> {
// 2 = `()`
let used_space = indent.width() + result.len() + 2;
let max_space = self.config.ideal_width + self.config.leeway;
let max_space = self.config.max_width;
debug!("compute_budgets_for_args: used_space: {}, max_space: {}",
used_space,
max_space);
if used_space < max_space {
budgets = Some((one_line_budget,
max_space - used_space,
indent + result.len() + 1));
return (one_line_budget,
max_space - used_space,
indent + result.len() + 1);
}
}
// Didn't work. we must force vertical layout and put args on a newline.
if let None = budgets {
let new_indent = indent.block_indent(self.config);
let used_space = new_indent.width() + 2; // account for `(` and `)`
let max_space = self.config.ideal_width + self.config.leeway;
if used_space > max_space {
// Whoops! bankrupt.
// TODO: take evasive action, perhaps kill the indent or something.
} else {
budgets = Some((0, max_space - used_space, new_indent));
}
let new_indent = indent.block_indent(self.config);
let used_space = new_indent.width() + 2; // account for `(` and `)`
let max_space = self.config.max_width;
if used_space <= max_space {
(0, max_space - used_space, new_indent)
} else {
// Whoops! bankrupt.
// TODO: take evasive action, perhaps kill the indent or something.
panic!("in compute_budgets_for_args");
}
budgets.unwrap()
}
fn newline_for_brace(&self, where_clause: &ast::WhereClause) -> bool {
@ -642,7 +637,7 @@ impl<'a> FmtVisitor<'a> {
} else {
0
};
let budget = self.config.ideal_width - indent.width() - comma_cost - 1; // 1 = )
let budget = self.config.max_width - indent.width() - comma_cost - 1; // 1 = )
let fmt = ListFormatting {
tactic: ListTactic::HorizontalVertical,
@ -779,7 +774,7 @@ impl<'a> FmtVisitor<'a> {
};
// 1 = ,
let budget = self.config.ideal_width - offset.width() + self.config.tab_spaces - 1;
let budget = self.config.max_width - offset.width() + self.config.tab_spaces - 1;
let fmt = ListFormatting {
tactic: tactic,
separator: ",",
@ -984,7 +979,7 @@ impl<'a> FmtVisitor<'a> {
// FIXME: if where_pred_indent != Visual, then the budgets below might
// be out by a char or two.
let budget = self.config.ideal_width + self.config.leeway - offset.width();
let budget = self.config.max_width - offset.width();
let span_start = span_for_where_pred(&where_clause.predicates[0]).lo;
let items = itemize_list(self.codemap,
where_clause.predicates.iter(),

View File

@ -1,6 +1,5 @@
max_width = 100
ideal_width = 80
leeway = 5
tab_spaces = 2
newline_style = "Unix"
fn_brace_style = "SameLineWhere"

View File

@ -27,7 +27,7 @@ enum Bar {
}
enum LongVariants {
First(LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG, // comment
First(LOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOONG, // comment
VARIANT),
// This is the second variant
Second,

View File

@ -2,8 +2,8 @@
// Test some of the ways function signatures can be customised.
// Test compressed layout of args.
fn foo(a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc,
d: Ddddddddddddddddddddddddd, e: Eeeeeeeeeeeeeeeeeee) {
fn foo(a: Aaaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbbbb, c: Ccccccccccccccccc, d: Ddddddddddddddddddddddddd,
e: Eeeeeeeeeeeeeeeeeee) {
foo();
}

View File

@ -64,8 +64,7 @@ fn qux(a: dadsfa, // Comment 1
/// Blah blah blah.
impl Bar {
fn foo(&mut self,
a: sdfsdfcccccccccccccccccccccccccccccccccccccccccccccccccc, /* commen
* t on a */
a: sdfsdfcccccccccccccccccccccccccccccccccccccccccccccccccc, // comment on a
b: sdfasdfsdfasfs /* closing comment */)
-> isize {
}