Skip to content

Commit dec3902

Browse files
committed
leave post comment for self
1 parent ae7330e commit dec3902

File tree

4 files changed

+160
-42
lines changed

4 files changed

+160
-42
lines changed

src/items.rs

+41-30
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ use crate::expr::{
2020
ExprType, RhsTactics,
2121
};
2222
use crate::lists::{
23-
definitive_tactic, extract_pre_comment, itemize_list, write_list, ListFormatting, ListItem,
24-
Separator,
23+
definitive_tactic, extract_post_comment, extract_pre_comment, get_comment_end,
24+
has_extra_newline, itemize_list, write_list, ListFormatting, ListItem, Separator,
2525
};
2626
use crate::macros::{rewrite_macro, MacroPosition};
2727
use crate::overflow;
@@ -2281,6 +2281,10 @@ fn rewrite_args(
22812281
variadic: bool,
22822282
generics_str_contains_newline: bool,
22832283
) -> Option<String> {
2284+
let terminator = ")";
2285+
let separator = ",";
2286+
let next_span_start = span.hi();
2287+
22842288
let mut arg_item_strs = args
22852289
.iter()
22862290
.map(|arg| {
@@ -2290,17 +2294,20 @@ fn rewrite_args(
22902294
.collect::<Vec<_>>();
22912295

22922296
// Account for sugary self.
2293-
// FIXME: the comment for the self argument is dropped. This is blocked
2294-
// on rust issue #27522.
2295-
let mut comment_str = "";
2297+
let mut pre_comment_str = "";
2298+
let mut post_comment_str = "";
22962299
let min_args = explicit_self
22972300
.and_then(|explicit_self| rewrite_explicit_self(explicit_self, args, context))
22982301
.map_or(1, |self_str| {
2299-
let comment_span = mk_sp(span.lo(), args[0].pat.span.lo());
2300-
comment_str = context
2301-
.snippet_provider
2302-
.span_to_snippet(comment_span)
2303-
.unwrap();
2302+
pre_comment_str = context.snippet(mk_sp(span.lo(), args[0].pat.span.lo()));
2303+
2304+
let next_start = if args.len() > 1 {
2305+
args[1].pat.span().lo()
2306+
} else {
2307+
span.hi()
2308+
};
2309+
post_comment_str = context.snippet(mk_sp(args[0].ty.span.hi(), next_start));
2310+
23042311
arg_item_strs[0] = self_str;
23052312
2
23062313
});
@@ -2317,14 +2324,18 @@ fn rewrite_args(
23172324
// it is explicit.
23182325
if args.len() >= min_args || variadic {
23192326
let comment_span_start = if min_args == 2 {
2320-
let second_arg_start = if arg_has_pattern(&args[1]) {
2321-
args[1].pat.span.lo()
2327+
let remove_comma_byte_pos = context
2328+
.snippet_provider
2329+
.span_after(mk_sp(args[0].ty.span.hi(), args[1].pat.span.lo()), ",");
2330+
let first_post_and_second_pre_span =
2331+
mk_sp(remove_comma_byte_pos, args[1].pat.span.lo());
2332+
if count_newlines(context.snippet(first_post_and_second_pre_span)) > 0 {
2333+
context
2334+
.snippet_provider
2335+
.span_after(first_post_and_second_pre_span, "\n")
23222336
} else {
2323-
args[1].ty.span.lo()
2324-
};
2325-
let reduced_span = mk_sp(span.lo(), second_arg_start);
2326-
2327-
context.snippet_provider.span_after_last(reduced_span, ",")
2337+
remove_comma_byte_pos
2338+
}
23282339
} else {
23292340
span.lo()
23302341
};
@@ -2349,8 +2360,8 @@ fn rewrite_args(
23492360
.iter()
23502361
.map(ArgumentKind::Regular)
23512362
.chain(variadic_arg),
2352-
")",
2353-
",",
2363+
terminator,
2364+
separator,
23542365
|arg| match *arg {
23552366
ArgumentKind::Regular(arg) => span_lo_for_arg(arg),
23562367
ArgumentKind::Variadic(start) => start,
@@ -2364,22 +2375,30 @@ fn rewrite_args(
23642375
ArgumentKind::Variadic(..) => Some("...".to_owned()),
23652376
},
23662377
comment_span_start,
2367-
span.hi(),
2378+
next_span_start,
23682379
false,
23692380
);
23702381

23712382
arg_items.extend(more_items);
23722383
}
23732384

2385+
let arg_items_len = arg_items.len();
23742386
let fits_in_one_line = !generics_str_contains_newline
23752387
&& (arg_items.is_empty()
2376-
|| arg_items.len() == 1 && arg_item_strs[0].len() <= one_line_budget);
2388+
|| arg_items_len == 1 && arg_item_strs[0].len() <= one_line_budget);
23772389

23782390
for (index, (item, arg)) in arg_items.iter_mut().zip(arg_item_strs).enumerate() {
2391+
// add pre comment and post comment for first arg(self)
23792392
if index == 0 && explicit_self.is_some() {
2380-
let (pre_comment, pre_comment_style) = extract_pre_comment(comment_str);
2393+
let (pre_comment, pre_comment_style) = extract_pre_comment(pre_comment_str);
23812394
item.pre_comment = pre_comment;
23822395
item.pre_comment_style = pre_comment_style;
2396+
2397+
let comment_end =
2398+
get_comment_end(post_comment_str, separator, terminator, arg_items_len == 1);
2399+
2400+
item.new_lines = has_extra_newline(post_comment_str, comment_end);
2401+
item.post_comment = extract_post_comment(post_comment_str, comment_end, separator);
23832402
}
23842403
item.item = Some(arg);
23852404
}
@@ -2430,14 +2449,6 @@ fn rewrite_args(
24302449
write_list(&arg_items, &fmt)
24312450
}
24322451

2433-
fn arg_has_pattern(arg: &ast::Arg) -> bool {
2434-
if let ast::PatKind::Ident(_, ident, _) = arg.pat.node {
2435-
ident != symbol::keywords::Invalid.ident()
2436-
} else {
2437-
true
2438-
}
2439-
}
2440-
24412452
fn compute_budgets_for_args(
24422453
context: &RewriteContext<'_>,
24432454
result: &str,

src/lists.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -683,7 +683,7 @@ pub fn get_comment_end(
683683

684684
// Account for extra whitespace between items. This is fiddly
685685
// because of the way we divide pre- and post- comments.
686-
fn has_extra_newline(post_snippet: &str, comment_end: usize) -> bool {
686+
pub fn has_extra_newline(post_snippet: &str, comment_end: usize) -> bool {
687687
if post_snippet.is_empty() || comment_end == 0 {
688688
return false;
689689
}

tests/source/issue-3198.rs

+65-6
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,99 @@
11
impl TestTrait {
2-
fn foo_one(/* Important comment1 */
2+
fn foo_one_pre(/* Important comment1 */
33
self) {
44
}
55

6-
fn foo(
6+
fn foo_one_post(self
7+
/* Important comment1 */) {
8+
}
9+
10+
fn foo_pre(
711
/* Important comment1 */
812
self,
913
/* Important comment2 */
1014
a: i32,
1115
) {
1216
}
1317

14-
fn bar(
18+
fn foo_post(
19+
self
20+
/* Important comment1 */,
21+
a: i32
22+
/* Important comment2 */,
23+
) {
24+
}
25+
26+
fn bar_pre(
1527
/* Important comment1 */
1628
&mut self,
1729
/* Important comment2 */
1830
a: i32,
1931
) {
2032
}
2133

22-
fn baz(
34+
fn bar_post(
35+
&mut self
36+
/* Important comment1 */,
37+
a: i32
38+
/* Important comment2 */,
39+
) {
40+
}
41+
42+
fn baz_pre(
2343
/* Important comment1 */
2444
self: X< 'a , 'b >,
2545
/* Important comment2 */
2646
a: i32,
2747
) {
2848
}
2949

30-
fn baz_tree(
31-
/* Important comment1 */
50+
fn baz_post(
51+
self: X< 'a , 'b >
52+
/* Important comment1 */,
53+
a: i32
54+
/* Important comment2 */,
55+
) {
56+
}
3257

58+
fn baz_tree_pre(
59+
/* Important comment1 */
3360
self: X< 'a , 'b >,
3461
/* Important comment2 */
3562
a: i32,
3663
/* Important comment3 */
3764
b: i32,
3865
) {
3966
}
67+
68+
fn baz_tree_post(
69+
self: X< 'a , 'b >
70+
/* Important comment1 */,
71+
a: i32
72+
/* Important comment2 */,
73+
b: i32
74+
/* Important comment3 */,){
75+
}
76+
77+
fn multi_line(
78+
self: X<'a, 'b>, /* Important comment1-1 */
79+
/* Important comment1-2 */
80+
a: i32, /* Important comment2 */
81+
b: i32, /* Important comment3 */
82+
) {
83+
}
84+
85+
fn two_line_comment(
86+
self: X<'a, 'b>, /* Important comment1-1
87+
Important comment1-2 */
88+
a: i32, /* Important comment2 */
89+
b: i32, /* Important comment3 */
90+
) {
91+
}
92+
93+
fn no_first_line_comment(
94+
self: X<'a, 'b>,
95+
/* Important comment2 */a: i32,
96+
/* Important comment3 */b: i32,
97+
) {
98+
}
4099
}

tests/target/issue-3198.rs

+53-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,31 @@
11
impl TestTrait {
2-
fn foo_one(/* Important comment1 */ self) {}
2+
fn foo_one_pre(/* Important comment1 */ self) {}
33

4-
fn foo(/* Important comment1 */ self, /* Important comment2 */ a: i32) {}
4+
fn foo_one_post(self /* Important comment1 */) {}
55

6-
fn bar(/* Important comment1 */ &mut self, /* Important comment2 */ a: i32) {}
6+
fn foo_pre(/* Important comment1 */ self, /* Important comment2 */ a: i32) {}
77

8-
fn baz(/* Important comment1 */ self: X<'a, 'b>, /* Important comment2 */ a: i32) {}
8+
fn foo_post(self /* Important comment1 */, a: i32 /* Important comment2 */) {}
99

10-
fn baz_tree(
10+
fn bar_pre(/* Important comment1 */ &mut self, /* Important comment2 */ a: i32) {}
11+
12+
fn bar_post(&mut self /* Important comment1 */, a: i32 /* Important comment2 */) {}
13+
14+
fn baz_pre(
15+
/* Important comment1 */
16+
self: X<'a, 'b>,
17+
/* Important comment2 */
18+
a: i32,
19+
) {
20+
}
21+
22+
fn baz_post(
23+
self: X<'a, 'b>, /* Important comment1 */
24+
a: i32, /* Important comment2 */
25+
) {
26+
}
27+
28+
fn baz_tree_pre(
1129
/* Important comment1 */
1230
self: X<'a, 'b>,
1331
/* Important comment2 */
@@ -16,4 +34,34 @@ impl TestTrait {
1634
b: i32,
1735
) {
1836
}
37+
38+
fn baz_tree_post(
39+
self: X<'a, 'b>, /* Important comment1 */
40+
a: i32, /* Important comment2 */
41+
b: i32, /* Important comment3 */
42+
) {
43+
}
44+
45+
fn multi_line(
46+
self: X<'a, 'b>, /* Important comment1-1 */
47+
/* Important comment1-2 */
48+
a: i32, /* Important comment2 */
49+
b: i32, /* Important comment3 */
50+
) {
51+
}
52+
53+
fn two_line_comment(
54+
self: X<'a, 'b>, /* Important comment1-1
55+
Important comment1-2 */
56+
a: i32, /* Important comment2 */
57+
b: i32, /* Important comment3 */
58+
) {
59+
}
60+
61+
fn no_first_line_comment(
62+
self: X<'a, 'b>,
63+
/* Important comment2 */ a: i32,
64+
/* Important comment3 */ b: i32,
65+
) {
66+
}
1967
}

0 commit comments

Comments
 (0)