fmt: reset const field align after multi line exprs (#9916)
parent
c82c8059cf
commit
dee733aae4
|
@ -37,9 +37,9 @@ const (
|
||||||
'vlib/v/tests/generics_test.v', /* multi_generic_args<Foo<int>, Foo<int> >(...) becomes .... Foo<int>>(...) which does not parse */
|
'vlib/v/tests/generics_test.v', /* multi_generic_args<Foo<int>, Foo<int> >(...) becomes .... Foo<int>>(...) which does not parse */
|
||||||
'vlib/v/tests/string_interpolation_test.v' /* TODO byteptr: &byte.str() behaves differently than byteptr.str() */,
|
'vlib/v/tests/string_interpolation_test.v' /* TODO byteptr: &byte.str() behaves differently than byteptr.str() */,
|
||||||
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */
|
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */
|
||||||
'vlib/v/gen/c/cheaders.v' /* the preprocessor directives are formated to the V standard, even though they are in a string literal */,
|
|
||||||
'examples/c_interop_wkhtmltopdf.v', /* &charptr --> &&char */
|
'examples/c_interop_wkhtmltopdf.v', /* &charptr --> &&char */
|
||||||
'examples/path_tracing.v', /* block --> line comments corrupts code */
|
'examples/path_tracing.v', /* block --> line comments corrupts code */
|
||||||
|
'vlib/v/gen/c/cheaders.v' /* infix wrapping error */,
|
||||||
]
|
]
|
||||||
vfmt_verify_list = [
|
vfmt_verify_list = [
|
||||||
'cmd/',
|
'cmd/',
|
||||||
|
|
|
@ -657,6 +657,9 @@ fn expr_is_single_line(expr ast.Expr) bool {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
ast.StringLiteral {
|
||||||
|
return expr.pos.line_nr == expr.pos.last_line
|
||||||
|
}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -888,6 +891,12 @@ pub fn (mut f Fmt) comp_for(node ast.CompFor) {
|
||||||
f.writeln('}')
|
f.writeln('}')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct ConstAlignInfo {
|
||||||
|
mut:
|
||||||
|
max int
|
||||||
|
last_idx int
|
||||||
|
}
|
||||||
|
|
||||||
pub fn (mut f Fmt) const_decl(node ast.ConstDecl) {
|
pub fn (mut f Fmt) const_decl(node ast.ConstDecl) {
|
||||||
if node.is_pub {
|
if node.is_pub {
|
||||||
f.write('pub ')
|
f.write('pub ')
|
||||||
|
@ -901,22 +910,36 @@ pub fn (mut f Fmt) const_decl(node ast.ConstDecl) {
|
||||||
f.inside_const = false
|
f.inside_const = false
|
||||||
}
|
}
|
||||||
f.write('const ')
|
f.write('const ')
|
||||||
mut max := 0
|
mut align_infos := []ConstAlignInfo{}
|
||||||
if node.is_block {
|
if node.is_block {
|
||||||
f.writeln('(')
|
f.writeln('(')
|
||||||
for field in node.fields {
|
mut info := ConstAlignInfo{}
|
||||||
if field.name.len > max {
|
for i, field in node.fields {
|
||||||
max = field.name.len
|
if field.name.len > info.max {
|
||||||
|
info.max = field.name.len
|
||||||
|
}
|
||||||
|
if !expr_is_single_line(field.expr) {
|
||||||
|
info.last_idx = i
|
||||||
|
align_infos << info
|
||||||
|
info = ConstAlignInfo{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
info.last_idx = node.fields.len
|
||||||
|
align_infos << info
|
||||||
f.indent++
|
f.indent++
|
||||||
|
} else {
|
||||||
|
align_infos << ConstAlignInfo{0, 1}
|
||||||
}
|
}
|
||||||
mut prev_field := if node.fields.len > 0 {
|
mut prev_field := if node.fields.len > 0 {
|
||||||
ast.Node(node.fields[0])
|
ast.Node(node.fields[0])
|
||||||
} else {
|
} else {
|
||||||
ast.Node(ast.NodeError{})
|
ast.Node(ast.NodeError{})
|
||||||
}
|
}
|
||||||
for field in node.fields {
|
mut align_idx := 0
|
||||||
|
for i, field in node.fields {
|
||||||
|
if i > align_infos[align_idx].last_idx {
|
||||||
|
align_idx++
|
||||||
|
}
|
||||||
if field.comments.len > 0 {
|
if field.comments.len > 0 {
|
||||||
if f.should_insert_newline_before_node(ast.Expr(field.comments[0]), prev_field) {
|
if f.should_insert_newline_before_node(ast.Expr(field.comments[0]), prev_field) {
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
|
@ -929,7 +952,7 @@ pub fn (mut f Fmt) const_decl(node ast.ConstDecl) {
|
||||||
}
|
}
|
||||||
name := field.name.after('.')
|
name := field.name.after('.')
|
||||||
f.write('$name ')
|
f.write('$name ')
|
||||||
f.write(strings.repeat(` `, max - field.name.len))
|
f.write(strings.repeat(` `, align_infos[align_idx].max - field.name.len))
|
||||||
f.write('= ')
|
f.write('= ')
|
||||||
f.expr(field.expr)
|
f.expr(field.expr)
|
||||||
f.writeln('')
|
f.writeln('')
|
||||||
|
|
|
@ -1 +1,27 @@
|
||||||
const font = $embed_file('../assets/fonts/RobotoMono-Regular.ttf')
|
const font = $embed_file('../assets/fonts/RobotoMono-Regular.ttf')
|
||||||
|
|
||||||
|
const (
|
||||||
|
test_alignment = 123
|
||||||
|
foo = Foo{
|
||||||
|
f: 'foo'
|
||||||
|
}
|
||||||
|
spam = 456
|
||||||
|
egg = 'lorem ipsum'
|
||||||
|
spameggs = true
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
bar = 'A string
|
||||||
|
spanning multiple
|
||||||
|
lines'
|
||||||
|
baz = 'short'
|
||||||
|
some_long_name = MyStruct{
|
||||||
|
x: 42
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
a = 123
|
||||||
|
abc = 123
|
||||||
|
b = 123
|
||||||
|
)
|
||||||
|
|
|
@ -498,8 +498,8 @@ static inline void make_secret(uint64_t seed, uint64_t *secret){
|
||||||
#define _PUSH_MANY(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array_push_many(arr, tmp.data, tmp.len);}
|
#define _PUSH_MANY(arr, val, tmp, tmp_typ) {tmp_typ tmp = (val); array_push_many(arr, tmp.data, tmp.len);}
|
||||||
#define _IN_MAP(val, m) map_exists(m, val)
|
#define _IN_MAP(val, m) map_exists(m, val)
|
||||||
'
|
'
|
||||||
c_headers =
|
c_headers = c_helper_macros + c_unsigned_comparison_functions + c_common_macros +
|
||||||
c_helper_macros + c_unsigned_comparison_functions + c_common_macros + r'
|
r'
|
||||||
// c_headers
|
// c_headers
|
||||||
typedef int (*qsort_callback_func)(const void*, const void*);
|
typedef int (*qsort_callback_func)(const void*, const void*);
|
||||||
#include <stdio.h> // TODO remove all these includes, define all function signatures and types manually
|
#include <stdio.h> // TODO remove all these includes, define all function signatures and types manually
|
||||||
|
@ -728,8 +728,7 @@ typedef bool (*MapEqFn)(voidptr, voidptr);
|
||||||
typedef void (*MapCloneFn)(voidptr, voidptr);
|
typedef void (*MapCloneFn)(voidptr, voidptr);
|
||||||
typedef void (*MapFreeFn)(voidptr);
|
typedef void (*MapFreeFn)(voidptr);
|
||||||
'
|
'
|
||||||
bare_c_headers = c_helper_macros + c_unsigned_comparison_functions +
|
bare_c_headers = c_helper_macros + c_unsigned_comparison_functions + c_common_macros +
|
||||||
c_common_macros +
|
|
||||||
'
|
'
|
||||||
|
|
||||||
#define _VFREESTANDING
|
#define _VFREESTANDING
|
||||||
|
|
Loading…
Reference in New Issue