Skip to content

Commit ef75b72

Browse files
authored
Merge pull request #3200 from scampi/issue-3194
compute the span after a struct-like item based on the ident description
2 parents 34333ba + a4e97fa commit ef75b72

File tree

3 files changed

+71
-3
lines changed

3 files changed

+71
-3
lines changed

src/items.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,8 @@ impl<'a> FmtVisitor<'a> {
464464
BracePos::Auto
465465
},
466466
self.block_indent,
467-
mk_sp(span.lo(), body_start),
467+
// make a span that starts right after `enum Foo`
468+
mk_sp(ident.span.hi(), body_start),
468469
last_line_width(&enum_header),
469470
)
470471
.unwrap();
@@ -1186,7 +1187,8 @@ fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent)
11861187
context.config.brace_style(),
11871188
BracePos::None,
11881189
offset,
1189-
mk_sp(generics.span.lo(), hi),
1190+
// make a span that starts right after `struct Foo`
1191+
mk_sp(p.ident.span.hi(), hi),
11901192
last_line_width(&header_str),
11911193
)?
11921194
} else {
@@ -1208,7 +1210,7 @@ pub fn format_struct_struct(
12081210
let header_str = struct_parts.format_header(context);
12091211
result.push_str(&header_str);
12101212

1211-
let header_hi = span.lo() + BytePos(header_str.len() as u32);
1213+
let header_hi = struct_parts.ident.span.hi();
12121214
let body_lo = context.snippet_provider.span_after(span, "{");
12131215

12141216
let generics_str = match struct_parts.generics {
@@ -1222,6 +1224,7 @@ pub fn format_struct_struct(
12221224
BracePos::Auto
12231225
},
12241226
offset,
1227+
// make a span that starts right after `struct Foo`
12251228
mk_sp(header_hi, body_lo),
12261229
last_line_width(&result),
12271230
)?,

tests/source/issue-3194.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
mod m { struct S where A: B; }
2+
3+
mod n { struct Foo where A: B { foo: usize } }
4+
5+
mod o { enum Bar where A: B { Bar } }
6+
7+
mod with_comments {
8+
mod m { struct S /* before where */ where A: B; /* after where */ }
9+
10+
mod n { struct Foo /* before where */ where A: B /* after where */ { foo: usize } }
11+
12+
mod o { enum Bar /* before where */ where A: B /* after where */ { Bar } }
13+
}

tests/target/issue-3194.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
mod m {
2+
struct S
3+
where
4+
A: B;
5+
}
6+
7+
mod n {
8+
struct Foo
9+
where
10+
A: B,
11+
{
12+
foo: usize,
13+
}
14+
}
15+
16+
mod o {
17+
enum Bar
18+
where
19+
A: B,
20+
{
21+
Bar,
22+
}
23+
}
24+
25+
mod with_comments {
26+
mod m {
27+
struct S
28+
/* before where */
29+
where
30+
A: B; /* after where */
31+
}
32+
33+
mod n {
34+
struct Foo
35+
/* before where */
36+
where
37+
A: B, /* after where */
38+
{
39+
foo: usize,
40+
}
41+
}
42+
43+
mod o {
44+
enum Bar
45+
/* before where */
46+
where
47+
A: B, /* after where */
48+
{
49+
Bar,
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)