Skip to content

Commit

Permalink
Merge pull request rust-lang#3409 from rchaser53/issue-3343
Browse files Browse the repository at this point in the history
add config inline_attribute_width
  • Loading branch information
scampi authored Feb 23, 2019
2 parents 29a597c + be7b3ba commit 6a75fee
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 2 deletions.
21 changes: 21 additions & 0 deletions Configurations.md
Original file line number Diff line number Diff line change
Expand Up @@ -2408,6 +2408,27 @@ pub enum Foo {}
pub enum Foo {}
```

## `inline_attribute_width`

Write an item and its attribute on the same line if their combined width is below a threshold

- **Default value**: 0
- **Possible values**: any positive integer
- **Stable**: No (tracking issue: #3343)

### Example

#### `0` (default):
```rust
#[cfg(feature = "alloc")]
use core::slice;
```

#### `50`:
```rust
#[cfg(feature = "alloc")] use core::slice;
```

## `emit_mode`

Internal option
Expand Down
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ create_config! {
"Minimum number of blank lines which must be put between items";
edition: Edition, Edition::Edition2015, true, "The edition of the parser (RFC 2052)";
version: Version, Version::One, false, "Version of formatting rules";
inline_attribute_width: usize, 0, false,
"Write an item and its attribute on the same line \
if their combined width is below a threshold";

// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one";
Expand Down
18 changes: 17 additions & 1 deletion src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,23 @@ impl UseTree {
let lo = attrs.last().as_ref()?.span().hi();
let hi = self.span.lo();
let span = mk_sp(lo, hi);
combine_strs_with_missing_comments(context, &attr_str, &use_str, span, shape, false)

let allow_extend = if attrs.len() == 1 {
let line_len = attr_str.len() + 1 + use_str.len();
!attrs.first().unwrap().is_sugared_doc
&& context.config.inline_attribute_width() >= line_len
} else {
false
};

combine_strs_with_missing_comments(
context,
&attr_str,
&use_str,
span,
shape,
allow_extend,
)
} else {
Some(use_str)
}
Expand Down
17 changes: 16 additions & 1 deletion src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,22 @@ fn rewrite_reorderable_item(
_ => return None,
};

combine_strs_with_missing_comments(context, &attrs_str, &item_str, missed_span, shape, false)
let allow_extend = if attrs.len() == 1 {
let line_len = attrs_str.len() + 1 + item_str.len();
!attrs.first().unwrap().is_sugared_doc
&& context.config.inline_attribute_width() >= line_len
} else {
false
};

combine_strs_with_missing_comments(
context,
&attrs_str,
&item_str,
missed_span,
shape,
allow_extend,
)
}

/// Rewrite a list of items with reordering. Every item in `items` must have
Expand Down
48 changes: 48 additions & 0 deletions tests/source/issue-3343.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// rustfmt-inline_attribute_width: 50

#[cfg(feature = "alloc")]
use core::slice;

#[cfg(feature = "alloc")]
use total_len_is::_50__;

#[cfg(feature = "alloc")]
use total_len_is::_51___;

#[cfg(feature = "alloc")]
extern crate len_is_50_;

#[cfg(feature = "alloc")]
extern crate len_is_51__;

/// this is a comment to test is_sugared_doc property
use core::convert;

#[fooooo]
#[barrrrr]
use total_len_is_::_51______;

#[cfg(not(all(
feature = "std",
any(
target_os = "linux",
target_os = "android",
target_os = "netbsd",
target_os = "dragonfly",
target_os = "haiku",
target_os = "emscripten",
target_os = "solaris",
target_os = "cloudabi",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "openbsd",
target_os = "bitrig",
target_os = "redox",
target_os = "fuchsia",
windows,
all(target_arch = "wasm32", feature = "stdweb"),
all(target_arch = "wasm32", feature = "wasm-bindgen"),
)
)))]
use core::slice;
45 changes: 45 additions & 0 deletions tests/target/issue-3343.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// rustfmt-inline_attribute_width: 50

#[cfg(feature = "alloc")] use core::slice;

#[cfg(feature = "alloc")] use total_len_is::_50__;

#[cfg(feature = "alloc")]
use total_len_is::_51___;

#[cfg(feature = "alloc")] extern crate len_is_50_;

#[cfg(feature = "alloc")]
extern crate len_is_51__;

/// this is a comment to test is_sugared_doc property
use core::convert;

#[fooooo]
#[barrrrr]
use total_len_is_::_51______;

#[cfg(not(all(
feature = "std",
any(
target_os = "linux",
target_os = "android",
target_os = "netbsd",
target_os = "dragonfly",
target_os = "haiku",
target_os = "emscripten",
target_os = "solaris",
target_os = "cloudabi",
target_os = "macos",
target_os = "ios",
target_os = "freebsd",
target_os = "openbsd",
target_os = "bitrig",
target_os = "redox",
target_os = "fuchsia",
windows,
all(target_arch = "wasm32", feature = "stdweb"),
all(target_arch = "wasm32", feature = "wasm-bindgen"),
)
)))]
use core::slice;

0 comments on commit 6a75fee

Please sign in to comment.