From d59c5b01e1143da78f454ff99f8d8be39860962e Mon Sep 17 00:00:00 2001 From: Lukas Neubert Date: Mon, 14 Dec 2020 05:03:17 +0100 Subject: [PATCH] fmt: intelligent line wrap for trailing struct syntax (#7309) --- vlib/gg/gg.v | 16 +- vlib/v/fmt/fmt.v | 24 ++- .../tests/fn_trailing_arg_syntax_expected.vv | 26 +++ .../fmt/tests/fn_trailing_arg_syntax_input.vv | 20 ++ .../fmt/tests/fn_trailing_arg_syntax_keep.vv | 7 +- vlib/v/table/types.v | 182 +++--------------- 6 files changed, 102 insertions(+), 173 deletions(-) create mode 100644 vlib/v/fmt/tests/fn_trailing_arg_syntax_expected.vv create mode 100644 vlib/v/fmt/tests/fn_trailing_arg_syntax_input.vv diff --git a/vlib/gg/gg.v b/vlib/gg/gg.v index e855978d4d..b6831e0fdb 100644 --- a/vlib/gg/gg.v +++ b/vlib/gg/gg.v @@ -121,18 +121,22 @@ fn gg_init_sokol_window(user_data voidptr) { exists := $if !android { os.is_file(g.config.font_path) } $else { true } if g.config.font_path != '' && exists { // t := time.ticks() - g.ft = new_ft(font_path: g.config.font_path, custom_bold_font_path: g.config.custom_bold_font_path, scale: sapp.dpi_scale()) or { - panic(err) - } + g.ft = new_ft( + font_path: g.config.font_path + custom_bold_font_path: g.config.custom_bold_font_path + scale: sapp.dpi_scale() + ) or { panic(err) } // println('FT took ${time.ticks()-t} ms') g.font_inited = true } else { if !exists { sfont := system_font_path() eprintln('font file "$g.config.font_path" does not exist, the system font was used instead.') - g.ft = new_ft(font_path: sfont, custom_bold_font_path: g.config.custom_bold_font_path, scale: sapp.dpi_scale()) or { - panic(err) - } + g.ft = new_ft( + font_path: sfont + custom_bold_font_path: g.config.custom_bold_font_path + scale: sapp.dpi_scale() + ) or { panic(err) } g.font_inited = true } } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index def3780c2e..92dcce9296 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -1858,15 +1858,17 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) { } f.comments(it.pre_comments, inline: true, has_nl: true, level: .indent) f.indent++ - single_line_short_args := use_short_args && it.fields.len < 4 - if use_short_args && !single_line_short_args { - f.writeln('') - } + mut short_args_multiline := false + mut field_start_positions := []int{} for i, field in it.fields { + field_start_positions << f.out.len f.write('$field.name: ') f.prefix_expr_cast_expr(field.expr) + if field.expr is ast.StructInit { + short_args_multiline = true + } f.comments(field.comments, inline: true, has_nl: false, level: .indent) - if single_line_short_args { + if use_short_args { if i < it.fields.len - 1 { f.write(', ') } @@ -1875,6 +1877,18 @@ pub fn (mut f Fmt) struct_init(it ast.StructInit) { } f.comments(field.next_comments, inline: false, has_nl: true, level: .keep) } + if use_short_args { + if f.line_len > max_len[3] || short_args_multiline { + mut fields := []string{} + for pos in field_start_positions.reverse() { + fields << f.out.cut_last(f.out.len - pos).trim_suffix(', ') + } + f.writeln('') + for field in fields.reverse() { + f.writeln(field) + } + } + } f.indent-- if !use_short_args { f.write('}') diff --git a/vlib/v/fmt/tests/fn_trailing_arg_syntax_expected.vv b/vlib/v/fmt/tests/fn_trailing_arg_syntax_expected.vv new file mode 100644 index 0000000000..593f8059ba --- /dev/null +++ b/vlib/v/fmt/tests/fn_trailing_arg_syntax_expected.vv @@ -0,0 +1,26 @@ +struct Bar { + x string + y int + b Baz +} + +struct Baz { + x int +} + +fn main() { + bar_func(x: 'this line is short enough', y: 13) + bar_func( + x: 'a very long content should cause vfmt to use multiple lines instead of one.' + y: 123456789 + ) + bar_func( + x: 'some string' + b: Baz{ + x: 0 + } + ) +} + +fn bar_func(bar Bar) { +} diff --git a/vlib/v/fmt/tests/fn_trailing_arg_syntax_input.vv b/vlib/v/fmt/tests/fn_trailing_arg_syntax_input.vv new file mode 100644 index 0000000000..1f9bd5cdbe --- /dev/null +++ b/vlib/v/fmt/tests/fn_trailing_arg_syntax_input.vv @@ -0,0 +1,20 @@ +struct Bar { + x string + y int + b Baz +} + +struct Baz { + x int +} + +fn main() { + bar_func(x: 'this line is short enough', y: 13) + bar_func(x: 'a very long content should cause vfmt to use multiple lines instead of one.', y: 123456789) + bar_func(x: 'some string', b: Baz{ + x: 0 + }) +} + +fn bar_func(bar Bar) { +} diff --git a/vlib/v/fmt/tests/fn_trailing_arg_syntax_keep.vv b/vlib/v/fmt/tests/fn_trailing_arg_syntax_keep.vv index 7bd9594724..f6942b3b5e 100644 --- a/vlib/v/fmt/tests/fn_trailing_arg_syntax_keep.vv +++ b/vlib/v/fmt/tests/fn_trailing_arg_syntax_keep.vv @@ -16,12 +16,7 @@ fn main() { foo_func(Baz{ x: 'Baz as Foo sumtype' }) - 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') } diff --git a/vlib/v/table/types.v b/vlib/v/table/types.v index 6e3116c223..6b95e0a222 100644 --- a/vlib/v/table/types.v +++ b/vlib/v/table/types.v @@ -498,168 +498,38 @@ pub fn (mut t Table) register_builtin_type_symbols() { // reserve index 0 so nothing can go there // save index check, 0 will mean not found t.register_type_symbol(kind: .placeholder, name: 'reserved_0') - t.register_type_symbol( - kind: .void - name: 'void' - cname: 'void' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .voidptr - name: 'voidptr' - cname: 'voidptr' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .byteptr - name: 'byteptr' - cname: 'byteptr' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .charptr - name: 'charptr' - cname: 'charptr' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .i8 - name: 'i8' - cname: 'i8' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .i16 - name: 'i16' - cname: 'i16' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .int - name: 'int' - cname: 'int' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .i64 - name: 'i64' - cname: 'i64' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .byte - name: 'byte' - cname: 'byte' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .u16 - name: 'u16' - cname: 'u16' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .u32 - name: 'u32' - cname: 'u32' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .u64 - name: 'u64' - cname: 'u64' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .f32 - name: 'f32' - cname: 'f32' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .f64 - name: 'f64' - cname: 'f64' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .char - name: 'char' - cname: 'char' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .bool - name: 'bool' - cname: 'bool' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .none_ - name: 'none' - cname: 'none' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .string - name: 'string' - cname: 'string' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .ustring - name: 'ustring' - cname: 'ustring' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .array - name: 'array' - cname: 'array' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .map - name: 'map' - cname: 'map' - 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: .rune - name: 'rune' - cname: 'rune' - mod: 'builtin' - ) - t.register_type_symbol( - kind: .any - name: 'any' - cname: 'any' - mod: 'builtin' - ) + t.register_type_symbol(kind: .void, name: 'void', cname: 'void', mod: 'builtin') + t.register_type_symbol(kind: .voidptr, name: 'voidptr', cname: 'voidptr', mod: 'builtin') + t.register_type_symbol(kind: .byteptr, name: 'byteptr', cname: 'byteptr', mod: 'builtin') + t.register_type_symbol(kind: .charptr, name: 'charptr', cname: 'charptr', mod: 'builtin') + t.register_type_symbol(kind: .i8, name: 'i8', cname: 'i8', mod: 'builtin') + t.register_type_symbol(kind: .i16, name: 'i16', cname: 'i16', mod: 'builtin') + t.register_type_symbol(kind: .int, name: 'int', cname: 'int', mod: 'builtin') + t.register_type_symbol(kind: .i64, name: 'i64', cname: 'i64', mod: 'builtin') + t.register_type_symbol(kind: .byte, name: 'byte', cname: 'byte', mod: 'builtin') + t.register_type_symbol(kind: .u16, name: 'u16', cname: 'u16', mod: 'builtin') + t.register_type_symbol(kind: .u32, name: 'u32', cname: 'u32', mod: 'builtin') + t.register_type_symbol(kind: .u64, name: 'u64', cname: 'u64', mod: 'builtin') + t.register_type_symbol(kind: .f32, name: 'f32', cname: 'f32', mod: 'builtin') + t.register_type_symbol(kind: .f64, name: 'f64', cname: 'f64', mod: 'builtin') + t.register_type_symbol(kind: .char, name: 'char', cname: 'char', mod: 'builtin') + t.register_type_symbol(kind: .bool, name: 'bool', cname: 'bool', mod: 'builtin') + t.register_type_symbol(kind: .none_, name: 'none', cname: 'none', mod: 'builtin') + t.register_type_symbol(kind: .string, name: 'string', cname: 'string', mod: 'builtin') + t.register_type_symbol(kind: .ustring, name: 'ustring', cname: 'ustring', mod: 'builtin') + t.register_type_symbol(kind: .array, name: 'array', cname: 'array', mod: 'builtin') + t.register_type_symbol(kind: .map, name: 'map', cname: 'map', 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: .rune, name: 'rune', cname: 'rune', mod: 'builtin') + t.register_type_symbol(kind: .any, name: 'any', cname: 'any', mod: 'builtin') t.register_type_symbol( 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') } [inline]