From 78d9091afff88464a32d14508cd4671e8d419e7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Fri, 12 Oct 2018 20:41:56 +0200 Subject: [PATCH 1/2] do not vertically align list items in case the tactic is Horizontal --- src/vertical.rs | 18 ++++++++++++++++-- tests/target/issue-2633.rs | 13 +++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 tests/target/issue-2633.rs diff --git a/src/vertical.rs b/src/vertical.rs index d18fa4b9b75..dbb3e732d62 100644 --- a/src/vertical.rs +++ b/src/vertical.rs @@ -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( 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( 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 { 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()) diff --git a/tests/target/issue-2633.rs b/tests/target/issue-2633.rs new file mode 100644 index 00000000000..a381945fd8b --- /dev/null +++ b/tests/target/issue-2633.rs @@ -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: () }, +} From 8f7a0470b073803317e85e306308a4b9457dc5cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Campinas?= Date: Sat, 13 Oct 2018 09:57:43 +0200 Subject: [PATCH 2/2] handle lines prefixed with a # inside code blocks --- src/comment.rs | 11 +++++++++-- tests/source/wrapped_hidden_code_block.rs | 10 ++++++++++ tests/target/wrapped_hidden_code_block.rs | 13 +++++++++++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 tests/source/wrapped_hidden_code_block.rs create mode 100644 tests/target/wrapped_hidden_code_block.rs diff --git a/src/comment.rs b/src/comment.rs index 213879e50a1..c946cc6e1c0 100644 --- a/src/comment.rs +++ b/src/comment.rs @@ -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::>() diff --git a/tests/source/wrapped_hidden_code_block.rs b/tests/source/wrapped_hidden_code_block.rs new file mode 100644 index 00000000000..d8de8443812 --- /dev/null +++ b/tests/source/wrapped_hidden_code_block.rs @@ -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() {} diff --git a/tests/target/wrapped_hidden_code_block.rs b/tests/target/wrapped_hidden_code_block.rs new file mode 100644 index 00000000000..572f448c4e7 --- /dev/null +++ b/tests/target/wrapped_hidden_code_block.rs @@ -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() {}