Skip to content

Commit ae7b41a

Browse files
committed
fix: recover comments between trait keyword and ident
1 parent 3d8117a commit ae7b41a

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/items.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,12 +1178,24 @@ pub(crate) fn format_trait(
11781178
let header_rewrite = crate::header::format_header(context, shape, header);
11791179

11801180
result.push_str(&header_rewrite);
1181-
result.push(' ');
11821181

1183-
let shape =
1184-
Shape::indented(offset, context.config).offset_left(last_line_width(&result), item.span)?;
1182+
// +1 to account for space between `trait` and the `ident`
1183+
let offset_left = last_line_width(&result) + 1;
1184+
let shape = Shape::indented(offset, context.config).offset_left(offset_left, item.span)?;
11851185
let generics_str = rewrite_generics(context, rewrite_ident(context, ident), generics, shape)?;
1186-
result.push_str(&generics_str);
1186+
1187+
let comment_span_lo = context.snippet_provider.span_after(item.span, "trait");
1188+
let comment_span = mk_sp(comment_span_lo, ident.span.lo() - BytePos(1));
1189+
1190+
result = combine_strs_with_missing_comments(
1191+
context,
1192+
&result,
1193+
&generics_str,
1194+
comment_span,
1195+
// infinite_width so we don't put the `ident + generics_str` on the next line
1196+
shape.infinite_width(),
1197+
true,
1198+
)?;
11871199

11881200
// Keep track of the last position that we've written. This will help us recover comments
11891201
let mut current_rewite_lo = generics.span.hi();

tests/source/trait.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ unsafe
122122
/* post unsafe comment */
123123
auto /* post auto comment */ trait PubConstUnsafeAutoExampleWithComments {}
124124

125+
pub /* first */ trait /* second */ MyTrait /* third */ {}
126+
125127
// #3006
126128
trait Foo<'a> {
127129
type Bar< 'a >;

tests/target/trait.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,8 @@ auto /* post auto comment */ trait PubConstUnsafeAutoExampleWithComments
161161
{
162162
}
163163

164+
pub /* first */ trait /* second */ MyTrait /* third */ {}
165+
164166
// #3006
165167
trait Foo<'a> {
166168
type Bar<'a>;

0 commit comments

Comments
 (0)