Skip to content

Commit 3bf6f97

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.
1 parent 7a3b7c9 commit 3bf6f97

File tree

7 files changed

+75
-2
lines changed

7 files changed

+75
-2
lines changed

src/config/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ create_config! {
145145
emit_mode: EmitMode, EmitMode::Files, false,
146146
"What emit Mode to use when none is supplied";
147147
make_backup: bool, false, false, "Backup changed files";
148+
inline_attribute_width: usize, 0, false,
149+
"Attributes is in line when line width is within inline_attribute_width";
148150
}
149151

150152
/// Loads a config by checking the client-supplied options and if appropriate, the

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.clone().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
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
inline_attribute_width = 50

tests/source/issue-3343/one.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-version: One
2+
// rustfmt-config: inline_attribute_width.toml
3+
4+
#[cfg(feature = "alloc")]
5+
use core::slice;
6+
7+
#[cfg(feature = "alloc")]
8+
use total_len_is::_50__;
9+
10+
#[cfg(feature = "alloc")]
11+
use total_len_is::_51___;
12+
13+
/// comment
14+
use core::convert;

tests/source/issue-3343/two.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-version: Two
2+
// rustfmt-config: inline_attribute_width.toml
3+
4+
#[cfg(feature = "alloc")]
5+
use core::slice;
6+
7+
#[cfg(feature = "alloc")]
8+
use total_len_is::_50__;
9+
10+
#[cfg(feature = "alloc")]
11+
use total_len_is::_51___;
12+
13+
/// comment
14+
use core::convert;

tests/target/issue-3343/one.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// rustfmt-version: One
2+
// rustfmt-config: inline_attribute_width.toml
3+
4+
#[cfg(feature = "alloc")]
5+
use core::slice;
6+
7+
#[cfg(feature = "alloc")]
8+
use total_len_is::_50__;
9+
10+
#[cfg(feature = "alloc")]
11+
use total_len_is::_51___;
12+
13+
/// comment
14+
use core::convert;

tests/target/issue-3343/two.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// rustfmt-version: Two
2+
// rustfmt-config: inline_attribute_width.toml
3+
4+
#[cfg(feature = "alloc")] use core::slice;
5+
6+
#[cfg(feature = "alloc")] use total_len_is::_50__;
7+
8+
#[cfg(feature = "alloc")]
9+
use total_len_is::_51___;
10+
11+
/// comment
12+
use core::convert;

0 commit comments

Comments
 (0)