fmt: correct indent for StructDecl multi line default exprs (#8148)

pull/8159/head
Lukas Neubert 2021-01-17 05:01:55 +01:00 committed by GitHub
parent ef627c9d21
commit 371730f8a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 4 deletions

View File

@ -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

View File

@ -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

View File

@ -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'
}]
}