Skip to content

Commit cd081d4

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

File tree

4 files changed

+106
-25
lines changed

4 files changed

+106
-25
lines changed

src/items.rs

Lines changed: 32 additions & 13 deletions
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,26 @@ 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
2302+
pre_comment_str = context
23012303
.snippet_provider
2302-
.span_to_snippet(comment_span)
2304+
.span_to_snippet(mk_sp(span.lo(), args[0].pat.span.lo()))
23032305
.unwrap();
2306+
2307+
let next_start = if args.len() > 1 {
2308+
args[1].pat.span().lo()
2309+
} else {
2310+
span.hi()
2311+
};
2312+
post_comment_str = context
2313+
.snippet_provider
2314+
.span_to_snippet(mk_sp(args[0].ty.span.hi(), next_start))
2315+
.unwrap();
2316+
23042317
arg_item_strs[0] = self_str;
23052318
2
23062319
});
@@ -2349,8 +2362,8 @@ fn rewrite_args(
23492362
.iter()
23502363
.map(ArgumentKind::Regular)
23512364
.chain(variadic_arg),
2352-
")",
2353-
",",
2365+
terminator,
2366+
separator,
23542367
|arg| match *arg {
23552368
ArgumentKind::Regular(arg) => span_lo_for_arg(arg),
23562369
ArgumentKind::Variadic(start) => start,
@@ -2364,22 +2377,28 @@ fn rewrite_args(
23642377
ArgumentKind::Variadic(..) => Some("...".to_owned()),
23652378
},
23662379
comment_span_start,
2367-
span.hi(),
2380+
next_span_start,
23682381
false,
23692382
);
23702383

23712384
arg_items.extend(more_items);
23722385
}
23732386

2387+
let arg_items_len = arg_items.len();
23742388
let fits_in_one_line = !generics_str_contains_newline
23752389
&& (arg_items.is_empty()
2376-
|| arg_items.len() == 1 && arg_item_strs[0].len() <= one_line_budget);
2390+
|| arg_items_len == 1 && arg_item_strs[0].len() <= one_line_budget);
23772391

23782392
for (index, (item, arg)) in arg_items.iter_mut().zip(arg_item_strs).enumerate() {
23792393
if index == 0 && explicit_self.is_some() {
2380-
let (pre_comment, pre_comment_style) = extract_pre_comment(comment_str);
2394+
let (pre_comment, pre_comment_style) = extract_pre_comment(pre_comment_str);
23812395
item.pre_comment = pre_comment;
23822396
item.pre_comment_style = pre_comment_style;
2397+
2398+
let comment_end =
2399+
get_comment_end(post_comment_str, separator, terminator, arg_items_len == 1);
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
}

src/lists.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,77 @@
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+
}
4077
}

tests/target/issue-3198.rs

Lines changed: 30 additions & 5 deletions
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,11 @@ 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+
}
1944
}

0 commit comments

Comments
 (0)