From aa4cd311bb07ea25d89096e8fdd5d64b73666c28 Mon Sep 17 00:00:00 2001 From: est31 Date: Tue, 6 Jun 2017 03:11:17 +0200 Subject: [PATCH] Add config options for spaces around the colon in struct literal fields In Rust, colons are used for three purposes: * Type annotations, including type ascription * Trait bounds * Struct literal fields This commit adds options for the last missing of the three purposes, struct literal fields. --- Configurations.md | 54 +++++++++++++++++++ src/config.rs | 4 ++ src/expr.rs | 10 ++-- src/items.rs | 10 +++- ...pace_after_struct_lit_field_colon-false.rs | 6 +++ ...space_after_struct_lit_field_colon-true.rs | 6 +++ ...ace_before_struct_lit_field_colon-false.rs | 6 +++ ...pace_before_struct_lit_field_colon-true.rs | 6 +++ ...pace_after_struct_lit_field_colon-false.rs | 6 +++ ...space_after_struct_lit_field_colon-true.rs | 6 +++ ...ace_before_struct_lit_field_colon-false.rs | 6 +++ ...pace_before_struct_lit_field_colon-true.rs | 6 +++ tests/target/space-before-type-annotation.rs | 2 +- .../space-not-after-type-annotation-colon.rs | 2 +- 14 files changed, 121 insertions(+), 9 deletions(-) create mode 100644 tests/source/configs-space_after_struct_lit_field_colon-false.rs create mode 100644 tests/source/configs-space_after_struct_lit_field_colon-true.rs create mode 100644 tests/source/configs-space_before_struct_lit_field_colon-false.rs create mode 100644 tests/source/configs-space_before_struct_lit_field_colon-true.rs create mode 100644 tests/target/configs-space_after_struct_lit_field_colon-false.rs create mode 100644 tests/target/configs-space_after_struct_lit_field_colon-true.rs create mode 100644 tests/target/configs-space_before_struct_lit_field_colon-false.rs create mode 100644 tests/target/configs-space_before_struct_lit_field_colon-true.rs diff --git a/Configurations.md b/Configurations.md index a0c6ba407d9..0b0a5570356 100644 --- a/Configurations.md +++ b/Configurations.md @@ -1123,6 +1123,33 @@ fn lorem(t: T) { See also: [`space_before_bound`](#space_before_bound). +## `space_after_struct_lit_field_colon` + +Leave a space after the colon in a struct literal field + +- **Default value**: `true` +- **Possible values**: `true`, `false` + +#### `false`: + +```rust +let lorem = Lorem { + ipsum:dolor, + sit:amet, +}; +``` + +#### `true`: + +```rust +let lorem = Lorem { + ipsum: dolor, + sit: amet, +}; +``` + +See also: [`space_before_struct_lit_field_colon`](#space_before_struct_lit_field_colon). + ## `space_after_type_annotation_colon` Leave a space after the colon in a type annotation @@ -1173,6 +1200,33 @@ fn lorem(t: T) { See also: [`space_after_bound_colon`](#space_after_bound_colon). +## `space_before_struct_lit_field_colon` + +Leave a space before the colon in a struct literal field + +- **Default value**: `true` +- **Possible values**: `true`, `false` + +#### `false`: + +```rust +let lorem = Lorem { + ipsum: dolor, + sit: amet, +}; +``` + +#### `true`: + +```rust +let lorem = Lorem { + ipsum : dolor, + sit : amet, +}; +``` + +See also: [`space_after_struct_lit_field_colon`](#space_after_struct_lit_field_colon). + ## `space_before_type_annotation` Leave a space before the colon in a type annotation diff --git a/src/config.rs b/src/config.rs index 1d912093c2e..112d8e35d1d 100644 --- a/src/config.rs +++ b/src/config.rs @@ -564,6 +564,10 @@ create_config! { "Leave a space before the colon in a type annotation"; space_after_type_annotation_colon: bool, true, "Leave a space after the colon in a type annotation"; + space_before_struct_lit_field_colon: bool, false, + "Leave a space before the colon in a struct literal field"; + space_after_struct_lit_field_colon: bool, true, + "Leave a space after the colon in a struct literal field"; space_before_bound: bool, false, "Leave a space before the colon in a trait or lifetime bound"; space_after_bound_colon: bool, true, "Leave a space after the colon in a trait or lifetime bound"; diff --git a/src/expr.rs b/src/expr.rs index 16dfe6fcaa6..8392ab438cd 100644 --- a/src/expr.rs +++ b/src/expr.rs @@ -2050,18 +2050,18 @@ fn rewrite_struct_lit<'a>(context: &RewriteContext, // FIXME if context.config.struct_lit_style() == Visual, but we run out // of space, we should fall back to BlockIndent. } - -pub fn type_annotation_separator(config: &Config) -> &str { - colon_spaces(config.space_before_type_annotation(), - config.space_after_type_annotation_colon()) +pub fn struct_lit_field_separator(config: &Config) -> &str { + colon_spaces(config.space_before_struct_lit_field_colon(), + config.space_after_struct_lit_field_colon()) } + fn rewrite_field(context: &RewriteContext, field: &ast::Field, shape: Shape) -> Option { let name = &field.ident.node.to_string(); if field.is_shorthand { Some(name.to_string()) } else { - let separator = type_annotation_separator(context.config); + let separator = struct_lit_field_separator(context.config); let overhead = name.len() + separator.len(); let mut expr_shape = try_opt!(shape.sub_width(overhead)); expr_shape.offset += overhead; diff --git a/src/items.rs b/src/items.rs index fdcf96454c7..bf7933b547c 100644 --- a/src/items.rs +++ b/src/items.rs @@ -14,10 +14,10 @@ use {Indent, Shape}; use codemap::SpanUtils; use utils::{format_mutability, format_visibility, contains_skip, end_typaram, wrap_str, last_line_width, format_unsafety, trim_newlines, stmt_expr, semicolon_for_expr, - trimmed_last_line_width}; + trimmed_last_line_width, colon_spaces}; use lists::{write_list, itemize_list, ListItem, ListFormatting, SeparatorTactic, list_helper, DefinitiveListTactic, ListTactic, definitive_tactic, format_item_list}; -use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs, type_annotation_separator}; +use expr::{is_empty_block, is_simple_block_stmt, rewrite_assign_rhs}; use comment::{FindUncommented, contains_comment, rewrite_comment, recover_comment_removed}; use visitor::FmtVisitor; use rewrite::{Rewrite, RewriteContext}; @@ -27,6 +27,12 @@ use syntax::{ast, abi, codemap, ptr, symbol}; use syntax::codemap::{Span, BytePos, mk_sp}; use syntax::ast::ImplItem; +fn type_annotation_separator(config: &Config) -> &str { + colon_spaces(config.space_before_type_annotation(), + config.space_after_type_annotation_colon()) +} + + // Statements of the form // let pat: ty = init; impl Rewrite for ast::Local { diff --git a/tests/source/configs-space_after_struct_lit_field_colon-false.rs b/tests/source/configs-space_after_struct_lit_field_colon-false.rs new file mode 100644 index 00000000000..3ee99fa5d1d --- /dev/null +++ b/tests/source/configs-space_after_struct_lit_field_colon-false.rs @@ -0,0 +1,6 @@ +// rustfmt-space_after_struct_lit_field_colon: false + +const LOREM: Lorem = Lorem { + ipsum:dolor, + sit : amet, +}; diff --git a/tests/source/configs-space_after_struct_lit_field_colon-true.rs b/tests/source/configs-space_after_struct_lit_field_colon-true.rs new file mode 100644 index 00000000000..6105d4725d8 --- /dev/null +++ b/tests/source/configs-space_after_struct_lit_field_colon-true.rs @@ -0,0 +1,6 @@ +// rustfmt-space_after_struct_lit_field_colon: true + +const LOREM: Lorem = Lorem { + ipsum:dolor, + sit : amet, +}; diff --git a/tests/source/configs-space_before_struct_lit_field_colon-false.rs b/tests/source/configs-space_before_struct_lit_field_colon-false.rs new file mode 100644 index 00000000000..a2d71c8bf0b --- /dev/null +++ b/tests/source/configs-space_before_struct_lit_field_colon-false.rs @@ -0,0 +1,6 @@ +// rustfmt-space_before_struct_lit_field_colon: false + +const LOREM: Lorem = Lorem { + ipsum:dolor, + sit : amet, +}; diff --git a/tests/source/configs-space_before_struct_lit_field_colon-true.rs b/tests/source/configs-space_before_struct_lit_field_colon-true.rs new file mode 100644 index 00000000000..50e4ba12d82 --- /dev/null +++ b/tests/source/configs-space_before_struct_lit_field_colon-true.rs @@ -0,0 +1,6 @@ +// rustfmt-space_before_struct_lit_field_colon: true + +const LOREM: Lorem = Lorem { + ipsum:dolor, + sit : amet, +}; diff --git a/tests/target/configs-space_after_struct_lit_field_colon-false.rs b/tests/target/configs-space_after_struct_lit_field_colon-false.rs new file mode 100644 index 00000000000..8f475059464 --- /dev/null +++ b/tests/target/configs-space_after_struct_lit_field_colon-false.rs @@ -0,0 +1,6 @@ +// rustfmt-space_after_struct_lit_field_colon: false + +const LOREM: Lorem = Lorem { + ipsum:dolor, + sit:amet, +}; diff --git a/tests/target/configs-space_after_struct_lit_field_colon-true.rs b/tests/target/configs-space_after_struct_lit_field_colon-true.rs new file mode 100644 index 00000000000..34fb792dcb5 --- /dev/null +++ b/tests/target/configs-space_after_struct_lit_field_colon-true.rs @@ -0,0 +1,6 @@ +// rustfmt-space_after_struct_lit_field_colon: true + +const LOREM: Lorem = Lorem { + ipsum: dolor, + sit: amet, +}; diff --git a/tests/target/configs-space_before_struct_lit_field_colon-false.rs b/tests/target/configs-space_before_struct_lit_field_colon-false.rs new file mode 100644 index 00000000000..48336954786 --- /dev/null +++ b/tests/target/configs-space_before_struct_lit_field_colon-false.rs @@ -0,0 +1,6 @@ +// rustfmt-space_before_struct_lit_field_colon: false + +const LOREM: Lorem = Lorem { + ipsum: dolor, + sit: amet, +}; diff --git a/tests/target/configs-space_before_struct_lit_field_colon-true.rs b/tests/target/configs-space_before_struct_lit_field_colon-true.rs new file mode 100644 index 00000000000..e4f1cdb4575 --- /dev/null +++ b/tests/target/configs-space_before_struct_lit_field_colon-true.rs @@ -0,0 +1,6 @@ +// rustfmt-space_before_struct_lit_field_colon: true + +const LOREM: Lorem = Lorem { + ipsum : dolor, + sit : amet, +}; diff --git a/tests/target/space-before-type-annotation.rs b/tests/target/space-before-type-annotation.rs index 54b9541eaae..0ad06dbb70c 100644 --- a/tests/target/space-before-type-annotation.rs +++ b/tests/target/space-before-type-annotation.rs @@ -9,5 +9,5 @@ struct S { fieldVar : i32, } fn f() { - S { fieldVar : 42 } + S { fieldVar: 42 } } diff --git a/tests/target/space-not-after-type-annotation-colon.rs b/tests/target/space-not-after-type-annotation-colon.rs index 3bdfa57fb73..b07620fb4c1 100644 --- a/tests/target/space-not-after-type-annotation-colon.rs +++ b/tests/target/space-not-after-type-annotation-colon.rs @@ -10,5 +10,5 @@ struct S { fieldVar :i32, } fn f() { - S { fieldVar :42 } + S { fieldVar: 42 } }