Merge pull request #3093 from scampi/issue2633

do not vertically align list items in case the tactic is Horizontal
This commit is contained in:
Nick Cameron 2018-10-15 07:57:39 +12:00 committed by GitHub
commit 2f8c1fea72
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 61 additions and 4 deletions

View File

@ -752,9 +752,16 @@ fn trim_custom_comment_prefix(s: &str) -> String {
.map(|line| {
let left_trimmed = line.trim_left();
if left_trimmed.starts_with(RUSTFMT_CUSTOM_COMMENT_PREFIX) {
left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX)
let orig = left_trimmed.trim_left_matches(RUSTFMT_CUSTOM_COMMENT_PREFIX);
// due to comment wrapping, a line that was originaly behind `#` is split over
// multiple lines, which needs then to be prefixed with a `#`
if !orig.trim_left().starts_with("# ") {
format!("# {}", orig)
} else {
orig.to_string()
}
} else {
line
line.to_string()
}
})
.collect::<Vec<_>>()

View File

@ -19,7 +19,7 @@ use syntax::source_map::{BytePos, Span};
use comment::{combine_strs_with_missing_comments, contains_comment};
use expr::rewrite_field;
use items::{rewrite_struct_field, rewrite_struct_field_prefix};
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
use lists::{definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator};
use rewrite::{Rewrite, RewriteContext};
use shape::{Indent, Shape};
use source_map::SpanUtils;
@ -227,7 +227,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
field_prefix_max_width = 0;
}
let items = itemize_list(
let mut items = itemize_list(
context.snippet_provider,
fields.iter(),
"}",
@ -248,6 +248,20 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
one_line_width,
);
if tactic == DefinitiveListTactic::Horizontal {
// since the items fits on a line, there is no need to align them
let do_rewrite =
|field: &T| -> Option<String> { field.rewrite_aligned_item(context, item_shape, 0) };
fields
.iter()
.zip(items.iter_mut())
.for_each(|(field, list_item): (&T, &mut ListItem)| {
if list_item.item.is_some() {
list_item.item = do_rewrite(field);
}
});
}
let fmt = ListFormatting::new(item_shape, context.config)
.tactic(tactic)
.trailing_separator(context.config.trailing_comma())

View File

@ -0,0 +1,10 @@
// rustfmt-max_width: 79
// rustfmt-wrap_comments: true
/// ```rust
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature, stdsimd)not(dox), feature(cfg_target_feature, target_feature, stdsimd))]
///
/// // Est lectus hendrerit lorem, eget dignissim orci nisl sit amet massa. Etiam volutpat lobortis eros.
/// let x = 42;
/// ```
fn func() {}

View File

@ -0,0 +1,13 @@
// rustfmt-struct_field_align_threshold: 5
#[derive(Fail, Debug, Clone)]
pub enum BuildError {
LineTooLong { length: usize, limit: usize },
DisallowedByte { b: u8, pos: usize },
ContainsNewLine { pos: usize },
}
enum Foo {
A { a: usize, bbbbb: () },
B { a: (), bbbbb: () },
}

View File

@ -0,0 +1,13 @@
// rustfmt-max_width: 79
// rustfmt-wrap_comments: true
/// ```rust
/// # #![cfg_attr(not(dox), feature(cfg_target_feature, target_feature,
/// # stdsimd)not(dox), feature(cfg_target_feature, target_feature,
/// # stdsimd))]
///
/// // Est lectus hendrerit lorem, eget dignissim orci nisl sit amet massa.
/// // Etiam volutpat lobortis eros.
/// let x = 42;
/// ```
fn func() {}