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

fmt: keep single line if in struct init (#8734)

This commit is contained in:
zakuro 2021-02-15 03:22:24 +09:00 committed by GitHub
parent e534b4397d
commit d3bcd5d305
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 19 additions and 6 deletions

View File

@ -58,11 +58,7 @@ pub fn new_builder(pref &pref.Preferences) Builder {
parent: 0
}
compiled_dir: compiled_dir
max_nr_errors: if pref.error_limit > 0 {
pref.error_limit
} else {
100
}
max_nr_errors: if pref.error_limit > 0 { pref.error_limit } else { 100 }
cached_msvc: msvc
}
// max_nr_errors: pref.error_limit ?? 100 TODO potential syntax?

View File

@ -36,6 +36,7 @@ pub mut:
file ast.File
did_imports bool
is_assign bool
is_struct_init bool
auto_imports []string // automatically inserted imports that the user forgot to specify
import_pos int // position of the imports in the resulting string for later autoimports insertion
used_imports []string // to remove unused imports
@ -1636,7 +1637,7 @@ pub fn (mut f Fmt) if_expr(node ast.IfExpr) {
dollar := if node.is_comptime { '$' } else { '' }
mut single_line := node.branches.len == 2 && node.has_else
&& branch_is_single_line(node.branches[0]) && branch_is_single_line(node.branches[1])
&& (node.is_expr || f.is_assign || f.single_line_fields)
&& (node.is_expr || f.is_assign || f.is_struct_init || f.single_line_fields)
f.single_line_if = single_line
if_start := f.line_len
for {
@ -2067,6 +2068,12 @@ pub fn (mut f Fmt) map_init(it ast.MapInit) {
}
pub fn (mut f Fmt) struct_init(it ast.StructInit) {
struct_init_save := f.is_struct_init
f.is_struct_init = true
defer {
f.is_struct_init = struct_init_save
}
type_sym := f.table.get_type_symbol(it.typ)
// f.write('<old name: $type_sym.name>')
mut name := type_sym.name

View File

@ -1,3 +1,8 @@
struct Foo {
n int
s string
}
fn valid_single_line() {
// Variable initialization
a, b := if true { 'a', 'b' } else { 'b', 'a' }
@ -10,6 +15,11 @@ fn valid_single_line() {
_ := if false { Foo{} } else { Foo{5, 6} }
// As argument for a function call
some_func(if cond { 'param1' } else { 'param2' })
// struct init
foo := Foo{
n: if true { 1 } else { 0 }
s: if false { 'false' } else { 'true' }
}
}
fn requires_multiple_lines() {