fmt: fix formatting of struct field with default value and new attr syntax (stage 2) (#19683)

This commit is contained in:
Joe C 2023-10-28 20:21:13 +11:00 committed by GitHub
parent 22e57d2f41
commit 11be033f67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 10 additions and 1 deletions

View File

@ -324,6 +324,7 @@ pub:
comments []Comment
i int
has_default_expr bool
attrs_has_at bool // TODO: remove in next stage
attrs []Attr
is_pub bool
default_val string

View File

@ -145,7 +145,8 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
f.mark_types_import_as_used(field.typ)
attrs_len := inline_attrs_len(field.attrs)
has_attrs := field.attrs.len > 0
if has_attrs {
// TODO: this will get removed in next stage
if has_attrs && !field.attrs_has_at {
f.write(strings.repeat(` `, field_align.max_type_len - field_types[i].len))
f.single_line_attrs(field.attrs, same_line: true)
}
@ -167,6 +168,10 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl, is_anon bool) {
inc_indent = false
}
}
if has_attrs && field.attrs_has_at {
// TODO: calculate correct padding
f.single_line_attrs(field.attrs, same_line: true)
}
// Handle comments after field type
if after_type_comments.len > 0 {
if after_type_comments[0].pos.line_nr > field.pos.line_nr {

View File

@ -295,7 +295,9 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
has_default_expr = true
comments << p.eat_comments()
}
mut has_at := false // TODO: remove in next stage
if p.tok.kind == .at {
has_at = true
p.inside_struct_attr_decl = true
// attrs are stored in `p.attrs`
p.attributes()
@ -316,6 +318,7 @@ fn (mut p Parser) struct_decl(is_anon bool) ast.StructDecl {
i: i
default_expr: default_expr
has_default_expr: has_default_expr
attrs_has_at: has_at
attrs: p.attrs
is_pub: is_embed || is_field_pub
is_mut: is_embed || is_field_mut