Skip to content

Consistent format of struct post-comments in new line - with and without trailing comma #5607

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
1 change: 1 addition & 0 deletions src/attr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ fn format_derive(
context.snippet_provider.span_after(attr.span, "("),
attr.span.hi(),
false,
context.config.version(),
);

Some(items)
Expand Down
1 change: 1 addition & 0 deletions src/closures.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ fn rewrite_closure_fn_decl(
context.snippet_provider.span_after(span, "|"),
body.span.lo(),
false,
context.config.version(),
);
let item_vec = param_items.collect::<Vec<_>>();
// 1 = space between parameters and return type.
Expand Down
2 changes: 2 additions & 0 deletions src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1623,6 +1623,7 @@ fn rewrite_struct_lit<'a>(
body_lo,
span.hi(),
false,
context.config.version(),
);
let item_vec = items.collect::<Vec<_>>();

Expand Down Expand Up @@ -1773,6 +1774,7 @@ fn rewrite_tuple_in_visual_indent_style<'a, T: 'a + IntoOverflowableItem<'a>>(
list_lo,
span.hi() - BytePos(1),
false,
context.config.version(),
);
let item_vec: Vec<_> = items.collect();
let tactic = definitive_tactic(
Expand Down
1 change: 1 addition & 0 deletions src/imports.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,7 @@ impl UseTree {
context.snippet_provider.span_after(a.span, "{"),
a.span.hi(),
false,
context.config.version(),
);

// in case of a global path and the nested list starts at the root,
Expand Down
16 changes: 13 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,8 +381,8 @@ impl<'a> FmtVisitor<'a> {
return None;
}

let res = Stmt::from_ast_node(block.stmts.first()?, true)
.rewrite(&self.get_context(), self.shape())?;
let res =
Stmt::from_ast_node(block.stmts.first()?, true).rewrite(&context, self.shape())?;

let width = self.block_indent.width() + fn_str.len() + res.len() + 5;
if !res.contains('\n') && width <= self.config.max_width() {
Expand Down Expand Up @@ -441,7 +441,12 @@ impl<'a> FmtVisitor<'a> {

self.last_pos = body_start;

match self.format_variant_list(enum_def, body_start, span.hi()) {
match self.format_variant_list(
enum_def,
body_start,
span.hi(),
self.get_context().config.version(),
) {
Some(ref s) if enum_def.variants.is_empty() => self.push_str(s),
rw => {
self.push_rewrite(mk_sp(body_start, span.hi()), rw);
Expand All @@ -456,6 +461,7 @@ impl<'a> FmtVisitor<'a> {
enum_def: &ast::EnumDef,
body_lo: BytePos,
body_hi: BytePos,
version: Version,
) -> Option<String> {
if enum_def.variants.is_empty() {
let mut buffer = String::with_capacity(128);
Expand Down Expand Up @@ -510,6 +516,7 @@ impl<'a> FmtVisitor<'a> {
body_lo,
body_hi,
false,
version,
)
.collect()
};
Expand Down Expand Up @@ -2599,6 +2606,7 @@ fn rewrite_params(
span.lo(),
span.hi(),
false,
context.config.version(),
)
.collect();

Expand Down Expand Up @@ -2868,6 +2876,7 @@ fn rewrite_bounds_on_where_clause(
span_start,
span_end,
false,
context.config.version(),
);
let comma_tactic = if where_clause_option.suppress_comma || force_single_line {
SeparatorTactic::Never
Expand Down Expand Up @@ -2947,6 +2956,7 @@ fn rewrite_where_clause(
span_start,
span_end,
false,
context.config.version(),
);
let item_vec = items.collect::<Vec<_>>();
// FIXME: we don't need to collect here
Expand Down
30 changes: 26 additions & 4 deletions src/lists.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use rustc_span::BytePos;

use crate::comment::{find_comment_end, rewrite_comment, FindUncommented};
use crate::config::lists::*;
use crate::config::{Config, IndentStyle};
use crate::config::{Config, IndentStyle, Version};
use crate::rewrite::RewriteContext;
use crate::shape::{Indent, Shape};
use crate::utils::{
Expand Down Expand Up @@ -570,6 +570,7 @@ where
terminator: &'a str,
separator: &'a str,
leave_last: bool,
version: Version,
}

pub(crate) fn extract_pre_comment(pre_snippet: &str) -> (Option<String>, ListItemCommentStyle) {
Expand Down Expand Up @@ -612,11 +613,25 @@ pub(crate) fn extract_post_comment(
comment_end: usize,
separator: &str,
is_last: bool,
version: Version,
) -> Option<String> {
let white_space: &[_] = &[' ', '\t'];

// Cleanup post-comment: strip separators and whitespace.
let post_snippet = post_snippet[..comment_end].trim();
let post_snippet = if version == Version::One {
post_snippet[..comment_end].trim()
} else {
// If trimmed string starts with comment - preserve leading new-line.
let post_snippet_start_trimmed = post_snippet[..comment_end].trim_start();
if post_snippet_start_trimmed.starts_with("//")
|| post_snippet_start_trimmed.starts_with("/*")
{
post_snippet[..comment_end].trim_matches(white_space)
} else {
post_snippet_start_trimmed
}
.trim_end()
};

let last_inline_comment_ends_with_separator = if is_last {
if let Some(line) = post_snippet.lines().last() {
Expand Down Expand Up @@ -767,8 +782,13 @@ where
let comment_end =
get_comment_end(post_snippet, self.separator, self.terminator, is_last);
let new_lines = has_extra_newline(post_snippet, comment_end);
let post_comment =
extract_post_comment(post_snippet, comment_end, self.separator, is_last);
let post_comment = extract_post_comment(
post_snippet,
comment_end,
self.separator,
is_last,
self.version,
);

self.prev_span_end = (self.get_hi)(&item) + BytePos(comment_end as u32);

Expand Down Expand Up @@ -800,6 +820,7 @@ pub(crate) fn itemize_list<'a, T, I, F1, F2, F3>(
prev_span_end: BytePos,
next_span_start: BytePos,
leave_last: bool,
version: Version,
) -> ListItems<'a, I, F1, F2, F3>
where
I: Iterator<Item = T>,
Expand All @@ -818,6 +839,7 @@ where
terminator,
separator,
leave_last,
version,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,7 @@ pub(crate) fn rewrite_macro_def(
context.snippet_provider.span_after(span, "{"),
span.hi(),
false,
context.config.version(),
)
.collect::<Vec<_>>();

Expand Down
1 change: 1 addition & 0 deletions src/matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ fn rewrite_match_arms(
open_brace_pos,
span.hi(),
false,
context.config.version(),
);
let arms_vec: Vec<_> = items.collect();
// We will add/remove commas inside `arm.rewrite()`, and hence no separator here.
Expand Down
5 changes: 3 additions & 2 deletions src/overflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ impl<'a> Context<'a> {
tactic
}

fn rewrite_items(&self) -> Option<(bool, String)> {
fn rewrite_items(&self, version: Version) -> Option<(bool, String)> {
let span = self.items_span();
let items = itemize_list(
self.context.snippet_provider,
Expand All @@ -600,6 +600,7 @@ impl<'a> Context<'a> {
span.lo(),
span.hi(),
true,
version,
);
let mut list_items: Vec<_> = items.collect();

Expand Down Expand Up @@ -682,7 +683,7 @@ impl<'a> Context<'a> {
}

fn rewrite(&self, shape: Shape) -> Option<String> {
let (extendable, items_str) = self.rewrite_items()?;
let (extendable, items_str) = self.rewrite_items(self.context.config.version())?;

// If we are using visual indent style and failed to format, retry with block indent.
if !self.context.use_block_indent()
Expand Down
2 changes: 2 additions & 0 deletions src/patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,7 @@ fn rewrite_struct_pat(
context.snippet_provider.span_after(span, "{"),
span.hi(),
false,
context.config.version(),
);
let item_vec = items.collect::<Vec<_>>();

Expand Down Expand Up @@ -516,6 +517,7 @@ fn count_wildcard_suffix_len(
context.snippet_provider.span_after(span, "("),
span.hi() - BytePos(1),
false,
context.config.version(),
)
.collect();

Expand Down
2 changes: 2 additions & 0 deletions src/reorder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ fn rewrite_reorderable_or_regroupable_items(
span.lo(),
span.hi(),
false,
context.config.version(),
);
for (item, list_item) in normalized_items.iter_mut().zip(list_items) {
item.list_item = Some(list_item.clone());
Expand Down Expand Up @@ -155,6 +156,7 @@ fn rewrite_reorderable_or_regroupable_items(
span.lo(),
span.hi(),
false,
context.config.version(),
);

let mut item_pair_vec: Vec<_> = list_items.zip(reorderable_items.iter()).collect();
Expand Down
1 change: 1 addition & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,7 @@ where
list_lo,
span.hi(),
false,
context.config.version(),
);

let item_vec: Vec<_> = items.collect();
Expand Down
1 change: 1 addition & 0 deletions src/vertical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ fn rewrite_aligned_items_inner<T: AlignedItem>(
span.lo(),
span.hi(),
false,
context.config.version(),
)
.collect::<Vec<_>>();

Expand Down
Loading