Skip to content

Commit 5f7e470

Browse files
committed
leave the comment in parentheses of argumentless Fn
1 parent b860fea commit 5f7e470

File tree

3 files changed

+120
-37
lines changed

3 files changed

+120
-37
lines changed

src/types.rs

Lines changed: 69 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@ use syntax::symbol::keywords;
88
use crate::config::lists::*;
99
use crate::config::{IndentStyle, TypeDensity};
1010
use crate::expr::{format_expr, rewrite_assign_rhs, rewrite_tuple, rewrite_unary_prefix, ExprType};
11-
use crate::lists::{definitive_tactic, itemize_list, write_list, ListFormatting, Separator};
11+
use crate::lists::{
12+
definitive_tactic, itemize_list, write_list, ListFormatting, ListItem, Separator,
13+
};
1214
use crate::macros::{rewrite_macro, MacroPosition};
1315
use crate::overflow;
1416
use crate::pairs::{rewrite_pair, PairParts};
@@ -314,46 +316,76 @@ where
314316
let offset = shape.indent + 1;
315317
Shape::legacy(budget, offset)
316318
};
319+
317320
let list_lo = context.snippet_provider.span_after(span, "(");
318-
let items = itemize_list(
319-
context.snippet_provider,
320-
inputs,
321-
")",
322-
",",
323-
|arg| arg.span().lo(),
324-
|arg| arg.span().hi(),
325-
|arg| arg.rewrite(context, list_shape),
326-
list_lo,
327-
span.hi(),
328-
false,
329-
);
321+
let (list_str, tactic) = if inputs.len() == 0 {
322+
let tactic = if output.contains('\n') {
323+
DefinitiveListTactic::Vertical
324+
} else {
325+
definitive_tactic(
326+
&Vec::<ListItem>::new(),
327+
ListTactic::HorizontalVertical,
328+
Separator::Comma,
329+
shape.width.saturating_sub(2 + output.len()),
330+
)
331+
};
332+
let list_hi = context.snippet_provider.span_after_last(span, ")");
333+
let comment = context
334+
.snippet_provider
335+
.span_to_snippet(mk_sp(list_lo, list_hi - BytePos(1)))?
336+
.trim();
337+
let comment = if comment.starts_with("//") {
338+
format!(
339+
"{}{}{}",
340+
&shape.indent.to_string_with_newline(context.config),
341+
comment,
342+
&shape.indent.to_string_with_newline(context.config)
343+
)
344+
} else {
345+
comment.to_string()
346+
};
347+
(comment, tactic)
348+
} else {
349+
let items = itemize_list(
350+
context.snippet_provider,
351+
inputs,
352+
")",
353+
",",
354+
|arg| arg.span().lo(),
355+
|arg| arg.span().hi(),
356+
|arg| arg.rewrite(context, list_shape),
357+
list_lo,
358+
span.hi(),
359+
false,
360+
);
330361

331-
let item_vec: Vec<_> = items.collect();
362+
let item_vec: Vec<_> = items.collect();
332363

333-
// If the return type is multi-lined, then force to use multiple lines for
334-
// arguments as well.
335-
let tactic = if output.contains('\n') {
336-
DefinitiveListTactic::Vertical
337-
} else {
338-
definitive_tactic(
339-
&*item_vec,
340-
ListTactic::HorizontalVertical,
341-
Separator::Comma,
342-
shape.width.saturating_sub(2 + output.len()),
343-
)
344-
};
345-
let trailing_separator = if !context.use_block_indent() || variadic {
346-
SeparatorTactic::Never
347-
} else {
348-
context.config.trailing_comma()
349-
};
364+
// If the return type is multi-lined, then force to use multiple lines for
365+
// arguments as well.
366+
let tactic = if output.contains('\n') {
367+
DefinitiveListTactic::Vertical
368+
} else {
369+
definitive_tactic(
370+
&*item_vec,
371+
ListTactic::HorizontalVertical,
372+
Separator::Comma,
373+
shape.width.saturating_sub(2 + output.len()),
374+
)
375+
};
376+
let trailing_separator = if !context.use_block_indent() || variadic {
377+
SeparatorTactic::Never
378+
} else {
379+
context.config.trailing_comma()
380+
};
350381

351-
let fmt = ListFormatting::new(list_shape, context.config)
352-
.tactic(tactic)
353-
.trailing_separator(trailing_separator)
354-
.ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
355-
.preserve_newline(true);
356-
let list_str = write_list(&item_vec, &fmt)?;
382+
let fmt = ListFormatting::new(list_shape, context.config)
383+
.tactic(tactic)
384+
.trailing_separator(trailing_separator)
385+
.ends_with_newline(tactic.ends_with_newline(context.config.indent_style()))
386+
.preserve_newline(true);
387+
(write_list(&item_vec, &fmt)?, tactic)
388+
};
357389

358390
let args = if tactic == DefinitiveListTactic::Horizontal || !context.use_block_indent() {
359391
format!("({})", list_str)

tests/source/issue-3508.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
fn foo<F>(foo2: F)
2+
where
3+
F: Fn(
4+
// this comment is deleted
5+
),
6+
{
7+
}
8+
9+
fn foo_block<F>(foo2: F)
10+
where
11+
F: Fn(
12+
/* this comment is deleted */
13+
),
14+
{
15+
}
16+
17+
fn bar(
18+
bar2: impl Fn(
19+
// this comment is deleted
20+
),
21+
) {
22+
}
23+
24+
fn bar_block(
25+
bar2: impl Fn(
26+
/* this comment is deleted */
27+
),
28+
) {
29+
}

tests/target/issue-3508.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
fn foo<F>(foo2: F)
2+
where
3+
F: Fn(
4+
// this comment is deleted
5+
),
6+
{
7+
}
8+
9+
fn foo_block<F>(foo2: F)
10+
where
11+
F: Fn(/* this comment is deleted */),
12+
{
13+
}
14+
15+
fn bar(
16+
bar2: impl Fn(
17+
// this comment is deleted
18+
),
19+
) {
20+
}
21+
22+
fn bar_block(bar2: impl Fn(/* this comment is deleted */)) {}

0 commit comments

Comments
 (0)