From 0e408bf83ce26e086724e3ef8a55919588e420f1 Mon Sep 17 00:00:00 2001 From: rchaser53 Date: Tue, 19 Feb 2019 21:13:26 +0900 Subject: [PATCH] add config inline_attribute_width MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/config/mod.rs | 3 +++ src/imports.rs | 20 ++++++++++++++-- tests/source/issue-3343.rs | 48 ++++++++++++++++++++++++++++++++++++++ tests/target/issue-3343.rs | 45 +++++++++++++++++++++++++++++++++++ 4 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 tests/source/issue-3343.rs create mode 100644 tests/target/issue-3343.rs diff --git a/src/config/mod.rs b/src/config/mod.rs index 6c09e4b0b1a..4f78a105cdb 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -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"; diff --git a/src/imports.rs b/src/imports.rs index 43835c30e4b..0fa6dec2265 100644 --- a/src/imports.rs +++ b/src/imports.rs @@ -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, }; @@ -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) } diff --git a/tests/source/issue-3343.rs b/tests/source/issue-3343.rs new file mode 100644 index 00000000000..760d84dc3c3 --- /dev/null +++ b/tests/source/issue-3343.rs @@ -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; diff --git a/tests/target/issue-3343.rs b/tests/target/issue-3343.rs new file mode 100644 index 00000000000..4a1496ef364 --- /dev/null +++ b/tests/target/issue-3343.rs @@ -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;