Skip to content

Commit

Permalink
Merge pull request rust-lang#3200 from scampi/issue-3194
Browse files Browse the repository at this point in the history
compute the span after a struct-like item based on the ident description
  • Loading branch information
nrc authored Nov 14, 2018
2 parents 34333ba + a4e97fa commit ef75b72
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,8 @@ impl<'a> FmtVisitor<'a> {
BracePos::Auto
},
self.block_indent,
mk_sp(span.lo(), body_start),
// make a span that starts right after `enum Foo`
mk_sp(ident.span.hi(), body_start),
last_line_width(&enum_header),
)
.unwrap();
Expand Down Expand Up @@ -1186,7 +1187,8 @@ fn format_unit_struct(context: &RewriteContext, p: &StructParts, offset: Indent)
context.config.brace_style(),
BracePos::None,
offset,
mk_sp(generics.span.lo(), hi),
// make a span that starts right after `struct Foo`
mk_sp(p.ident.span.hi(), hi),
last_line_width(&header_str),
)?
} else {
Expand All @@ -1208,7 +1210,7 @@ pub fn format_struct_struct(
let header_str = struct_parts.format_header(context);
result.push_str(&header_str);

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

let generics_str = match struct_parts.generics {
Expand All @@ -1222,6 +1224,7 @@ pub fn format_struct_struct(
BracePos::Auto
},
offset,
// make a span that starts right after `struct Foo`
mk_sp(header_hi, body_lo),
last_line_width(&result),
)?,
Expand Down
13 changes: 13 additions & 0 deletions tests/source/issue-3194.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
mod m { struct S where A: B; }

mod n { struct Foo where A: B { foo: usize } }

mod o { enum Bar where A: B { Bar } }

mod with_comments {
mod m { struct S /* before where */ where A: B; /* after where */ }

mod n { struct Foo /* before where */ where A: B /* after where */ { foo: usize } }

mod o { enum Bar /* before where */ where A: B /* after where */ { Bar } }
}
52 changes: 52 additions & 0 deletions tests/target/issue-3194.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
mod m {
struct S
where
A: B;
}

mod n {
struct Foo
where
A: B,
{
foo: usize,
}
}

mod o {
enum Bar
where
A: B,
{
Bar,
}
}

mod with_comments {
mod m {
struct S
/* before where */
where
A: B; /* after where */
}

mod n {
struct Foo
/* before where */
where
A: B, /* after where */
{
foo: usize,
}
}

mod o {
enum Bar
/* before where */
where
A: B, /* after where */
{
Bar,
}
}
}

0 comments on commit ef75b72

Please sign in to comment.