fmt: keep comments in struct init without fields (#7212)

pull/7218/head
Lukas Neubert 2020-12-09 15:35:03 +01:00 committed by GitHub
parent ada02d4498
commit 0e7192c9b7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 2 deletions

View File

@ -1829,8 +1829,14 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
name = ''
}
if it.fields.len == 0 {
// `Foo{}` on one line if there are no fields
f.write('$name{}')
// `Foo{}` on one line if there are no fields or comments
if it.pre_comments.len == 0 {
f.write('$name{}')
} else {
f.writeln('$name{')
f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent)
f.write('}')
}
} else if it.is_short {
// `Foo{1,2,3}` (short syntax )
// if name != '' {

View File

@ -24,4 +24,7 @@ fn main() {
// after age line
// after line2
}
_ := User{
// Just a comment
}
}