From 11be033f679f40a27d7e89705e048bc8ed32a1df Mon Sep 17 00:00:00 2001 From: Joe C Date: Sat, 28 Oct 2023 20:21:13 +1100 Subject: [PATCH] fmt: fix formatting of struct field with default value and new attr syntax (stage 2) (#19683) --- vlib/v/ast/ast.v | 1 + vlib/v/fmt/struct.v | 7 ++++++- vlib/v/parser/struct.v | 3 +++ 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index c64929d2f0..8fe4306415 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -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 diff --git a/vlib/v/fmt/struct.v b/vlib/v/fmt/struct.v index 59dd281113..07a510a8d3 100644 --- a/vlib/v/fmt/struct.v +++ b/vlib/v/fmt/struct.v @@ -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 { diff --git a/vlib/v/parser/struct.v b/vlib/v/parser/struct.v index c730ea882f..7064bce722 100644 --- a/vlib/v/parser/struct.v +++ b/vlib/v/parser/struct.v @@ -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