mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
fmt: combine simple attributes into a single line (#9120)
This commit is contained in:
@@ -761,7 +761,7 @@ pub fn (mut f Fmt) struct_decl(node ast.StructDecl) {
|
||||
has_attrs := field.attrs.len > 0
|
||||
if has_attrs {
|
||||
f.write(strings.repeat(` `, field_align.max_type_len - field_types[i].len))
|
||||
f.inline_attrs(field.attrs)
|
||||
f.single_line_attrs(field.attrs, inline: true)
|
||||
}
|
||||
if field.has_default_expr {
|
||||
mut align := default_expr_aligns[default_expr_align_i]
|
||||
@@ -1098,24 +1098,43 @@ pub fn (mut f Fmt) or_expr(node ast.OrExpr) {
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut f Fmt) attrs(attrs []table.Attr) {
|
||||
for attr in attrs {
|
||||
pub fn (mut f Fmt) attrs(attrs []table.Attr) {
|
||||
mut sorted_attrs := attrs.clone()
|
||||
// Sort the attributes. The ones with arguments come first.
|
||||
sorted_attrs.sort(a.arg.len > b.arg.len)
|
||||
for i, attr in sorted_attrs {
|
||||
if attr.arg.len == 0 {
|
||||
f.single_line_attrs(sorted_attrs[i..], {})
|
||||
break
|
||||
}
|
||||
f.writeln('[$attr]')
|
||||
}
|
||||
}
|
||||
|
||||
fn (mut f Fmt) inline_attrs(attrs []table.Attr) {
|
||||
pub struct AttrsOptions {
|
||||
inline bool
|
||||
}
|
||||
|
||||
pub fn (mut f Fmt) single_line_attrs(attrs []table.Attr, options AttrsOptions) {
|
||||
if attrs.len == 0 {
|
||||
return
|
||||
}
|
||||
f.write(' [')
|
||||
for i, attr in attrs {
|
||||
mut sorted_attrs := attrs.clone()
|
||||
sorted_attrs.sort(a.name < b.name)
|
||||
if options.inline {
|
||||
f.write(' ')
|
||||
}
|
||||
f.write('[')
|
||||
for i, attr in sorted_attrs {
|
||||
if i > 0 {
|
||||
f.write('; ')
|
||||
}
|
||||
f.write('$attr')
|
||||
}
|
||||
f.write(']')
|
||||
if !options.inline {
|
||||
f.writeln('')
|
||||
}
|
||||
}
|
||||
|
||||
fn inline_attrs_len(attrs []table.Attr) int {
|
||||
|
||||
4
vlib/v/fmt/tests/attrs_expected.vv
Normal file
4
vlib/v/fmt/tests/attrs_expected.vv
Normal file
@@ -0,0 +1,4 @@
|
||||
[export: 'JNICALL Java_io_vlang_V_callStaticMethods']
|
||||
[tom: 'jerry']
|
||||
[direct_array_access; inline; unsafe]
|
||||
fn heavily_tagged() {}
|
||||
6
vlib/v/fmt/tests/attrs_input.vv
Normal file
6
vlib/v/fmt/tests/attrs_input.vv
Normal file
@@ -0,0 +1,6 @@
|
||||
[inline]
|
||||
[export: 'JNICALL Java_io_vlang_V_callStaticMethods']
|
||||
[direct_array_access]
|
||||
[unsafe]
|
||||
[tom: 'jerry']
|
||||
fn heavily_tagged() {}
|
||||
@@ -1,18 +1,23 @@
|
||||
[inline]
|
||||
[if debug]
|
||||
[foo: bar]
|
||||
[deprecated: 'use bar() instead']
|
||||
[foo: bar]
|
||||
[if debug; inline]
|
||||
fn keep_attributes() {
|
||||
println('hi !')
|
||||
}
|
||||
|
||||
[bar: 'foo']
|
||||
fn attr_with_arg() {}
|
||||
|
||||
['a_string_name']
|
||||
fn name_only_attr() {}
|
||||
|
||||
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']
|
||||
pets string [json: 'pet_animals'; raw]
|
||||
}
|
||||
|
||||
[_allow_multiple_values]
|
||||
|
||||
Reference in New Issue
Block a user