From 0e7192c9b7574d72db3148099549234869bc92e9 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Wed, 9 Dec 2020 15:35:03 +0100 Subject: [PATCH] fmt: keep comments in struct init without fields (#7212) --- vlib/v/fmt/fmt.v | 10 ++++++++-- vlib/v/fmt/tests/comments_keep.vv | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index e5bc06b111..955b0effa4 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -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 != '' { diff --git a/vlib/v/fmt/tests/comments_keep.vv b/vlib/v/fmt/tests/comments_keep.vv index 6d5e6ce8a9..57b111bde0 100644 --- a/vlib/v/fmt/tests/comments_keep.vv +++ b/vlib/v/fmt/tests/comments_keep.vv @@ -24,4 +24,7 @@ fn main() { // after age line // after line2 } + _ := User{ + // Just a comment + } }