Skip to content

add config inline_attribute_width #3409

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

Merged
merged 3 commits into from
Feb 23, 2019
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
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;