From 93acc929f7e13ad0f61f1bc66f6725535fd4a562 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Fri, 29 Jan 2021 19:19:14 +0100 Subject: [PATCH] fmt: keep lines of comments inside an array (#8420) --- vlib/v/fmt/fmt.v | 10 +++++++++- vlib/v/fmt/tests/comments_embedded_keep.vv | 12 ++++++++++++ vlib/v/fmt/tests/comments_expected.vv | 3 ++- 3 files changed, 23 insertions(+), 2 deletions(-) create mode 100644 vlib/v/fmt/tests/comments_embedded_keep.vv diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index fb74601f81..4eaffb96ae 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1929,8 +1929,16 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) { } f.expr(expr) if i < it.ecmnts.len && it.ecmnts[i].len > 0 { - f.write(' ') + mut last_cmt := it.ecmnts[i][0] + if last_cmt.pos.line_nr > expr.position().last_line { + f.writeln('') + } else { + f.write(' ') + } for cmt in it.ecmnts[i] { + if cmt.pos.line_nr > last_cmt.pos.last_line { + f.writeln('') + } f.comment(cmt, iembed: true) } } diff --git a/vlib/v/fmt/tests/comments_embedded_keep.vv b/vlib/v/fmt/tests/comments_embedded_keep.vv new file mode 100644 index 0000000000..8a6554f89a --- /dev/null +++ b/vlib/v/fmt/tests/comments_embedded_keep.vv @@ -0,0 +1,12 @@ +fn only_comments_array() { + arr := [ + /* 1, */ + /* 2, */ + /* 3, */ + /* 4, */ + ] + arr2 := [ + /* 1, */ /* 2, *//* 3, */ + /* 4, */ + ] +} diff --git a/vlib/v/fmt/tests/comments_expected.vv b/vlib/v/fmt/tests/comments_expected.vv index 85a7e56eb2..692275012e 100644 --- a/vlib/v/fmt/tests/comments_expected.vv +++ b/vlib/v/fmt/tests/comments_expected.vv @@ -40,7 +40,8 @@ fn main() { e := c // more comments = more good arr := [ - /* block foo bar */ /* inline foo bar */ + /* block foo bar */ + /* inline foo bar */ 0, ] // before arg comment