Skip to content

Commit 0e408bf

Browse files
committed
add config inline_attribute_width
If the line width is width within config width, attribute is inline. I don't want to change default rustfmt behavior, so config default value is 0. - fix description - fix test comment - remove unnecessary clone - remove unnecessary test file - fix test for β version - attributes => attribute
1 parent 7a3b7c9 commit 0e408bf

File tree

4 files changed

+114
-2
lines changed

4 files changed

+114
-2
lines changed

src/config/mod.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,9 @@ create_config! {
105105
"Minimum number of blank lines which must be put between items";
106106
edition: Edition, Edition::Edition2015, true, "The edition of the parser (RFC 2052)";
107107
version: Version, Version::One, false, "Version of formatting rules";
108+
inline_attribute_width: usize, 0, false,
109+
"Write an item and its attribute on the same line \
110+
if their combined width is below a threshold";
108111

109112
// Options that can change the source code beyond whitespace/blocks (somewhat linty things)
110113
merge_derives: bool, true, true, "Merge multiple `#[derive(...)]` into a single one";

src/imports.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use syntax::source_map::{self, BytePos, Span, DUMMY_SP};
77

88
use crate::comment::combine_strs_with_missing_comments;
99
use crate::config::lists::*;
10-
use crate::config::{Edition, IndentStyle};
10+
use crate::config::{Edition, IndentStyle, Version};
1111
use crate::lists::{
1212
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
1313
};
@@ -249,7 +249,23 @@ impl UseTree {
249249
let lo = attrs.last().as_ref()?.span().hi();
250250
let hi = self.span.lo();
251251
let span = mk_sp(lo, hi);
252-
combine_strs_with_missing_comments(context, &attr_str, &use_str, span, shape, false)
252+
253+
let allow_extend = if context.config.version() == Version::Two {
254+
let line_len = attr_str.len() + 1 + use_str.len();
255+
!attrs.first().unwrap().is_sugared_doc
256+
&& context.config.inline_attribute_width() >= line_len
257+
} else {
258+
false
259+
};
260+
261+
combine_strs_with_missing_comments(
262+
context,
263+
&attr_str,
264+
&use_str,
265+
span,
266+
shape,
267+
allow_extend,
268+
)
253269
} else {
254270
Some(use_str)
255271
}

tests/source/issue-3343.rs

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// rustfmt-inline_attribute_width: 50
2+
3+
#[cfg(feature = "alloc")]
4+
use core::slice;
5+
6+
#[cfg(feature = "alloc")]
7+
use total_len_is::_50__;
8+
9+
#[cfg(feature = "alloc")]
10+
use total_len_is::_51___;
11+
12+
#[cfg(feature = "alloc")]
13+
extern crate len_is_50_;
14+
15+
#[cfg(feature = "alloc")]
16+
extern crate len_is_51__;
17+
18+
/// this is a comment to test is_sugared_doc property
19+
use core::convert;
20+
21+
#[fooooo]
22+
#[barrrrr]
23+
use total_len_is_::_51______;
24+
25+
#[cfg(not(all(
26+
feature = "std",
27+
any(
28+
target_os = "linux",
29+
target_os = "android",
30+
target_os = "netbsd",
31+
target_os = "dragonfly",
32+
target_os = "haiku",
33+
target_os = "emscripten",
34+
target_os = "solaris",
35+
target_os = "cloudabi",
36+
target_os = "macos",
37+
target_os = "ios",
38+
target_os = "freebsd",
39+
target_os = "openbsd",
40+
target_os = "bitrig",
41+
target_os = "redox",
42+
target_os = "fuchsia",
43+
windows,
44+
all(target_arch = "wasm32", feature = "stdweb"),
45+
all(target_arch = "wasm32", feature = "wasm-bindgen"),
46+
)
47+
)))]
48+
use core::slice;

tests/target/issue-3343.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// rustfmt-inline_attribute_width: 50
2+
3+
#[cfg(feature = "alloc")] use core::slice;
4+
5+
#[cfg(feature = "alloc")] use total_len_is::_50__;
6+
7+
#[cfg(feature = "alloc")]
8+
use total_len_is::_51___;
9+
10+
#[cfg(feature = "alloc")] extern crate len_is_50_;
11+
12+
#[cfg(feature = "alloc")]
13+
extern crate len_is_51__;
14+
15+
/// this is a comment to test is_sugared_doc property
16+
use core::convert;
17+
18+
#[fooooo]
19+
#[barrrrr]
20+
use total_len_is_::_51______;
21+
22+
#[cfg(not(all(
23+
feature = "std",
24+
any(
25+
target_os = "linux",
26+
target_os = "android",
27+
target_os = "netbsd",
28+
target_os = "dragonfly",
29+
target_os = "haiku",
30+
target_os = "emscripten",
31+
target_os = "solaris",
32+
target_os = "cloudabi",
33+
target_os = "macos",
34+
target_os = "ios",
35+
target_os = "freebsd",
36+
target_os = "openbsd",
37+
target_os = "bitrig",
38+
target_os = "redox",
39+
target_os = "fuchsia",
40+
windows,
41+
all(target_arch = "wasm32", feature = "stdweb"),
42+
all(target_arch = "wasm32", feature = "wasm-bindgen"),
43+
)
44+
)))]
45+
use core::slice;

0 commit comments

Comments
 (0)