Skip to content

More function decl variety #243

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 1, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ create_config! {
fn_brace_style: BraceStyle,
fn_return_indent: ReturnIndent,
fn_args_paren_newline: bool,
fn_args_layout: Density,
fn_args_density: Density,
fn_args_layout: StructLitStyle,
fn_arg_indent: BlockIndentStyle,
where_density: Density, // Should we at least try to put the where clause on the same line as
// the rest of the function decl?
Expand Down Expand Up @@ -122,7 +123,8 @@ impl Default for Config {
fn_brace_style: BraceStyle::SameLineWhere,
fn_return_indent: ReturnIndent::WithArgs,
fn_args_paren_newline: true,
fn_args_layout: Density::Tall,
fn_args_density: Density::Tall,
fn_args_layout: StructLitStyle::Visual,
fn_arg_indent: BlockIndentStyle::Visual,
where_density: Density::Tall,
where_indent: BlockIndentStyle::Tabbed,
Expand All @@ -131,7 +133,7 @@ impl Default for Config {
generics_indent: BlockIndentStyle::Visual,
struct_trailing_comma: SeparatorTactic::Vertical,
struct_lit_trailing_comma: SeparatorTactic::Vertical,
struct_lit_style: StructLitStyle::BlockIndent,
struct_lit_style: StructLitStyle::Block,
enum_trailing_comma: true,
report_todo: ReportTactic::Always,
report_fixme: ReportTactic::Never,
Expand Down
8 changes: 4 additions & 4 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -940,10 +940,10 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
// Foo { a: Foo } - indent is +3, width is -5.
let h_budget = try_opt!(width.checked_sub(path_str.len() + 5));
let (indent, v_budget) = match context.config.struct_lit_style {
StructLitStyle::VisualIndent => {
StructLitStyle::Visual => {
(offset + path_str.len() + 3, h_budget)
}
StructLitStyle::BlockIndent => {
StructLitStyle::Block => {
// If we are all on one line, then we'll ignore the indent, and we
// have a smaller budget.
let indent = context.block_indent + context.config.tab_spaces;
Expand Down Expand Up @@ -1012,15 +1012,15 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext,
let fields_str = write_list(&items.collect::<Vec<_>>(), &fmt);

match context.config.struct_lit_style {
StructLitStyle::BlockIndent if fields_str.contains('\n') => {
StructLitStyle::Block if fields_str.contains('\n') => {
let inner_indent = make_indent(context.block_indent + context.config.tab_spaces);
let outer_indent = make_indent(context.block_indent);
Some(format!("{} {{\n{}{}\n{}}}", path_str, inner_indent, fields_str, outer_indent))
}
_ => Some(format!("{} {{ {} }}", path_str, fields_str)),
}

// FIXME if context.config.struct_lit_style == VisualIndent, but we run out
// FIXME if context.config.struct_lit_style == Visual, but we run out
// of space, we should fall back to BlockIndent.
}

Expand Down
25 changes: 19 additions & 6 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

// Formatting top-level items - functions, structs, enums, traits, impls.

use {ReturnIndent, BraceStyle};
use {ReturnIndent, BraceStyle, StructLitStyle};
use utils::{format_mutability, format_visibility, make_indent, contains_skip, span_after,
end_typaram};
use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, ListTactic};
Expand Down Expand Up @@ -225,6 +225,10 @@ impl<'a> FmtVisitor<'a> {
result.push_str("(\n");
result.push_str(&make_indent(arg_indent));
}
} else if self.config.fn_args_layout == StructLitStyle::Block {
arg_indent = indent + self.config.tab_spaces;
result.push_str("(\n");
result.push_str(&make_indent(arg_indent));
} else {
result.push('(');
}
Expand All @@ -245,14 +249,20 @@ impl<'a> FmtVisitor<'a> {
indent,
arg_indent,
args_span));
if self.config.fn_args_layout == StructLitStyle::Block {
result.push('\n');
}
result.push(')');

// Return type.
if !ret_str.is_empty() {
// If we've already gone multi-line, or the return type would push
// over the max width, then put the return type on a new line.
if result.contains("\n") ||
result.len() + indent + ret_str.len() > self.config.max_width {
// Unless we are formatting args like a block, in which case there
// should always be room for the return type.
if (result.contains("\n") ||
result.len() + indent + ret_str.len() > self.config.max_width) &&
self.config.fn_args_layout != StructLitStyle::Block {
let indent = match self.config.fn_return_indent {
ReturnIndent::WithWhereClause => indent + 4,
// TODO we might want to check that using the arg indent doesn't
Expand Down Expand Up @@ -285,8 +295,11 @@ impl<'a> FmtVisitor<'a> {
}
}

let where_density = if self.config.where_density == Density::Compressed &&
!result.contains('\n') {
let where_density = if (self.config.where_density == Density::Compressed &&
(!result.contains('\n') ||
self.config.fn_args_layout == StructLitStyle::Block)) ||
(self.config.fn_args_layout == StructLitStyle::Block &&
ret_str.is_empty()) {
Density::Compressed
} else {
Density::Tall
Expand Down Expand Up @@ -363,7 +376,7 @@ impl<'a> FmtVisitor<'a> {
};

let fmt = ListFormatting {
tactic: self.config.fn_args_layout.to_list_tactic(),
tactic: self.config.fn_args_density.to_list_tactic(),
separator: ",",
trailing_separator: SeparatorTactic::Never,
indent: indent,
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,13 @@ impl_enum_decodable!(ReturnIndent, WithArgs, WithWhereClause);
pub enum StructLitStyle {
// First line on the same line as the opening brace, all lines aligned with
// the first line.
VisualIndent,
Visual,
// First line is on a new line and all lines align with block indent.
BlockIndent,
Block,
// FIXME Maybe we should also have an option to align types.
}

impl_enum_decodable!(StructLitStyle, VisualIndent, BlockIndent);
impl_enum_decodable!(StructLitStyle, Visual, Block);

enum ErrorKind {
// Line has exceeded character limit
Expand Down
5 changes: 3 additions & 2 deletions tests/config/small_tabs.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ newline_style = "Unix"
fn_brace_style = "SameLineWhere"
fn_return_indent = "WithArgs"
fn_args_paren_newline = true
fn_args_layout = "Tall"
fn_args_density = "Tall"
fn_args_layout = "Visual"
fn_arg_indent = "Visual"
where_density = "Tall"
where_indent = "Tabbed"
Expand All @@ -15,7 +16,7 @@ where_pred_indent = "Visual"
generics_indent = "Visual"
struct_trailing_comma = "Vertical"
struct_lit_trailing_comma = "Vertical"
struct_lit_style = "BlockIndent"
struct_lit_style = "Block"
enum_trailing_comma = true
report_todo = "Always"
report_fixme = "Never"
Expand Down
36 changes: 36 additions & 0 deletions tests/source/fn-custom-6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// rustfmt-fn_args_layout: Block
// rustfmt-where_indent: Inherit
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) {
foo();
}

fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) {
bar();
}

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String {
foo();
}

fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String {
bar();
}

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) where T: UUUUUUUUUUU {
foo();
}

fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) where T: UUUUUUUUUUU {
bar();
}

fn foo(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb) -> String where T: UUUUUUUUUUU {
foo();
}

fn bar(a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb, c: Cccccccccccccccccc, d: Dddddddddddddddd, e: Eeeeeeeeeeeeeee) -> String where T: UUUUUUUUUUU {
bar();
}
2 changes: 1 addition & 1 deletion tests/source/fn-custom.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_layout: Compressed
// rustfmt-fn_args_density: Compressed
// Test some of the ways function signatures can be customised.

// Test compressed layout of args.
Expand Down
2 changes: 1 addition & 1 deletion tests/source/struct_lits_visual.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-struct_lit_style: VisualIndent
// rustfmt-struct_lit_style: Visual

// Struct literal expressions.

Expand Down
70 changes: 70 additions & 0 deletions tests/target/fn-custom-6.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
// rustfmt-fn_args_layout: Block
// rustfmt-where_indent: Inherit
// rustfmt-fn_brace_style: PreferSameLine
// Test different indents.

fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) {
foo();
}

fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) {
bar();
}

fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) -> String {
foo();
}

fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) -> String {
bar();
}

fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) where T: UUUUUUUUUUU {
foo();
}

fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) where T: UUUUUUUUUUU {
bar();
}

fn foo(
a: Aaaaaaaaaaaaaa, b: Bbbbbbbbbbbbbb
) -> String
where T: UUUUUUUUUUU {
foo();
}

fn bar(
a: Aaaaaaaaaaaaaa,
b: Bbbbbbbbbbbbbb,
c: Cccccccccccccccccc,
d: Dddddddddddddddd,
e: Eeeeeeeeeeeeeee
) -> String
where T: UUUUUUUUUUU {
bar();
}
2 changes: 1 addition & 1 deletion tests/target/fn-custom.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-fn_args_layout: Compressed
// rustfmt-fn_args_density: Compressed
// Test some of the ways function signatures can be customised.

// Test compressed layout of args.
Expand Down
2 changes: 1 addition & 1 deletion tests/target/struct_lits_visual.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// rustfmt-struct_lit_style: VisualIndent
// rustfmt-struct_lit_style: Visual

// Struct literal expressions.

Expand Down