Skip to content

Commit c593229

Browse files
authored
Merge pull request #2535 from nrc/import-ord
Import ordering
2 parents 5516223 + 3a138a2 commit c593229

30 files changed

+537
-174
lines changed

Configurations.md

+14-14
Original file line numberDiff line numberDiff line change
@@ -1297,28 +1297,28 @@ Reorder import statements in group
12971297

12981298
**Note:** This option takes effect only when [`reorder_imports`](#reorder_imports) is set to `true`.
12991299

1300-
#### `false` (default):
1300+
#### `true` (default):
13011301

13021302
```rust
1303-
use std::mem;
13041303
use std::io;
1304+
use std::mem;
13051305

1306-
use lorem;
1307-
use ipsum;
13081306
use dolor;
1307+
use ipsum;
1308+
use lorem;
13091309
use sit;
13101310
```
13111311

1312-
#### `true`:
1312+
#### `false`:
13131313

1314-
```rust
1315-
use std::io;
1316-
use std::mem;
13171314

1315+
```rust
13181316
use dolor;
13191317
use ipsum;
13201318
use lorem;
13211319
use sit;
1320+
use std::io;
1321+
use std::mem;
13221322
```
13231323

13241324
See also [`reorder_imports`](#reorder_imports).
@@ -1360,7 +1360,11 @@ Reorder `extern crate` statements in group
13601360
- **Possible values**: `true`, `false`
13611361
- **Stable**: No
13621362

1363-
#### `true` (default):
1363+
#### `false` (default):
1364+
1365+
This value has no influence beyond the effect of the [`reorder_extern_crates`](#reorder_extern_crates) option. Set [`reorder_extern_crates`](#reorder_extern_crates) to `false` if you do not want `extern crate` groups to be collapsed and ordered.
1366+
1367+
#### `true`:
13641368

13651369
**Note:** This only takes effect when [`reorder_extern_crates`](#reorder_extern_crates) is set to `true`.
13661370

@@ -1374,10 +1378,6 @@ extern crate lorem;
13741378
extern crate sit;
13751379
```
13761380

1377-
#### `false`:
1378-
1379-
This value has no influence beyond the effect of the [`reorder_extern_crates`](#reorder_extern_crates) option. Set [`reorder_extern_crates`](#reorder_extern_crates) to `false` if you do not want `extern crate` groups to be collapsed and ordered.
1380-
13811381
## `reorder_modules`
13821382

13831383
Reorder `mod` declarations alphabetically in group.
@@ -1386,7 +1386,7 @@ Reorder `mod` declarations alphabetically in group.
13861386
- **Possible values**: `true`, `false`
13871387
- **Stable**: No
13881388

1389-
#### `true`
1389+
#### `true` (default)
13901390

13911391
```rust
13921392
mod a;

bootstrap.sh

+2-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@
77
cargo build --release
88

99
target/release/rustfmt --write-mode=overwrite src/lib.rs
10-
target/release/rustfmt --write-mode=overwrite src/bin/rustfmt.rs
11-
target/release/rustfmt --write-mode=overwrite src/bin/cargo-fmt.rs
12-
target/release/rustfmt --write-mode=overwrite tests/system.rs
10+
target/release/rustfmt --write-mode=overwrite src/bin/main.rs
11+
target/release/rustfmt --write-mode=overwrite src/cargo-fmt/main.rs
1312

1413
for filename in tests/target/*.rs; do
1514
if ! grep -q "rustfmt-" "$filename"; then

src/bin/main.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,16 @@ extern crate env_logger;
1414
extern crate getopts;
1515
extern crate rustfmt_nightly as rustfmt;
1616

17-
use std::{env, error};
1817
use std::fs::File;
1918
use std::io::{self, Read, Write};
2019
use std::path::{Path, PathBuf};
2120
use std::str::FromStr;
21+
use std::{env, error};
2222

2323
use getopts::{Matches, Options};
2424

25-
use rustfmt::config::{get_toml_path, Color, Config, WriteMode};
2625
use rustfmt::config::file_lines::FileLines;
26+
use rustfmt::config::{get_toml_path, Color, Config, WriteMode};
2727
use rustfmt::{run, FileName, Input, Summary};
2828

2929
type FmtError = Box<error::Error + Send + Sync>;

src/chains.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ use std::borrow::Cow;
7777
use std::cmp::min;
7878
use std::iter;
7979

80-
use syntax::{ast, ptr};
8180
use syntax::codemap::Span;
81+
use syntax::{ast, ptr};
8282

8383
pub fn rewrite_chain(expr: &ast::Expr, context: &RewriteContext, shape: Shape) -> Option<String> {
8484
debug!("rewrite_chain {:?}", shape);

src/closures.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
// except according to those terms.
1010

1111
use config::lists::*;
12-
use syntax::{ast, ptr};
1312
use syntax::codemap::Span;
1413
use syntax::parse::classify;
14+
use syntax::{ast, ptr};
1515

1616
use codemap::SpanUtils;
1717
use expr::{block_contains_comment, is_simple_block, is_unsafe_block, rewrite_cond, ToExpr};

src/codemap.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
//! This includes extension traits and methods for looking up spans and line ranges for AST nodes.
1313
1414
use config::file_lines::LineRange;
15-
use visitor::SnippetProvider;
1615
use syntax::codemap::{BytePos, CodeMap, Span};
16+
use visitor::SnippetProvider;
1717

1818
use comment::FindUncommented;
1919

src/config/file_lines.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010

1111
//! This module contains types and functions to support formatting specific line ranges.
1212
13-
use std::{cmp, iter, str};
1413
use std::collections::HashMap;
1514
use std::rc::Rc;
15+
use std::{cmp, iter, str};
1616

1717
use serde::de::{Deserialize, Deserializer};
1818
use serde_json as json;

src/config/license.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use std::io;
21
use std::fmt;
32
use std::fs::File;
3+
use std::io;
44
use std::io::Read;
55

66
use regex;

src/config/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::{env, fs};
1211
use std::cell::Cell;
1312
use std::default::Default;
1413
use std::fs::File;
1514
use std::io::{Error, ErrorKind, Read};
1615
use std::path::{Path, PathBuf};
16+
use std::{env, fs};
1717

1818
use regex::Regex;
1919

@@ -23,9 +23,9 @@ mod config_type;
2323
mod options;
2424

2525
pub mod file_lines;
26+
pub mod license;
2627
pub mod lists;
2728
pub mod summary;
28-
pub mod license;
2929

3030
use config::config_type::ConfigType;
3131
use config::file_lines::FileLines;
@@ -70,11 +70,11 @@ create_config! {
7070
// Ordering
7171
reorder_extern_crates: bool, true, false, "Reorder extern crate statements alphabetically";
7272
reorder_extern_crates_in_group: bool, true, false, "Reorder extern crate statements in group";
73-
reorder_imports: bool, false, false, "Reorder import statements alphabetically";
74-
reorder_imports_in_group: bool, false, false, "Reorder import statements in group";
73+
reorder_imports: bool, true, false, "Reorder import statements alphabetically";
74+
reorder_imports_in_group: bool, true, false, "Reorder import statements in group";
7575
reorder_imported_names: bool, true, false,
7676
"Reorder lists of names in import statements alphabetically";
77-
reorder_modules: bool, false, false, "Reorder module statemtents alphabetically in group";
77+
reorder_modules: bool, true, false, "Reorder module statemtents alphabetically in group";
7878

7979
// Spaces around punctuation
8080
binop_separator: SeparatorPlace, SeparatorPlace::Front, false,

src/config/summary.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use std::time::{Duration, Instant};
1211
use std::default::Default;
12+
use std::time::{Duration, Instant};
1313

1414
#[must_use]
1515
#[derive(Debug, Default, Clone, Copy)]

src/expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use std::cmp::min;
1313
use std::iter::repeat;
1414

1515
use config::lists::*;
16-
use syntax::{ast, ptr};
1716
use syntax::codemap::{BytePos, CodeMap, Span};
17+
use syntax::{ast, ptr};
1818

1919
use chains::rewrite_chain;
2020
use closures;

src/format-diff/main.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ extern crate regex;
2323
extern crate serde_derive;
2424
extern crate serde_json as json;
2525

26-
use std::{env, fmt, process};
2726
use std::collections::HashSet;
2827
use std::error::Error;
2928
use std::io::{self, BufRead};
29+
use std::{env, fmt, process};
3030

3131
use regex::Regex;
3232

src/items.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ use std::cmp::min;
1515

1616
use config::lists::*;
1717
use regex::Regex;
18-
use syntax::{abi, ast, ptr, symbol};
1918
use syntax::codemap::{self, BytePos, Span};
2019
use syntax::visit;
20+
use syntax::{abi, ast, ptr, symbol};
2121

2222
use codemap::{LineRangeUtils, SpanUtils};
2323
use comment::{combine_strs_with_missing_comments, contains_comment, recover_comment_removed,
@@ -26,8 +26,8 @@ use config::{BraceStyle, Config, Density, IndentStyle};
2626
use expr::{format_expr, is_empty_block, is_simple_block_stmt, rewrite_assign_rhs,
2727
rewrite_assign_rhs_with, ExprType, RhsTactics};
2828
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
29-
use rewrite::{Rewrite, RewriteContext};
3029
use overflow;
30+
use rewrite::{Rewrite, RewriteContext};
3131
use shape::{Indent, Shape};
3232
use spanned::Spanned;
3333
use types::TraitTyParamBounds;

src/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ use std::path::PathBuf;
3838
use std::rc::Rc;
3939
use std::time::Duration;
4040

41-
use syntax::errors::{DiagnosticBuilder, Handler};
42-
use syntax::errors::emitter::{ColorConfig, EmitterWriter};
4341
use syntax::ast;
44-
use syntax::codemap::{CodeMap, FilePathMapping};
4542
pub use syntax::codemap::FileName;
43+
use syntax::codemap::{CodeMap, FilePathMapping};
44+
use syntax::errors::emitter::{ColorConfig, EmitterWriter};
45+
use syntax::errors::{DiagnosticBuilder, Handler};
4646
use syntax::parse::{self, ParseSess};
4747

4848
use checkstyle::{output_footer, output_header};

src/lists.rs

+38-19
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl AsRef<ListItem> for ListItem {
5656
}
5757
}
5858

59-
#[derive(PartialEq, Eq)]
59+
#[derive(PartialEq, Eq, Debug)]
6060
pub enum ListItemCommentStyle {
6161
// Try to keep the comment on the same line with the item.
6262
SameLine,
@@ -66,6 +66,7 @@ pub enum ListItemCommentStyle {
6666
None,
6767
}
6868

69+
#[derive(Debug)]
6970
pub struct ListItem {
7071
// None for comments mean that they are not present.
7172
pub pre_comment: Option<String>,
@@ -118,6 +119,18 @@ impl ListItem {
118119
new_lines: false,
119120
}
120121
}
122+
123+
// true if the item causes something to be written.
124+
fn is_substantial(&self) -> bool {
125+
fn empty(s: &Option<String>) -> bool {
126+
match *s {
127+
Some(ref s) if !s.is_empty() => false,
128+
_ => true,
129+
}
130+
}
131+
132+
!(empty(&self.pre_comment) && empty(&self.item) && empty(&self.post_comment))
133+
}
121134
}
122135

123136
/// The type of separator for lists.
@@ -220,6 +233,10 @@ where
220233
item_last_line_width -= indent_str.len();
221234
}
222235

236+
if !item.is_substantial() {
237+
continue;
238+
}
239+
223240
match tactic {
224241
DefinitiveListTactic::Horizontal if !first => {
225242
result.push(' ');
@@ -276,26 +293,28 @@ where
276293
rewrite_comment(comment, block_mode, formatting.shape, formatting.config)?;
277294
result.push_str(&comment);
278295

279-
if tactic == DefinitiveListTactic::Vertical {
280-
// We cannot keep pre-comments on the same line if the comment if normalized.
281-
let keep_comment = if formatting.config.normalize_comments()
282-
|| item.pre_comment_style == ListItemCommentStyle::DifferentLine
283-
{
284-
false
296+
if !inner_item.is_empty() {
297+
if tactic == DefinitiveListTactic::Vertical {
298+
// We cannot keep pre-comments on the same line if the comment if normalized.
299+
let keep_comment = if formatting.config.normalize_comments()
300+
|| item.pre_comment_style == ListItemCommentStyle::DifferentLine
301+
{
302+
false
303+
} else {
304+
// We will try to keep the comment on the same line with the item here.
305+
// 1 = ` `
306+
let total_width = total_item_width(item) + item_sep_len + 1;
307+
total_width <= formatting.shape.width
308+
};
309+
if keep_comment {
310+
result.push(' ');
311+
} else {
312+
result.push('\n');
313+
result.push_str(indent_str);
314+
}
285315
} else {
286-
// We will try to keep the comment on the same line with the item here.
287-
// 1 = ` `
288-
let total_width = total_item_width(item) + item_sep_len + 1;
289-
total_width <= formatting.shape.width
290-
};
291-
if keep_comment {
292316
result.push(' ');
293-
} else {
294-
result.push('\n');
295-
result.push_str(indent_str);
296317
}
297-
} else {
298-
result.push(' ');
299318
}
300319
item_max_width = None;
301320
}
@@ -304,7 +323,7 @@ where
304323
result.push_str(formatting.separator.trim());
305324
result.push(' ');
306325
}
307-
result.push_str(&inner_item[..]);
326+
result.push_str(inner_item);
308327

309328
// Post-comments
310329
if tactic != DefinitiveListTactic::Vertical && item.post_comment.is_some() {

src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
use std::collections::HashMap;
2323

2424
use config::lists::*;
25-
use syntax::{ast, ptr};
2625
use syntax::codemap::{BytePos, Span};
2726
use syntax::parse::new_parser_from_tts;
2827
use syntax::parse::parser::Parser;
@@ -31,6 +30,7 @@ use syntax::print::pprust;
3130
use syntax::symbol;
3231
use syntax::tokenstream::{Cursor, ThinTokenStream, TokenStream, TokenTree};
3332
use syntax::util::ThinVec;
33+
use syntax::{ast, ptr};
3434

3535
use codemap::SpanUtils;
3636
use comment::{contains_comment, remove_trailing_white_spaces, CharClasses, FindUncommented,

0 commit comments

Comments
 (0)