Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not vertically align list items in case the tactic is Horizontal #3093

Merged
merged 2 commits into from
Oct 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/comment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<_>>()
Expand Down
18 changes: 16 additions & 2 deletions src/vertical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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(),
"}",
Expand All @@ -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())
Expand Down
10 changes: 10 additions & 0 deletions tests/source/wrapped_hidden_code_block.rs
Original file line number Diff line number Diff line change
@@ -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() {}
13 changes: 13 additions & 0 deletions tests/target/issue-2633.rs
Original file line number Diff line number Diff line change
@@ -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: () },
}
13 changes: 13 additions & 0 deletions tests/target/wrapped_hidden_code_block.rs
Original file line number Diff line number Diff line change
@@ -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() {}