From 371730f8a8b91401a77777edc79bb9199e0781b2 Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Sun, 17 Jan 2021 05:01:55 +0100 Subject: [PATCH] fmt: correct indent for StructDecl multi line default exprs (#8148) --- vlib/v/doc/doc.v | 8 ++++---- vlib/v/fmt/fmt.v | 14 ++++++++++++++ vlib/v/fmt/tests/struct_keep.vv | 10 ++++++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index 16f5fa847c..941735720c 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -46,10 +46,10 @@ pub mut: base_path string table &table.Table = &table.Table{} checker checker.Checker = checker.Checker{ - table: 0 - cur_fn: 0 - pref: 0 -} + table: 0 + cur_fn: 0 + pref: 0 + } fmt fmt.Fmt filename string pos int diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index bb23c3bb1f..88895ae5ee 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -687,6 +687,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { mut field_align_i := 0 mut comment_align_i := 0 mut default_expr_align_i := 0 + mut inc_indent := false // for correct indents with multi line default exprs for i, field in node.fields { if i == node.mut_pos { f.writeln('mut:') @@ -738,7 +739,15 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) { pad_len := align.max_attrs_len - attrs_len + align.max_type_len - field_types[i].len f.write(strings.repeat(` `, pad_len)) f.write(' = ') + if !expr_is_single_line(field.default_expr) { + f.indent++ + inc_indent = true + } f.prefix_expr_cast_expr(field.default_expr) + if inc_indent { + f.indent-- + inc_indent = false + } } // Handle comments after field type (same line) if comm_idx < comments.len { @@ -1007,6 +1016,11 @@ fn expr_is_single_line(expr ast.Expr) bool { return false } } + ast.ArrayInit { + if expr.exprs.len > 0 { + return expr_is_single_line(expr.exprs[0]) + } + } else {} } return true diff --git a/vlib/v/fmt/tests/struct_keep.vv b/vlib/v/fmt/tests/struct_keep.vv index 0ef610c93a..68ed58bc3b 100644 --- a/vlib/v/fmt/tests/struct_keep.vv +++ b/vlib/v/fmt/tests/struct_keep.vv @@ -34,3 +34,13 @@ pub: a int b int } + +struct KeepMultiLineDefaultExprsIndent { + buttons []PeriodButton = [PeriodButton{ + period: pr.Period.m1 + text: 'M1' + }, PeriodButton{ + period: pr.Period.m5 + text: 'M5' + }] +}