Skip to content

Commit

Permalink
add config inline_attribute_width
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rchaser53 committed Feb 23, 2019
1 parent 7a3b7c9 commit 0e408bf
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
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
20 changes: 18 additions & 2 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use syntax::source_map::{self, BytePos, Span, DUMMY_SP};

use crate::comment::combine_strs_with_missing_comments;
use crate::config::lists::*;
use crate::config::{Edition, IndentStyle};
use crate::config::{Edition, IndentStyle, Version};
use crate::lists::{
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
};
Expand Down 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 context.config.version() == Version::Two {
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
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 0e408bf

Please sign in to comment.