From c1b1ec9005f0b48304e87473c27aeca9e7ace1ab Mon Sep 17 00:00:00 2001 From: zakuro Date: Mon, 11 Jan 2021 05:06:48 +0900 Subject: [PATCH] fmt: keep same line comment in array init (#7980) --- vlib/v/fmt/fmt.v | 7 ++++++- vlib/v/fmt/tests/array_init_keep.vv | 2 ++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index afe5944f03..3dd91638c1 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1944,7 +1944,10 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) { f.array_init_break << (last_line_nr < line_nr) } } - mut penalty := if f.array_init_break[f.array_init_depth - 1] { 0 } else { 3 } + is_same_line_comment := i > 0 && + (expr is ast.Comment && line_nr == it.exprs[i - 1].position().line_nr) + line_break := f.array_init_break[f.array_init_depth - 1] + mut penalty := if line_break && !is_same_line_comment { 0 } else { 3 } if penalty > 0 { if i == 0 || it.exprs[i - 1] is ast.ArrayInit || it.exprs[i - 1] is ast.StructInit || it.exprs[i - 1] is ast.MapInit || it.exprs[i - 1] is ast.CallExpr { @@ -1976,6 +1979,8 @@ pub fn (mut f Fmt) array_init(it ast.ArrayInit) { f.write(',') } f.writeln('') + } else if is_same_line_comment { + f.writeln('') } } else if expr !is ast.Comment { f.write(',') diff --git a/vlib/v/fmt/tests/array_init_keep.vv b/vlib/v/fmt/tests/array_init_keep.vv index 118fa04287..4efe011e52 100644 --- a/vlib/v/fmt/tests/array_init_keep.vv +++ b/vlib/v/fmt/tests/array_init_keep.vv @@ -5,6 +5,8 @@ fn main() { /* test 1 */ 2, /* test 2 */ + 3, /* 3 */ + 4, /* 4-1 */ /* 4-2 */ ] arr2 := [ 'foo',