/
attrs.v
64 lines (59 loc) · 1.28 KB
/
attrs.v
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
// Copyright (c) 2019-2023 Alexander Medvednikov. All rights reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
module fmt
import v.ast
pub fn (mut f Fmt) attrs(attrs []ast.Attr) {
mut sorted_attrs := attrs.clone()
// Sort the attributes. The ones with arguments come first
sorted_attrs.sort_with_compare(fn (a &ast.Attr, b &ast.Attr) int {
d := b.arg.len - a.arg.len
return if d != 0 { d } else { compare_strings(b.arg, a.arg) }
})
for i, attr in sorted_attrs {
if attr.arg.len == 0 {
f.single_line_attrs(sorted_attrs[i..])
break
}
f.writeln('@[${attr}]')
}
}
@[params]
pub struct AttrsOptions {
same_line bool
}
pub fn (mut f Fmt) single_line_attrs(attrs []ast.Attr, options AttrsOptions) {
if attrs.len == 0 {
return
}
mut sorted_attrs := attrs.clone()
sorted_attrs.sort(a.name < b.name)
if options.same_line {
f.write(' ')
}
f.write('@[')
for i, attr in sorted_attrs {
if i > 0 {
f.write('; ')
}
f.write('${attr}')
}
f.write(']')
if !options.same_line {
f.writeln('')
}
}
fn inline_attrs_len(attrs []ast.Attr) int {
if attrs.len == 0 {
return 0
}
mut n := 2 // ' ['.len
for i, attr in attrs {
if i > 0 {
n += 2 // '; '.len
}
n += '${attr}'.len
}
n++ // ']'.len
return n
}