vfmt: align field attributes (when no inline comment after type) (#6089)
parent
bd32f0969f
commit
5e2824e2f2
|
@ -595,6 +595,8 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
name := node.name.after('.')
|
name := node.name.after('.')
|
||||||
f.writeln('$name {')
|
f.writeln('$name {')
|
||||||
mut max := 0
|
mut max := 0
|
||||||
|
mut max_type := 0
|
||||||
|
mut field_types := []string{cap: node.fields.len}
|
||||||
for field in node.fields {
|
for field in node.fields {
|
||||||
end_pos := field.pos.pos + field.pos.len
|
end_pos := field.pos.pos + field.pos.len
|
||||||
mut comments_len := 0 // Length of comments between field name and type
|
mut comments_len := 0 // Length of comments between field name and type
|
||||||
|
@ -609,6 +611,11 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
if comments_len + field.name.len > max {
|
if comments_len + field.name.len > max {
|
||||||
max = comments_len + field.name.len
|
max = comments_len + field.name.len
|
||||||
}
|
}
|
||||||
|
ft := f.type_to_str(field.typ)
|
||||||
|
field_types << ft
|
||||||
|
if ft.len > max_type {
|
||||||
|
max_type = ft.len
|
||||||
|
}
|
||||||
}
|
}
|
||||||
for i, field in node.fields {
|
for i, field in node.fields {
|
||||||
if i == node.mut_pos {
|
if i == node.mut_pos {
|
||||||
|
@ -623,8 +630,11 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
if comments.len == 0 {
|
if comments.len == 0 {
|
||||||
f.write('\t$field.name ')
|
f.write('\t$field.name ')
|
||||||
f.write(strings.repeat(` `, max - field.name.len))
|
f.write(strings.repeat(` `, max - field.name.len))
|
||||||
f.write(f.type_to_str(field.typ))
|
f.write(field_types[i])
|
||||||
f.inline_attrs(field.attrs)
|
if field.attrs.len > 0 {
|
||||||
|
f.write(strings.repeat(` `, max_type - field_types[i].len))
|
||||||
|
f.inline_attrs(field.attrs)
|
||||||
|
}
|
||||||
if field.has_default_expr {
|
if field.has_default_expr {
|
||||||
f.write(' = ')
|
f.write(' = ')
|
||||||
f.prefix_expr_cast_expr(field.default_expr)
|
f.prefix_expr_cast_expr(field.default_expr)
|
||||||
|
@ -654,7 +664,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||||
j++
|
j++
|
||||||
}
|
}
|
||||||
f.write(strings.repeat(` `, max - field.name.len - comments_len))
|
f.write(strings.repeat(` `, max - field.name.len - comments_len))
|
||||||
f.write(f.type_to_str(field.typ))
|
f.write(field_types[i])
|
||||||
f.inline_attrs(field.attrs)
|
f.inline_attrs(field.attrs)
|
||||||
if field.has_default_expr {
|
if field.has_default_expr {
|
||||||
f.write(' = ')
|
f.write(' = ')
|
||||||
|
@ -1126,7 +1136,7 @@ fn (mut f Fmt) inline_attrs(attrs []table.Attr) {
|
||||||
f.write(' [')
|
f.write(' [')
|
||||||
for i, attr in attrs {
|
for i, attr in attrs {
|
||||||
if i > 0 {
|
if i > 0 {
|
||||||
f.write(';')
|
f.write('; ')
|
||||||
}
|
}
|
||||||
f.write('$attr')
|
f.write('$attr')
|
||||||
}
|
}
|
||||||
|
|
|
@ -5,3 +5,12 @@
|
||||||
fn keep_attributes() {
|
fn keep_attributes() {
|
||||||
println('hi !')
|
println('hi !')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct User {
|
||||||
|
age int
|
||||||
|
nums []int
|
||||||
|
last_name string [json: lastName]
|
||||||
|
is_registered bool [json: IsRegistered]
|
||||||
|
typ int [json: 'type']
|
||||||
|
pets string [raw; json: 'pet_animals']
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in New Issue