1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

fmt: heavily refactor and improve/fix logic for structs that use short args syntax (#7653)

This commit is contained in:
Lukas Neubert 2020-12-28 17:14:08 +01:00 committed by GitHub
parent ef6011b94c
commit 97bfabf194
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 41 additions and 39 deletions

View File

@ -1931,41 +1931,38 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) {
} else { } else {
use_short_args := f.use_short_fn_args use_short_args := f.use_short_fn_args
f.use_short_fn_args = false f.use_short_fn_args = false
mut multiline_short_args := false
if !use_short_args { if !use_short_args {
f.writeln('$name{') f.writeln('$name{')
} }
init_start := f.out.len
f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent) f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent)
f.indent++ f.indent++
mut short_args_multiline := false short_args_loop: for {
mut field_start_positions := []int{} for i, field in it.fields {
for i, field in it.fields { f.write('$field.name: ')
field_start_positions << f.out.len f.prefix_expr_cast_expr(field.expr)
f.write('$field.name: ') f.comments(field.comments, inline: true, has_nl: false, level: .indent)
f.prefix_expr_cast_expr(field.expr) if use_short_args && !multiline_short_args {
if field.expr is ast.StructInit { if i < it.fields.len - 1 {
short_args_multiline = true f.write(', ')
} }
f.comments(field.comments, inline: true, has_nl: false, level: .indent) } else {
if use_short_args { f.writeln('')
if i < it.fields.len - 1 {
f.write(', ')
} }
} else { f.comments(field.next_comments, inline: false, has_nl: true, level: .keep)
f.writeln('') if use_short_args && !multiline_short_args &&
} (field.comments.len > 0 ||
f.comments(field.next_comments, inline: false, has_nl: true, level: .keep) field.next_comments.len > 0 || !expr_is_single_line(field.expr) || f.line_len > max_len.last()) {
} multiline_short_args = true
if use_short_args { f.out.go_back_to(init_start)
if f.line_len > max_len[3] || short_args_multiline { f.line_len = init_start
mut fields := []string{} f.remove_new_line()
for pos in field_start_positions.reverse() { f.writeln('')
fields << f.out.cut_last(f.out.len - pos).trim_suffix(', ') continue short_args_loop
}
f.writeln('')
for field in fields.reverse() {
f.writeln(field)
} }
} }
break
} }
f.indent-- f.indent--
if !use_short_args { if !use_short_args {

View File

@ -18,6 +18,21 @@ fn main() {
}) })
bar_func(x: 'bar', y: 2, z: 3, a: 4) bar_func(x: 'bar', y: 2, z: 3, a: 4)
func_from_other_file(val: 'something') func_from_other_file(val: 'something')
// pre comment
bar_func(x: 'struct has a pre comment')
bar_func(
x: 'first field'
// comment between fields
y: 100
)
bar_func(
x: 'Look! A comment to my right.' // comment after field
)
func_from_other_file(
xyz: AnotherStruct{
f: 'here'
}
)
} }
fn bar_func(bar Bar) { fn bar_func(bar Bar) {

View File

@ -459,12 +459,7 @@ fn (mut p Parser) interface_decl() ast.InterfaceDecl {
method.comments = mcomments method.comments = mcomments
methods << method methods << method
// println('register method $name') // println('register method $name')
ts.register_method( ts.register_method(name: name, params: args, return_type: method.return_type, is_pub: true)
name: name
params: args
return_type: method.return_type
is_pub: true
)
} }
p.top_level_statement_end() p.top_level_statement_end()
p.check(.rcbr) p.check(.rcbr)

View File

@ -519,12 +519,7 @@ pub fn (mut t Table) register_builtin_type_symbols() {
t.register_type_symbol(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin') t.register_type_symbol(kind: .chan, name: 'chan', cname: 'chan', mod: 'builtin')
t.register_type_symbol(kind: .size_t, name: 'size_t', cname: 'size_t', mod: 'builtin') t.register_type_symbol(kind: .size_t, name: 'size_t', cname: 'size_t', mod: 'builtin')
t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin') t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin')
t.register_type_symbol( t.register_type_symbol(kind: .any_float, name: 'any_float', cname: 'any_float', mod: 'builtin')
kind: .any_float
name: 'any_float'
cname: 'any_float'
mod: 'builtin'
)
t.register_type_symbol(kind: .any_int, name: 'any_int', cname: 'any_int', mod: 'builtin') t.register_type_symbol(kind: .any_int, name: 'any_int', cname: 'any_int', mod: 'builtin')
} }