fmt: fix struct field default expressions and tags

pull/5129/head
Delyan Angelov 2020-05-29 21:22:27 +03:00
parent 1e504fb388
commit 077e06b44e
2 changed files with 24 additions and 1 deletions

View File

@ -463,10 +463,25 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
f.write('\t$field.name ')
f.write(strings.repeat(` `, max - field.name.len))
f.write(f.type_to_str(field.typ))
if field.attrs.len > 0 {
f.write(' [' + field.attrs.join(';') + ']')
}
if field.has_default_expr {
f.write(' = ')
mut is_pe_amp_ce := false
mut ce := ast.CastExpr{}
if field.default_expr is ast.PrefixExpr {
pe := field.default_expr as ast.PrefixExpr
if pe.right is ast.CastExpr && pe.op == .amp {
ce = pe.right as ast.CastExpr
is_pe_amp_ce = true
f.expr(ce)
}
}
if !is_pe_amp_ce {
f.expr(field.default_expr)
}
}
// f.write('// $field.pos.line_nr')
if field.comment.text != '' && field.comment.pos.line_nr == field.pos.line_nr {
// Same line comment

View File

@ -0,0 +1,8 @@
struct Foo {
i int = 1
}
struct Bar {
f Foo = &Foo(0)
z int [skip] = -1
}