From 0cb1064428d90ba3a708b120bf400dddaa28581c Mon Sep 17 00:00:00 2001 From: Jooyung Han Date: Wed, 9 Feb 2022 11:10:12 +0900 Subject: [PATCH] Add newlines around list of structs Since a struct(parser.Map) occupies multiple lines, adding newlines around brackets([]) looks better even the list has only a single value. prop: [ { name: "foo", }], vs prop: [ { name: "foo", }, ], Bug: n/a Test: go test ./parser Change-Id: I1a574aa038a26235848b6c9b5b4f01a0ab2c8c00 --- parser/printer.go | 11 ++++++++++- parser/printer_test.go | 21 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/parser/printer.go b/parser/printer.go index ac7ffe1..f377505 100644 --- a/parser/printer.go +++ b/parser/printer.go @@ -139,7 +139,7 @@ func (p *printer) printExpression(value Expression) { func (p *printer) printList(list []Expression, pos, endPos scanner.Position) { p.requestSpace() p.printToken("[", pos) - if len(list) > 1 || pos.Line != endPos.Line { + if len(list) > 1 || pos.Line != endPos.Line || listHasMap(list) { p.requestNewline() p.indent(p.curIndent() + 4) for _, value := range list { @@ -392,3 +392,12 @@ func max(a, b int) int { return b } } + +func listHasMap(list []Expression) bool { + for _, value := range list { + if _, ok := value.(*Map); ok { + return true + } + } + return false +} diff --git a/parser/printer_test.go b/parser/printer_test.go index 077a782..c889b2a 100644 --- a/parser/printer_test.go +++ b/parser/printer_test.go @@ -426,6 +426,27 @@ stuff { ], ], } +`, + }, + { + input: ` +// test +stuff { + namespace: "google", + list_of_structs: [{ key1: "a", key2: "b" }], +} +`, + output: ` +// test +stuff { + namespace: "google", + list_of_structs: [ + { + key1: "a", + key2: "b", + }, + ], +} `, }, }