From c9a8d6448d68153d32deacf23708e9c22a5567ef Mon Sep 17 00:00:00 2001 From: Vincenzo Palazzo Date: Sat, 5 Feb 2022 09:05:35 +0100 Subject: [PATCH] fmt: preserve formatting with comments in a empty map (#13362) --- vlib/v/fmt/fmt.v | 8 +++++++- vlib/v/fmt/tests/empty_map_fmt_keep.vv | 11 +++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 vlib/v/fmt/tests/empty_map_fmt_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 003a69195..47cb658fd 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -2097,7 +2097,13 @@ pub fn (mut f Fmt) map_init(node ast.MapInit) { f.mark_types_import_as_used(info.key_type) f.write(f.table.type_to_str_using_aliases(node.typ, f.mod2alias)) } - f.write('{}') + if node.pos.line_nr == node.pos.last_line { + f.write('{}') + } else { + f.writeln('{') + f.comments(node.pre_cmnts, level: .indent) + f.write('}') + } return } f.writeln('{') diff --git a/vlib/v/fmt/tests/empty_map_fmt_keep.vv b/vlib/v/fmt/tests/empty_map_fmt_keep.vv new file mode 100644 index 000000000..661e42f8e --- /dev/null +++ b/vlib/v/fmt/tests/empty_map_fmt_keep.vv @@ -0,0 +1,11 @@ +struct Foo { + bar map[int]int +} + +fn main() { + foo := Foo{ + bar: { + // comment + } + } +} -- 2.39.5