Skip to content
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

Rfc: use block indent for multi-lined imports #1785

Merged
merged 6 commits into from
Jul 14, 2017
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
81 changes: 81 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,87 @@ match lorem {

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

## `imports_indent`

Indent style of imports

- **Default Value**: `"Visual"`
- **Possible values**: `"Block"`, `"Visual"`

#### `"Block"`

```rust
use foo::{
xxx,
yyy,
zzz,
};
```

#### `"Visual"`

```rust
use foo::{xxx,
yyy,
zzz};
```

See also: [`imports_layout`](#imports_layout).

## `imports_layout`

Item layout inside a imports block

- **Default value**: "Mixed"
- **Possible values**: "Horizontal", "HorizontalVertical", "Mixed", "Vertical"

#### `"Mixed"`

```rust
use foo::{xxx, yyy, zzz};

use foo::{aaa, bbb, ccc,
ddd, eee, fff};
```

#### `"Horizontal"`

**Note**: This option forces to put everything on one line and may exceeds `max_width`.

```rust
use foo::{xxx, yyy, zzz};

use foo::{aaa, bbb, ccc, ddd, eee, fff};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens when this gets too long to fit on one line?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When Horizontal layout is spcecified, rustfmt forces horitonzal layout and puts every thing on one line even if it exceeds max width.

```

#### `"HorizontalVertical"`

```rust
use foo::{xxx, yyy, zzz};

use foo::{aaa,
bbb,
ccc,
ddd,
eee,
fff};
```

#### `"Vertical"`

```rust
use foo::{xxx,
yyy,
zzz};

use foo::{aaa,
bbb,
ccc,
ddd,
eee,
fff};
```

## `item_brace_style`

Brace style for structs and enums
Expand Down
3 changes: 1 addition & 2 deletions src/bin/cargo-fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,9 @@ use std::str;
use std::collections::HashSet;
use std::iter::FromIterator;

use getopts::{Matches, Options};
use json::Value;

use getopts::{Options, Matches};

fn main() {
let exit_status = execute();
std::io::stdout().flush().unwrap();
Expand Down
8 changes: 4 additions & 4 deletions src/bin/rustfmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@ extern crate toml;
extern crate env_logger;
extern crate getopts;

use rustfmt::{run, Input, Summary};
use rustfmt::file_lines::FileLines;
use rustfmt::config::{Config, WriteMode, get_toml_path};

use std::{env, error};
use std::fs::File;
use std::io::{self, Read, Write};
Expand All @@ -29,6 +25,10 @@ use std::str::FromStr;

use getopts::{Matches, Options};

use rustfmt::{run, Input, Summary};
use rustfmt::file_lines::FileLines;
use rustfmt::config::{get_toml_path, Config, WriteMode};

type FmtError = Box<error::Error + Send + Sync>;
type FmtResult<T> = std::result::Result<T, FmtError>;

Expand Down
6 changes: 3 additions & 3 deletions src/chains.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,11 @@
/// ```

use Shape;
use rewrite::{Rewrite, RewriteContext};
use utils::{wrap_str, first_line_width, last_line_width, mk_sp, last_line_extendable};
use expr::rewrite_call;
use config::IndentStyle;
use expr::rewrite_call;
use macros::convert_try_mac;
use rewrite::{Rewrite, RewriteContext};
use utils::{first_line_width, last_line_extendable, last_line_width, mk_sp, wrap_str};

use std::cmp::min;
use std::iter;
Expand Down
5 changes: 3 additions & 2 deletions src/checkstyle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
use rustfmt_diff::{Mismatch, DiffLine};

use std::io::{self, Write};
use config::WriteMode;

use config::WriteMode;
use rustfmt_diff::{DiffLine, Mismatch};

pub fn output_header<T>(out: &mut T, mode: WriteMode) -> Result<(), io::Error>
where
Expand Down
6 changes: 3 additions & 3 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use syntax::codemap::Span;
use {Indent, Shape};
use config::Config;
use rewrite::RewriteContext;
use string::{StringFormat, rewrite_string};
use string::{rewrite_string, StringFormat};
use utils::wrap_str;

fn is_custom_comment(comment: &str) -> bool {
Expand Down Expand Up @@ -809,8 +809,8 @@ fn remove_comment_header(comment: &str) -> &str {

#[cfg(test)]
mod test {
use super::{CharClasses, CodeCharKind, FullCodeCharKind, contains_comment, rewrite_comment,
FindUncommented, CommentCodeSlices};
use super::{contains_comment, rewrite_comment, CharClasses, CodeCharKind, CommentCodeSlices,
FindUncommented, FullCodeCharKind};
use {Indent, Shape};

#[test]
Expand Down
9 changes: 5 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@

extern crate toml;

use std::{env, fs};
use std::cell::Cell;
use std::fs;
use std::fs::File;
use std::env;
use std::io::{Error, ErrorKind, Read};
use std::path::{Path, PathBuf};

use file_lines::FileLines;
use lists::{SeparatorTactic, ListTactic};
use lists::{ListTactic, SeparatorTactic};

macro_rules! configuration_option_enum{
($e:ident: $( $x:ident ),+ $(,)*) => {
Expand Down Expand Up @@ -557,9 +556,11 @@ create_config! {
chain_one_line_max: usize, 60, "Maximum length of a chain to fit on a single line";
chain_split_single_child: bool, false, "Split a chain with a single child if its length \
exceeds `chain_one_line_max`";
imports_indent: IndentStyle, IndentStyle::Visual, "Indent of imports";
imports_layout: ListTactic, ListTactic::Mixed, "Item layout inside a import block";
reorder_imports: bool, false, "Reorder import statements alphabetically";
reorder_imports_in_group: bool, false, "Reorder import statements in group";
reorder_imported_names: bool, false,
reorder_imported_names: bool, true,
"Reorder lists of names in import statements alphabetically";
single_line_if_else_max_width: usize, 50, "Maximum line length for single line if-else \
expressions. A value of zero means always break \
Expand Down
46 changes: 24 additions & 22 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,33 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::cmp::{Ordering, min};
use std::iter::ExactSizeIterator;
use std::cmp::{min, Ordering};
use std::fmt::Write;
use std::iter::ExactSizeIterator;

use syntax::{ast, ptr};
use syntax::codemap::{BytePos, CodeMap, Span};
use syntax::parse::classify;

use {Indent, Shape, Spanned};
use codemap::SpanUtils;
use rewrite::{Rewrite, RewriteContext};
use lists::{write_list, itemize_list, ListFormatting, SeparatorTactic, ListTactic,
DefinitiveListTactic, definitive_tactic, ListItem, struct_lit_shape,
struct_lit_tactic, shape_for_tactic, struct_lit_formatting};
use string::{StringFormat, rewrite_string};
use utils::{extra_offset, last_line_width, wrap_str, binary_search, first_line_width,
semicolon_for_stmt, trimmed_last_line_width, left_most_sub_expr, stmt_expr,
colon_spaces, contains_skip, mk_sp, last_line_extendable, paren_overhead};
use visitor::FmtVisitor;
use config::{Config, IndentStyle, MultilineStyle, ControlBraceStyle, Style};
use comment::{FindUncommented, rewrite_comment, contains_comment, recover_comment_removed};
use types::{rewrite_path, PathContext, can_be_overflowed_type};
use items::{span_lo_for_arg, span_hi_for_arg};
use chains::rewrite_chain;
use codemap::SpanUtils;
use comment::{contains_comment, recover_comment_removed, rewrite_comment, FindUncommented};
use config::{Config, ControlBraceStyle, IndentStyle, MultilineStyle, Style};
use items::{span_hi_for_arg, span_lo_for_arg};
use lists::{definitive_tactic, itemize_list, shape_for_tactic, struct_lit_formatting,
struct_lit_shape, struct_lit_tactic, write_list, DefinitiveListTactic, ListFormatting,
ListItem, ListTactic, SeparatorTactic};
use macros::{rewrite_macro, MacroPosition};
use patterns::{TuplePatField, can_be_overflowed_pat};
use patterns::{can_be_overflowed_pat, TuplePatField};
use rewrite::{Rewrite, RewriteContext};
use string::{rewrite_string, StringFormat};
use types::{can_be_overflowed_type, rewrite_path, PathContext};
use utils::{binary_search, colon_spaces, contains_skip, extra_offset, first_line_width,
last_line_extendable, last_line_width, left_most_sub_expr, mk_sp, paren_overhead,
semicolon_for_stmt, stmt_expr, trimmed_last_line_width, wrap_str};
use vertical::rewrite_with_alignment;

use syntax::{ast, ptr};
use syntax::codemap::{CodeMap, Span, BytePos};
use syntax::parse::classify;
use visitor::FmtVisitor;

impl Rewrite for ast::Expr {
fn rewrite(&self, context: &RewriteContext, shape: Shape) -> Option<String> {
Expand Down Expand Up @@ -501,10 +501,12 @@ where
DefinitiveListTactic::Mixed
},
};
let mut ends_with_newline = tactic.ends_with_newline(context.config.array_layout());
if context.config.array_horizontal_layout_threshold() > 0 &&
items.len() > context.config.array_horizontal_layout_threshold()
{
tactic = DefinitiveListTactic::Mixed;
ends_with_newline = false;
if context.config.array_layout() == IndentStyle::Block {
nested_shape = try_opt!(
shape
Expand All @@ -525,7 +527,7 @@ where
SeparatorTactic::Vertical
},
shape: nested_shape,
ends_with_newline: false,
ends_with_newline: ends_with_newline,
config: context.config,
};
let list_str = try_opt!(write_list(&items, &fmt));
Expand Down
10 changes: 5 additions & 5 deletions src/filemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@

// TODO: add tests

use strings::string_buffer::StringBuffer;

use std::fs::{self, File};
use std::io::{self, Write, Read, BufWriter};
use std::io::{self, BufWriter, Read, Write};

use strings::string_buffer::StringBuffer;

use config::{NewlineStyle, Config, WriteMode};
use checkstyle::{output_checkstyle_file, output_footer, output_header};
use config::{Config, NewlineStyle, WriteMode};
use rustfmt_diff::{make_diff, print_diff, Mismatch};
use checkstyle::{output_header, output_footer, output_checkstyle_file};

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