Skip to content

Commit 05d043c

Browse files
authored
Merge pull request #1785 from topecongiro/rfc/import
Rfc: use block indent for multi-lined imports
2 parents ef63830 + 988e387 commit 05d043c

38 files changed

+441
-207
lines changed

Configurations.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -979,6 +979,87 @@ match lorem {
979979

980980
See also: [`match_block_trailing_comma`](#match_block_trailing_comma), [`wrap_match_arms`](#wrap_match_arms).
981981

982+
## `imports_indent`
983+
984+
Indent style of imports
985+
986+
- **Default Value**: `"Visual"`
987+
- **Possible values**: `"Block"`, `"Visual"`
988+
989+
#### `"Block"`
990+
991+
```rust
992+
use foo::{
993+
xxx,
994+
yyy,
995+
zzz,
996+
};
997+
```
998+
999+
#### `"Visual"`
1000+
1001+
```rust
1002+
use foo::{xxx,
1003+
yyy,
1004+
zzz};
1005+
```
1006+
1007+
See also: [`imports_layout`](#imports_layout).
1008+
1009+
## `imports_layout`
1010+
1011+
Item layout inside a imports block
1012+
1013+
- **Default value**: "Mixed"
1014+
- **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"
1015+
1016+
#### `"Mixed"`
1017+
1018+
```rust
1019+
use foo::{xxx, yyy, zzz};
1020+
1021+
use foo::{aaa, bbb, ccc,
1022+
ddd, eee, fff};
1023+
```
1024+
1025+
#### `"Horizontal"`
1026+
1027+
**Note**: This option forces to put everything on one line and may exceeds `max_width`.
1028+
1029+
```rust
1030+
use foo::{xxx, yyy, zzz};
1031+
1032+
use foo::{aaa, bbb, ccc, ddd, eee, fff};
1033+
```
1034+
1035+
#### `"HorizontalVertical"`
1036+
1037+
```rust
1038+
use foo::{xxx, yyy, zzz};
1039+
1040+
use foo::{aaa,
1041+
bbb,
1042+
ccc,
1043+
ddd,
1044+
eee,
1045+
fff};
1046+
```
1047+
1048+
#### `"Vertical"`
1049+
1050+
```rust
1051+
use foo::{xxx,
1052+
yyy,
1053+
zzz};
1054+
1055+
use foo::{aaa,
1056+
bbb,
1057+
ccc,
1058+
ddd,
1059+
eee,
1060+
fff};
1061+
```
1062+
9821063
## `item_brace_style`
9831064

9841065
Brace style for structs and enums

src/bin/cargo-fmt.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ use std::str;
2424
use std::collections::HashSet;
2525
use std::iter::FromIterator;
2626

27+
use getopts::{Matches, Options};
2728
use json::Value;
2829

29-
use getopts::{Options, Matches};
30-
3130
fn main() {
3231
let exit_status = execute();
3332
std::io::stdout().flush().unwrap();

src/bin/rustfmt.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@ extern crate toml;
1717
extern crate env_logger;
1818
extern crate getopts;
1919

20-
use rustfmt::{run, Input, Summary};
21-
use rustfmt::file_lines::FileLines;
22-
use rustfmt::config::{Config, WriteMode, get_toml_path};
23-
2420
use std::{env, error};
2521
use std::fs::File;
2622
use std::io::{self, Read, Write};
@@ -29,6 +25,10 @@ use std::str::FromStr;
2925

3026
use getopts::{Matches, Options};
3127

28+
use rustfmt::{run, Input, Summary};
29+
use rustfmt::file_lines::FileLines;
30+
use rustfmt::config::{get_toml_path, Config, WriteMode};
31+
3232
type FmtError = Box<error::Error + Send + Sync>;
3333
type FmtResult<T> = std::result::Result<T, FmtError>;
3434

src/chains.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@
7777
/// ```
7878
7979
use Shape;
80-
use rewrite::{Rewrite, RewriteContext};
81-
use utils::{wrap_str, first_line_width, last_line_width, mk_sp, last_line_extendable};
82-
use expr::rewrite_call;
8380
use config::IndentStyle;
81+
use expr::rewrite_call;
8482
use macros::convert_try_mac;
83+
use rewrite::{Rewrite, RewriteContext};
84+
use utils::{first_line_width, last_line_extendable, last_line_width, mk_sp, wrap_str};
8585

8686
use std::cmp::min;
8787
use std::iter;

src/checkstyle.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10-
use rustfmt_diff::{Mismatch, DiffLine};
10+
1111
use std::io::{self, Write};
12-
use config::WriteMode;
1312

13+
use config::WriteMode;
14+
use rustfmt_diff::{DiffLine, Mismatch};
1415

1516
pub fn output_header<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
1617
where

src/comment.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use syntax::codemap::Span;
1717
use {Indent, Shape};
1818
use config::Config;
1919
use rewrite::RewriteContext;
20-
use string::{StringFormat, rewrite_string};
20+
use string::{rewrite_string, StringFormat};
2121
use utils::wrap_str;
2222

2323
fn is_custom_comment(comment: &str) -> bool {
@@ -809,8 +809,8 @@ fn remove_comment_header(comment: &str) -> &str {
809809

810810
#[cfg(test)]
811811
mod test {
812-
use super::{CharClasses, CodeCharKind, FullCodeCharKind, contains_comment, rewrite_comment,
813-
FindUncommented, CommentCodeSlices};
812+
use super::{contains_comment, rewrite_comment, CharClasses, CodeCharKind, CommentCodeSlices,
813+
FindUncommented, FullCodeCharKind};
814814
use {Indent, Shape};
815815

816816
#[test]

src/config.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,14 @@
1010

1111
extern crate toml;
1212

13+
use std::{env, fs};
1314
use std::cell::Cell;
14-
use std::fs;
1515
use std::fs::File;
16-
use std::env;
1716
use std::io::{Error, ErrorKind, Read};
1817
use std::path::{Path, PathBuf};
1918

2019
use file_lines::FileLines;
21-
use lists::{SeparatorTactic, ListTactic};
20+
use lists::{ListTactic, SeparatorTactic};
2221

2322
macro_rules! configuration_option_enum{
2423
($e:ident: $( $x:ident ),+ $(,)*) => {
@@ -557,9 +556,11 @@ create_config! {
557556
chain_one_line_max: usize, 60, "Maximum length of a chain to fit on a single line";
558557
chain_split_single_child: bool, false, "Split a chain with a single child if its length \
559558
exceeds `chain_one_line_max`";
559+
imports_indent: IndentStyle, IndentStyle::Visual, "Indent of imports";
560+
imports_layout: ListTactic, ListTactic::Mixed, "Item layout inside a import block";
560561
reorder_imports: bool, false, "Reorder import statements alphabetically";
561562
reorder_imports_in_group: bool, false, "Reorder import statements in group";
562-
reorder_imported_names: bool, false,
563+
reorder_imported_names: bool, true,
563564
"Reorder lists of names in import statements alphabetically";
564565
single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \
565566
expressions. A value of zero means always break \

src/expr.rs

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,33 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::cmp::{Ordering, min};
12-
use std::iter::ExactSizeIterator;
11+
use std::cmp::{min, Ordering};
1312
use std::fmt::Write;
13+
use std::iter::ExactSizeIterator;
14+
15+
use syntax::{ast, ptr};
16+
use syntax::codemap::{BytePos, CodeMap, Span};
17+
use syntax::parse::classify;
1418

1519
use {Indent, Shape, Spanned};
16-
use codemap::SpanUtils;
17-
use rewrite::{Rewrite, RewriteContext};
18-
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic,
19-
DefinitiveListTactic, definitive_tactic, ListItem, struct_lit_shape,
20-
struct_lit_tactic, shape_for_tactic, struct_lit_formatting};
21-
use string::{StringFormat, rewrite_string};
22-
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
23-
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
24-
colon_spaces, contains_skip, mk_sp, last_line_extendable, paren_overhead};
25-
use visitor::FmtVisitor;
26-
use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
27-
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
28-
use types::{rewrite_path, PathContext, can_be_overflowed_type};
29-
use items::{span_lo_for_arg, span_hi_for_arg};
3020
use chains::rewrite_chain;
21+
use codemap::SpanUtils;
22+
use comment::{contains_comment, recover_comment_removed, rewrite_comment, FindUncommented};
23+
use config::{Config, ControlBraceStyle, IndentStyle, MultilineStyle, Style};
24+
use items::{span_hi_for_arg, span_lo_for_arg};
25+
use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
26+
struct_lit_shape, struct_lit_tactic, write_list, DefinitiveListTactic, ListFormatting,
27+
ListItem, ListTactic, SeparatorTactic};
3128
use macros::{rewrite_macro, MacroPosition};
32-
use patterns::{TuplePatField, can_be_overflowed_pat};
29+
use patterns::{can_be_overflowed_pat, TuplePatField};
30+
use rewrite::{Rewrite, RewriteContext};
31+
use string::{rewrite_string, StringFormat};
32+
use types::{can_be_overflowed_type, rewrite_path, PathContext};
33+
use utils::{binary_search, colon_spaces, contains_skip, extra_offset, first_line_width,
34+
last_line_extendable, last_line_width, left_most_sub_expr, mk_sp, paren_overhead,
35+
semicolon_for_stmt, stmt_expr, trimmed_last_line_width, wrap_str};
3336
use vertical::rewrite_with_alignment;
34-
35-
use syntax::{ast, ptr};
36-
use syntax::codemap::{CodeMap, Span, BytePos};
37-
use syntax::parse::classify;
37+
use visitor::FmtVisitor;
3838

3939
impl Rewrite for ast::Expr {
4040
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
@@ -501,10 +501,12 @@ where
501501
DefinitiveListTactic::Mixed
502502
},
503503
};
504+
let mut ends_with_newline = tactic.ends_with_newline(context.config.array_layout());
504505
if context.config.array_horizontal_layout_threshold() > 0 &&
505506
items.len() > context.config.array_horizontal_layout_threshold()
506507
{
507508
tactic = DefinitiveListTactic::Mixed;
509+
ends_with_newline = false;
508510
if context.config.array_layout() == IndentStyle::Block {
509511
nested_shape = try_opt!(
510512
shape
@@ -525,7 +527,7 @@ where
525527
SeparatorTactic::Vertical
526528
},
527529
shape: nested_shape,
528-
ends_with_newline: false,
530+
ends_with_newline: ends_with_newline,
529531
config: context.config,
530532
};
531533
let list_str = try_opt!(write_list(&items, &fmt));

src/filemap.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@
1111

1212
// TODO: add tests
1313

14-
use strings::string_buffer::StringBuffer;
15-
1614
use std::fs::{self, File};
17-
use std::io::{self, Write, Read, BufWriter};
15+
use std::io::{self, BufWriter, Read, Write};
16+
17+
use strings::string_buffer::StringBuffer;
1818

19-
use config::{NewlineStyle, Config, WriteMode};
19+
use checkstyle::{output_checkstyle_file, output_footer, output_header};
20+
use config::{Config, NewlineStyle, WriteMode};
2021
use rustfmt_diff::{make_diff, print_diff, Mismatch};
21-
use checkstyle::{output_header, output_footer, output_checkstyle_file};
2222

2323
// A map of the files of a crate, with their new content
2424
pub type FileMap = Vec<FileRecord>;

0 commit comments

Comments
 (0)